Member.php 91 KB

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