Member.php 75 KB

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