Member.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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. if (!isset($_GET["from"]) && $this->from == '')
  636. $this->from = 'members';
  637. $this->currentMember = \App\Models\Member::findOrFail($id);
  638. $this->currentStatus = $this->currentMember->getStatus();
  639. $this->showDetail = true;
  640. }
  641. public function add()
  642. {
  643. //$this->from = 'members';
  644. $this->emit('load-select');
  645. $this->emit('hide-search');
  646. $this->showDetail = false;
  647. $this->resetFields();
  648. $this->add = true;
  649. $this->update = false;
  650. $this->emit('setEdit', true);
  651. $this->emit('setEditCorso', false);
  652. }
  653. public function store($close)
  654. {
  655. $this->isSaving = true;
  656. $this->emit('load-select');
  657. $rules = [
  658. 'first_name' => 'required',
  659. 'last_name' => 'required',
  660. 'email' => 'required',
  661. 'phone' => 'required',
  662. 'birth_nation_id' => 'required',
  663. //'birth_place' => 'required',
  664. 'address' => 'required',
  665. 'zip_code' => 'required',
  666. 'nation_id' => 'required',
  667. 'fiscal_code' => 'required',
  668. 'birth_date' => 'before_or_equal:today',
  669. 'gender' => 'required'
  670. ];
  671. if ($this->isBirthItaly)
  672. {
  673. $rules['birth_province_id'] = 'required';
  674. $rules['birth_city_id'] = 'required';
  675. }
  676. else
  677. {
  678. $rules['birth_place'] = 'required';
  679. }
  680. if ($this->isItaly)
  681. {
  682. $rules['province_id'] = 'required';
  683. $rules['city_id'] = 'required';
  684. }
  685. $zzz = [];
  686. if ($this->birth_nation_id == null)
  687. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  688. if ($this->isBirthItaly)
  689. {
  690. if ($this->birth_province_id == null)
  691. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  692. if ($this->birth_city_id == null)
  693. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  694. }
  695. if ($this->nation_id == null)
  696. $zzz[] = 'nation_id='.$this->nation_id;
  697. if ($this->isItaly)
  698. {
  699. if ($this->province_id == null)
  700. $zzz[] = 'province_id='.$this->province_id;
  701. if ($this->city_id == null)
  702. $zzz[] = 'city_id='.$this->city_id;
  703. }
  704. $this->emit('setErrorMsg', $zzz);
  705. $this->validate($rules);
  706. //$this->emit('setErrors');
  707. try {
  708. $name = '';
  709. if ($this->image)
  710. {
  711. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  712. $this->image->storeAs('public', $name);
  713. }
  714. $docs = implode("|", $this->document_files);
  715. $member = \App\Models\Member::create([
  716. 'first_name' => strtoupper($this->first_name),
  717. 'last_name' => strtoupper($this->last_name),
  718. 'status' => $this->status,
  719. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  720. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  721. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  722. 'birth_date' => $this->birth_date,
  723. 'birth_place' => $this->birth_place,
  724. 'father_name' => $this->father_name,
  725. 'mother_name' => $this->mother_name,
  726. 'father_email' => strtolower($this->father_email),
  727. 'mother_email' => strtolower($this->mother_email),
  728. 'father_phone' => $this->father_phone,
  729. 'mother_phone' => $this->mother_phone,
  730. 'father_fiscal_code' => $this->father_fiscal_code,
  731. 'mother_fiscal_code' => $this->mother_fiscal_code,
  732. 'father_doc_number' => $this->father_doc_number,
  733. 'father_doc_type' => $this->father_doc_type,
  734. 'mother_doc_number' => $this->mother_doc_number,
  735. 'mother_doc_type' => $this->mother_doc_type,
  736. 'document_type' => $this->document_type,
  737. 'document_number' => $this->document_number,
  738. 'document_from' => $this->document_from,
  739. 'document_expire_date' => $this->document_expire_date,
  740. 'document_files' => $docs,
  741. 'gender' => $this->gender,
  742. 'no_send_mail' => $this->no_send_mail,
  743. 'exclude_from_records' => $this->exclude_from_records,
  744. 'fiscal_code' => $this->fiscal_code,
  745. 'address' => $this->address,
  746. 'zip_code' => $this->zip_code,
  747. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  748. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  749. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  750. 'phone' => $this->phone,
  751. 'phone2' => $this->phone2,
  752. 'phone3' => $this->phone3,
  753. 'email' => strtolower($this->email),
  754. 'image' => $name,
  755. 'enabled' => $this->enabled
  756. ]);
  757. session()->flash('success, Tesserato creato');
  758. updateMemberData($member->id);
  759. $this->resetFields();
  760. if ($close)
  761. {
  762. $this->add = false;
  763. }
  764. else
  765. {
  766. $this->edit($member->id);
  767. }
  768. $this->emit('setEdit', false);
  769. } catch (\Exception $ex) {
  770. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  771. }
  772. }
  773. public function duplicate($id){
  774. $member = \App\Models\Member::findOrFail($id);
  775. $newMember = $member->replicate();
  776. $newMember->save();
  777. $this->edit($newMember->id);
  778. }
  779. public function edit($id){
  780. if (!isset($_GET["from"]) && $this->from == '')
  781. $this->from = 'members';
  782. $this->showDetail = false;
  783. $this->emit('setEdit', true);
  784. $this->emit('setEditCorso', false);
  785. $this->emit('hide-search');
  786. try {
  787. $member = \App\Models\Member::findOrFail($id);
  788. if( !$member) {
  789. session()->flash('error','Tesserato non trovato');
  790. } else {
  791. $this->first_name = $member->first_name;
  792. $this->last_name = $member->last_name;
  793. $this->status = $member->status;
  794. $this->birth_city_id = $member->birth_city_id;
  795. $this->birth_province_id = $member->birth_province_id;
  796. $this->birth_nation_id = $member->birth_nation_id;
  797. $this->birth_date = $member->birth_date;
  798. $this->birth_place = $member->birth_place;
  799. $this->father_name = $member->father_name;
  800. $this->mother_name = $member->mother_name;
  801. $this->father_email = strtolower($member->father_email);
  802. $this->mother_email = strtolower($member->mother_email);
  803. $this->father_phone = $member->father_phone;
  804. $this->mother_phone = $member->mother_phone;
  805. $this->father_fiscal_code = $member->father_fiscal_code;
  806. $this->mother_fiscal_code = $member->mother_fiscal_code;
  807. $this->father_doc_number = $member->father_doc_number;
  808. $this->mother_doc_number = $member->mother_doc_number;
  809. $this->father_doc_type = $member->father_doc_type;
  810. $this->mother_doc_type = $member->mother_doc_type;
  811. $this->document_type = $member->document_type;
  812. $this->document_number = $member->document_number;
  813. $this->document_from = $member->document_from;
  814. $this->document_expire_date = $member->document_expire_date;
  815. $this->document_files = explode("|", $member->document_files);
  816. $this->gender = $member->gender;
  817. $this->no_send_mail = $member->no_send_mail;
  818. $this->exclude_from_records = $member->exclude_from_records;
  819. $this->fiscal_code = $member->fiscal_code;
  820. $this->address = $member->address;
  821. $this->zip_code = $member->zip_code;
  822. $this->nation_id = $member->nation_id;
  823. $this->province_id = $member->province_id;
  824. $this->city_id = $member->city_id;
  825. $this->phone = $member->phone;
  826. $this->phone2 = $member->phone2;
  827. $this->phone3 = $member->phone3;
  828. $this->email = strtolower($member->email);
  829. $this->image_old = $member->image;
  830. $this->enabled = $member->enabled;
  831. $this->dataId = $member->id;
  832. $this->active = $member->getStatus();
  833. $this->money = $member->getMoney();
  834. $this->checkIsBirthItaly();
  835. $date1 = new DateTime($this->birth_date);
  836. $date2 = new DateTime("now");
  837. $interval = $date1->diff($date2);
  838. $this->age = $interval->y . " anni";
  839. $this->under18 = $interval->y < 18;
  840. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  841. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  842. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  843. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  844. $this->update = true;
  845. $this->add = false;
  846. $this->emit('setIds', $this->nation_id, $this->birth_nation_id);
  847. $this->emit('load-select');
  848. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  849. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  850. $this->emit('load-cities', $this->province_id, 'cityClass');
  851. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  852. }
  853. } catch (\Exception $ex) {
  854. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  855. }
  856. }
  857. public function updateAAA($close)
  858. {
  859. $this->isSaving = true;
  860. $this->emit('load-select');
  861. $rules = [
  862. 'first_name' => 'required',
  863. 'last_name' => 'required',
  864. 'email' => 'required',
  865. 'phone' => 'required',
  866. 'birth_nation_id' => 'required',
  867. //'birth_place' => 'required',
  868. 'address' => 'required',
  869. 'zip_code' => 'required',
  870. 'nation_id' => 'required',
  871. 'fiscal_code' => 'required',
  872. 'birth_date' => 'before_or_equal:today'
  873. ];
  874. if ($this->isBirthItaly)
  875. {
  876. $rules['birth_province_id'] = 'required';
  877. $rules['birth_city_id'] = 'required';
  878. }
  879. else
  880. {
  881. $rules['birth_place'] = 'required';
  882. }
  883. if ($this->isItaly)
  884. {
  885. $rules['province_id'] = 'required';
  886. $rules['city_id'] = 'required';
  887. }
  888. $zzz = [];
  889. if ($this->birth_nation_id == null)
  890. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  891. if ($this->isBirthItaly)
  892. {
  893. if ($this->birth_province_id == null)
  894. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  895. if ($this->birth_city_id == null)
  896. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  897. }
  898. if ($this->nation_id == null)
  899. $zzz[] = 'nation_id='.$this->nation_id;
  900. if ($this->isItaly)
  901. {
  902. if ($this->province_id == null)
  903. $zzz[] = 'province_id='.$this->province_id;
  904. if ($this->city_id == null)
  905. $zzz[] = 'city_id='.$this->city_id;
  906. }
  907. $this->emit('setErrorMsg', $zzz);
  908. $this->validate($rules);
  909. try {
  910. $name = '';
  911. if ($this->image)
  912. {
  913. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  914. $this->image->storeAs('public', $name);
  915. }
  916. $docs = implode("|", $this->document_files);
  917. \App\Models\Member::whereId($this->dataId)->update([
  918. 'first_name' => strtoupper($this->first_name),
  919. 'last_name' => strtoupper($this->last_name),
  920. 'status' => $this->status,
  921. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  922. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  923. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  924. 'birth_date' => $this->birth_date,
  925. 'birth_place' => $this->birth_place,
  926. 'father_name' => $this->father_name,
  927. 'mother_name' => $this->mother_name,
  928. 'father_email' => strtolower($this->father_email),
  929. 'mother_email' => strtolower($this->mother_email),
  930. 'father_phone' => $this->father_phone,
  931. 'mother_phone' => $this->mother_phone,
  932. 'father_fiscal_code' => $this->father_fiscal_code,
  933. 'mother_fiscal_code' => $this->mother_fiscal_code,
  934. 'father_doc_number' => $this->father_doc_number,
  935. 'father_doc_type' => $this->father_doc_type,
  936. 'mother_doc_number' => $this->mother_doc_number,
  937. 'mother_doc_type' => $this->mother_doc_type,
  938. 'document_type' => $this->document_type,
  939. 'document_number' => $this->document_number,
  940. 'document_from' => $this->document_from,
  941. 'document_expire_date' => $this->document_expire_date,
  942. 'document_files' => $docs,
  943. 'gender' => $this->gender,
  944. 'no_send_mail' => $this->no_send_mail,
  945. 'exclude_from_records' => $this->exclude_from_records,
  946. 'fiscal_code' => $this->fiscal_code,
  947. 'address' => $this->address,
  948. 'zip_code' => $this->zip_code,
  949. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  950. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  951. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  952. 'image' => $name != '' ? $name : $this->image_old,
  953. 'phone' => $this->phone,
  954. 'phone2' => $this->phone2,
  955. 'phone3' => $this->phone3,
  956. 'email' => strtolower($this->email),
  957. 'enabled' => $this->enabled
  958. ]);
  959. updateMemberData($this->dataId);
  960. session()->flash('success','Tesserato aggiornato');
  961. if ($close)
  962. {
  963. $this->resetFields();
  964. $this->update = false;
  965. }
  966. $this->emit('setEdit', false);
  967. } catch (\Exception $ex) {
  968. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  969. }
  970. }
  971. public function cancel()
  972. {
  973. $this->add = false;
  974. $this->update = false;
  975. $this->showDetail = false;
  976. $this->resetFields();
  977. $this->emit('setEdit', false);
  978. //$this->emit('setEditCorso', false);
  979. $this->emit('reload');
  980. }
  981. public function delete($id)
  982. {
  983. try{
  984. \App\Models\Member::find($id)->delete();
  985. session()->flash('success',"Tesserato eliminato");
  986. $this->emit('reload');
  987. }catch(\Exception $e){
  988. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  989. }
  990. }
  991. public function checkUncheckAll()
  992. {
  993. if (!$this->checkedAll)
  994. {
  995. $this->multipleIds = array();
  996. }
  997. else
  998. {
  999. foreach($this->records as $r)
  1000. {
  1001. $this->multipleIds[] = $r->id;
  1002. }
  1003. }
  1004. }
  1005. public function multipleDelete()
  1006. {
  1007. try{
  1008. foreach($this->multipleIds as $id)
  1009. {
  1010. \App\Models\Member::find($id)->delete();
  1011. }
  1012. }catch(\Exception $e){
  1013. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1014. }
  1015. $this->multipleAction = '';
  1016. }
  1017. // Card
  1018. public function addCard()
  1019. {
  1020. $this->resetCardFields();
  1021. $this->addCard = true;
  1022. $this->updateCard = false;
  1023. }
  1024. public function storeCard()
  1025. {
  1026. $this->validate(['card_card_id' => 'required']);
  1027. try {
  1028. // in base alla card selezionata calcolo la scadenza
  1029. $expire_date = null;
  1030. if ($this->card_date != '')
  1031. {
  1032. $y = date("Y", strtotime($this->card_date));
  1033. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1034. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  1035. {
  1036. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1037. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  1038. $y += 1;
  1039. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1040. if ($next_exp > $this->card_date)
  1041. {
  1042. $expire_date = $next_exp;
  1043. }
  1044. else
  1045. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1046. }
  1047. else
  1048. {
  1049. if ($card->one_year_expire)
  1050. {
  1051. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1052. }
  1053. }
  1054. }
  1055. \App\Models\MemberCard::create([
  1056. 'member_id' => $this->dataId,
  1057. 'card_id' => $this->card_card_id,
  1058. 'number' => $this->card_number,
  1059. 'date' => $this->card_date,
  1060. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1061. 'expire_date' => $expire_date,
  1062. 'status' => $this->card_status,
  1063. 'discipline1_id' => $this->card_discipline1_id,
  1064. 'discipline2_id' => $this->card_discipline2_id,
  1065. 'discipline3_id' => $this->card_discipline3_id,
  1066. ]);
  1067. updateMemberData($this->dataId);
  1068. session()->flash('success, Tesserato creato');
  1069. $this->resetCardFields();
  1070. $this->addCard = false;
  1071. } catch (\Exception $ex) {
  1072. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1073. }
  1074. }
  1075. public function editCard($id){
  1076. try {
  1077. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1078. if( !$memberCard) {
  1079. session()->flash('error','Tesserato non trovato');
  1080. } else {
  1081. $this->card_card_id = $memberCard->card_id;
  1082. $this->card_number = $memberCard->number;
  1083. $this->card_date = $memberCard->date;
  1084. $this->card_accept_date = $memberCard->accept_date;
  1085. $this->card_status = $memberCard->status;
  1086. $this->card_discipline1_id = $memberCard->discipline1_id;
  1087. $this->card_discipline2_id = $memberCard->discipline2_id;
  1088. $this->card_discipline3_id = $memberCard->discipline3_id;
  1089. $this->cardDataId = $memberCard->id;
  1090. $this->updateCard = true;
  1091. $this->addCard = false;
  1092. }
  1093. } catch (\Exception $ex) {
  1094. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1095. }
  1096. }
  1097. public function updateCard()
  1098. {
  1099. //$this->validate();
  1100. $this->validate(['card_card_id' => 'required']);
  1101. try {
  1102. // in base alla card selezionata calcolo la scadenza
  1103. $expire_date = null;
  1104. if ($this->card_date != '')
  1105. {
  1106. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1107. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  1108. {
  1109. $y = date("Y", strtotime($this->card_date));
  1110. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1111. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1112. if ($next_exp > $this->card_date)
  1113. {
  1114. $expire_date = $next_exp;
  1115. }
  1116. else
  1117. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1118. }
  1119. else
  1120. {
  1121. if ($card->one_year_expire)
  1122. {
  1123. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1124. }
  1125. }
  1126. }
  1127. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1128. 'member_id' => $this->dataId,
  1129. 'card_id' => $this->card_card_id,
  1130. 'number' => $this->card_number,
  1131. 'date' => $this->card_date,
  1132. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1133. 'expire_date' => $expire_date,
  1134. 'status' => $this->card_status,
  1135. 'discipline1_id' => $this->card_discipline1_id,
  1136. 'discipline2_id' => $this->card_discipline2_id,
  1137. 'discipline3_id' => $this->card_discipline3_id,
  1138. ]);
  1139. updateMemberData($this->dataId);
  1140. session()->flash('success','Tesserato aggiornato');
  1141. $this->resetCardFields();
  1142. $this->updateCard = false;
  1143. } catch (\Exception $ex) {
  1144. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1145. }
  1146. }
  1147. public function cancelCard()
  1148. {
  1149. $this->addCard = false;
  1150. $this->updateCard = false;
  1151. $this->resetCardFields();
  1152. }
  1153. public function deleteCard($id)
  1154. {
  1155. try{
  1156. \App\Models\MemberCard::find($id)->delete();
  1157. session()->flash('success',"Tesserato eliminato");
  1158. }catch(\Exception $e){
  1159. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1160. }
  1161. }
  1162. // Courses
  1163. public function addCourse()
  1164. {
  1165. $this->resetCourseFields();
  1166. /*if ($this->under18)
  1167. {
  1168. $this->course_months[] = array("m" => 9, "status" => "");
  1169. $this->course_months[] = array("m" => 10, "status" => "");
  1170. $this->course_months[] = array("m" => 11, "status" => "");
  1171. $this->course_months[] = array("m" => 12, "status" => "");
  1172. $this->course_months[] = array("m" => 1, "status" => "");
  1173. $this->course_months[] = array("m" => 2, "status" => "");
  1174. $this->course_months[] = array("m" => 3, "status" => "");
  1175. $this->course_months[] = array("m" => 4, "status" => "");
  1176. $this->course_months[] = array("m" => 5, "status" => "");
  1177. $this->course_months[] = array("m" => 6, "status" => "");
  1178. }*/
  1179. $this->addCourse = true;
  1180. $this->updateCourse = false;
  1181. $this->emit('setEditCorso', true);
  1182. }
  1183. public function storeCourse()
  1184. {
  1185. $this->validate(['course_course_id' => 'required']);
  1186. try {
  1187. \App\Models\MemberCourse::create([
  1188. 'member_id' => $this->dataId,
  1189. 'course_id' => $this->course_course_id,
  1190. 'date_from' => $this->course_date_from,
  1191. 'date_to' => $this->course_date_to,
  1192. 'course_subscription_id' => $this->course_course_subscription_id,
  1193. 'status' => $this->course_status,
  1194. 'subscribed' => false, // default non iscritto
  1195. 'price' => currencyToDouble($this->course_price),
  1196. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1197. 'notes' => $this->course_note,
  1198. 'months' => json_encode($this->course_months),
  1199. 'when' => json_encode($this->course_when)
  1200. ]);
  1201. // Se il corso ha associato una categoria iscrivo anche al gruppo
  1202. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1203. if ($c)
  1204. {
  1205. if ($c->category_id > 0)
  1206. {
  1207. \App\Models\MemberCategory::create([
  1208. 'member_id' => $this->dataId,
  1209. 'category_id' => $c->category_id,
  1210. 'date' => \Carbon\Carbon::now()
  1211. ]);
  1212. }
  1213. }
  1214. session()->flash('success, Corso creato');
  1215. $this->resetCourseFields();
  1216. $this->addCourse = false;
  1217. $this->emit('setEditCorso', false);
  1218. } catch (\Exception $ex) {
  1219. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1220. }
  1221. }
  1222. public function editCourse($id){
  1223. try {
  1224. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1225. if( !$memberCourse) {
  1226. session()->flash('error','Corso non trovato');
  1227. } else {
  1228. $this->course_course_id = $memberCourse->course_id;
  1229. // Carico i dati relativi alla struttura ad albero
  1230. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1231. $this->course_name = $c->name . " (" . $c->year . ")";
  1232. $this->course_level_id = $c->course_level_id;
  1233. $this->course_type_id = $c->course_type_id;
  1234. $this->course_frequency_id = $c->course_frequency_id;
  1235. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->get();
  1236. foreach($all as $a)
  1237. {
  1238. $levels_ids[] = $a->course_level_id;
  1239. }
  1240. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1241. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  1242. foreach($all as $a)
  1243. {
  1244. $types_ids[] = $a->course_type_id;
  1245. }
  1246. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1247. $frequencies_ids = [];
  1248. $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();
  1249. foreach($all as $a)
  1250. {
  1251. $frequencies_ids[] = $a->course_frequency_id;
  1252. }
  1253. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1254. $this->course_when = array();
  1255. $this->course_date_from = $memberCourse->date_from;
  1256. $this->course_date_to = $memberCourse->date_to;
  1257. foreach(json_decode($memberCourse->when) as $z)
  1258. {
  1259. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1260. }
  1261. //$this->course_when = json_decode($memberCourse->when);
  1262. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1263. $this->course_status = $memberCourse->status;
  1264. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1265. $this->course_price = formatPrice($memberCourse->price);
  1266. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1267. $this->course_note = $memberCourse->notes;
  1268. foreach(json_decode($memberCourse->months) as $z)
  1269. {
  1270. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1271. }
  1272. $this->courseDataId = $memberCourse->id;
  1273. $this->updateCourse = true;
  1274. $this->addCourse = false;
  1275. $this->emit('setEditCorso', true);
  1276. }
  1277. } catch (\Exception $ex) {
  1278. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1279. }
  1280. }
  1281. public function updateCourse()
  1282. {
  1283. //$this->validate();
  1284. $this->validate(['course_course_id' => 'required']);
  1285. try {
  1286. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1287. 'member_id' => $this->dataId,
  1288. 'course_id' => $this->course_course_id,
  1289. 'date_from' => $this->course_date_from,
  1290. 'date_to' => $this->course_date_to,
  1291. 'course_subscription_id' => $this->course_course_subscription_id,
  1292. 'status' => $this->course_status,
  1293. 'price' => currencyToDouble($this->course_price),
  1294. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1295. 'notes' => $this->course_note,
  1296. 'months' => json_encode($this->course_months),
  1297. 'when' => json_encode($this->course_when)
  1298. ]);
  1299. session()->flash('success','Corso aggiornato');
  1300. $this->resetCourseFields();
  1301. $this->updateCourse = false;
  1302. $this->emit('setEditCorso', false);
  1303. } catch (\Exception $ex) {
  1304. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1305. }
  1306. }
  1307. public function cancelCourse()
  1308. {
  1309. $this->addCourse = false;
  1310. $this->updateCourse = false;
  1311. $this->emit('setEditCorso', false);
  1312. $this->resetCourseFields();
  1313. }
  1314. public function deleteCourse($id)
  1315. {
  1316. try{
  1317. \App\Models\MemberCourse::find($id)->delete();
  1318. session()->flash('success',"Corso eliminato");
  1319. }catch(\Exception $e){
  1320. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1321. }
  1322. }
  1323. public function addRow()
  1324. {
  1325. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1326. }
  1327. public function delRow($idx)
  1328. {
  1329. unset($this->course_when[$idx]);
  1330. }
  1331. // Certificates
  1332. public function addCertificate()
  1333. {
  1334. $this->resetCertificateFields();
  1335. $this->addCertificate = true;
  1336. $this->updateCertificate = false;
  1337. }
  1338. public function storeCertificate()
  1339. {
  1340. $this->validate(['certificate_expire_date' => 'required']);
  1341. // $this->validate();
  1342. try {
  1343. $name = '';
  1344. try{
  1345. if ($this->certificate_filename)
  1346. {
  1347. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1348. $this->certificate_filename->storeAs('public', $name);
  1349. }
  1350. } catch (\Exception $ex) {
  1351. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1352. }
  1353. if ($this->dataId > -1)
  1354. {
  1355. \App\Models\MemberCertificate::create([
  1356. 'member_id' => $this->dataId,
  1357. 'type' => $this->certificate_type,
  1358. 'filename' => $name,
  1359. 'expire_date' => $this->certificate_expire_date,
  1360. 'status' => $this->certificate_status
  1361. ]);
  1362. updateMemberData($this->dataId);
  1363. }
  1364. /*else
  1365. {
  1366. $this->certificateTmp = new \App\Models\MemberCertificate();
  1367. $this->certificateTmp->type = $this->certificate_type;
  1368. $this->certificateTmp->filename = $name;
  1369. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1370. $this->certificateTmp->status = $this->certificate_status;
  1371. $this->certificateTmp->status = $this->certificate_status;
  1372. // s $this->member_certificates[] = $certificateTmp;
  1373. }*/
  1374. session()->flash('success, Tesserato creato');
  1375. $this->resetCertificateFields();
  1376. $this->addCertificate = false;
  1377. } catch (\Exception $ex) {
  1378. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1379. }
  1380. }
  1381. public function editCertificate($id){
  1382. try {
  1383. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1384. if( !$memberCertificate) {
  1385. session()->flash('error','Tesserato non trovato');
  1386. } else {
  1387. $this->certificate_type = $memberCertificate->type;
  1388. $this->certificate_filename_old = $memberCertificate->filename;
  1389. $this->certificate_expire_date = $memberCertificate->expire_date;
  1390. $this->certificate_status = $memberCertificate->status;
  1391. $this->cardCertificateId = $memberCertificate->id;
  1392. $this->updateCertificate = true;
  1393. $this->addCertificate = false;
  1394. }
  1395. } catch (\Exception $ex) {
  1396. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1397. }
  1398. }
  1399. public function updateCertificate()
  1400. {
  1401. $this->validate(['certificate_expire_date' => 'required']);
  1402. try {
  1403. $name = '';
  1404. try{
  1405. if ($this->certificate_filename)
  1406. {
  1407. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1408. $this->certificate_filename->storeAs('public', $name);
  1409. }
  1410. } catch (\Exception $ex) {
  1411. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1412. }
  1413. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1414. 'member_id' => $this->dataId,
  1415. 'type' => $this->certificate_type,
  1416. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1417. 'expire_date' => $this->certificate_expire_date,
  1418. 'status' => $this->certificate_status
  1419. ]);
  1420. updateMemberData($this->dataId);
  1421. session()->flash('success','Tesserato aggiornato');
  1422. $this->resetCertificateFields();
  1423. $this->updateCertificate = false;
  1424. } catch (\Exception $ex) {
  1425. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1426. }
  1427. }
  1428. public function cancelCertificate()
  1429. {
  1430. $this->addCertificate = false;
  1431. $this->updateCertificate = false;
  1432. $this->resetCertificateFields();
  1433. }
  1434. public function deleteCertificate($id)
  1435. {
  1436. try{
  1437. \App\Models\MemberCertificate::find($id)->delete();
  1438. session()->flash('success',"Tesserato eliminato");
  1439. }catch(\Exception $e){
  1440. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1441. }
  1442. }
  1443. // Gruppi di appartenenza
  1444. public function storeCategory()
  1445. {
  1446. $this->validate(['category_category_id' => 'required']);
  1447. try {
  1448. \App\Models\MemberCategory::create([
  1449. 'member_id' => $this->dataId,
  1450. 'category_id' => $this->category_category_id,
  1451. 'date' => \Carbon\Carbon::now()
  1452. ]);
  1453. session()->flash('success, Associazione creato');
  1454. $this->resetCategoryFields();
  1455. $this->addCard = false;
  1456. } catch (\Exception $ex) {
  1457. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1458. }
  1459. }
  1460. public function hideMsg()
  1461. {
  1462. $this->groupMsg = '';
  1463. }
  1464. public function storeCategoryWithID($id)
  1465. {
  1466. $this->groupMsg = '';
  1467. try {
  1468. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first())
  1469. {
  1470. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1471. }
  1472. else
  1473. {
  1474. \App\Models\MemberCategory::create([
  1475. 'member_id' => $this->dataId,
  1476. 'category_id' => $id,
  1477. 'date' => \Carbon\Carbon::now()
  1478. ]);
  1479. session()->flash('success, Associazione creato');
  1480. $this->resetCategoryFields();
  1481. $this->addCard = false;
  1482. }
  1483. } catch (\Exception $ex) {
  1484. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1485. }
  1486. }
  1487. public function deleteCategory($id)
  1488. {
  1489. try{
  1490. \App\Models\MemberCategory::find($id)->delete();
  1491. session()->flash('success',"Associazione eliminata");
  1492. }catch(\Exception $e){
  1493. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1494. }
  1495. }
  1496. public function setCourse($id)
  1497. {
  1498. $this->course_course_id = $id;
  1499. }
  1500. public function getNation($nation)
  1501. {
  1502. if ($nation > 0)
  1503. {
  1504. $ret = \App\Models\Nation::findOrFail($nation);
  1505. return $ret->name;
  1506. }
  1507. return "";
  1508. }
  1509. public function getProvince($province)
  1510. {
  1511. if ($province > 0)
  1512. {
  1513. $ret = \App\Models\Province::findOrFail($province);
  1514. return $ret->name;
  1515. }
  1516. return "";
  1517. }
  1518. public function getCity($city)
  1519. {
  1520. if ($city > 0)
  1521. {
  1522. $ret = \App\Models\City::findOrFail($city);
  1523. return $ret->name;
  1524. }
  1525. return "";
  1526. }
  1527. public function getFiscalCode()
  1528. {
  1529. $this->error_fc = false;
  1530. $cf = new codicefiscale();
  1531. $cf->setDateSeparator('-');
  1532. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0)
  1533. {
  1534. $code = '';
  1535. if ($this->birth_city_id > 0)
  1536. {
  1537. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1538. }
  1539. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1540. $this->fiscal_code = $codice;
  1541. }
  1542. else
  1543. $this->error_fc = true;
  1544. }
  1545. public function setMonth($m)
  1546. {
  1547. $exist = -1;
  1548. foreach($this->course_months as $idx => $x)
  1549. {
  1550. if ($x["m"] == $m)
  1551. {
  1552. $exist = $idx;
  1553. break;
  1554. }
  1555. }
  1556. if ($exist > -1)
  1557. {
  1558. array_splice($this->course_months, $exist, 1);
  1559. }
  1560. else
  1561. {
  1562. $this->course_months[] = array("m" => $m, "status" => "");
  1563. }
  1564. /*
  1565. if (in_array($m, $this->course_months))
  1566. {
  1567. $i = array_search($m, $this->course_months);
  1568. array_splice($this->course_months, $i, 1);
  1569. }
  1570. else
  1571. {
  1572. $this->course_months[] = $m;
  1573. }*/
  1574. }
  1575. public function newPayment($course)
  1576. {
  1577. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1578. $this->emit('setEdit', false);
  1579. $this->emit('setEditCorso', false);
  1580. 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"]);
  1581. }
  1582. public function block($course)
  1583. {
  1584. $xxx = json_decode($course["months"]);
  1585. foreach($xxx as $idx => $mm)
  1586. {
  1587. if (in_array($mm->m, $this->payMonths))
  1588. {
  1589. $xxx[$idx]->status = 2;
  1590. }
  1591. }
  1592. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1593. $c->months = json_encode($xxx);
  1594. $c->save();
  1595. $this->payMonths = array();
  1596. }
  1597. public function reactivate($course)
  1598. {
  1599. $xxx = json_decode($course["months"]);
  1600. foreach($xxx as $idx => $mm)
  1601. {
  1602. if (in_array($mm->m, $this->suspendedMonths))
  1603. {
  1604. $xxx[$idx]->status = "";
  1605. }
  1606. }
  1607. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1608. $c->months = json_encode($xxx);
  1609. $c->save();
  1610. $this->suspendedMonths = array();
  1611. }
  1612. public function setPayMonth($m, $months, $selectedCourseMember)
  1613. {
  1614. if ($this->selectedCourseMember != $selectedCourseMember)
  1615. $this->payMonths = array();
  1616. $this->selectedCourseMember = $selectedCourseMember;
  1617. if (in_array($m, $this->payMonths))
  1618. {
  1619. $i = array_search($m, $this->payMonths);
  1620. array_splice($this->payMonths, $i, 1);
  1621. }
  1622. else if (in_array($m, $this->suspendedMonths))
  1623. {
  1624. $i = array_search($m, $this->suspendedMonths);
  1625. array_splice($this->suspendedMonths, $i, 1);
  1626. }
  1627. else
  1628. {
  1629. foreach(json_decode($months) as $mm)
  1630. {
  1631. if ($mm->m == $m)
  1632. {
  1633. if ($mm->status == "")
  1634. $this->payMonths[] = $m;
  1635. if ($mm->status == "2")
  1636. $this->suspendedMonths[] = $m;
  1637. }
  1638. }
  1639. }
  1640. if (sizeof($this->payMonths) > 0 && sizeof($this->suspendedMonths) > 0)
  1641. {
  1642. $this->payMonths = array();
  1643. $this->suspendedMonths = array();
  1644. }
  1645. }
  1646. public function setCreateSubscription($subscribed, $selectedCourseMember)
  1647. {
  1648. if ($this->selectedCourseMember != $selectedCourseMember)
  1649. $this->payMonths = array();
  1650. $this->selectedCourseMember = $selectedCourseMember;
  1651. if (!$subscribed)
  1652. $this->createSubscription = !$this->createSubscription;
  1653. }
  1654. public function showHideCourse($id)
  1655. {
  1656. if (in_array($id, $this->showCourse))
  1657. {
  1658. $i = array_search($id, $this->showCourse);
  1659. array_splice($this->showCourse, $i, 1);
  1660. }
  1661. else
  1662. {
  1663. $this->showCourse[] = $id;
  1664. }
  1665. }
  1666. public function getMonthStatus($m, $months, $selectedCourseMember)
  1667. {
  1668. $class = "grey";
  1669. foreach(json_decode($months) as $mm)
  1670. {
  1671. if ($mm->m == $m)
  1672. {
  1673. if ($mm->status == "")
  1674. {
  1675. if (in_array($m, $this->payMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1676. $class = "blue";
  1677. else
  1678. $class = "orange";
  1679. }
  1680. if ($mm->status == "1")
  1681. {
  1682. $class = "green";
  1683. }
  1684. if ($mm->status == "2")
  1685. {
  1686. if (in_array($m, $this->suspendedMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1687. $class = "blue";
  1688. else
  1689. $class = "yellow";
  1690. }
  1691. }
  1692. }
  1693. return $class;
  1694. }
  1695. public function getSubscriptionStatus($subscribed, $selectedCourseMember)
  1696. {
  1697. $class = "grey";
  1698. if ($this->createSubscription && $this->selectedCourseMember == $selectedCourseMember)
  1699. $class = "blue";
  1700. else
  1701. $class = "orange";
  1702. if ($subscribed)
  1703. $class = "green";
  1704. return $class;
  1705. }
  1706. public function checkMonth($m)
  1707. {
  1708. $ret = false;
  1709. foreach($this->course_months as $idx => $x)
  1710. {
  1711. if ($x["m"] == $m)
  1712. {
  1713. $ret = true;
  1714. break;
  1715. }
  1716. }
  1717. return $ret;
  1718. }
  1719. public function setDay($idx, $d)
  1720. {
  1721. if (in_array($d, $this->course_when[$idx]["day"]))
  1722. {
  1723. $i = array_search($d, $this->course_when[$idx]["day"]);
  1724. array_splice($this->course_when[$idx]["day"], $i, 1);
  1725. }
  1726. else
  1727. {
  1728. $this->course_when[$idx]["day"][] = $d;
  1729. }
  1730. }
  1731. public function updateBorsellino($importoBorsellino, $s)
  1732. {
  1733. if ($importoBorsellino > 0)
  1734. {
  1735. $imp = $importoBorsellino;
  1736. if ($s == "-")
  1737. $imp = $imp * -1;
  1738. $money = new \App\Models\Money();
  1739. $money->member_id = $this->currentMember->id;
  1740. $money->record_id = null;
  1741. $money->amount = $imp;
  1742. $money->date = date("Y-m-d");
  1743. $money->note = 'Forzato ' . $imp;
  1744. $money->save();
  1745. }
  1746. $this->emit('saved');
  1747. }
  1748. public function getMonth($m)
  1749. {
  1750. $ret = '';
  1751. switch ($m) {
  1752. case 1:
  1753. $ret = 'Gennaio';
  1754. break;
  1755. case 2:
  1756. $ret = 'Febbraio';
  1757. break;
  1758. case 3:
  1759. $ret = 'Marzo';
  1760. break;
  1761. case 4:
  1762. $ret = 'Aprile';
  1763. break;
  1764. case 5:
  1765. $ret = 'Maggio';
  1766. break;
  1767. case 6:
  1768. $ret = 'Giugno';
  1769. break;
  1770. case 7:
  1771. $ret = 'Luglio';
  1772. break;
  1773. case 8:
  1774. $ret = 'Agosto';
  1775. break;
  1776. case 9:
  1777. $ret = 'Settembre';
  1778. break;
  1779. case 10:
  1780. $ret = 'Ottobre';
  1781. break;
  1782. case 11:
  1783. $ret = 'Novembre';
  1784. break;
  1785. case 12:
  1786. $ret = 'Dicembre';
  1787. break;
  1788. default:
  1789. $ret = '';
  1790. break;
  1791. }
  1792. return $ret;
  1793. }
  1794. }
  1795. class codicefiscale {
  1796. /**
  1797. * Array delle consonanti
  1798. */
  1799. protected $_consonanti = array(
  1800. 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K',
  1801. 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
  1802. 'V', 'W', 'X', 'Y', 'Z'
  1803. );
  1804. /**
  1805. * Array delle vocali
  1806. */
  1807. protected $_vocali = array(
  1808. 'A', 'E', 'I', 'O', 'U'
  1809. );
  1810. /**
  1811. * Array per il calcolo della lettera del mese
  1812. * Al numero del mese corrisponde una lettera
  1813. */
  1814. protected $_mesi = array(
  1815. 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
  1816. 6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
  1817. 11 => 'S', 12 => 'T'
  1818. );
  1819. protected $_pari = array(
  1820. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
  1821. '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
  1822. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4,
  1823. 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9,
  1824. 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14,
  1825. 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19,
  1826. 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24,
  1827. 'Z' => 25
  1828. );
  1829. protected $_dispari = array(
  1830. '0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9,
  1831. '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21,
  1832. 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9,
  1833. 'F' => 13, 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21,
  1834. 'K' => 2, 'L' => 4, 'M' => 18, 'N' => 20, 'O' => 11,
  1835. 'P' => 3, 'Q' => 6, 'R' => 8, 'S' => 12, 'T' => 14,
  1836. 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25, 'Y' => 24,
  1837. 'Z' => 23
  1838. );
  1839. protected $_controllo = array(
  1840. '0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
  1841. '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
  1842. '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
  1843. '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P',
  1844. '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T',
  1845. '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X',
  1846. '24' => 'Y', '25' => 'Z'
  1847. );
  1848. /**
  1849. * Stringa di errore
  1850. */
  1851. protected $_error = null;
  1852. /**
  1853. * Separatore per la data di nascita
  1854. */
  1855. protected $_dateSeparator = '/';
  1856. /**
  1857. * Percorso del file del database SQLite
  1858. * dei codici catastali
  1859. */
  1860. protected $_dbCatastali = null;
  1861. /**
  1862. * Trasforma la stringa passata in un array di lettere
  1863. * e lo incrocia con un ulteriore array
  1864. */
  1865. protected function _getLettere($string, array $haystack) {
  1866. $letters = array();
  1867. foreach(str_split($string) as $needle) {
  1868. if (in_array($needle, $haystack)) {
  1869. $letters[] = $needle;
  1870. }
  1871. }
  1872. return $letters;
  1873. }
  1874. /**
  1875. * Ritorna un array con le vocali di una data stringa
  1876. */
  1877. protected function _getVocali($string) {
  1878. return $this->_getLettere($string, $this->_vocali);
  1879. }
  1880. /**
  1881. * Ritorna un array con le consonanti di una data stringa
  1882. */
  1883. protected function _getConsonanti($string) {
  1884. return $this->_getLettere($string, $this->_consonanti);
  1885. }
  1886. /**
  1887. * Pulisce la stringa filtrando tutti i caratteri che
  1888. * non sono lettere. Lo switch $toupper se impostato a TRUE
  1889. * converte la stringa risultante in MAIUSCOLO.
  1890. */
  1891. protected function _sanitize($string, $toupper = true) {
  1892. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  1893. return ($toupper) ? strtoupper($result) : $result;
  1894. }
  1895. /**
  1896. * Se la stringa passata a funzione e' costituita
  1897. * da meno di 3 caratteri, rimpiazza le lettere
  1898. * mancanti con la lettera X.
  1899. */
  1900. protected function _addMissingX($string) {
  1901. $code = $string;
  1902. while(strlen($code) < 3) {
  1903. $code .= 'X';
  1904. }
  1905. return $code;
  1906. }
  1907. /**
  1908. * Ottiene il codice identificativo del nome
  1909. */
  1910. protected function _calcolaNome($string) {
  1911. $nome = $this->_sanitize($string);
  1912. $code = '';
  1913. // Se il nome inserito e' piu' corto di 3 lettere
  1914. // si aggiungono tante X quanti sono i caratteri
  1915. // mancanti.
  1916. if (strlen($nome) < 3) {
  1917. return $this->_addMissingX($nome);
  1918. }
  1919. $nome_cons = $this->_getConsonanti($nome);
  1920. // Se le consonanti contenute nel nome sono minori
  1921. // o uguali a 3 vengono considerate nell'ordine in cui
  1922. // compaiono.
  1923. if (count($nome_cons) <= 3) {
  1924. $code = implode('', $nome_cons);
  1925. } else {
  1926. // Se invece abbiamo almeno 4 consonanti, prendiamo
  1927. // la prima, la terza e la quarta.
  1928. for($i=0; $i<4; $i++) {
  1929. if ($i == 1) continue;
  1930. if (!empty($nome_cons[$i])) {
  1931. $code .= $nome_cons[$i];
  1932. }
  1933. }
  1934. }
  1935. // Se compaiono meno di 3 consonanti nel nome, si
  1936. // utilizzano le vocali, nell'ordine in cui compaiono
  1937. // nel nome.
  1938. if (strlen($code) < 3) {
  1939. $nome_voc = $this->_getVocali($nome);
  1940. while (strlen($code) < 3) {
  1941. $code .= array_shift($nome_voc);
  1942. }
  1943. }
  1944. return $code;
  1945. }
  1946. protected function _calcolaCognome($string) {
  1947. $cognome = $this->_sanitize($string);
  1948. $code = '';
  1949. // Se il cognome inserito e' piu' corto di 3 lettere
  1950. // si aggiungono tante X quanti sono i caratteri
  1951. // mancanti.
  1952. if (strlen($cognome) < 3) {
  1953. return $this->_addMissingX($cognome);
  1954. }
  1955. $cognome_cons = $this->_getConsonanti($cognome);
  1956. // Per il calcolo del cognome si prendono le prime
  1957. // 3 consonanti.
  1958. for ($i=0; $i<3; $i++) {
  1959. if (array_key_exists($i, $cognome_cons)) {
  1960. $code .= $cognome_cons[$i];
  1961. }
  1962. }
  1963. // Se le consonanti non bastano, vengono prese
  1964. // le vocali nell'ordine in cui compaiono.
  1965. if (strlen($code) < 3) {
  1966. $cognome_voc = $this->_getVocali($cognome);
  1967. while (strlen($code) < 3) {
  1968. $code .= array_shift($cognome_voc);
  1969. }
  1970. }
  1971. return $code;
  1972. }
  1973. /**
  1974. * Imposta il separatore di data ( default: / )
  1975. */
  1976. public function setDateSeparator($char) {
  1977. $this->_dateSeparator = $char;
  1978. return $this;
  1979. }
  1980. /**
  1981. * Ritorna la parte di codice fiscale corrispondente
  1982. * alla data di nascita del soggetto (Forma: AAMGG)
  1983. */
  1984. protected function _calcolaDataNascita($data, $sesso) {
  1985. $dn = explode($this->_dateSeparator, $data);
  1986. $giorno = (int) @$dn[2];
  1987. $mese = (int) @$dn[1];
  1988. $anno = (int) @$dn[0];
  1989. // Le ultime due cifre dell'anno di nascita
  1990. $aa = substr($anno, -2);
  1991. // La lettera corrispondente al mese di nascita
  1992. $mm = $this->_mesi[$mese];
  1993. // Il giorno viene calcolato a seconda del sesso
  1994. // del soggetto di cui si calcola il codice:
  1995. // se e' Maschio si mette il giorno reale, se e'
  1996. // Femmina viene aggiungo 40 a questo numero.
  1997. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  1998. // Bug #1: Thanks to Luca
  1999. if (strlen($gg) < 2) $gg = '0' . $gg;
  2000. return $aa . $mm . $gg;
  2001. }
  2002. /**
  2003. * Ritorna la cifra di controllo sulla base dei
  2004. * 15 caratteri del codice fiscale calcolati.
  2005. */
  2006. protected function _calcolaCifraControllo($codice) {
  2007. $code = str_split($codice);
  2008. $sum = 0;
  2009. for($i=1; $i <= count($code); $i++) {
  2010. $cifra = $code[$i-1];
  2011. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  2012. }
  2013. $sum %= 26;
  2014. return $this->_controllo[$sum];
  2015. }
  2016. /**
  2017. * Imposta il messaggio di errore
  2018. */
  2019. protected function _setError($string) {
  2020. $this->_error = $string;
  2021. }
  2022. /**
  2023. * Verifica la presenza di un errore.
  2024. * Ritorna TRUE se presente, FALSE altrimenti.
  2025. */
  2026. public function hasError() {
  2027. return !is_null($this->_error);
  2028. }
  2029. /**
  2030. * Ritorna la stringa di errore
  2031. */
  2032. public function getError() {
  2033. return $this->_error;
  2034. }
  2035. /**
  2036. * Ritorna il codice fiscale utilizzando i parametri
  2037. * passati a funzione. Se si verifica
  2038. */
  2039. public function calcola($nome, $cognome, $data, $sesso, $comune) {
  2040. $codice = $this->_calcolaCognome($cognome) .
  2041. $this->_calcolaNome($nome) .
  2042. $this->_calcolaDataNascita($data, $sesso) .
  2043. $comune;
  2044. if ($this->hasError()) {
  2045. return false;
  2046. }
  2047. $codice .= $this->_calcolaCifraControllo($codice);
  2048. if (strlen($codice) != 16) {
  2049. //$this->_setError(self::ERR_GENERIC);
  2050. return 'ERROR';
  2051. }
  2052. return $codice;
  2053. }
  2054. }