Member.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\Attributes\Url;
  5. use Livewire\WithFileUploads;
  6. use Livewire\WithPagination;
  7. use DateTime;
  8. class Member extends Component
  9. {
  10. use WithPagination;
  11. protected $paginationTheme = 'bootstrap';
  12. protected $listeners = ['storeCategoryWithID' => 'storeCategoryWithID', 'setCourse' => 'setCourse', 'hideMsg' => 'hideMsg'];
  13. use WithFileUploads;
  14. public $sortField ='id';
  15. public $sortAsc = false;
  16. public $groupMsg = '';
  17. protected $messages = [
  18. 'birth_nation_id.required' => 'birth_nation_id',
  19. 'birth_province_id.required' => 'birth_province_id',
  20. 'birth_city_id.required' => 'birth_city_id',
  21. 'nation_id.required' => 'nation_id',
  22. 'province_id.required' => 'province_id',
  23. 'city_id.required' => 'city_id',
  24. 'gender' => 'Obbligatorio'
  25. ];
  26. public function sortBy($field)
  27. {
  28. if($this->sortField === $field)
  29. {
  30. $this->sortAsc = ! $this->sortAsc;
  31. } else {
  32. $this->sortAsc = true;
  33. }
  34. $this->sortField = $field;
  35. }
  36. public $records, $first_name, $last_name, $image, $image_old, $status, $birth_place, $birth_city_id, $birth_province_id, $birth_nation_id, $birth_date, $gender, $fiscal_code, $address, $zip_code, $nation_id, $province_id, $city_id, $phone, $phone2, $phone3, $email, $enabled, $dataId, $update = false, $add = false;
  37. public $isSaving = false;
  38. public $father_name, $mother_name, $father_email, $mother_email, $father_phone, $mother_phone, $father_fiscal_code, $mother_fiscal_code, $father_doc_number, $father_doc_type, $mother_doc_number, $mother_doc_type;
  39. public $document_type, $document_number, $document_from, $document_expire_date, $document_files, $document_files_old;
  40. public $selectId = 0;
  41. public $causalId = 0;
  42. public $error_fc = false;
  43. public $under18 = false;
  44. public $refreshAfter = 0;
  45. public $checkedAll = false;
  46. public $multipleIds = [];
  47. public $multipleAction = '';
  48. public $isItaly = true;
  49. public $isBirthItaly = true;
  50. public $showDetail = false;
  51. public $currentMember;
  52. public $currentStatus;
  53. public $age = '';
  54. public $disciplines = array();
  55. public $cards = array();
  56. public $categories = array();
  57. public $courses = array();
  58. public $course_names = array();
  59. public $course_levels = array();
  60. public $course_types = array();
  61. public $course_frequencies = array();
  62. public $course_subscriptions = array();
  63. public $createSubscription = false;
  64. public $payMonths = array();
  65. public $showCourse = array();
  66. public $searchTxt;
  67. public $search;
  68. public $showReset = false;
  69. public $advanced = false;
  70. public $birthNations = array();
  71. public $birthProvinces = array();
  72. public $birthCities = array();
  73. public $active;
  74. // Card data
  75. public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_status, $card_discipline1_id, $card_discipline2_id, $card_discipline3_id, $addCard, $updateCard, $cardDataId;
  76. // Categories data
  77. public $member_categories = array(), $category_category_id;
  78. // Courses data
  79. public $member_courses = array(), $course_course_id, $course_date_from, $course_date_to, $course_when = array(), $addCourse, $updateCourse, $courseDataId, $course_course_subscription_id, $course_status, $course_note, $course_price, $course_subscription_price, $course_subscribed, $course_months = array(), $course_exist;
  80. public $course_name, $course_level_id, $course_type_id, $course_frequency_id;
  81. // Certificates data
  82. public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
  83. public $filterCard = [];
  84. public $filterCategory = [];
  85. public $filterCertNormal = 0;
  86. public $filterCertAgonistic = 0;
  87. public $filterCertScaduto = 0;
  88. public $filterCertInScadenza = 0;
  89. protected $rules = [
  90. 'first_name' => 'required',
  91. 'last_name' => 'required',
  92. 'email' => 'required',
  93. 'phone' => 'required',
  94. 'birth_date' => 'before_or_equal:today'
  95. ];
  96. public function resetFields(){
  97. $this->dataId = -1;
  98. $this->first_name = '';
  99. $this->last_name = '';
  100. $this->status = '';
  101. $this->birth_city_id = null;
  102. $this->birth_province_id = null;
  103. $this->birth_nation_id = null;
  104. $this->birth_date = null;
  105. $this->birth_place = '';
  106. $this->father_name = '';
  107. $this->mother_name = '';
  108. $this->father_email = '';
  109. $this->mother_email = '';
  110. $this->father_phone = '';
  111. $this->mother_phone = '';
  112. $this->father_fiscal_code = '';
  113. $this->mother_fiscal_code = '';
  114. $this->father_doc_number = '';
  115. $this->mother_doc_number = '';
  116. $this->father_doc_type = '';
  117. $this->mother_doc_type = '';
  118. $this->gender = null;
  119. $this->fiscal_code = '';
  120. $this->address = '';
  121. $this->zip_code = '';
  122. $this->document_type = '';
  123. $this->document_number = '';
  124. $this->document_from = '';
  125. $this->document_expire_date = null;
  126. $this->document_files = array();
  127. //$this->nation_id = null;
  128. //$this->province_id = null;
  129. $this->nation_id = null;
  130. $this->province_id = null;
  131. $this->city_id = null;
  132. $this->phone = '';
  133. $this->phone2 = '';
  134. $this->phone3 = '';
  135. $this->email = '';
  136. $this->enabled = true;
  137. $this->under18 = false;
  138. $this->image = null;
  139. $this->age = '';
  140. $this->error_fc = false;
  141. $this->isSaving = false;
  142. $this->emit('load-data-table');
  143. }
  144. public function resetCardFields(){
  145. $this->card_card_id = null;
  146. $this->card_number = '';
  147. $this->card_date = null;
  148. $this->card_accept_date = null;
  149. $this->card_status = 0;
  150. $this->card_discipline1_id = null;
  151. $this->card_discipline2_id = null;
  152. $this->card_discipline3_id = null;
  153. }
  154. public function resetCertificateFields(){
  155. $this->certificate_type = 'N';
  156. $this->certificate_filename = '';
  157. $this->certificate_filename_old = '';
  158. $this->certificate_expire_date = null;
  159. $this->certificate_status = 0;
  160. }
  161. public function resetCourseFields(){
  162. $this->course_course_id = null;
  163. $this->course_when = array();
  164. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  165. $this->course_date_from = null;
  166. $this->course_date_to = null;
  167. $this->course_course_subscription_id = null;
  168. $this->course_status = 0;
  169. $this->course_price = 0;
  170. $this->course_subscription_price = 0;
  171. $this->course_exist = false;
  172. $this->course_subscribed = false;
  173. $this->course_months = array();
  174. $this->course_note = '';
  175. $this->course_name = '';
  176. $this->course_level_id = '';
  177. $this->course_type_id = '';
  178. $this->course_frequency_id = '';
  179. }
  180. public function executeMultipleAction(){
  181. if ($this->multipleAction == 'delete')
  182. $this->multipleDelete();
  183. }
  184. public function updatedImage()
  185. {
  186. $this->validate([
  187. 'image' => 'image|max:1024',
  188. ]);
  189. $this->image_old = '';
  190. }
  191. public function updatedCourseName()
  192. {
  193. $this->course_course_id = null;
  194. $this->course_level_id = '';
  195. $this->course_type_id = '';
  196. $this->course_frequency_id = '';
  197. $levels_ids = [];
  198. if ($this->course_name != '')
  199. {
  200. $all = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->get();
  201. foreach($all as $a)
  202. {
  203. $levels_ids[] = $a->course_level_id;
  204. }
  205. }
  206. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  207. $this->course_types = [];
  208. $this->course_frequencies = [];
  209. }
  210. public function updatedCourseLevelId()
  211. {
  212. $this->course_course_id = null;
  213. $this->course_type_id = '';
  214. $this->course_frequency_id = '';
  215. $types_ids = [];
  216. if ($this->course_level_id != '')
  217. {
  218. $all = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->where('course_level_id', $this->course_level_id)->get();
  219. foreach($all as $a)
  220. {
  221. $types_ids[] = $a->course_type_id;
  222. }
  223. }
  224. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  225. $this->course_frequencies = [];
  226. }
  227. public function updatedCourseTypeId()
  228. {
  229. $this->course_course_id = null;
  230. $this->course_frequency_id = '';
  231. $frequencies_ids = [];
  232. if ($this->course_type_id != '')
  233. {
  234. $all = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->get();
  235. foreach($all as $a)
  236. {
  237. $frequencies_ids[] = $a->course_frequency_id;
  238. }
  239. }
  240. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  241. }
  242. public function updatedCourseFrequencyId()
  243. {
  244. $this->course_course_id = null;
  245. if ($this->course_frequency_id != '')
  246. {
  247. $this->course_course_id = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->where('course_frequency_id', $this->course_frequency_id)->first()->id;
  248. if ($this->course_course_id > 0)
  249. {
  250. $c = \App\Models\Course::findOrFail($this->course_course_id);
  251. $this->course_price = formatPrice($c->price);
  252. $this->course_subscription_price = formatPrice($c->subscription_price);
  253. // Controllo se sono già iscritto la corso
  254. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  255. }
  256. else
  257. {
  258. $this->course_price = 0;
  259. $this->course_subscription_price = 0;
  260. $this->course_exist = false;
  261. }
  262. }
  263. else
  264. {
  265. $this->course_price = 0;
  266. $this->course_subscription_price = 0;
  267. $this->course_exist = false;
  268. }
  269. }
  270. public $documents = [];
  271. public function removeDocument($idx)
  272. {
  273. unset($this->document_files[$idx]);
  274. }
  275. public function updatedDocuments()
  276. {
  277. foreach ($this->documents as $document)
  278. {
  279. $name = $document->getClientOriginalName(); // . '.'.$allegato->extension();
  280. $document->storeAs('public', $name);
  281. $this->document_files[] = $name;
  282. }
  283. $this->documents = [];
  284. }
  285. public function resetCategoryFields(){
  286. $this->category_category_id = null;
  287. }
  288. public function getCategories($records, $indentation)
  289. {
  290. foreach($records as $record)
  291. {
  292. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  293. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  294. if(count($record->childs))
  295. $this->getCategories($record->childs, $indentation + 1);
  296. }
  297. }
  298. public function getCourses($records, $indentation)
  299. {
  300. foreach($records as $record)
  301. {
  302. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  303. $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
  304. if(count($record->childs))
  305. $this->getCourses($record->childs, $indentation + 1);
  306. }
  307. }
  308. public function updatedBirthDate()
  309. {
  310. $this->validateOnly('birth_date');
  311. $date1 = new DateTime($this->birth_date);
  312. $date2 = new DateTime("now");
  313. $interval = $date1->diff($date2);
  314. $this->age = $interval->y . " anni";
  315. $this->under18 = $interval->y < 18;
  316. }
  317. public function mount()
  318. {
  319. if (isset($_GET["new"]))
  320. {
  321. $this->refreshAfter = 1;
  322. $this->add();
  323. }
  324. $this->cards = \App\Models\Card::select('id', 'name')->get();
  325. $this->disciplines = \App\Models\Discipline::select('id', 'name')->get();
  326. $this->categories = array();
  327. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  328. $this->courses = \App\Models\Course::select('id', 'name')->where('parent_id', null)->get();
  329. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  330. // $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->get(), 0);
  331. /*$this->nations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  332. $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  333. $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  334. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  335. if ($c)
  336. $this->causalId = $c->id;
  337. if (isset($_GET["member_detail"]))
  338. {
  339. $this->showDetailF($_GET["member_detail"]);
  340. $this->refreshAfter = 1;
  341. }
  342. /*$this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  343. $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  344. $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  345. }
  346. public function updated()
  347. {
  348. if ($this->isSaving)
  349. {
  350. $zzz = [];
  351. if ($this->birth_nation_id == null)
  352. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  353. if ($this->isBirthItaly)
  354. {
  355. if ($this->birth_province_id == null)
  356. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  357. if ($this->birth_city_id == null)
  358. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  359. }
  360. if ($this->nation_id == null)
  361. $zzz[] = 'nation_id='.$this->nation_id;
  362. if ($this->isItaly)
  363. {
  364. if ($this->province_id == null)
  365. $zzz[] = 'province_id='.$this->province_id;
  366. if ($this->city_id == null)
  367. $zzz[] = 'city_id='.$this->city_id;
  368. }
  369. $this->emit('setErrorMsg', $zzz);
  370. }
  371. // $this->emit('load-select');
  372. }
  373. public function hydrate()
  374. {
  375. if ($this->isSaving)
  376. {
  377. $zzz = [];
  378. if ($this->birth_nation_id == null)
  379. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  380. if ($this->isBirthItaly)
  381. {
  382. if ($this->birth_province_id == null)
  383. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  384. if ($this->birth_city_id == null)
  385. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  386. }
  387. if ($this->nation_id == null)
  388. $zzz[] = 'nation_id='.$this->nation_id;
  389. if ($this->isItaly)
  390. {
  391. if ($this->province_id == null)
  392. $zzz[] = 'province_id='.$this->province_id;
  393. if ($this->city_id == null)
  394. $zzz[] = 'city_id='.$this->city_id;
  395. }
  396. $this->emit('setErrorMsg', $zzz);
  397. }
  398. $this->emit('load-select');
  399. // $this->emit('destroy-data-table');
  400. }
  401. public function checkIsItaly()
  402. {
  403. $n = \App\Models\Nation::findOrFail($this->nation_id);
  404. $this->isItaly = $n->is_italy;
  405. }
  406. public function checkIsBirthItaly()
  407. {
  408. if ($this->birth_nation_id > 0)
  409. {
  410. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  411. $this->isBirthItaly = $n->is_italy;
  412. }
  413. else
  414. $this->isBirthItaly = false;
  415. }
  416. /*public function loadProvinces()
  417. {
  418. $n = \App\Models\Nation::findOrFail($this->nation_id);
  419. $this->isItaly = $n->is_italy;
  420. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
  421. $this->cities = array();
  422. $this->selectId++;
  423. }
  424. public function loadCities()
  425. {
  426. $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
  427. $this->selectId++;
  428. }
  429. public function loadBirthProvinces()
  430. {
  431. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  432. $this->isBirthItaly = $n->is_italy;
  433. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
  434. $this->birthCities = array();
  435. $this->selectId++;
  436. }
  437. public function loadBirthCities()
  438. {
  439. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  440. $this->selectId++;
  441. }
  442. */
  443. public function search()
  444. {
  445. if ($this->searchTxt != '')
  446. {
  447. $this->search = $this->searchTxt;
  448. $this->showReset = true;
  449. }
  450. }
  451. public function resetSearch()
  452. {
  453. $this->showReset = false;
  454. $this->searchTxt = '';
  455. $this->search = $this->searchTxt;
  456. }
  457. public function advancedSearch()
  458. {
  459. $this->advanced = true;
  460. }
  461. public function advancedSearchCancel()
  462. {
  463. $this->filterCard = [];
  464. $this->filterCategory = [];
  465. $this->filterCertNormal = 0;
  466. $this->filterCertAgonistic = 0;
  467. $this->filterCertScaduto = 0;
  468. $this->filterCertInScadenza = 0;
  469. $this->advanced = false;
  470. }
  471. public function render()
  472. {
  473. $this->course_names = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  474. $datas = [];
  475. // $this->emit('destroy-data-table');
  476. if (false)
  477. {
  478. if (!$this->advanced)
  479. {
  480. /*if ($this->search != '')
  481. $datas = \App\Models\Member::select('members.*') // , \DB::raw('SUM(records.id) As total'))
  482. ->where('first_name', 'LIKE', '%' . $this->search . '%')
  483. ->orWhere('last_name', 'LIKE', '%' . $this->search . '%')
  484. ->orWhere('email', 'LIKE', '%' . $this->search . '%');
  485. $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
  486. else*/
  487. $this->records = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone')->get();
  488. //$datas = \App\Models\Member::select('members.*');
  489. }
  490. else
  491. {
  492. //$this->records = \App\Models\Member::where('id', '>', 0);
  493. $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  494. if (sizeof($this->filterCard) > 0)
  495. {
  496. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->filterCard)->pluck('member_id');
  497. //$this->records->whereIn('id', $card_ids);
  498. $datas = $datas->whereIn('id', $card_ids);
  499. }
  500. if (sizeof($this->filterCategory) > 0)
  501. {
  502. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', $this->filterCategory)->pluck('member_id');
  503. //$this->records->whereIn('id', $cats_ids);
  504. $datas = $datas->whereIn('id', $cats_ids);
  505. }
  506. $certs = [];
  507. if ($this->filterCertNormal > 0)
  508. {
  509. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  510. //$this->records->whereIn('id', $normal);
  511. $datas = $datas->whereIn('id', $normal);;
  512. }
  513. if ($this->filterCertAgonistic > 0)
  514. {
  515. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  516. //$this->records->whereIn('id', $agonistic);
  517. $datas = $datas->whereIn('id', $agonistic);
  518. }
  519. if ($this->filterCertScaduto > 0)
  520. {
  521. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  522. //$this->records->whereIn('id', $scaduto);
  523. $datas = $datas->whereIn('id', $scaduto);
  524. }
  525. if ($this->filterCertInScadenza > 0)
  526. {
  527. $scaduto = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  528. //$this->records->whereIn('id', $scaduto);
  529. $datas = $datas->whereIn('id', $scaduto);
  530. }
  531. if (sizeof($certs) > 0)
  532. {
  533. $datas = $datas->whereIn('id', $certs);
  534. //$this->records->whereIn('id', $certs);
  535. }
  536. $this->records = $datas->get();
  537. }
  538. foreach($this->records as $r)
  539. {
  540. $r->age = $r->getAge();
  541. $active = $r->isActive();
  542. $r->status = $active["status"];
  543. $r->date = $active["date"];
  544. $r->certificate = $r->hasCertificate()["date"];
  545. $r->state = $r->getStatus()["status"];
  546. }
  547. /*
  548. if ($this->sortAsc)
  549. $this->records = $this->records->sortBy($this->sortField);
  550. else
  551. $this->records = $this->records->sortByDesc($this->sortField);
  552. */
  553. //$datas = $datas->get(); // ->orderBy($this->sortField, $this->sortAsc ? 'ASC' : 'DESC')->paginate(10);
  554. //$this->records = $this->records->get();
  555. $this->emit('load-data-table');
  556. }
  557. $this->loadMemberCards();
  558. $this->loadMemberCourses();
  559. $this->loadMemberCategories();
  560. $this->loadMemberCertificates();
  561. return view('livewire.member', ['datas' => $datas]);
  562. }
  563. public function updatedCourseCourseId()
  564. {
  565. if ($this->course_course_id > 0)
  566. {
  567. $c = \App\Models\Course::findOrFail($this->course_course_id);
  568. $this->course_price = formatPrice($c->price);
  569. $this->course_subscription_price = formatPrice($c->subscription_price);
  570. // Controllo se sono già iscritto la corso
  571. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  572. }
  573. else
  574. {
  575. $this->course_price = 0;
  576. $this->course_subscription_price = 0;
  577. $this->course_exist = false;
  578. }
  579. }
  580. public function loadMemberCards()
  581. {
  582. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  583. // return view('livewire.member');
  584. }
  585. public function loadMemberCourses()
  586. {
  587. $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
  588. // return view('livewire.member');
  589. }
  590. public function loadMemberCategories()
  591. {
  592. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  593. // return view('livewire.member');
  594. }
  595. public function loadMemberCertificates()
  596. {
  597. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->orderBy('expire_date', 'DESC')->get();
  598. // return view('livewire.member');
  599. }
  600. public function showDetailF($id)
  601. {
  602. $this->currentMember = \App\Models\Member::findOrFail($id);
  603. $this->currentStatus = $this->currentMember->getStatus();
  604. $this->showDetail = true;
  605. }
  606. public function add()
  607. {
  608. $this->emit('load-select');
  609. $this->emit('hide-search');
  610. $this->showDetail = false;
  611. $this->resetFields();
  612. $this->add = true;
  613. $this->update = false;
  614. $this->emit('setEdit', true);
  615. $this->emit('setEditCorso', false);
  616. }
  617. public function store($close)
  618. {
  619. $this->isSaving = true;
  620. $this->emit('load-select');
  621. $rules = [
  622. 'first_name' => 'required',
  623. 'last_name' => 'required',
  624. 'email' => 'required',
  625. 'phone' => 'required',
  626. 'birth_nation_id' => 'required',
  627. //'birth_place' => 'required',
  628. 'address' => 'required',
  629. 'zip_code' => 'required',
  630. 'nation_id' => 'required',
  631. 'fiscal_code' => 'required',
  632. 'birth_date' => 'before_or_equal:today',
  633. 'gender' => 'required'
  634. ];
  635. if ($this->isBirthItaly)
  636. {
  637. $rules['birth_province_id'] = 'required';
  638. $rules['birth_city_id'] = 'required';
  639. }
  640. else
  641. {
  642. $rules['birth_place'] = 'required';
  643. }
  644. if ($this->isItaly)
  645. {
  646. $rules['province_id'] = 'required';
  647. $rules['city_id'] = 'required';
  648. }
  649. $zzz = [];
  650. if ($this->birth_nation_id == null)
  651. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  652. if ($this->isBirthItaly)
  653. {
  654. if ($this->birth_province_id == null)
  655. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  656. if ($this->birth_city_id == null)
  657. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  658. }
  659. if ($this->nation_id == null)
  660. $zzz[] = 'nation_id='.$this->nation_id;
  661. if ($this->isItaly)
  662. {
  663. if ($this->province_id == null)
  664. $zzz[] = 'province_id='.$this->province_id;
  665. if ($this->city_id == null)
  666. $zzz[] = 'city_id='.$this->city_id;
  667. }
  668. $this->emit('setErrorMsg', $zzz);
  669. $this->validate($rules);
  670. //$this->emit('setErrors');
  671. try {
  672. $name = '';
  673. if ($this->image)
  674. {
  675. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  676. $this->image->storeAs('public', $name);
  677. }
  678. $docs = implode("|", $this->document_files);
  679. $member = \App\Models\Member::create([
  680. 'first_name' => strtoupper($this->first_name),
  681. 'last_name' => strtoupper($this->last_name),
  682. 'status' => $this->status,
  683. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  684. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  685. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  686. 'birth_date' => $this->birth_date,
  687. 'birth_place' => $this->birth_place,
  688. 'father_name' => $this->father_name,
  689. 'mother_name' => $this->mother_name,
  690. 'father_email' => strtolower($this->father_email),
  691. 'mother_email' => strtolower($this->mother_email),
  692. 'father_phone' => $this->father_phone,
  693. 'mother_phone' => $this->mother_phone,
  694. 'father_fiscal_code' => $this->father_fiscal_code,
  695. 'mother_fiscal_code' => $this->mother_fiscal_code,
  696. 'father_doc_number' => $this->father_doc_number,
  697. 'father_doc_type' => $this->father_doc_type,
  698. 'mother_doc_number' => $this->mother_doc_number,
  699. 'mother_doc_type' => $this->mother_doc_type,
  700. 'document_type' => $this->document_type,
  701. 'document_number' => $this->document_number,
  702. 'document_from' => $this->document_from,
  703. 'document_expire_date' => $this->document_expire_date,
  704. 'document_files' => $docs,
  705. 'gender' => $this->gender,
  706. 'fiscal_code' => $this->fiscal_code,
  707. 'address' => $this->address,
  708. 'zip_code' => $this->zip_code,
  709. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  710. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  711. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  712. 'phone' => $this->phone,
  713. 'phone2' => $this->phone2,
  714. 'phone3' => $this->phone3,
  715. 'email' => strtolower($this->email),
  716. 'image' => $name,
  717. 'enabled' => $this->enabled
  718. ]);
  719. session()->flash('success, Tesserato creato');
  720. $this->resetFields();
  721. if ($close)
  722. {
  723. $this->add = false;
  724. }
  725. else
  726. {
  727. $this->edit($member->id);
  728. }
  729. $this->emit('setEdit', false);
  730. } catch (\Exception $ex) {
  731. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  732. }
  733. }
  734. public function duplicate($id){
  735. $member = \App\Models\Member::findOrFail($id);
  736. $newMember = $member->replicate();
  737. $newMember->save();
  738. $this->edit($newMember->id);
  739. }
  740. public function edit($id){
  741. $this->showDetail = false;
  742. $this->emit('setEdit', true);
  743. $this->emit('setEditCorso', false);
  744. $this->emit('hide-search');
  745. try {
  746. $member = \App\Models\Member::findOrFail($id);
  747. if( !$member) {
  748. session()->flash('error','Tesserato non trovato');
  749. } else {
  750. $this->first_name = $member->first_name;
  751. $this->last_name = $member->last_name;
  752. $this->status = $member->status;
  753. $this->birth_city_id = $member->birth_city_id;
  754. $this->birth_province_id = $member->birth_province_id;
  755. $this->birth_nation_id = $member->birth_nation_id;
  756. $this->birth_date = $member->birth_date;
  757. $this->birth_place = $member->birth_place;
  758. $this->father_name = $member->father_name;
  759. $this->mother_name = $member->mother_name;
  760. $this->father_email = strtolower($member->father_email);
  761. $this->mother_email = strtolower($member->mother_email);
  762. $this->father_phone = $member->father_phone;
  763. $this->mother_phone = $member->mother_phone;
  764. $this->father_fiscal_code = $member->father_fiscal_code;
  765. $this->mother_fiscal_code = $member->mother_fiscal_code;
  766. $this->father_doc_number = $member->father_doc_number;
  767. $this->mother_doc_number = $member->mother_doc_number;
  768. $this->father_doc_type = $member->father_doc_type;
  769. $this->mother_doc_type = $member->mother_doc_type;
  770. $this->document_type = $member->document_type;
  771. $this->document_number = $member->document_number;
  772. $this->document_from = $member->document_from;
  773. $this->document_expire_date = $member->document_expire_date;
  774. $this->document_files = explode("|", $member->document_files);
  775. $this->gender = $member->gender;
  776. $this->fiscal_code = $member->fiscal_code;
  777. $this->address = $member->address;
  778. $this->zip_code = $member->zip_code;
  779. $this->nation_id = $member->nation_id;
  780. $this->province_id = $member->province_id;
  781. $this->city_id = $member->city_id;
  782. $this->phone = $member->phone;
  783. $this->phone2 = $member->phone2;
  784. $this->phone3 = $member->phone3;
  785. $this->email = strtolower($member->email);
  786. $this->image_old = $member->image;
  787. $this->enabled = $member->enabled;
  788. $this->dataId = $member->id;
  789. $this->active = $member->getStatus();
  790. $this->checkIsBirthItaly();
  791. $date1 = new DateTime($this->birth_date);
  792. $date2 = new DateTime("now");
  793. $interval = $date1->diff($date2);
  794. $this->age = $interval->y . " anni";
  795. $this->under18 = $interval->y < 18;
  796. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  797. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  798. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  799. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  800. $this->update = true;
  801. $this->add = false;
  802. $this->emit('setIds', $this->nation_id, $this->birth_nation_id);
  803. $this->emit('load-select');
  804. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  805. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  806. $this->emit('load-cities', $this->province_id, 'cityClass');
  807. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  808. }
  809. } catch (\Exception $ex) {
  810. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  811. }
  812. }
  813. public function updateAAA($close)
  814. {
  815. $this->isSaving = true;
  816. $this->emit('load-select');
  817. $rules = [
  818. 'first_name' => 'required',
  819. 'last_name' => 'required',
  820. 'email' => 'required',
  821. 'phone' => 'required',
  822. 'birth_nation_id' => 'required',
  823. //'birth_place' => 'required',
  824. 'address' => 'required',
  825. 'zip_code' => 'required',
  826. 'nation_id' => 'required',
  827. 'fiscal_code' => 'required',
  828. 'birth_date' => 'before_or_equal:today'
  829. ];
  830. if ($this->isBirthItaly)
  831. {
  832. $rules['birth_province_id'] = 'required';
  833. $rules['birth_city_id'] = 'required';
  834. }
  835. else
  836. {
  837. $rules['birth_place'] = 'required';
  838. }
  839. if ($this->isItaly)
  840. {
  841. $rules['province_id'] = 'required';
  842. $rules['city_id'] = 'required';
  843. }
  844. $zzz = [];
  845. if ($this->birth_nation_id == null)
  846. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  847. if ($this->isBirthItaly)
  848. {
  849. if ($this->birth_province_id == null)
  850. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  851. if ($this->birth_city_id == null)
  852. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  853. }
  854. if ($this->nation_id == null)
  855. $zzz[] = 'nation_id='.$this->nation_id;
  856. if ($this->isItaly)
  857. {
  858. if ($this->province_id == null)
  859. $zzz[] = 'province_id='.$this->province_id;
  860. if ($this->city_id == null)
  861. $zzz[] = 'city_id='.$this->city_id;
  862. }
  863. $this->emit('setErrorMsg', $zzz);
  864. $this->validate($rules);
  865. try {
  866. $name = '';
  867. if ($this->image)
  868. {
  869. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  870. $this->image->storeAs('public', $name);
  871. }
  872. $docs = implode("|", $this->document_files);
  873. \App\Models\Member::whereId($this->dataId)->update([
  874. 'first_name' => strtoupper($this->first_name),
  875. 'last_name' => strtoupper($this->last_name),
  876. 'status' => $this->status,
  877. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  878. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  879. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  880. 'birth_date' => $this->birth_date,
  881. 'birth_place' => $this->birth_place,
  882. 'father_name' => $this->father_name,
  883. 'mother_name' => $this->mother_name,
  884. 'father_email' => strtolower($this->father_email),
  885. 'mother_email' => strtolower($this->mother_email),
  886. 'father_phone' => $this->father_phone,
  887. 'mother_phone' => $this->mother_phone,
  888. 'father_fiscal_code' => $this->father_fiscal_code,
  889. 'mother_fiscal_code' => $this->mother_fiscal_code,
  890. 'father_doc_number' => $this->father_doc_number,
  891. 'father_doc_type' => $this->father_doc_type,
  892. 'mother_doc_number' => $this->mother_doc_number,
  893. 'mother_doc_type' => $this->mother_doc_type,
  894. 'document_type' => $this->document_type,
  895. 'document_number' => $this->document_number,
  896. 'document_from' => $this->document_from,
  897. 'document_expire_date' => $this->document_expire_date,
  898. 'document_files' => $docs,
  899. 'gender' => $this->gender,
  900. 'fiscal_code' => $this->fiscal_code,
  901. 'address' => $this->address,
  902. 'zip_code' => $this->zip_code,
  903. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  904. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  905. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  906. 'image' => $name != '' ? $name : $this->image_old,
  907. 'phone' => $this->phone,
  908. 'phone2' => $this->phone2,
  909. 'phone3' => $this->phone3,
  910. 'email' => strtolower($this->email),
  911. 'enabled' => $this->enabled
  912. ]);
  913. session()->flash('success','Tesserato aggiornato');
  914. if ($close)
  915. {
  916. $this->resetFields();
  917. $this->update = false;
  918. }
  919. $this->emit('setEdit', false);
  920. } catch (\Exception $ex) {
  921. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  922. }
  923. }
  924. public function cancel()
  925. {
  926. $this->add = false;
  927. $this->update = false;
  928. $this->showDetail = false;
  929. $this->resetFields();
  930. $this->emit('setEdit', false);
  931. //$this->emit('setEditCorso', false);
  932. $this->emit('reload');
  933. }
  934. public function delete($id)
  935. {
  936. try{
  937. \App\Models\Member::find($id)->delete();
  938. session()->flash('success',"Tesserato eliminato");
  939. $this->emit('reload');
  940. }catch(\Exception $e){
  941. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  942. }
  943. }
  944. public function checkUncheckAll()
  945. {
  946. if (!$this->checkedAll)
  947. {
  948. $this->multipleIds = array();
  949. }
  950. else
  951. {
  952. foreach($this->records as $r)
  953. {
  954. $this->multipleIds[] = $r->id;
  955. }
  956. }
  957. }
  958. public function multipleDelete()
  959. {
  960. try{
  961. foreach($this->multipleIds as $id)
  962. {
  963. \App\Models\Member::find($id)->delete();
  964. }
  965. }catch(\Exception $e){
  966. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  967. }
  968. $this->multipleAction = '';
  969. }
  970. // Card
  971. public function addCard()
  972. {
  973. $this->resetCardFields();
  974. $this->addCard = true;
  975. $this->updateCard = false;
  976. }
  977. public function storeCard()
  978. {
  979. $this->validate(['card_card_id' => 'required']);
  980. try {
  981. // in base alla card selezionata calcolo la scadenza
  982. $expire_date = null;
  983. if ($this->card_date != '')
  984. {
  985. $y = date("Y", strtotime($this->card_date));
  986. $card = \App\Models\Card::findOrFail($this->card_card_id);
  987. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  988. {
  989. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  990. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  991. $y += 1;
  992. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  993. if ($next_exp > $this->card_date)
  994. {
  995. $expire_date = $next_exp;
  996. }
  997. else
  998. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  999. }
  1000. else
  1001. {
  1002. if ($card->one_year_expire)
  1003. {
  1004. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1005. }
  1006. }
  1007. }
  1008. \App\Models\MemberCard::create([
  1009. 'member_id' => $this->dataId,
  1010. 'card_id' => $this->card_card_id,
  1011. 'number' => $this->card_number,
  1012. 'date' => $this->card_date,
  1013. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1014. 'expire_date' => $expire_date,
  1015. 'status' => $this->card_status,
  1016. 'discipline1_id' => $this->card_discipline1_id,
  1017. 'discipline2_id' => $this->card_discipline2_id,
  1018. 'discipline3_id' => $this->card_discipline3_id,
  1019. ]);
  1020. session()->flash('success, Tesserato creato');
  1021. $this->resetCardFields();
  1022. $this->addCard = false;
  1023. } catch (\Exception $ex) {
  1024. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1025. }
  1026. }
  1027. public function editCard($id){
  1028. try {
  1029. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1030. if( !$memberCard) {
  1031. session()->flash('error','Tesserato non trovato');
  1032. } else {
  1033. $this->card_card_id = $memberCard->card_id;
  1034. $this->card_number = $memberCard->number;
  1035. $this->card_date = $memberCard->date;
  1036. $this->card_accept_date = $memberCard->accept_date;
  1037. $this->card_status = $memberCard->status;
  1038. $this->card_discipline1_id = $memberCard->discipline1_id;
  1039. $this->card_discipline2_id = $memberCard->discipline2_id;
  1040. $this->card_discipline3_id = $memberCard->discipline3_id;
  1041. $this->cardDataId = $memberCard->id;
  1042. $this->updateCard = true;
  1043. $this->addCard = false;
  1044. }
  1045. } catch (\Exception $ex) {
  1046. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1047. }
  1048. }
  1049. public function updateCard()
  1050. {
  1051. //$this->validate();
  1052. $this->validate(['card_card_id' => 'required']);
  1053. try {
  1054. // in base alla card selezionata calcolo la scadenza
  1055. $expire_date = null;
  1056. if ($this->card_date != '')
  1057. {
  1058. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1059. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  1060. {
  1061. $y = date("Y", strtotime($this->card_date));
  1062. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1063. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1064. if ($next_exp > $this->card_date)
  1065. {
  1066. $expire_date = $next_exp;
  1067. }
  1068. else
  1069. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1070. }
  1071. else
  1072. {
  1073. if ($card->one_year_expire)
  1074. {
  1075. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1076. }
  1077. }
  1078. }
  1079. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1080. 'member_id' => $this->dataId,
  1081. 'card_id' => $this->card_card_id,
  1082. 'number' => $this->card_number,
  1083. 'date' => $this->card_date,
  1084. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1085. 'expire_date' => $expire_date,
  1086. 'status' => $this->card_status,
  1087. 'discipline1_id' => $this->card_discipline1_id,
  1088. 'discipline2_id' => $this->card_discipline2_id,
  1089. 'discipline3_id' => $this->card_discipline3_id,
  1090. ]);
  1091. session()->flash('success','Tesserato aggiornato');
  1092. $this->resetCardFields();
  1093. $this->updateCard = false;
  1094. } catch (\Exception $ex) {
  1095. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1096. }
  1097. }
  1098. public function cancelCard()
  1099. {
  1100. $this->addCard = false;
  1101. $this->updateCard = false;
  1102. $this->resetCardFields();
  1103. }
  1104. public function deleteCard($id)
  1105. {
  1106. try{
  1107. \App\Models\MemberCard::find($id)->delete();
  1108. session()->flash('success',"Tesserato eliminato");
  1109. }catch(\Exception $e){
  1110. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1111. }
  1112. }
  1113. // Courses
  1114. public function addCourse()
  1115. {
  1116. $this->resetCourseFields();
  1117. $this->addCourse = true;
  1118. $this->updateCourse = false;
  1119. $this->emit('setEditCorso', true);
  1120. }
  1121. public function storeCourse()
  1122. {
  1123. $this->validate(['course_course_id' => 'required']);
  1124. try {
  1125. \App\Models\MemberCourse::create([
  1126. 'member_id' => $this->dataId,
  1127. 'course_id' => $this->course_course_id,
  1128. 'date_from' => $this->course_date_from,
  1129. 'date_to' => $this->course_date_to,
  1130. 'course_subscription_id' => $this->course_course_subscription_id,
  1131. 'status' => $this->course_status,
  1132. 'subscribed' => false, // default non iscritto
  1133. 'price' => currencyToDouble($this->course_price),
  1134. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1135. 'notes' => $this->course_note,
  1136. 'months' => json_encode($this->course_months),
  1137. 'when' => json_encode($this->course_when)
  1138. ]);
  1139. session()->flash('success, Corso creato');
  1140. $this->resetCourseFields();
  1141. $this->addCourse = false;
  1142. $this->emit('setEditCorso', false);
  1143. } catch (\Exception $ex) {
  1144. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1145. }
  1146. }
  1147. public function editCourse($id){
  1148. try {
  1149. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1150. if( !$memberCourse) {
  1151. session()->flash('error','Corso non trovato');
  1152. } else {
  1153. $this->course_course_id = $memberCourse->course_id;
  1154. // Carico i dati relativi alla struttura ad albero
  1155. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1156. $this->course_name = $c->name;
  1157. $this->course_level_id = $c->course_level_id;
  1158. $this->course_type_id = $c->course_type_id;
  1159. $this->course_frequency_id = $c->course_frequency_id;
  1160. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->get();
  1161. foreach($all as $a)
  1162. {
  1163. $levels_ids[] = $a->course_level_id;
  1164. }
  1165. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1166. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('course_level_id', $this->course_level_id)->get();
  1167. foreach($all as $a)
  1168. {
  1169. $types_ids[] = $a->course_type_id;
  1170. }
  1171. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1172. $frequencies_ids = [];
  1173. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->get();
  1174. foreach($all as $a)
  1175. {
  1176. $frequencies_ids[] = $a->course_frequency_id;
  1177. }
  1178. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1179. $this->course_date_from = $memberCourse->date_from;
  1180. $this->course_date_to = $memberCourse->date_to;
  1181. foreach(json_decode($memberCourse->when) as $z)
  1182. {
  1183. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1184. }
  1185. //$this->course_when = json_decode($memberCourse->when);
  1186. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1187. $this->course_status = $memberCourse->status;
  1188. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1189. $this->course_price = formatPrice($memberCourse->price);
  1190. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1191. $this->course_note = $memberCourse->notes;
  1192. foreach(json_decode($memberCourse->months) as $z)
  1193. {
  1194. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1195. }
  1196. $this->courseDataId = $memberCourse->id;
  1197. $this->updateCourse = true;
  1198. $this->addCourse = false;
  1199. $this->emit('setEditCorso', true);
  1200. }
  1201. } catch (\Exception $ex) {
  1202. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1203. }
  1204. }
  1205. public function updateCourse()
  1206. {
  1207. //$this->validate();
  1208. $this->validate(['course_course_id' => 'required']);
  1209. try {
  1210. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1211. 'member_id' => $this->dataId,
  1212. 'course_id' => $this->course_course_id,
  1213. 'date_from' => $this->course_date_from,
  1214. 'date_to' => $this->course_date_to,
  1215. 'course_subscription_id' => $this->course_course_subscription_id,
  1216. 'status' => $this->course_status,
  1217. 'price' => currencyToDouble($this->course_price),
  1218. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1219. 'notes' => $this->course_note,
  1220. 'months' => json_encode($this->course_months),
  1221. 'when' => json_encode($this->course_when)
  1222. ]);
  1223. session()->flash('success','Corso aggiornato');
  1224. $this->resetCourseFields();
  1225. $this->updateCourse = false;
  1226. $this->emit('setEditCorso', false);
  1227. } catch (\Exception $ex) {
  1228. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1229. }
  1230. }
  1231. public function cancelCourse()
  1232. {
  1233. $this->addCourse = false;
  1234. $this->updateCourse = false;
  1235. $this->emit('setEditCorso', false);
  1236. $this->resetCourseFields();
  1237. }
  1238. public function deleteCourse($id)
  1239. {
  1240. try{
  1241. \App\Models\MemberCourse::find($id)->delete();
  1242. session()->flash('success',"Corso eliminato");
  1243. }catch(\Exception $e){
  1244. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1245. }
  1246. }
  1247. public function addRow()
  1248. {
  1249. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1250. }
  1251. public function delRow($idx)
  1252. {
  1253. unset($this->course_when[$idx]);
  1254. }
  1255. // Certificates
  1256. public function addCertificate()
  1257. {
  1258. $this->resetCertificateFields();
  1259. $this->addCertificate = true;
  1260. $this->updateCertificate = false;
  1261. }
  1262. public function storeCertificate()
  1263. {
  1264. $this->validate(['certificate_expire_date' => 'required']);
  1265. // $this->validate();
  1266. try {
  1267. $name = '';
  1268. try{
  1269. if ($this->certificate_filename)
  1270. {
  1271. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1272. $this->certificate_filename->storeAs('public', $name);
  1273. }
  1274. } catch (\Exception $ex) {
  1275. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1276. }
  1277. if ($this->dataId > -1)
  1278. {
  1279. \App\Models\MemberCertificate::create([
  1280. 'member_id' => $this->dataId,
  1281. 'type' => $this->certificate_type,
  1282. 'filename' => $name,
  1283. 'expire_date' => $this->certificate_expire_date,
  1284. 'status' => $this->certificate_status
  1285. ]);
  1286. }
  1287. /*else
  1288. {
  1289. $this->certificateTmp = new \App\Models\MemberCertificate();
  1290. $this->certificateTmp->type = $this->certificate_type;
  1291. $this->certificateTmp->filename = $name;
  1292. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1293. $this->certificateTmp->status = $this->certificate_status;
  1294. $this->certificateTmp->status = $this->certificate_status;
  1295. // s $this->member_certificates[] = $certificateTmp;
  1296. }*/
  1297. session()->flash('success, Tesserato creato');
  1298. $this->resetCertificateFields();
  1299. $this->addCertificate = false;
  1300. } catch (\Exception $ex) {
  1301. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1302. }
  1303. }
  1304. public function editCertificate($id){
  1305. try {
  1306. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1307. if( !$memberCertificate) {
  1308. session()->flash('error','Tesserato non trovato');
  1309. } else {
  1310. $this->certificate_type = $memberCertificate->type;
  1311. $this->certificate_filename_old = $memberCertificate->filename;
  1312. $this->certificate_expire_date = $memberCertificate->expire_date;
  1313. $this->certificate_status = $memberCertificate->status;
  1314. $this->cardCertificateId = $memberCertificate->id;
  1315. $this->updateCertificate = true;
  1316. $this->addCertificate = false;
  1317. }
  1318. } catch (\Exception $ex) {
  1319. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1320. }
  1321. }
  1322. public function updateCertificate()
  1323. {
  1324. $this->validate(['certificate_expire_date' => 'required']);
  1325. try {
  1326. $name = '';
  1327. try{
  1328. if ($this->certificate_filename)
  1329. {
  1330. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1331. $this->certificate_filename->storeAs('public', $name);
  1332. }
  1333. } catch (\Exception $ex) {
  1334. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1335. }
  1336. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1337. 'member_id' => $this->dataId,
  1338. 'type' => $this->certificate_type,
  1339. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1340. 'expire_date' => $this->certificate_expire_date,
  1341. 'status' => $this->certificate_status
  1342. ]);
  1343. session()->flash('success','Tesserato aggiornato');
  1344. $this->resetCertificateFields();
  1345. $this->updateCertificate = false;
  1346. } catch (\Exception $ex) {
  1347. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1348. }
  1349. }
  1350. public function cancelCertificate()
  1351. {
  1352. $this->addCertificate = false;
  1353. $this->updateCertificate = false;
  1354. $this->resetCertificateFields();
  1355. }
  1356. public function deleteCertificate($id)
  1357. {
  1358. try{
  1359. \App\Models\MemberCertificate::find($id)->delete();
  1360. session()->flash('success',"Tesserato eliminato");
  1361. }catch(\Exception $e){
  1362. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1363. }
  1364. }
  1365. // Gruppi di appartenenza
  1366. public function storeCategory()
  1367. {
  1368. $this->validate(['category_category_id' => 'required']);
  1369. try {
  1370. \App\Models\MemberCategory::create([
  1371. 'member_id' => $this->dataId,
  1372. 'category_id' => $this->category_category_id,
  1373. 'date' => \Carbon\Carbon::now()
  1374. ]);
  1375. session()->flash('success, Associazione creato');
  1376. $this->resetCategoryFields();
  1377. $this->addCard = false;
  1378. } catch (\Exception $ex) {
  1379. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1380. }
  1381. }
  1382. public function hideMsg()
  1383. {
  1384. $this->groupMsg = '';
  1385. }
  1386. public function storeCategoryWithID($id)
  1387. {
  1388. $this->groupMsg = '';
  1389. try {
  1390. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first())
  1391. {
  1392. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1393. }
  1394. else
  1395. {
  1396. \App\Models\MemberCategory::create([
  1397. 'member_id' => $this->dataId,
  1398. 'category_id' => $id,
  1399. 'date' => \Carbon\Carbon::now()
  1400. ]);
  1401. session()->flash('success, Associazione creato');
  1402. $this->resetCategoryFields();
  1403. $this->addCard = false;
  1404. }
  1405. } catch (\Exception $ex) {
  1406. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1407. }
  1408. }
  1409. public function deleteCategory($id)
  1410. {
  1411. try{
  1412. \App\Models\MemberCategory::find($id)->delete();
  1413. session()->flash('success',"Associazione eliminata");
  1414. }catch(\Exception $e){
  1415. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1416. }
  1417. }
  1418. public function setCourse($id)
  1419. {
  1420. $this->course_course_id = $id;
  1421. }
  1422. public function getNation($nation)
  1423. {
  1424. if ($nation > 0)
  1425. {
  1426. $ret = \App\Models\Nation::findOrFail($nation);
  1427. return $ret->name;
  1428. }
  1429. return "";
  1430. }
  1431. public function getProvince($province)
  1432. {
  1433. if ($province > 0)
  1434. {
  1435. $ret = \App\Models\Province::findOrFail($province);
  1436. return $ret->name;
  1437. }
  1438. return "";
  1439. }
  1440. public function getCity($city)
  1441. {
  1442. if ($city > 0)
  1443. {
  1444. $ret = \App\Models\City::findOrFail($city);
  1445. return $ret->name;
  1446. }
  1447. return "";
  1448. }
  1449. public function getFiscalCode()
  1450. {
  1451. $this->error_fc = false;
  1452. $cf = new codicefiscale();
  1453. $cf->setDateSeparator('-');
  1454. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0)
  1455. {
  1456. $code = '';
  1457. if ($this->birth_city_id > 0)
  1458. {
  1459. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1460. }
  1461. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1462. $this->fiscal_code = $codice;
  1463. }
  1464. else
  1465. $this->error_fc = true;
  1466. }
  1467. public function setMonth($m)
  1468. {
  1469. $exist = -1;
  1470. foreach($this->course_months as $idx => $x)
  1471. {
  1472. if ($x["m"] == $m)
  1473. {
  1474. $exist = $idx;
  1475. break;
  1476. }
  1477. }
  1478. if ($exist > -1)
  1479. {
  1480. array_splice($this->course_months, $exist, 1);
  1481. }
  1482. else
  1483. {
  1484. $this->course_months[] = array("m" => $m, "status" => "");
  1485. }
  1486. /*
  1487. if (in_array($m, $this->course_months))
  1488. {
  1489. $i = array_search($m, $this->course_months);
  1490. array_splice($this->course_months, $i, 1);
  1491. }
  1492. else
  1493. {
  1494. $this->course_months[] = $m;
  1495. }*/
  1496. }
  1497. public function newPayment($course)
  1498. {
  1499. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1500. $this->emit('setEdit', false);
  1501. $this->emit('setEditCorso', false);
  1502. return redirect()->to('/in?new=1&memberId=' . $this->dataId . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=' . $this->createSubscription . (sizeof($this->payMonths) > 0 ? '&months=' . implode("|", $this->payMonths) : "") . '&price=' . $course["price"] . '&subscription_price=' . $course["subscription_price"] . "&courseId=" . $course["id"]);
  1503. }
  1504. public function block($course)
  1505. {
  1506. $xxx = json_decode($course["months"]);
  1507. foreach($xxx as $idx => $mm)
  1508. {
  1509. if (in_array($mm->m, $this->payMonths))
  1510. {
  1511. $xxx[$idx]->status = 2;
  1512. }
  1513. }
  1514. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1515. $c->months = json_encode($xxx);
  1516. $c->save();
  1517. $this->payMonths = array();
  1518. }
  1519. public function setPayMonth($m, $months)
  1520. {
  1521. if (in_array($m, $this->payMonths))
  1522. {
  1523. $i = array_search($m, $this->payMonths);
  1524. array_splice($this->payMonths, $i, 1);
  1525. }
  1526. else
  1527. {
  1528. foreach(json_decode($months) as $mm)
  1529. {
  1530. if ($mm->m == $m)
  1531. {
  1532. if ($mm->status == "")
  1533. $this->payMonths[] = $m;
  1534. }
  1535. }
  1536. }
  1537. }
  1538. public function setCreateSubscription($subscribed)
  1539. {
  1540. if (!$subscribed)
  1541. $this->createSubscription = !$this->createSubscription;
  1542. }
  1543. public function showHideCourse($id)
  1544. {
  1545. if (in_array($id, $this->showCourse))
  1546. {
  1547. $i = array_search($id, $this->showCourse);
  1548. array_splice($this->showCourse, $i, 1);
  1549. }
  1550. else
  1551. {
  1552. $this->showCourse[] = $id;
  1553. }
  1554. }
  1555. public function getMonthStatus($m, $months)
  1556. {
  1557. $class = "grey";
  1558. foreach(json_decode($months) as $mm)
  1559. {
  1560. if ($mm->m == $m)
  1561. {
  1562. if ($mm->status == "")
  1563. {
  1564. if (in_array($m, $this->payMonths))
  1565. $class = "blue";
  1566. else
  1567. $class = "orange";
  1568. }
  1569. if ($mm->status == "1")
  1570. {
  1571. $class = "green";
  1572. }
  1573. if ($mm->status == "2")
  1574. {
  1575. $class = "yellow";
  1576. }
  1577. }
  1578. }
  1579. return $class;
  1580. }
  1581. public function getSubscriptionStatus($subscribed)
  1582. {
  1583. $class = "grey";
  1584. if ($this->createSubscription)
  1585. $class = "blue";
  1586. else
  1587. $class = "orange";
  1588. if ($subscribed)
  1589. $class = "green";
  1590. return $class;
  1591. }
  1592. public function checkMonth($m)
  1593. {
  1594. $ret = false;
  1595. foreach($this->course_months as $idx => $x)
  1596. {
  1597. if ($x["m"] == $m)
  1598. {
  1599. $ret = true;
  1600. break;
  1601. }
  1602. }
  1603. return $ret;
  1604. }
  1605. public function setDay($idx, $d)
  1606. {
  1607. if (in_array($d, $this->course_when[$idx]["day"]))
  1608. {
  1609. $i = array_search($d, $this->course_when[$idx]["day"]);
  1610. array_splice($this->course_when[$idx]["day"], $i, 1);
  1611. }
  1612. else
  1613. {
  1614. $this->course_when[$idx]["day"][] = $d;
  1615. }
  1616. }
  1617. public function getMonth($m)
  1618. {
  1619. $ret = '';
  1620. switch ($m) {
  1621. case 1:
  1622. $ret = 'Gennaio';
  1623. break;
  1624. case 2:
  1625. $ret = 'Febbraio';
  1626. break;
  1627. case 3:
  1628. $ret = 'Marzo';
  1629. break;
  1630. case 4:
  1631. $ret = 'Aprile';
  1632. break;
  1633. case 5:
  1634. $ret = 'Maggio';
  1635. break;
  1636. case 6:
  1637. $ret = 'Giugno';
  1638. break;
  1639. case 7:
  1640. $ret = 'Luglio';
  1641. break;
  1642. case 8:
  1643. $ret = 'Agosto';
  1644. break;
  1645. case 9:
  1646. $ret = 'Settembre';
  1647. break;
  1648. case 10:
  1649. $ret = 'Ottobre';
  1650. break;
  1651. case 11:
  1652. $ret = 'Novembre';
  1653. break;
  1654. case 12:
  1655. $ret = 'Dicembre';
  1656. break;
  1657. default:
  1658. $ret = '';
  1659. break;
  1660. }
  1661. return $ret;
  1662. }
  1663. }
  1664. class codicefiscale {
  1665. /**
  1666. * Array delle consonanti
  1667. */
  1668. protected $_consonanti = array(
  1669. 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K',
  1670. 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
  1671. 'V', 'W', 'X', 'Y', 'Z'
  1672. );
  1673. /**
  1674. * Array delle vocali
  1675. */
  1676. protected $_vocali = array(
  1677. 'A', 'E', 'I', 'O', 'U'
  1678. );
  1679. /**
  1680. * Array per il calcolo della lettera del mese
  1681. * Al numero del mese corrisponde una lettera
  1682. */
  1683. protected $_mesi = array(
  1684. 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
  1685. 6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
  1686. 11 => 'S', 12 => 'T'
  1687. );
  1688. protected $_pari = array(
  1689. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
  1690. '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
  1691. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4,
  1692. 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9,
  1693. 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14,
  1694. 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19,
  1695. 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24,
  1696. 'Z' => 25
  1697. );
  1698. protected $_dispari = array(
  1699. '0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9,
  1700. '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21,
  1701. 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9,
  1702. 'F' => 13, 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21,
  1703. 'K' => 2, 'L' => 4, 'M' => 18, 'N' => 20, 'O' => 11,
  1704. 'P' => 3, 'Q' => 6, 'R' => 8, 'S' => 12, 'T' => 14,
  1705. 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25, 'Y' => 24,
  1706. 'Z' => 23
  1707. );
  1708. protected $_controllo = array(
  1709. '0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
  1710. '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
  1711. '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
  1712. '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P',
  1713. '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T',
  1714. '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X',
  1715. '24' => 'Y', '25' => 'Z'
  1716. );
  1717. /**
  1718. * Stringa di errore
  1719. */
  1720. protected $_error = null;
  1721. /**
  1722. * Separatore per la data di nascita
  1723. */
  1724. protected $_dateSeparator = '/';
  1725. /**
  1726. * Percorso del file del database SQLite
  1727. * dei codici catastali
  1728. */
  1729. protected $_dbCatastali = null;
  1730. /**
  1731. * Trasforma la stringa passata in un array di lettere
  1732. * e lo incrocia con un ulteriore array
  1733. */
  1734. protected function _getLettere($string, array $haystack) {
  1735. $letters = array();
  1736. foreach(str_split($string) as $needle) {
  1737. if (in_array($needle, $haystack)) {
  1738. $letters[] = $needle;
  1739. }
  1740. }
  1741. return $letters;
  1742. }
  1743. /**
  1744. * Ritorna un array con le vocali di una data stringa
  1745. */
  1746. protected function _getVocali($string) {
  1747. return $this->_getLettere($string, $this->_vocali);
  1748. }
  1749. /**
  1750. * Ritorna un array con le consonanti di una data stringa
  1751. */
  1752. protected function _getConsonanti($string) {
  1753. return $this->_getLettere($string, $this->_consonanti);
  1754. }
  1755. /**
  1756. * Pulisce la stringa filtrando tutti i caratteri che
  1757. * non sono lettere. Lo switch $toupper se impostato a TRUE
  1758. * converte la stringa risultante in MAIUSCOLO.
  1759. */
  1760. protected function _sanitize($string, $toupper = true) {
  1761. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  1762. return ($toupper) ? strtoupper($result) : $result;
  1763. }
  1764. /**
  1765. * Se la stringa passata a funzione e' costituita
  1766. * da meno di 3 caratteri, rimpiazza le lettere
  1767. * mancanti con la lettera X.
  1768. */
  1769. protected function _addMissingX($string) {
  1770. $code = $string;
  1771. while(strlen($code) < 3) {
  1772. $code .= 'X';
  1773. }
  1774. return $code;
  1775. }
  1776. /**
  1777. * Ottiene il codice identificativo del nome
  1778. */
  1779. protected function _calcolaNome($string) {
  1780. $nome = $this->_sanitize($string);
  1781. $code = '';
  1782. // Se il nome inserito e' piu' corto di 3 lettere
  1783. // si aggiungono tante X quanti sono i caratteri
  1784. // mancanti.
  1785. if (strlen($nome) < 3) {
  1786. return $this->_addMissingX($nome);
  1787. }
  1788. $nome_cons = $this->_getConsonanti($nome);
  1789. // Se le consonanti contenute nel nome sono minori
  1790. // o uguali a 3 vengono considerate nell'ordine in cui
  1791. // compaiono.
  1792. if (count($nome_cons) <= 3) {
  1793. $code = implode('', $nome_cons);
  1794. } else {
  1795. // Se invece abbiamo almeno 4 consonanti, prendiamo
  1796. // la prima, la terza e la quarta.
  1797. for($i=0; $i<4; $i++) {
  1798. if ($i == 1) continue;
  1799. if (!empty($nome_cons[$i])) {
  1800. $code .= $nome_cons[$i];
  1801. }
  1802. }
  1803. }
  1804. // Se compaiono meno di 3 consonanti nel nome, si
  1805. // utilizzano le vocali, nell'ordine in cui compaiono
  1806. // nel nome.
  1807. if (strlen($code) < 3) {
  1808. $nome_voc = $this->_getVocali($nome);
  1809. while (strlen($code) < 3) {
  1810. $code .= array_shift($nome_voc);
  1811. }
  1812. }
  1813. return $code;
  1814. }
  1815. protected function _calcolaCognome($string) {
  1816. $cognome = $this->_sanitize($string);
  1817. $code = '';
  1818. // Se il cognome inserito e' piu' corto di 3 lettere
  1819. // si aggiungono tante X quanti sono i caratteri
  1820. // mancanti.
  1821. if (strlen($cognome) < 3) {
  1822. return $this->_addMissingX($cognome);
  1823. }
  1824. $cognome_cons = $this->_getConsonanti($cognome);
  1825. // Per il calcolo del cognome si prendono le prime
  1826. // 3 consonanti.
  1827. for ($i=0; $i<3; $i++) {
  1828. if (array_key_exists($i, $cognome_cons)) {
  1829. $code .= $cognome_cons[$i];
  1830. }
  1831. }
  1832. // Se le consonanti non bastano, vengono prese
  1833. // le vocali nell'ordine in cui compaiono.
  1834. if (strlen($code) < 3) {
  1835. $cognome_voc = $this->_getVocali($cognome);
  1836. while (strlen($code) < 3) {
  1837. $code .= array_shift($cognome_voc);
  1838. }
  1839. }
  1840. return $code;
  1841. }
  1842. /**
  1843. * Imposta il separatore di data ( default: / )
  1844. */
  1845. public function setDateSeparator($char) {
  1846. $this->_dateSeparator = $char;
  1847. return $this;
  1848. }
  1849. /**
  1850. * Ritorna la parte di codice fiscale corrispondente
  1851. * alla data di nascita del soggetto (Forma: AAMGG)
  1852. */
  1853. protected function _calcolaDataNascita($data, $sesso) {
  1854. $dn = explode($this->_dateSeparator, $data);
  1855. $giorno = (int) @$dn[2];
  1856. $mese = (int) @$dn[1];
  1857. $anno = (int) @$dn[0];
  1858. // Le ultime due cifre dell'anno di nascita
  1859. $aa = substr($anno, -2);
  1860. // La lettera corrispondente al mese di nascita
  1861. $mm = $this->_mesi[$mese];
  1862. // Il giorno viene calcolato a seconda del sesso
  1863. // del soggetto di cui si calcola il codice:
  1864. // se e' Maschio si mette il giorno reale, se e'
  1865. // Femmina viene aggiungo 40 a questo numero.
  1866. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  1867. // Bug #1: Thanks to Luca
  1868. if (strlen($gg) < 2) $gg = '0' . $gg;
  1869. return $aa . $mm . $gg;
  1870. }
  1871. /**
  1872. * Ritorna la cifra di controllo sulla base dei
  1873. * 15 caratteri del codice fiscale calcolati.
  1874. */
  1875. protected function _calcolaCifraControllo($codice) {
  1876. $code = str_split($codice);
  1877. $sum = 0;
  1878. for($i=1; $i <= count($code); $i++) {
  1879. $cifra = $code[$i-1];
  1880. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  1881. }
  1882. $sum %= 26;
  1883. return $this->_controllo[$sum];
  1884. }
  1885. /**
  1886. * Imposta il messaggio di errore
  1887. */
  1888. protected function _setError($string) {
  1889. $this->_error = $string;
  1890. }
  1891. /**
  1892. * Verifica la presenza di un errore.
  1893. * Ritorna TRUE se presente, FALSE altrimenti.
  1894. */
  1895. public function hasError() {
  1896. return !is_null($this->_error);
  1897. }
  1898. /**
  1899. * Ritorna la stringa di errore
  1900. */
  1901. public function getError() {
  1902. return $this->_error;
  1903. }
  1904. /**
  1905. * Ritorna il codice fiscale utilizzando i parametri
  1906. * passati a funzione. Se si verifica
  1907. */
  1908. public function calcola($nome, $cognome, $data, $sesso, $comune) {
  1909. $codice = $this->_calcolaCognome($cognome) .
  1910. $this->_calcolaNome($nome) .
  1911. $this->_calcolaDataNascita($data, $sesso) .
  1912. $comune;
  1913. if ($this->hasError()) {
  1914. return false;
  1915. }
  1916. $codice .= $this->_calcolaCifraControllo($codice);
  1917. if (strlen($codice) != 16) {
  1918. //$this->_setError(self::ERR_GENERIC);
  1919. return 'ERROR';
  1920. }
  1921. return $codice;
  1922. }
  1923. }