Member.php 80 KB

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