Member.php 74 KB

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