Member.php 81 KB

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