Member.php 85 KB

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