Member.php 80 KB

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