Member.php 90 KB

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