Member.php 81 KB

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