Member.php 62 KB

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