Member.php 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013
  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 Illuminate\Support\Facades\Log;
  8. use DateTime;
  9. class Member extends Component
  10. {
  11. use WithPagination;
  12. protected $paginationTheme = 'bootstrap';
  13. protected $listeners = ['storeCategoryWithID' => 'storeCategoryWithID', 'setCourse' => 'setCourse', 'hideMsg' => 'hideMsg'];
  14. use WithFileUploads;
  15. public $sortField = 'id';
  16. public $sortAsc = false;
  17. public $groupMsg = '';
  18. public $type = 'dati';
  19. public $from = '';
  20. public $selectedCourseMember = 0;
  21. public $festivita = [];
  22. protected $messages = [
  23. 'birth_nation_id.required' => 'birth_nation_id',
  24. 'birth_province_id.required' => 'birth_province_id',
  25. 'birth_city_id.required' => 'birth_city_id',
  26. 'nation_id.required' => 'nation_id',
  27. 'province_id.required' => 'province_id',
  28. 'city_id.required' => 'city_id',
  29. 'gender' => 'gender'
  30. ];
  31. public function change($type)
  32. {
  33. $this->type = $type;
  34. if ($type === 'corsi' && $this->dataId > 0) {
  35. $this->loadMemberCards();
  36. $this->checkCourseAvailability();
  37. }
  38. if ($type === 'presenze') {
  39. //$this->emit('load-chart', $this->mesi, $this->valori);
  40. //if (sizeof($this->mesi) == 0 && sizeof($this->valori) == 0)
  41. $this->loadMemberPresences();
  42. //else
  43. // $this->emit('load-chart', $this->mesi, $this->valori);
  44. }
  45. $this->dispatchBrowserEvent('scroll-to-top');
  46. }
  47. public function sortBy($field)
  48. {
  49. if ($this->sortField === $field) {
  50. $this->sortAsc = ! $this->sortAsc;
  51. } else {
  52. $this->sortAsc = true;
  53. }
  54. $this->sortField = $field;
  55. }
  56. 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;
  57. public $isSaving = false;
  58. 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, $mother_document_files, $father_document_files;
  59. public $document_type, $document_number, $document_from, $document_expire_date, $document_files, $document_files_old;
  60. public $selectId = 0;
  61. public $causalId = 0;
  62. public $error_fc = false;
  63. public $under18 = false;
  64. public $money = 0;
  65. public $refreshAfter = 0;
  66. public $checkedAll = false;
  67. public $multipleIds = [];
  68. public $multipleAction = '';
  69. public $isItaly = true;
  70. public $isBirthItaly = true;
  71. public $showDetail = false;
  72. public $currentMember;
  73. public $currentStatus;
  74. public $age = '';
  75. public $disciplines = array();
  76. public $cards = array();
  77. public $categories = array();
  78. public $courses = array();
  79. public $course_names = array();
  80. public $course_levels = array();
  81. public $course_types = array();
  82. public $course_frequencies = array();
  83. public $course_subscriptions = array();
  84. public $createSubscription = false;
  85. public $payMonths = array();
  86. public $suspendedMonths = array();
  87. public $showCourse = array();
  88. public $searchTxt;
  89. public $search;
  90. public $showReset = false;
  91. public $advanced = false;
  92. public $birthNations = array();
  93. public $birthProvinces = array();
  94. public $birthCities = array();
  95. public $active;
  96. // Card data
  97. 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;
  98. // Categories data
  99. public $member_categories = array(), $category_category_id;
  100. // Courses data
  101. 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;
  102. public $course_name, $course_level_id, $course_type_id, $course_frequency_id;
  103. // Certificates data
  104. public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $cardCertificateId, $addCertificate, $updateCertificate, $certificateDataId;
  105. public $filterCard = [];
  106. public $filterCategory = [];
  107. public $filterCertNormal = 0;
  108. public $filterCertAgonistic = 0;
  109. public $filterCertScaduto = 0;
  110. public $filterCertInScadenza = 0;
  111. public $already_existing = false;
  112. public $tabOrder = ['dati', 'tesseramento', 'corsi', 'gruppi'];
  113. public $presenceYears = [];
  114. public $presenceYearFilter = '';
  115. public $presenceTitle = [];
  116. public $presenceTitleFilter = '';
  117. public $member_presences = [];
  118. public $totals = 0;
  119. public $presenze = 0;
  120. public $assenze = 0;
  121. public $annullate = 0;
  122. public $recuperi = 0;
  123. protected $rules = [
  124. 'first_name' => 'required',
  125. 'last_name' => 'required',
  126. 'email' => 'required',
  127. 'phone' => 'required',
  128. 'birth_date' => 'before_or_equal:today'
  129. ];
  130. public $mesi = [];
  131. public $valori = [];
  132. public function resetFields()
  133. {
  134. $this->dataId = -1;
  135. $this->first_name = '';
  136. $this->last_name = '';
  137. $this->status = '';
  138. $this->birth_city_id = null;
  139. $this->birth_province_id = null;
  140. $this->birth_nation_id = null;
  141. $this->birth_date = null;
  142. $this->birth_place = '';
  143. $this->importoBorsellino = 0;
  144. $this->father_name = '';
  145. $this->mother_name = '';
  146. $this->father_email = '';
  147. $this->mother_email = '';
  148. $this->father_phone = '';
  149. $this->mother_phone = '';
  150. $this->father_fiscal_code = '';
  151. $this->mother_fiscal_code = '';
  152. $this->father_doc_number = '';
  153. $this->mother_doc_number = '';
  154. $this->father_doc_type = '';
  155. $this->mother_doc_type = '';
  156. $this->mother_document_files = array();
  157. $this->father_document_files = array();
  158. $this->gender = null;
  159. $this->no_send_mail = false;
  160. $this->exclude_from_records = false;
  161. $this->fiscal_code = '';
  162. $this->address = '';
  163. $this->zip_code = '';
  164. $this->document_type = '';
  165. $this->document_number = '';
  166. $this->document_from = '';
  167. $this->document_expire_date = null;
  168. $this->document_files = array();
  169. //$this->nation_id = null;
  170. //$this->province_id = null;
  171. $this->nation_id = null;
  172. $this->province_id = null;
  173. $this->city_id = null;
  174. $this->phone = '';
  175. $this->phone2 = '';
  176. $this->phone3 = '';
  177. $this->email = '';
  178. $this->enabled = true;
  179. $this->under18 = false;
  180. $this->money = 0;
  181. $this->image = null;
  182. $this->age = '';
  183. $this->error_fc = false;
  184. $this->isSaving = false;
  185. $this->emit('load-data-table');
  186. }
  187. public function resetCardFields()
  188. {
  189. $this->card_card_id = null;
  190. $this->card_number = '';
  191. $this->card_date = null;
  192. $this->card_accept_date = null;
  193. $this->card_status = 0;
  194. $this->card_discipline1_id = null;
  195. $this->card_discipline2_id = null;
  196. $this->card_discipline3_id = null;
  197. }
  198. public function resetCertificateFields()
  199. {
  200. $this->certificate_type = '';
  201. $this->certificate_filename = '';
  202. $this->certificate_filename_old = '';
  203. $this->certificate_expire_date = date('Y-m-d', strtotime('+1 year'));
  204. $this->certificate_status = 0;
  205. }
  206. public function resetCourseFields()
  207. {
  208. $this->course_course_id = null;
  209. $this->course_when = array();
  210. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  211. $this->course_date_from = null;
  212. $this->course_date_to = null;
  213. $this->course_course_subscription_id = null;
  214. $this->course_status = 0;
  215. $this->course_price = 0;
  216. $this->course_subscription_price = 0;
  217. $this->course_exist = false;
  218. $this->course_subscribed = false;
  219. $this->course_months = array();
  220. $this->course_note = '';
  221. $this->course_name = '';
  222. $this->course_level_id = '';
  223. $this->course_type_id = '';
  224. $this->course_frequency_id = '';
  225. }
  226. public function executeMultipleAction()
  227. {
  228. if ($this->multipleAction == 'delete')
  229. $this->multipleDelete();
  230. }
  231. public function updatedImage()
  232. {
  233. $this->validate([
  234. 'image' => 'image|max:1024',
  235. ]);
  236. $this->image_old = '';
  237. }
  238. public function updatedPresenceYearFilter()
  239. {
  240. $this->loadMemberPresences();
  241. }
  242. public function updatedPresenceTitleFilter()
  243. {
  244. $this->loadMemberPresences();
  245. }
  246. public function updatedCourseName()
  247. {
  248. $this->course_course_id = null;
  249. $this->course_level_id = '';
  250. $this->course_type_id = '';
  251. $this->course_frequency_id = '';
  252. $levels_ids = [];
  253. if ($this->course_name != '') {
  254. list($n, $y) = explode("(", $this->course_name);
  255. $y = trim(str_replace(")", "", $y));
  256. $all = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('enabled', true)->get();
  257. foreach ($all as $a) {
  258. $levels_ids[] = $a->course_level_id;
  259. }
  260. }
  261. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  262. $this->course_types = [];
  263. $this->course_frequencies = [];
  264. }
  265. public function updatedCourseLevelId()
  266. {
  267. $this->course_course_id = null;
  268. $this->course_type_id = '';
  269. $this->course_frequency_id = '';
  270. $types_ids = [];
  271. if ($this->course_level_id != '') {
  272. list($n, $y) = explode("(", $this->course_name);
  273. $y = trim(str_replace(")", "", $y));
  274. $all = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  275. foreach ($all as $a) {
  276. $types_ids[] = $a->course_type_id;
  277. }
  278. }
  279. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  280. $this->course_frequencies = [];
  281. }
  282. public function updatedCourseTypeId()
  283. {
  284. $this->course_course_id = null;
  285. $this->course_frequency_id = '';
  286. $frequencies_ids = [];
  287. if ($this->course_type_id != '') {
  288. list($n, $y) = explode("(", $this->course_name);
  289. $y = trim(str_replace(")", "", $y));
  290. $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();
  291. foreach ($all as $a) {
  292. $frequencies_ids[] = $a->course_frequency_id;
  293. }
  294. }
  295. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  296. }
  297. public function updatedCourseFrequencyId()
  298. {
  299. $this->course_course_id = null;
  300. if ($this->course_frequency_id != '') {
  301. list($n, $y) = explode("(", $this->course_name);
  302. $y = trim(str_replace(")", "", $y));
  303. $course = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")
  304. ->where('year', $y)
  305. ->where('course_level_id', $this->course_level_id)
  306. ->where('course_type_id', $this->course_type_id)
  307. ->where('course_frequency_id', $this->course_frequency_id)
  308. ->first();
  309. if ($course) {
  310. $this->course_course_id = $course->id;
  311. $this->course_price = formatPrice($course->price);
  312. $this->course_subscription_price = formatPrice($course->subscription_price);
  313. } else {
  314. $this->course_price = 0;
  315. $this->course_subscription_price = 0;
  316. $this->course_exist = false;
  317. }
  318. } else {
  319. $this->course_price = 0;
  320. $this->course_subscription_price = 0;
  321. $this->course_exist = false;
  322. }
  323. }
  324. public $documents = [];
  325. public $father_documents = [];
  326. public $mother_documents = [];
  327. public function removeDocument($idx, $type)
  328. {
  329. if ($type == 'father') {
  330. unset($this->father_document_files[$idx]);
  331. } elseif ($type == 'mother') {
  332. unset($this->mother_document_files[$idx]);
  333. } else {
  334. unset($this->document_files[$idx]);
  335. }
  336. }
  337. public function updatedDocuments()
  338. {
  339. foreach ($this->documents as $document) {
  340. $name = $document->getClientOriginalName();
  341. $document->storeAs('public', $name);
  342. $this->document_files[] = $name;
  343. }
  344. $this->documents = [];
  345. }
  346. public function updatedFatherDocuments()
  347. {
  348. foreach ($this->father_documents as $document) {
  349. $name = $document->getClientOriginalName();
  350. $document->storeAs('public', $name);
  351. $this->father_document_files[] = $name;
  352. }
  353. $this->father_documents = [];
  354. }
  355. public function updatedMotherDocuments()
  356. {
  357. foreach ($this->mother_documents as $document) {
  358. $name = $document->getClientOriginalName();
  359. $document->storeAs('public', $name);
  360. $this->mother_document_files[] = $name;
  361. }
  362. $this->mother_documents = [];
  363. }
  364. public function resetCategoryFields()
  365. {
  366. $this->category_category_id = null;
  367. }
  368. public function getCategories($records, $indentation)
  369. {
  370. foreach ($records as $record) {
  371. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
  372. if (count($record->childs))
  373. $this->getCategories($record->childs, $indentation + 1);
  374. }
  375. }
  376. public function getCourses($records, $indentation)
  377. {
  378. foreach ($records as $record) {
  379. $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
  380. if (count($record->childs))
  381. $this->getCourses($record->childs, $indentation + 1);
  382. }
  383. }
  384. public function updatedBirthDate()
  385. {
  386. $this->validateOnly('birth_date');
  387. $date1 = new DateTime($this->birth_date);
  388. $date2 = new DateTime("now");
  389. $interval = $date1->diff($date2);
  390. $this->age = $interval->y . " anni";
  391. $this->under18 = $interval->y < 18;
  392. }
  393. public function mount()
  394. {
  395. if (isset($_GET["new"])) {
  396. $this->refreshAfter = 1;
  397. $this->add();
  398. }
  399. if (isset($_GET["from"])) {
  400. $this->from = $_GET["from"];
  401. }
  402. $this->cards = \App\Models\Card::select('id', 'name')->get();
  403. $this->disciplines = \App\Models\Discipline::select('id', 'name')->get();
  404. $this->categories = array();
  405. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->orderBy('name')->get(), 0);
  406. $this->courses = \App\Models\Course::select('id', 'name')->where('enabled', null)->get();
  407. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  408. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  409. if ($c)
  410. $this->causalId = $c->id;
  411. if (isset($_GET["member_detail"])) {
  412. $this->showDetailF($_GET["member_detail"]);
  413. $this->refreshAfter = 1;
  414. }
  415. // Anni dal 2025 al 2040
  416. for ($anno = 2025; $anno <= 2037; $anno++) {
  417. // Festività fisse
  418. $this->festivita[] = "$anno-01-01"; // Capodanno
  419. $this->festivita[] = "$anno-01-06"; // Epifania
  420. $this->festivita[] = "$anno-04-25"; // Festa della Liberazione
  421. $this->festivita[] = "$anno-05-01"; // Festa del Lavoro
  422. $this->festivita[] = "$anno-06-02"; // Festa della Repubblica
  423. $this->festivita[] = "$anno-08-15"; // Ferragosto
  424. $this->festivita[] = "$anno-11-01"; // Ognissanti
  425. $this->festivita[] = "$anno-12-08"; // Immacolata Concezione
  426. $this->festivita[] = "$anno-12-25"; // Natale
  427. $this->festivita[] = "$anno-12-26"; // Santo Stefano
  428. // Calcolo Pasqua e Pasquetta
  429. $pasqua = date("Y-m-d", easter_date($anno));
  430. $pasquetta = date("Y-m-d", strtotime("$pasqua +1 day"));
  431. $this->festivita[] = $pasqua; // Domenica di Pasqua
  432. $this->festivita[] = $pasquetta; // Lunedì dell'Angelo
  433. }
  434. }
  435. public function updated()
  436. {
  437. if ($this->isSaving) {
  438. $zzz = [];
  439. if ($this->birth_nation_id == null)
  440. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  441. if ($this->isBirthItaly) {
  442. if ($this->birth_province_id == null)
  443. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  444. if ($this->birth_city_id == null)
  445. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  446. }
  447. if ($this->nation_id == null)
  448. $zzz[] = 'nation_id=' . $this->nation_id;
  449. if ($this->isItaly) {
  450. if ($this->province_id == null)
  451. $zzz[] = 'province_id=' . $this->province_id;
  452. if ($this->city_id == null)
  453. $zzz[] = 'city_id=' . $this->city_id;
  454. }
  455. $this->emit('setErrorMsg', $zzz);
  456. }
  457. // $this->emit('load-select');
  458. }
  459. public function hydrate()
  460. {
  461. if ($this->isSaving) {
  462. $zzz = [];
  463. if ($this->birth_nation_id == null)
  464. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  465. if ($this->isBirthItaly) {
  466. if ($this->birth_province_id == null)
  467. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  468. if ($this->birth_city_id == null)
  469. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  470. }
  471. if ($this->nation_id == null)
  472. $zzz[] = 'nation_id=' . $this->nation_id;
  473. if ($this->isItaly) {
  474. if ($this->province_id == null)
  475. $zzz[] = 'province_id=' . $this->province_id;
  476. if ($this->city_id == null)
  477. $zzz[] = 'city_id=' . $this->city_id;
  478. }
  479. $this->emit('setErrorMsg', $zzz);
  480. }
  481. $this->emit('load-select');
  482. }
  483. public function checkIsItaly()
  484. {
  485. if ($this->nation_id > 0) {
  486. $n = \App\Models\Nation::findOrFail($this->nation_id);
  487. $this->isItaly = $n->is_italy;
  488. } else
  489. $this->isItaly = false;
  490. }
  491. public function checkIsBirthItaly()
  492. {
  493. if ($this->birth_nation_id > 0) {
  494. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  495. $this->isBirthItaly = $n->is_italy;
  496. } else
  497. $this->isBirthItaly = false;
  498. }
  499. public function search()
  500. {
  501. if ($this->searchTxt != '') {
  502. $this->search = $this->searchTxt;
  503. $this->showReset = true;
  504. }
  505. }
  506. public function resetSearch()
  507. {
  508. $this->showReset = false;
  509. $this->searchTxt = '';
  510. $this->search = $this->searchTxt;
  511. }
  512. public function advancedSearch()
  513. {
  514. $this->advanced = true;
  515. }
  516. public function advancedSearchCancel()
  517. {
  518. $this->filterCard = [];
  519. $this->filterCategory = [];
  520. $this->filterCertNormal = 0;
  521. $this->filterCertAgonistic = 0;
  522. $this->filterCertScaduto = 0;
  523. $this->filterCertInScadenza = 0;
  524. $this->advanced = false;
  525. }
  526. public function render()
  527. {
  528. $this->course_names = [];
  529. $allC = \App\Models\Course::orderBy('name')->get();
  530. foreach ($allC as $c) {
  531. $cN = $c->name . " (" . $c->year . ")";
  532. if (!in_array($cN, $this->course_names))
  533. $this->course_names[] = $cN;
  534. }
  535. //$this->course_names = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  536. $datas = [];
  537. $this->loadMemberCards();
  538. $this->loadMemberCourses();
  539. $this->loadMemberCategories();
  540. $this->loadMemberCertificates();
  541. return view('livewire.member', ['datas' => $datas]);
  542. }
  543. public function updatedCourseCourseId()
  544. {
  545. if ($this->course_course_id > 0) {
  546. $c = \App\Models\Course::findOrFail($this->course_course_id);
  547. $this->course_price = formatPrice($c->price);
  548. $this->course_subscription_price = formatPrice($c->subscription_price);
  549. // Controllo se sono già iscritto la corso
  550. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  551. } else {
  552. $this->course_price = 0;
  553. $this->course_subscription_price = 0;
  554. $this->course_exist = false;
  555. }
  556. }
  557. public function loadMemberCards()
  558. {
  559. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  560. // return view('livewire.member');
  561. }
  562. public function loadMemberCourses()
  563. {
  564. $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
  565. // return view('livewire.member');
  566. }
  567. public function loadMemberCategories()
  568. {
  569. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  570. // return view('livewire.member');
  571. }
  572. public function loadMemberCertificates()
  573. {
  574. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->orderBy('expire_date', 'DESC')->get();
  575. // return view('livewire.member');
  576. }
  577. public function loadMemberPresences()
  578. {
  579. $this->mesi = array('Settembre', 'Ottobre', 'Novembre', 'Dicembre', 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto');
  580. $this->valori = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  581. $this->loadCombo();
  582. $this->member_presences = [];
  583. // Carico tutti i calendar_id delle presenza
  584. $presences = \App\Models\Presence::where('member_id', $this->dataId)->where('status', '<>', 99)->pluck('calendar_id')->toArray();
  585. $presences_annullate = \App\Models\Presence::where('member_id', $this->dataId)->where('status', 99)->pluck('calendar_id')->toArray();
  586. $course_ids = array();
  587. // Tutti i calendari
  588. if ($this->presenceYearFilter == '' && sizeof($this->presenceYears) > 0)
  589. $this->presenceYearFilter = $this->presenceYears[0];
  590. if ($this->presenceTitleFilter == '' && sizeof($this->presenceTitle) > 0)
  591. $this->presenceTitleFilter = $this->presenceTitle[0];
  592. $curses_ids = \App\Models\Course::where('year', $this->presenceYearFilter)->pluck('id')->toArray();
  593. $calendars = \App\Models\Calendar::whereNull('manual')->whereIn('course_id', $curses_ids)->where('name', $this->presenceTitleFilter)->orderBy('from')->get();
  594. $this->totals = 0;
  595. $this->presenze = 0;
  596. $this->assenze = 0;
  597. $this->annullate = 0;
  598. $this->recuperi = 0;
  599. define("PRESENZE", 0);
  600. define("ASSENZE", 1);
  601. define("RECUPERO", 2);
  602. define("ANNULLATE", 3);
  603. $chartData = [
  604. PRESENZE => [
  605. "label" => "Presenze",
  606. "backgroundColor" => "#0c6197",
  607. "data" => array_fill(0, 12, 0),
  608. "grouped" => true,
  609. "stack" => "chartData",
  610. "barThickness" => "flex",
  611. "barPercentage" => 0.5,
  612. "categoryPercentage" => 0.3,
  613. ],
  614. ASSENZE => [
  615. "label" => "Assenze",
  616. "backgroundColor" => "#ff0000",
  617. "data" => array_fill(0, 12, 0),
  618. "grouped" => true,
  619. "stack" => "chartData",
  620. "barThickness" => "flex",
  621. "barPercentage" => 0.5,
  622. "categoryPercentage" => 0.3,
  623. ],
  624. RECUPERO => [
  625. "label" => "Recupero",
  626. "backgroundColor" => "#7136f6",
  627. "data" => array_fill(0, 12, 0),
  628. "grouped" => true,
  629. "stack" => "chartData",
  630. "barThickness" => "flex",
  631. "barPercentage" => 0.5,
  632. "categoryPercentage" => 0.3,
  633. ],
  634. ANNULLATE => [
  635. "label" => "Annullate",
  636. "backgroundColor" => "#808080",
  637. "data" => array_fill(0, 12, 0),
  638. "grouped" => true,
  639. "stack" => "chartData",
  640. "barThickness" => "flex",
  641. "barPercentage" => 0.5,
  642. "categoryPercentage" => 0.3,
  643. ],
  644. ];
  645. $monthMap = [
  646. 9 => 0,
  647. 10 => 1,
  648. 11 => 2,
  649. 12 => 3,
  650. 1 => 4,
  651. 2 => 5,
  652. 3 => 6,
  653. 4 => 7,
  654. 5 => 8,
  655. 6 => 9,
  656. 7 => 10,
  657. 8 => 11,
  658. ];
  659. foreach($calendars as $calendar)
  660. {
  661. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  662. $dow = date('w', strtotime($calendar->from));
  663. $d = $days[$dow];
  664. $h = date('H:i', strtotime($calendar->from));
  665. // Elenco corsi per tipologia in base al calendario
  666. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to)->pluck('id')->toArray();
  667. $months = date("n", strtotime($calendar->from));
  668. // Elenco utenti iscritti al corso "padre"
  669. if (\App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')->whereDate('date_from', '<=', $calendar->from)->whereDate('date_to', '>=', $calendar->from)->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->where('member_id', $this->dataId)->first())
  670. {
  671. $status = '';
  672. if (in_array($calendar->id, $presences))
  673. {
  674. $status = "<span style=\"color:#0c6197\">Prezenza ordinaria</span>";
  675. $this->presenze += 1;
  676. $this->valori[$monthMap[date('n', strtotime($calendar->from))]] += 1;
  677. $chartData[PRESENZE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
  678. }
  679. else
  680. {
  681. if (in_array($calendar->id, $presences_annullate))
  682. {
  683. $status = "<span style=\"color:gray\">Annullata</span>";
  684. $this->annullate += 1;
  685. $chartData[ANNULLATE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
  686. }
  687. else
  688. {
  689. if (date("Ymd") > date("Ymd", strtotime($calendar->from)))
  690. {
  691. $status = "<span style=\"color:red\">Assenza</span>";
  692. $this->assenze += 1;
  693. $chartData[ASSENZE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
  694. }
  695. }
  696. }
  697. if ($calendar->status == 99)
  698. {
  699. $status = "<span style=\"color:gray\">Annullata</span>";
  700. $this->annullate += 1;
  701. $chartData[ANNULLATE]["data"][$monthMap[date('n', strtotime($calendar->from))]] += 1;
  702. }
  703. $this->member_presences[] = array('calendar_id' => $calendar->id, 'from' => $calendar->from, 'to' => $calendar->to, 'status' => $status);//\App\Models\Presence::where('member_id', $this->dataId)->get();
  704. }
  705. //$courses = array(1);
  706. foreach($courses as $c)
  707. {
  708. $course_ids[] = $c;
  709. }
  710. //$course_ids = array_push($course_ids, $courses);
  711. }
  712. // Manuali (recuperi)
  713. $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->where('name', $this->presenceTitleFilter)->pluck('id')->toArray();
  714. $presences_recuperi = \App\Models\Presence::whereIn('calendar_id', $calendar_recuperi)->where('member_id', $this->dataId)->get();
  715. foreach($presences_recuperi as $p)
  716. {
  717. $this->member_presences[] = array('calendar_id' => $p->calendar->id, 'from' => $p->calendar->from, 'to' => $p->calendar->to, 'status' => '<span style="color:#7136f6">Recupero</span>');//\App\Models\Presence::where('member_id', $this->dataId)->get();
  718. $this->recuperi += 1;
  719. $chartData[RECUPERO]["data"][$monthMap[date('n', strtotime($p->calendar->from))]] += 1;
  720. }
  721. $sortVariable='from';
  722. usort(
  723. $this->member_presences,
  724. fn(array $a, array $b): int => $a[$sortVariable] <=> $b[$sortVariable]
  725. );
  726. $this->emit('load-chart', $this->mesi, $this->valori, $chartData);
  727. //usort($this->member_presences, $this->cmp);
  728. //usort($this->member_presences, function ($a, $b) { return $a['from'] > $b['from']; });
  729. //$this->presenceYears = \App\Models\Course::whereIn('id', $course_ids)->orderBy('year')->groupBy('year')->pluck('year')->toArray();
  730. //if (sizeof($this->presenceYears) > 0)
  731. // $this->presenceYearFilter = $this->presenceYears[0];
  732. //$this->presenceTitle = \App\Models\Course::whereIn('id', $course_ids)->orderBy('name')->groupBy('name')->pluck('name')->toArray();
  733. // return view('livewire.member');
  734. }
  735. public function loadCombo()
  736. {
  737. $course_ids = array();
  738. //$calendars = \App\Models\Calendar::whereNull('manual')->orderBy('from')->get();
  739. /*
  740. foreach($calendars as $calendar)
  741. {
  742. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  743. $dow = date('w', strtotime($calendar->from));
  744. $d = $days[$dow];
  745. // Elenco corsi per tipologia in base al calendario
  746. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to)->pluck('id')->toArray();
  747. //$courses = array(1);
  748. foreach($courses as $c)
  749. {
  750. $course_ids[] = $c;
  751. }
  752. //$course_ids = array_push($course_ids, $courses);
  753. }
  754. */
  755. $course_ids = \App\Models\MemberCourse::where('member_id', $this->dataId)->pluck('course_id')->toArray();
  756. // Manuali (recuperi)
  757. $calendar_recuperi = \App\Models\Calendar::where('manual', 1)->pluck('name')->toArray();
  758. $this->presenceYears = \App\Models\Course::where('created_at', '>', '2025-08-01')->whereIn('id', $course_ids)->orderBy('year')->groupBy('year')->pluck('year')->toArray();
  759. $this->presenceTitle = \App\Models\Course::whereIn('id', $course_ids)->orderBy('name')->groupBy('name')->pluck('name')->toArray();
  760. foreach($calendar_recuperi as $cr)
  761. {
  762. $this->presenceTitle[] = $cr;
  763. }
  764. }
  765. public function showDetailF($id)
  766. {
  767. if (!isset($_GET["from"]) && $this->from == '')
  768. $this->from = 'members';
  769. $this->currentMember = \App\Models\Member::findOrFail($id);
  770. $this->currentStatus = $this->currentMember->getStatus();
  771. $this->showDetail = true;
  772. }
  773. public function add()
  774. {
  775. //$this->from = 'members';
  776. $this->emit('load-select');
  777. $this->emit('hide-search');
  778. $this->showDetail = false;
  779. $this->resetFields();
  780. $this->add = true;
  781. $this->update = false;
  782. $this->emit('setEdit', true);
  783. $this->emit('setEditCorso', false);
  784. }
  785. private function moveToNextTab()
  786. {
  787. $currentIndex = array_search($this->type, $this->tabOrder);
  788. if ($currentIndex !== false && $currentIndex < count($this->tabOrder) - 1) {
  789. $this->type = $this->tabOrder[$currentIndex + 1];
  790. }
  791. }
  792. public function store($close)
  793. {
  794. $this->isSaving = true;
  795. $this->emit('load-select');
  796. $this->emit('goToTop');
  797. $rules = [
  798. 'first_name' => 'required',
  799. 'last_name' => 'required',
  800. 'email' => 'required',
  801. 'phone' => 'required',
  802. 'birth_nation_id' => 'required',
  803. 'address' => 'required',
  804. 'zip_code' => 'required',
  805. 'nation_id' => 'required',
  806. 'birth_date' => 'before_or_equal:today',
  807. 'gender' => 'required'
  808. ];
  809. if ($this->isBirthItaly) {
  810. $rules['birth_province_id'] = 'required';
  811. $rules['birth_city_id'] = 'required';
  812. $rules['fiscal_code'] = 'required';
  813. } else {
  814. $rules['birth_place'] = 'required';
  815. }
  816. if ($this->isItaly) {
  817. $rules['province_id'] = 'required';
  818. $rules['city_id'] = 'required';
  819. }
  820. $zzz = [];
  821. if ($this->birth_nation_id == null)
  822. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  823. if ($this->isBirthItaly) {
  824. if ($this->birth_province_id == null)
  825. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  826. if ($this->birth_city_id == null)
  827. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  828. }
  829. if ($this->nation_id == null)
  830. $zzz[] = 'nation_id=' . $this->nation_id;
  831. if ($this->isItaly) {
  832. if ($this->province_id == null)
  833. $zzz[] = 'province_id=' . $this->province_id;
  834. if ($this->city_id == null)
  835. $zzz[] = 'city_id=' . $this->city_id;
  836. }
  837. $this->emit('setErrorMsg', $zzz);
  838. if (!empty($this->fiscal_code)) {
  839. $existingMember = \App\Models\Member::where('fiscal_code', $this->fiscal_code)->first();
  840. if ($existingMember) {
  841. $this->already_existing = true;
  842. $this->emit('focus-error-field', 'fiscal_code');
  843. return;
  844. }
  845. }
  846. if ($this->under18) {
  847. $rules['father_name'] = 'required_without:mother_name';
  848. $rules['father_email'] = 'required_without:mother_email|email';
  849. $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
  850. $rules['mother_name'] = 'required_without:father_name';
  851. $rules['mother_email'] = 'required_without:father_email|email';
  852. $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
  853. }
  854. // override regole validazione utente
  855. $std_rules = $rules;
  856. $rules = [
  857. 'first_name' => 'required',
  858. 'last_name' => 'required'
  859. ];
  860. try {
  861. $this->validate($rules);
  862. } catch (\Illuminate\Validation\ValidationException $e) {
  863. $this->isSaving = false;
  864. $errorFields = array_keys($e->errors());
  865. if (!empty($errorFields)) {
  866. $this->emit('focus-error-field', $errorFields[0]);
  867. }
  868. Log::error('Validation failed', ['errors' => $e->errors()]);
  869. return;
  870. }
  871. try {
  872. $name = '';
  873. if ($this->image) {
  874. $name = md5($this->image . microtime()) . '.' . $this->image->extension();
  875. $this->image->storeAs('public', $name);
  876. }
  877. $docs = implode("|", $this->document_files);
  878. $father_docs = implode("|", $this->father_document_files);
  879. $mother_docs = implode("|", $this->mother_document_files);
  880. $to_complete = false;
  881. try {
  882. $this->validate($std_rules);
  883. } catch(\Illuminate\Validation\ValidationException $e) {
  884. $to_complete = true;
  885. }
  886. $member = \App\Models\Member::create([
  887. 'first_name' => strtoupper($this->first_name),
  888. 'last_name' => strtoupper($this->last_name),
  889. 'status' => $this->status,
  890. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  891. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  892. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  893. 'birth_date' => $this->birth_date,
  894. 'birth_place' => $this->birth_place,
  895. 'father_name' => $this->father_name,
  896. 'mother_name' => $this->mother_name,
  897. 'father_email' => strtolower($this->father_email),
  898. 'mother_email' => strtolower($this->mother_email),
  899. 'father_phone' => $this->father_phone,
  900. 'mother_phone' => $this->mother_phone,
  901. 'father_fiscal_code' => $this->father_fiscal_code,
  902. 'mother_fiscal_code' => $this->mother_fiscal_code,
  903. 'father_doc_number' => $this->father_doc_number,
  904. 'father_doc_type' => $this->father_doc_type,
  905. 'mother_doc_number' => $this->mother_doc_number,
  906. 'mother_doc_type' => $this->mother_doc_type,
  907. 'document_type' => $this->document_type,
  908. 'document_number' => $this->document_number,
  909. 'document_from' => $this->document_from,
  910. 'document_expire_date' => $this->document_expire_date,
  911. 'document_files' => $docs,
  912. 'mother_document_files' => $mother_docs,
  913. 'father_document_files' => $father_docs,
  914. 'gender' => $this->gender,
  915. 'no_send_mail' => $this->no_send_mail,
  916. 'exclude_from_records' => $this->exclude_from_records,
  917. 'fiscal_code' => $this->fiscal_code,
  918. 'address' => $this->address,
  919. 'zip_code' => $this->zip_code,
  920. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  921. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  922. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  923. 'phone' => $this->phone,
  924. 'phone2' => $this->phone2,
  925. 'phone3' => $this->phone3,
  926. 'email' => strtolower($this->email),
  927. 'image' => $name,
  928. 'to_complete' => $to_complete,
  929. 'enabled' => $this->enabled
  930. ]);
  931. session()->flash('success, Tesserato creato');
  932. updateMemberData($member->id);
  933. $this->resetFields();
  934. if ($close) {
  935. $this->add = false;
  936. } else {
  937. $this->edit($member->id);
  938. $this->emit('saved-and-continue', $this->type);
  939. $this->dispatchBrowserEvent('scroll-to-top');
  940. if ($this->birth_nation_id != null)
  941. $this->isBirthItaly = \App\Models\Nation::findOrFail($this->birth_nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
  942. if ($this->nation_id != null)
  943. $this->isItaly = \App\Models\Nation::findOrFail($this->nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
  944. }
  945. $this->emit('setEdit', false);
  946. } catch (\Exception $ex) {
  947. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  948. }
  949. }
  950. public function duplicate($id)
  951. {
  952. $member = \App\Models\Member::findOrFail($id);
  953. $newMember = $member->replicate();
  954. $newMember->save();
  955. $this->edit($newMember->id);
  956. }
  957. public function edit($id)
  958. {
  959. if (!isset($_GET["from"]) && $this->from == '')
  960. $this->from = 'members';
  961. $this->showDetail = false;
  962. $this->emit('setEdit', true);
  963. $this->emit('setEditCorso', false);
  964. $this->emit('hide-search');
  965. try {
  966. $member = \App\Models\Member::findOrFail($id);
  967. if (!$member) {
  968. session()->flash('error', 'Tesserato non trovato');
  969. } else {
  970. $this->first_name = $member->first_name;
  971. $this->last_name = $member->last_name;
  972. $this->status = $member->status;
  973. $this->birth_city_id = $member->birth_city_id;
  974. $this->birth_province_id = $member->birth_province_id;
  975. $this->birth_nation_id = $member->birth_nation_id;
  976. $this->birth_date = $member->birth_date;
  977. $this->birth_place = $member->birth_place;
  978. $this->father_name = $member->father_name;
  979. $this->mother_name = $member->mother_name;
  980. $this->father_email = strtolower($member->father_email);
  981. $this->mother_email = strtolower($member->mother_email);
  982. $this->father_phone = $member->father_phone;
  983. $this->mother_phone = $member->mother_phone;
  984. $this->father_fiscal_code = $member->father_fiscal_code;
  985. $this->mother_fiscal_code = $member->mother_fiscal_code;
  986. $this->father_doc_number = $member->father_doc_number;
  987. $this->mother_doc_number = $member->mother_doc_number;
  988. $this->father_doc_type = $member->father_doc_type;
  989. $this->mother_doc_type = $member->mother_doc_type;
  990. $this->father_document_files = explode("|", $member->father_document_files);
  991. $this->mother_document_files = explode("|", $member->mother_document_files);
  992. $this->document_type = $member->document_type;
  993. $this->document_number = $member->document_number;
  994. $this->document_from = $member->document_from;
  995. $this->document_expire_date = $member->document_expire_date;
  996. $this->document_files = explode("|", $member->document_files);
  997. $this->gender = $member->gender;
  998. $this->no_send_mail = $member->no_send_mail;
  999. $this->exclude_from_records = $member->exclude_from_records;
  1000. $this->fiscal_code = $member->fiscal_code;
  1001. $this->address = $member->address;
  1002. $this->zip_code = $member->zip_code;
  1003. $this->nation_id = $member->nation_id;
  1004. $this->province_id = $member->province_id;
  1005. $this->city_id = $member->city_id;
  1006. $this->phone = $member->phone;
  1007. $this->phone2 = $member->phone2;
  1008. $this->phone3 = $member->phone3;
  1009. $this->email = strtolower($member->email);
  1010. $this->image_old = $member->image;
  1011. $this->enabled = $member->enabled;
  1012. $this->dataId = $member->id;
  1013. $this->active = $member->getStatus();
  1014. $this->money = $member->getMoney();
  1015. $this->checkIsBirthItaly();
  1016. $date1 = new DateTime($this->birth_date);
  1017. $date2 = new DateTime("now");
  1018. $interval = $date1->diff($date2);
  1019. $this->age = $interval->y . " anni";
  1020. $this->under18 = $interval->y < 18;
  1021. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  1022. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  1023. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  1024. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  1025. $this->update = true;
  1026. $this->add = false;
  1027. //$this->loadMemberCards();
  1028. //$this->loadMemberCourses();
  1029. //$this->loadMemberPresences();
  1030. //$this->loadMemberCategories();
  1031. //$this->loadMemberCertificates();
  1032. $this->loadMemberPresences();
  1033. $this->emit('setIds', $this->nation_id, $this->birth_nation_id);
  1034. $this->emit('load-select');
  1035. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  1036. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  1037. $this->emit('load-cities', $this->province_id, 'cityClass');
  1038. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  1039. }
  1040. } catch (\Exception $ex) {
  1041. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1042. }
  1043. }
  1044. public function updateAAA($close)
  1045. {
  1046. $this->emit('goToTop');
  1047. $this->isSaving = true;
  1048. $this->emit('load-select');
  1049. $rules = [
  1050. 'first_name' => 'required',
  1051. 'last_name' => 'required',
  1052. 'email' => 'required',
  1053. 'phone' => 'required',
  1054. 'birth_nation_id' => 'required',
  1055. 'address' => 'required',
  1056. 'zip_code' => 'required',
  1057. 'nation_id' => 'required',
  1058. 'birth_date' => 'before_or_equal:today'
  1059. ];
  1060. if ($this->isBirthItaly) {
  1061. $rules['birth_province_id'] = 'required';
  1062. $rules['birth_city_id'] = 'required';
  1063. } else {
  1064. $rules['birth_place'] = 'required';
  1065. }
  1066. Log::info('isItaly', ['isItaly' => $this->isItaly]);
  1067. $this->isItaly = $this->checkIsItaly();
  1068. if ($this->isItaly) {
  1069. $rules['province_id'] = 'required';
  1070. $rules['city_id'] = 'required';
  1071. }
  1072. $zzz = [];
  1073. if ($this->birth_nation_id == null)
  1074. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  1075. if ($this->isBirthItaly) {
  1076. if ($this->birth_province_id == null)
  1077. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  1078. if ($this->birth_city_id == null)
  1079. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  1080. }
  1081. if ($this->nation_id == null)
  1082. $zzz[] = 'nation_id=' . $this->nation_id;
  1083. if ($this->isItaly) {
  1084. if ($this->province_id == null)
  1085. $zzz[] = 'province_id=' . $this->province_id;
  1086. if ($this->city_id == null)
  1087. $zzz[] = 'city_id=' . $this->city_id;
  1088. }
  1089. $this->emit('setErrorMsg', $zzz);
  1090. if ($this->under18) {
  1091. $rules['father_name'] = 'required_without:mother_name';
  1092. $rules['father_email'] = 'required_without:mother_email|email';
  1093. $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
  1094. $rules['mother_name'] = 'required_without:father_name';
  1095. $rules['mother_email'] = 'required_without:father_email|email';
  1096. $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
  1097. }
  1098. // override regole validazione utente
  1099. $std_rules = $rules;
  1100. $rules = [
  1101. 'first_name' => 'required',
  1102. 'last_name' => 'required'
  1103. ];
  1104. try {
  1105. $this->validate($rules);
  1106. } catch (\Illuminate\Validation\ValidationException $e) {
  1107. $this->isSaving = false;
  1108. $errorFields = array_keys($e->errors());
  1109. if (!empty($errorFields)) {
  1110. $this->emit('focus-error-field', $errorFields[0]);
  1111. }
  1112. Log::error('Validation failed', ['errors' => $e->errors()]);
  1113. return;
  1114. }
  1115. try {
  1116. $name = '';
  1117. if ($this->image) {
  1118. $name = md5($this->image . microtime()) . '.' . $this->image->extension();
  1119. $this->image->storeAs('public', $name);
  1120. }
  1121. $docs = implode("|", $this->document_files);
  1122. $father_docs = implode("|", $this->father_document_files);
  1123. $mother_docs = implode("|", $this->mother_document_files);
  1124. $to_complete = false;
  1125. try {
  1126. $this->validate($std_rules);
  1127. } catch(\Illuminate\Validation\ValidationException $e) {
  1128. $to_complete = true;
  1129. }
  1130. \App\Models\Member::whereId($this->dataId)->update([
  1131. 'first_name' => strtoupper($this->first_name),
  1132. 'last_name' => strtoupper($this->last_name),
  1133. 'status' => $this->status,
  1134. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  1135. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  1136. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  1137. 'birth_date' => $this->birth_date,
  1138. 'birth_place' => $this->birth_place,
  1139. 'father_name' => $this->father_name,
  1140. 'mother_name' => $this->mother_name,
  1141. 'father_email' => strtolower($this->father_email),
  1142. 'mother_email' => strtolower($this->mother_email),
  1143. 'father_phone' => $this->father_phone,
  1144. 'mother_phone' => $this->mother_phone,
  1145. 'father_fiscal_code' => $this->father_fiscal_code,
  1146. 'mother_fiscal_code' => $this->mother_fiscal_code,
  1147. 'father_doc_number' => $this->father_doc_number,
  1148. 'father_doc_type' => $this->father_doc_type,
  1149. 'mother_doc_number' => $this->mother_doc_number,
  1150. 'mother_doc_type' => $this->mother_doc_type,
  1151. 'father_document_files' => $father_docs,
  1152. 'mother_document_files' => $mother_docs,
  1153. 'document_type' => $this->document_type,
  1154. 'document_number' => $this->document_number,
  1155. 'document_from' => $this->document_from,
  1156. 'document_expire_date' => $this->document_expire_date,
  1157. 'document_files' => $docs,
  1158. 'gender' => $this->gender,
  1159. 'no_send_mail' => $this->no_send_mail,
  1160. 'exclude_from_records' => $this->exclude_from_records,
  1161. 'fiscal_code' => $this->fiscal_code,
  1162. 'address' => $this->address,
  1163. 'zip_code' => $this->zip_code,
  1164. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  1165. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  1166. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  1167. 'image' => $name != '' ? $name : $this->image_old,
  1168. 'phone' => $this->phone,
  1169. 'phone2' => $this->phone2,
  1170. 'phone3' => $this->phone3,
  1171. 'email' => strtolower($this->email),
  1172. 'to_complete' => $to_complete,
  1173. 'enabled' => $this->enabled
  1174. ]);
  1175. updateMemberData($this->dataId);
  1176. session()->flash('success', 'Tesserato aggiornato');
  1177. if ($close) {
  1178. $this->emit('saved-and-close');
  1179. //$this->resetFields();
  1180. //$this->update = false;
  1181. } else {
  1182. $this->emit('saved-and-continue', $this->type);
  1183. $this->dispatchBrowserEvent('scroll-to-top');
  1184. if ($this->birth_nation_id != null)
  1185. $this->isBirthItaly = \App\Models\Nation::findOrFail($this->birth_nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
  1186. if ($this->nation_id != null)
  1187. $this->isItaly = \App\Models\Nation::findOrFail($this->nation_id)->first()->is_italy;// $this->nation_id;//$this->checkIsItaly();
  1188. }
  1189. $this->emit('setEdit', false);
  1190. } catch (\Exception $ex) {
  1191. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1192. }
  1193. }
  1194. public function cancel()
  1195. {
  1196. $this->add = false;
  1197. $this->update = false;
  1198. $this->showDetail = false;
  1199. $this->resetFields();
  1200. $this->emit('setEdit', false);
  1201. //$this->emit('setEditCorso', false);
  1202. $this->emit('reload');
  1203. }
  1204. public function delete($id)
  1205. {
  1206. try {
  1207. \App\Models\Member::find($id)->delete();
  1208. session()->flash('success', "Tesserato eliminato");
  1209. $this->emit('reload');
  1210. } catch (\Exception $e) {
  1211. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1212. }
  1213. }
  1214. public function checkUncheckAll()
  1215. {
  1216. if (!$this->checkedAll) {
  1217. $this->multipleIds = array();
  1218. } else {
  1219. foreach ($this->records as $r) {
  1220. $this->multipleIds[] = $r->id;
  1221. }
  1222. }
  1223. }
  1224. public function multipleDelete()
  1225. {
  1226. try {
  1227. foreach ($this->multipleIds as $id) {
  1228. \App\Models\Member::find($id)->delete();
  1229. }
  1230. } catch (\Exception $e) {
  1231. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1232. }
  1233. $this->multipleAction = '';
  1234. }
  1235. // Card
  1236. public function addCard()
  1237. {
  1238. $this->resetCardFields();
  1239. $this->addCard = true;
  1240. $this->updateCard = false;
  1241. }
  1242. private function checkCourseAvailability()
  1243. {
  1244. $this->active = $this->getActiveStatus();
  1245. $this->emit('course-availability-updated');
  1246. }
  1247. private function getActiveStatus()
  1248. {
  1249. if ($this->dataId > 0) {
  1250. $member = \App\Models\Member::find($this->dataId);
  1251. if ($member) {
  1252. return $member->getStatus();
  1253. }
  1254. }
  1255. return ["status" => 0, "date" => null];
  1256. }
  1257. public function storeCard()
  1258. {
  1259. $this->validate(['card_card_id' => 'required']);
  1260. try {
  1261. // in base alla card selezionata calcolo la scadenza
  1262. $expire_date = null;
  1263. if ($this->card_date != '') {
  1264. $y = date("Y", strtotime($this->card_date));
  1265. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1266. if ($card->next_day_expire > 0 && $card->next_month_expire > 0) {
  1267. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1268. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  1269. $y += 1;
  1270. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1271. if ($next_exp > $this->card_date) {
  1272. $expire_date = $next_exp;
  1273. } else
  1274. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1275. } else {
  1276. if ($card->one_year_expire) {
  1277. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1278. }
  1279. }
  1280. }
  1281. \App\Models\MemberCard::create([
  1282. 'member_id' => $this->dataId,
  1283. 'card_id' => $this->card_card_id,
  1284. 'number' => $this->card_number,
  1285. 'date' => $this->card_date,
  1286. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1287. 'expire_date' => $expire_date,
  1288. 'status' => $this->card_status,
  1289. 'discipline1_id' => $this->card_discipline1_id,
  1290. 'discipline2_id' => $this->card_discipline2_id,
  1291. 'discipline3_id' => $this->card_discipline3_id,
  1292. ]);
  1293. updateMemberData($this->dataId);
  1294. session()->flash('success, Tesserato creato');
  1295. $this->resetCardFields();
  1296. $this->addCard = false;
  1297. $this->loadMemberCards();
  1298. $this->checkCourseAvailability();
  1299. } catch (\Exception $ex) {
  1300. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1301. }
  1302. }
  1303. public function editCard($id)
  1304. {
  1305. try {
  1306. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1307. if (!$memberCard) {
  1308. session()->flash('error', 'Tesserato non trovato');
  1309. } else {
  1310. $this->card_card_id = $memberCard->card_id;
  1311. $this->card_number = $memberCard->number;
  1312. $this->card_date = $memberCard->date;
  1313. $this->card_accept_date = $memberCard->accept_date;
  1314. $this->card_status = $memberCard->status;
  1315. $this->card_discipline1_id = $memberCard->discipline1_id;
  1316. $this->card_discipline2_id = $memberCard->discipline2_id;
  1317. $this->card_discipline3_id = $memberCard->discipline3_id;
  1318. $this->cardDataId = $memberCard->id;
  1319. $this->updateCard = true;
  1320. $this->addCard = false;
  1321. }
  1322. } catch (\Exception $ex) {
  1323. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1324. }
  1325. }
  1326. public function updateCard()
  1327. {
  1328. $this->validate(['card_card_id' => 'required']);
  1329. try {
  1330. Log::info('Starting card update', [
  1331. 'member_id' => $this->dataId,
  1332. 'card_id' => $this->card_card_id,
  1333. 'card_number' => $this->card_number
  1334. ]);
  1335. $expire_date = null;
  1336. if ($this->card_date != '') {
  1337. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1338. Log::info('Card details', [
  1339. 'card_id' => $card->id,
  1340. 'next_day_expire' => $card->next_day_expire,
  1341. 'next_month_expire' => $card->next_month_expire,
  1342. 'one_year_expire' => $card->one_year_expire
  1343. ]);
  1344. if ($card->next_day_expire > 0 && $card->next_month_expire > 0) {
  1345. $y = date("Y", strtotime($this->card_date));
  1346. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1347. $d = strlen($card->next_day_expire) == 1 ? ('0' . $card->next_day_expire) : $card->next_day_expire;
  1348. $next_exp = $y . "-" . $m . "-" . $d;
  1349. // Format dates for comparison
  1350. $input_date_obj = new \DateTime($this->card_date);
  1351. $next_exp_obj = new \DateTime($next_exp);
  1352. if ($next_exp_obj > $input_date_obj) {
  1353. $expire_date = $next_exp;
  1354. } else {
  1355. // Add 1 year to the expiration date if the input date is after the expiration date
  1356. $next_exp_obj->modify('+1 year');
  1357. $expire_date = $next_exp_obj->format('Y-m-d');
  1358. }
  1359. Log::info('Calculated expiration date (next_day_expire/next_month_expire rule)', [
  1360. 'input_date' => $this->card_date,
  1361. 'next_exp' => $next_exp,
  1362. 'expire_date' => $expire_date,
  1363. 'comparison' => ($next_exp_obj > $input_date_obj) ? 'next_exp is after input_date' : 'next_exp is before or equal to input_date'
  1364. ]);
  1365. } else {
  1366. if ($card->one_year_expire) {
  1367. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1368. Log::info('Calculated expiration date (one_year_expire rule)', [
  1369. 'input_date' => $this->card_date,
  1370. 'expire_date' => $expire_date
  1371. ]);
  1372. }
  1373. }
  1374. }
  1375. Log::info('Updating member card', [
  1376. 'card_id' => $this->cardDataId,
  1377. 'member_id' => $this->dataId,
  1378. 'card_number' => $this->card_number,
  1379. 'date' => $this->card_date,
  1380. 'expire_date' => $expire_date,
  1381. 'status' => $this->card_status
  1382. ]);
  1383. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1384. 'member_id' => $this->dataId,
  1385. 'card_id' => $this->card_card_id,
  1386. 'number' => $this->card_number,
  1387. 'date' => $this->card_date,
  1388. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1389. 'expire_date' => $expire_date,
  1390. 'status' => $this->card_status,
  1391. 'discipline1_id' => $this->card_discipline1_id,
  1392. 'discipline2_id' => $this->card_discipline2_id,
  1393. 'discipline3_id' => $this->card_discipline3_id,
  1394. ]);
  1395. updateMemberData($this->dataId);
  1396. Log::info('Card updated successfully', [
  1397. 'card_id' => $this->cardDataId,
  1398. 'member_id' => $this->dataId
  1399. ]);
  1400. session()->flash('success', 'Tesserato aggiornato');
  1401. $this->resetCardFields();
  1402. $this->updateCard = false;
  1403. $this->loadMemberCards();
  1404. $this->checkCourseAvailability();
  1405. } catch (\Exception $ex) {
  1406. Log::error('Error updating card', [
  1407. 'card_id' => $this->cardDataId,
  1408. 'member_id' => $this->dataId,
  1409. 'error_message' => $ex->getMessage(),
  1410. 'error_trace' => $ex->getTraceAsString()
  1411. ]);
  1412. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1413. }
  1414. }
  1415. public function cancelCard()
  1416. {
  1417. $this->addCard = false;
  1418. $this->updateCard = false;
  1419. $this->resetCardFields();
  1420. }
  1421. public function deleteCard($id)
  1422. {
  1423. try {
  1424. \App\Models\MemberCard::find($id)->delete();
  1425. session()->flash('success', "Tesserato eliminato");
  1426. } catch (\Exception $e) {
  1427. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1428. }
  1429. }
  1430. // Courses
  1431. public function addCourse()
  1432. {
  1433. $this->resetCourseFields();
  1434. $this->addCourse = true;
  1435. $this->updateCourse = false;
  1436. $this->emit('setEditCorso', true);
  1437. }
  1438. public function storeCourse()
  1439. {
  1440. $this->validate(['course_course_id' => 'required']);
  1441. try {
  1442. \App\Models\MemberCourse::create([
  1443. 'member_id' => $this->dataId,
  1444. 'course_id' => $this->course_course_id,
  1445. 'date_from' => $this->course_date_from,
  1446. 'date_to' => $this->course_date_to,
  1447. 'course_subscription_id' => $this->course_course_subscription_id,
  1448. 'status' => $this->course_status,
  1449. 'subscribed' => false, // default non iscritto
  1450. 'price' => currencyToDouble($this->course_price),
  1451. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1452. 'notes' => $this->course_note,
  1453. 'months' => json_encode($this->course_months),
  1454. 'when' => json_encode($this->course_when)
  1455. ]);
  1456. $course_name = '';
  1457. // Se il corso ha associato una categoria iscrivo anche al gruppo
  1458. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1459. if ($c) {
  1460. if ($c->category_id > 0) {
  1461. \App\Models\MemberCategory::create([
  1462. 'member_id' => $this->dataId,
  1463. 'category_id' => $c->category_id,
  1464. 'date' => \Carbon\Carbon::now()
  1465. ]);
  1466. }
  1467. $course_name = $c->name;
  1468. }
  1469. $days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
  1470. //foreach($this->course_months as $m)
  1471. //{
  1472. $from = date("Y-m-d", strtotime($c->date_from));
  1473. $to = date("Y-m-d", strtotime($c->date_to));
  1474. $endDate = strtotime($to);
  1475. foreach($this->course_when as $d)
  1476. {
  1477. foreach($d["day"] as $dd)
  1478. {
  1479. $day = '';
  1480. switch ($dd) {
  1481. case 'lun':
  1482. $day = $days[0];
  1483. break;
  1484. case 'mar':
  1485. $day = $days[1];
  1486. break;
  1487. case 'mer':
  1488. $day = $days[2];
  1489. break;
  1490. case 'gio':
  1491. $day = $days[3];
  1492. break;
  1493. case 'ven':
  1494. $day = $days[4];
  1495. break;
  1496. case 'sab':
  1497. $day = $days[5];
  1498. break;
  1499. case 'dom':
  1500. $day = $days[6];
  1501. break;
  1502. default:
  1503. $day = '';
  1504. break;
  1505. }
  1506. if ($day != '')
  1507. {
  1508. for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
  1509. {
  1510. // Controllo che non esiste un corso così
  1511. $exist = \App\Models\Calendar::where('from', date('Y-m-d ' . $d["from"] . ":00", $i))->where('to', date('Y-m-d ' . $d["to"] . ":00", $i))->where('name', $course_name)->first();
  1512. if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita))
  1513. {
  1514. // Creo il calendario del corso
  1515. $calendar = new \App\Models\Calendar();
  1516. $calendar->course_id = $this->course_course_id;
  1517. $calendar->court_id = null;
  1518. $calendar->name = $course_name;
  1519. $calendar->course_type_id = null;
  1520. $calendar->course_duration_id = null;
  1521. $calendar->course_frequency_id = null;
  1522. $calendar->course_level_id = null;
  1523. $calendar->instructor_id = null;
  1524. $calendar->from = date('Y-m-d ' . $d["from"] . ":00", $i);
  1525. $calendar->to = date('Y-m-d ' . $d["to"] . ":00", $i);
  1526. $calendar->note = '';
  1527. $calendar->status = 0;
  1528. $calendar->save();
  1529. }
  1530. }
  1531. }
  1532. }
  1533. }
  1534. //}
  1535. session()->flash('success, Corso creato');
  1536. $this->resetCourseFields();
  1537. $this->addCourse = false;
  1538. $this->emit('setEditCorso', false);
  1539. } catch (\Exception $ex) {
  1540. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1541. }
  1542. }
  1543. public function editCourse($id)
  1544. {
  1545. try {
  1546. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1547. if (!$memberCourse) {
  1548. session()->flash('error', 'Corso non trovato');
  1549. } else {
  1550. $this->course_course_id = $memberCourse->course_id;
  1551. // Carico i dati relativi alla struttura ad albero
  1552. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1553. $this->course_name = $c->name . " (" . $c->year . ")";
  1554. $this->course_level_id = $c->course_level_id;
  1555. $this->course_type_id = $c->course_type_id;
  1556. $this->course_frequency_id = $c->course_frequency_id;
  1557. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->get();
  1558. foreach ($all as $a) {
  1559. $levels_ids[] = $a->course_level_id;
  1560. }
  1561. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1562. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  1563. foreach ($all as $a) {
  1564. $types_ids[] = $a->course_type_id;
  1565. }
  1566. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1567. $frequencies_ids = [];
  1568. $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();
  1569. foreach ($all as $a) {
  1570. $frequencies_ids[] = $a->course_frequency_id;
  1571. }
  1572. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1573. $this->course_when = array();
  1574. $this->course_date_from = $memberCourse->date_from;
  1575. $this->course_date_to = $memberCourse->date_to;
  1576. foreach (json_decode($memberCourse->when) as $z) {
  1577. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1578. }
  1579. //$this->course_when = json_decode($memberCourse->when);
  1580. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1581. $this->course_status = $memberCourse->status;
  1582. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1583. $this->course_price = formatPrice($memberCourse->price);
  1584. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1585. $this->course_note = $memberCourse->notes;
  1586. foreach (json_decode($memberCourse->months) as $z) {
  1587. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1588. }
  1589. $this->courseDataId = $memberCourse->id;
  1590. $this->updateCourse = true;
  1591. $this->addCourse = false;
  1592. $this->emit('setEditCorso', true);
  1593. }
  1594. } catch (\Exception $ex) {
  1595. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1596. }
  1597. }
  1598. public function updateCourse()
  1599. {
  1600. //$this->validate();
  1601. $this->validate(['course_course_id' => 'required']);
  1602. try {
  1603. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1604. 'member_id' => $this->dataId,
  1605. 'course_id' => $this->course_course_id,
  1606. 'date_from' => $this->course_date_from,
  1607. 'date_to' => $this->course_date_to,
  1608. 'course_subscription_id' => $this->course_course_subscription_id,
  1609. 'status' => $this->course_status,
  1610. 'price' => currencyToDouble($this->course_price),
  1611. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1612. 'notes' => $this->course_note,
  1613. 'months' => json_encode($this->course_months),
  1614. 'when' => json_encode($this->course_when)
  1615. ]);
  1616. $course_name = '';
  1617. // Se il corso ha associato una categoria iscrivo anche al gruppo
  1618. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1619. if ($c) {
  1620. if ($c->category_id > 0) {
  1621. \App\Models\MemberCategory::create([
  1622. 'member_id' => $this->dataId,
  1623. 'category_id' => $c->category_id,
  1624. 'date' => \Carbon\Carbon::now()
  1625. ]);
  1626. }
  1627. $course_name = $c->name;
  1628. }
  1629. $days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
  1630. //foreach($this->course_months as $m)
  1631. //{
  1632. $from = date("Y-m-d", strtotime($c->date_from));
  1633. $to = date("Y-m-d", strtotime($c->date_to));
  1634. $endDate = strtotime($to);
  1635. foreach($this->course_when as $d)
  1636. {
  1637. foreach($d["day"] as $dd)
  1638. {
  1639. $day = '';
  1640. switch ($dd) {
  1641. case 'lun':
  1642. $day = $days[0];
  1643. break;
  1644. case 'mar':
  1645. $day = $days[1];
  1646. break;
  1647. case 'mer':
  1648. $day = $days[2];
  1649. break;
  1650. case 'gio':
  1651. $day = $days[3];
  1652. break;
  1653. case 'ven':
  1654. $day = $days[4];
  1655. break;
  1656. case 'sab':
  1657. $day = $days[5];
  1658. break;
  1659. case 'dom':
  1660. $day = $days[6];
  1661. break;
  1662. default:
  1663. $day = '';
  1664. break;
  1665. }
  1666. if ($day != '')
  1667. {
  1668. for($i = strtotime($day, strtotime($from)); $i <= $endDate; $i = strtotime('+1 week', $i))
  1669. {
  1670. // Controllo che non esiste un corso così
  1671. $exist = \App\Models\Calendar::where('from', date('Y-m-d ' . $d["from"] . ":00", $i))->where('to', date('Y-m-d ' . $d["to"] . ":00", $i))->where('name', $course_name)->first();
  1672. if (!$exist && !in_array(date('Y-m-d', $i), $this->festivita))
  1673. {
  1674. // Creo il calendario del corso
  1675. $calendar = new \App\Models\Calendar();
  1676. $calendar->course_id = $this->course_course_id;
  1677. $calendar->court_id = null;
  1678. $calendar->name = $course_name;
  1679. $calendar->course_type_id = null;
  1680. $calendar->course_duration_id = null;
  1681. $calendar->course_frequency_id = null;
  1682. $calendar->course_level_id = null;
  1683. $calendar->instructor_id = null;
  1684. $calendar->from = date('Y-m-d ' . $d["from"] . ":00", $i);
  1685. $calendar->to = date('Y-m-d ' . $d["to"] . ":00", $i);
  1686. $calendar->note = '';
  1687. $calendar->status = 0;
  1688. $calendar->save();
  1689. }
  1690. }
  1691. }
  1692. }
  1693. }
  1694. session()->flash('success', 'Corso aggiornato');
  1695. $this->resetCourseFields();
  1696. $this->updateCourse = false;
  1697. $this->emit('setEditCorso', false);
  1698. } catch (\Exception $ex) {
  1699. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1700. }
  1701. }
  1702. public function cancelCourse()
  1703. {
  1704. $this->addCourse = false;
  1705. $this->updateCourse = false;
  1706. $this->emit('setEditCorso', false);
  1707. $this->resetCourseFields();
  1708. }
  1709. public function deleteCourse($id)
  1710. {
  1711. try {
  1712. \App\Models\MemberCourse::find($id)->delete();
  1713. session()->flash('success', "Corso eliminato");
  1714. } catch (\Exception $e) {
  1715. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1716. }
  1717. }
  1718. public function addRow()
  1719. {
  1720. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1721. }
  1722. public function delRow($idx)
  1723. {
  1724. unset($this->course_when[$idx]);
  1725. }
  1726. // Certificates
  1727. public function addCertificate()
  1728. {
  1729. $this->resetCertificateFields();
  1730. $this->addCertificate = true;
  1731. $this->updateCertificate = false;
  1732. }
  1733. public function storeCertificate()
  1734. {
  1735. $this->validate(['certificate_expire_date' => 'required']);
  1736. // $this->validate();
  1737. try {
  1738. $name = '';
  1739. try {
  1740. if ($this->certificate_filename) {
  1741. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1742. $this->certificate_filename->storeAs('public', $name);
  1743. }
  1744. } catch (\Exception $ex) {
  1745. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1746. }
  1747. if ($this->dataId > -1) {
  1748. \App\Models\MemberCertificate::create([
  1749. 'member_id' => $this->dataId,
  1750. 'type' => $this->certificate_type,
  1751. 'filename' => $name,
  1752. 'expire_date' => $this->certificate_expire_date,
  1753. 'status' => $this->certificate_status
  1754. ]);
  1755. updateMemberData($this->dataId);
  1756. }
  1757. /*else
  1758. {
  1759. $this->certificateTmp = new \App\Models\MemberCertificate();
  1760. $this->certificateTmp->type = $this->certificate_type;
  1761. $this->certificateTmp->filename = $name;
  1762. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1763. $this->certificateTmp->status = $this->certificate_status;
  1764. $this->certificateTmp->status = $this->certificate_status;
  1765. // s $this->member_certificates[] = $certificateTmp;
  1766. }*/
  1767. session()->flash('success, Tesserato creato');
  1768. $this->resetCertificateFields();
  1769. $this->addCertificate = false;
  1770. } catch (\Exception $ex) {
  1771. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1772. }
  1773. }
  1774. public function editCertificate($id)
  1775. {
  1776. try {
  1777. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1778. if (!$memberCertificate) {
  1779. session()->flash('error', 'Tesserato non trovato');
  1780. } else {
  1781. $this->certificate_type = $memberCertificate->type;
  1782. $this->certificate_filename_old = $memberCertificate->filename;
  1783. $this->certificate_expire_date = $memberCertificate->expire_date;
  1784. $this->certificate_status = $memberCertificate->status;
  1785. $this->cardCertificateId = $memberCertificate->id;
  1786. $this->updateCertificate = true;
  1787. $this->addCertificate = false;
  1788. }
  1789. } catch (\Exception $ex) {
  1790. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1791. }
  1792. }
  1793. public function updateCertificate()
  1794. {
  1795. $this->validate(['certificate_expire_date' => 'required']);
  1796. try {
  1797. $name = '';
  1798. try {
  1799. if ($this->certificate_filename) {
  1800. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1801. $this->certificate_filename->storeAs('public', $name);
  1802. }
  1803. } catch (\Exception $ex) {
  1804. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1805. }
  1806. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1807. 'member_id' => $this->dataId,
  1808. 'type' => $this->certificate_type,
  1809. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1810. 'expire_date' => $this->certificate_expire_date,
  1811. 'status' => $this->certificate_status
  1812. ]);
  1813. updateMemberData($this->dataId);
  1814. session()->flash('success', 'Tesserato aggiornato');
  1815. $this->resetCertificateFields();
  1816. $this->updateCertificate = false;
  1817. } catch (\Exception $ex) {
  1818. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1819. }
  1820. }
  1821. public function cancelCertificate()
  1822. {
  1823. $this->addCertificate = false;
  1824. $this->updateCertificate = false;
  1825. $this->resetCertificateFields();
  1826. }
  1827. public function deleteCertificate($id)
  1828. {
  1829. try {
  1830. \App\Models\MemberCertificate::find($id)->delete();
  1831. session()->flash('success', "Tesserato eliminato");
  1832. } catch (\Exception $e) {
  1833. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1834. }
  1835. }
  1836. // Gruppi di appartenenza
  1837. public function storeCategory()
  1838. {
  1839. $this->validate(['category_category_id' => 'required']);
  1840. try {
  1841. \App\Models\MemberCategory::create([
  1842. 'member_id' => $this->dataId,
  1843. 'category_id' => $this->category_category_id,
  1844. 'date' => \Carbon\Carbon::now()
  1845. ]);
  1846. session()->flash('success, Associazione creato');
  1847. $this->resetCategoryFields();
  1848. $this->addCard = false;
  1849. } catch (\Exception $ex) {
  1850. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1851. }
  1852. }
  1853. public function hideMsg()
  1854. {
  1855. $this->groupMsg = '';
  1856. }
  1857. public function storeCategoryWithID($id)
  1858. {
  1859. $this->groupMsg = '';
  1860. try {
  1861. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first()) {
  1862. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1863. } else {
  1864. \App\Models\MemberCategory::create([
  1865. 'member_id' => $this->dataId,
  1866. 'category_id' => $id,
  1867. 'date' => \Carbon\Carbon::now()
  1868. ]);
  1869. session()->flash('success, Associazione creato');
  1870. $this->resetCategoryFields();
  1871. $this->addCard = false;
  1872. }
  1873. } catch (\Exception $ex) {
  1874. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1875. }
  1876. }
  1877. public function deleteCategory($id)
  1878. {
  1879. try {
  1880. \App\Models\MemberCategory::find($id)->delete();
  1881. session()->flash('success', "Associazione eliminata");
  1882. } catch (\Exception $e) {
  1883. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1884. }
  1885. }
  1886. public function setCourse($id)
  1887. {
  1888. $this->course_course_id = $id;
  1889. }
  1890. public function getNation($nation)
  1891. {
  1892. if ($nation > 0) {
  1893. $ret = \App\Models\Nation::findOrFail($nation);
  1894. return $ret->name;
  1895. }
  1896. return "";
  1897. }
  1898. public function getProvince($province)
  1899. {
  1900. if ($province > 0) {
  1901. $ret = \App\Models\Province::findOrFail($province);
  1902. return $ret->name;
  1903. }
  1904. return "";
  1905. }
  1906. public function getCity($city)
  1907. {
  1908. if ($city > 0) {
  1909. $ret = \App\Models\City::findOrFail($city);
  1910. return $ret->name;
  1911. }
  1912. return "";
  1913. }
  1914. public function getFiscalCode()
  1915. {
  1916. $this->error_fc = false;
  1917. $cf = new codicefiscale();
  1918. $cf->setDateSeparator('-');
  1919. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0) {
  1920. $code = '';
  1921. if ($this->birth_city_id > 0) {
  1922. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1923. }
  1924. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1925. $this->fiscal_code = $codice;
  1926. } else
  1927. $this->error_fc = true;
  1928. }
  1929. public function setMonth($m)
  1930. {
  1931. $exist = -1;
  1932. foreach ($this->course_months as $idx => $x) {
  1933. if ($x["m"] == $m) {
  1934. $exist = $idx;
  1935. break;
  1936. }
  1937. }
  1938. if ($exist > -1) {
  1939. array_splice($this->course_months, $exist, 1);
  1940. } else {
  1941. $this->course_months[] = array("m" => $m, "status" => "");
  1942. }
  1943. /*
  1944. if (in_array($m, $this->course_months))
  1945. {
  1946. $i = array_search($m, $this->course_months);
  1947. array_splice($this->course_months, $i, 1);
  1948. }
  1949. else
  1950. {
  1951. $this->course_months[] = $m;
  1952. }*/
  1953. }
  1954. public function newPayment($course)
  1955. {
  1956. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1957. $price = $course["price"];
  1958. if (sizeof($this->payMonths) == 1) {
  1959. $month = $this->payMonths[0];
  1960. $records = \App\Models\Record::where('member_course_id', $this->selectedCourseMember)->where('deleted', 0)->get();
  1961. foreach ($records as $record) {
  1962. if (in_array($month, json_decode($record->months))) {
  1963. foreach ($record->rows as $row) {
  1964. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1965. $tot = sizeof(json_decode($row->when));
  1966. foreach (json_decode($row->when) as $m) {
  1967. $price -= $row->amount / $tot;
  1968. }
  1969. }
  1970. }
  1971. }
  1972. }
  1973. }
  1974. $this->emit('setEdit', false);
  1975. $this->emit('setEditCorso', false);
  1976. 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=' . $price . '&subscription_price=' . $course["subscription_price"] . "&courseId=" . $course["id"]);
  1977. }
  1978. public function block($course)
  1979. {
  1980. $xxx = json_decode($course["months"]);
  1981. foreach ($xxx as $idx => $mm) {
  1982. if (in_array($mm->m, $this->payMonths)) {
  1983. $xxx[$idx]->status = 2;
  1984. }
  1985. }
  1986. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1987. $c->months = json_encode($xxx);
  1988. $c->save();
  1989. $this->payMonths = array();
  1990. }
  1991. public function reactivate($course)
  1992. {
  1993. $xxx = json_decode($course["months"]);
  1994. foreach ($xxx as $idx => $mm) {
  1995. if (in_array($mm->m, $this->suspendedMonths)) {
  1996. $xxx[$idx]->status = "";
  1997. }
  1998. }
  1999. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  2000. $c->months = json_encode($xxx);
  2001. $c->save();
  2002. $this->suspendedMonths = array();
  2003. }
  2004. public function setPayMonth($m, $months, $selectedCourseMember)
  2005. {
  2006. if ($this->selectedCourseMember != $selectedCourseMember)
  2007. $this->payMonths = array();
  2008. $this->selectedCourseMember = $selectedCourseMember;
  2009. if (in_array($m, $this->payMonths)) {
  2010. $i = array_search($m, $this->payMonths);
  2011. array_splice($this->payMonths, $i, 1);
  2012. } else if (in_array($m, $this->suspendedMonths)) {
  2013. $i = array_search($m, $this->suspendedMonths);
  2014. array_splice($this->suspendedMonths, $i, 1);
  2015. } else {
  2016. foreach (json_decode($months) as $mm) {
  2017. if ($mm->m == $m) {
  2018. if ($mm->status == "")
  2019. $this->payMonths[] = $m;
  2020. if ($mm->status == "1") {
  2021. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  2022. $price = $mc->price;
  2023. $payed = 0;
  2024. $extraC = '';
  2025. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  2026. foreach ($recordsPayed as $record) {
  2027. if (in_array($m, json_decode($record->months))) {
  2028. foreach ($record->rows as $row) {
  2029. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  2030. $tot = sizeof(json_decode($row->when));
  2031. $payed += $row->amount / $tot;
  2032. }
  2033. }
  2034. if ($payed < $price)
  2035. $this->payMonths[] = $m;
  2036. }
  2037. }
  2038. }
  2039. if ($mm->status == "2")
  2040. $this->suspendedMonths[] = $m;
  2041. }
  2042. }
  2043. }
  2044. if (sizeof($this->payMonths) > 0 && sizeof($this->suspendedMonths) > 0) {
  2045. $this->payMonths = array();
  2046. $this->suspendedMonths = array();
  2047. }
  2048. }
  2049. public function setCreateSubscription($subscribed, $selectedCourseMember)
  2050. {
  2051. if ($this->selectedCourseMember != $selectedCourseMember)
  2052. $this->payMonths = array();
  2053. $this->selectedCourseMember = $selectedCourseMember;
  2054. if (!$subscribed)
  2055. $this->createSubscription = !$this->createSubscription;
  2056. }
  2057. public function showHideCourse($id)
  2058. {
  2059. if (in_array($id, $this->showCourse)) {
  2060. $i = array_search($id, $this->showCourse);
  2061. array_splice($this->showCourse, $i, 1);
  2062. } else {
  2063. $this->showCourse[] = $id;
  2064. }
  2065. }
  2066. public function getMonthStatus($m, $months, $selectedCourseMember)
  2067. {
  2068. $class = "grey";
  2069. foreach (json_decode($months) as $mm) {
  2070. if ($mm->m == $m) {
  2071. if ($mm->status == "") {
  2072. if (in_array($m, $this->payMonths) && $this->selectedCourseMember == $selectedCourseMember)
  2073. $class = "blue";
  2074. else
  2075. $class = "orange";
  2076. }
  2077. if ($mm->status == "1") {
  2078. $class = "green";
  2079. }
  2080. if ($mm->status == "2") {
  2081. if (in_array($m, $this->suspendedMonths) && $this->selectedCourseMember == $selectedCourseMember)
  2082. $class = "blue";
  2083. else
  2084. $class = "yellow";
  2085. }
  2086. if ($class == 'green') {
  2087. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  2088. $price = $mc->price;
  2089. $payed = 0;
  2090. $extraC = '';
  2091. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  2092. foreach ($recordsPayed as $record) {
  2093. if (in_array($m, json_decode($record->months))) {
  2094. foreach ($record->rows as $row) {
  2095. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  2096. $tot = sizeof(json_decode($row->when));
  2097. $payed += $row->amount / $tot;
  2098. }
  2099. }
  2100. }
  2101. }
  2102. if ($payed < $price)
  2103. $class = 'orange half';
  2104. //$class .= $extraC;
  2105. }
  2106. }
  2107. }
  2108. return $class;
  2109. }
  2110. public function getSubscriptionStatus($subscribed, $selectedCourseMember)
  2111. {
  2112. $class = "grey";
  2113. if ($this->createSubscription && $this->selectedCourseMember == $selectedCourseMember)
  2114. $class = "blue";
  2115. else
  2116. $class = "orange";
  2117. if ($subscribed)
  2118. $class = "green";
  2119. return $class;
  2120. }
  2121. public function checkMonth($m)
  2122. {
  2123. $ret = false;
  2124. foreach ($this->course_months as $idx => $x) {
  2125. if ($x["m"] == $m) {
  2126. $ret = true;
  2127. break;
  2128. }
  2129. }
  2130. return $ret;
  2131. }
  2132. public function setDay($idx, $d)
  2133. {
  2134. if (in_array($d, $this->course_when[$idx]["day"])) {
  2135. $i = array_search($d, $this->course_when[$idx]["day"]);
  2136. array_splice($this->course_when[$idx]["day"], $i, 1);
  2137. } else {
  2138. $this->course_when[$idx]["day"][] = $d;
  2139. }
  2140. }
  2141. public function updateBorsellino($importoBorsellino, $s)
  2142. {
  2143. if ($importoBorsellino > 0) {
  2144. $imp = $importoBorsellino;
  2145. if ($s == "-")
  2146. $imp = $imp * -1;
  2147. $money = new \App\Models\Money();
  2148. $money->member_id = $this->currentMember->id;
  2149. $money->record_id = null;
  2150. $money->amount = $imp;
  2151. $money->date = date("Y-m-d");
  2152. $money->note = 'Forzato ' . $imp;
  2153. $money->save();
  2154. }
  2155. $this->emit('saved');
  2156. }
  2157. public function getMonth($m)
  2158. {
  2159. $ret = '';
  2160. switch ($m) {
  2161. case 1:
  2162. $ret = 'Gennaio';
  2163. break;
  2164. case 2:
  2165. $ret = 'Febbraio';
  2166. break;
  2167. case 3:
  2168. $ret = 'Marzo';
  2169. break;
  2170. case 4:
  2171. $ret = 'Aprile';
  2172. break;
  2173. case 5:
  2174. $ret = 'Maggio';
  2175. break;
  2176. case 6:
  2177. $ret = 'Giugno';
  2178. break;
  2179. case 7:
  2180. $ret = 'Luglio';
  2181. break;
  2182. case 8:
  2183. $ret = 'Agosto';
  2184. break;
  2185. case 9:
  2186. $ret = 'Settembre';
  2187. break;
  2188. case 10:
  2189. $ret = 'Ottobre';
  2190. break;
  2191. case 11:
  2192. $ret = 'Novembre';
  2193. break;
  2194. case 12:
  2195. $ret = 'Dicembre';
  2196. break;
  2197. default:
  2198. $ret = '';
  2199. break;
  2200. }
  2201. return $ret;
  2202. }
  2203. }
  2204. class codicefiscale
  2205. {
  2206. /**
  2207. * Array delle consonanti
  2208. */
  2209. protected $_consonanti = array(
  2210. 'B',
  2211. 'C',
  2212. 'D',
  2213. 'F',
  2214. 'G',
  2215. 'H',
  2216. 'J',
  2217. 'K',
  2218. 'L',
  2219. 'M',
  2220. 'N',
  2221. 'P',
  2222. 'Q',
  2223. 'R',
  2224. 'S',
  2225. 'T',
  2226. 'V',
  2227. 'W',
  2228. 'X',
  2229. 'Y',
  2230. 'Z'
  2231. );
  2232. /**
  2233. * Array delle vocali
  2234. */
  2235. protected $_vocali = array(
  2236. 'A',
  2237. 'E',
  2238. 'I',
  2239. 'O',
  2240. 'U'
  2241. );
  2242. /**
  2243. * Array per il calcolo della lettera del mese
  2244. * Al numero del mese corrisponde una lettera
  2245. */
  2246. protected $_mesi = array(
  2247. 1 => 'A',
  2248. 2 => 'B',
  2249. 3 => 'C',
  2250. 4 => 'D',
  2251. 5 => 'E',
  2252. 6 => 'H',
  2253. 7 => 'L',
  2254. 8 => 'M',
  2255. 9 => 'P',
  2256. 10 => 'R',
  2257. 11 => 'S',
  2258. 12 => 'T'
  2259. );
  2260. protected $_pari = array(
  2261. '0' => 0,
  2262. '1' => 1,
  2263. '2' => 2,
  2264. '3' => 3,
  2265. '4' => 4,
  2266. '5' => 5,
  2267. '6' => 6,
  2268. '7' => 7,
  2269. '8' => 8,
  2270. '9' => 9,
  2271. 'A' => 0,
  2272. 'B' => 1,
  2273. 'C' => 2,
  2274. 'D' => 3,
  2275. 'E' => 4,
  2276. 'F' => 5,
  2277. 'G' => 6,
  2278. 'H' => 7,
  2279. 'I' => 8,
  2280. 'J' => 9,
  2281. 'K' => 10,
  2282. 'L' => 11,
  2283. 'M' => 12,
  2284. 'N' => 13,
  2285. 'O' => 14,
  2286. 'P' => 15,
  2287. 'Q' => 16,
  2288. 'R' => 17,
  2289. 'S' => 18,
  2290. 'T' => 19,
  2291. 'U' => 20,
  2292. 'V' => 21,
  2293. 'W' => 22,
  2294. 'X' => 23,
  2295. 'Y' => 24,
  2296. 'Z' => 25
  2297. );
  2298. protected $_dispari = array(
  2299. '0' => 1,
  2300. '1' => 0,
  2301. '2' => 5,
  2302. '3' => 7,
  2303. '4' => 9,
  2304. '5' => 13,
  2305. '6' => 15,
  2306. '7' => 17,
  2307. '8' => 19,
  2308. '9' => 21,
  2309. 'A' => 1,
  2310. 'B' => 0,
  2311. 'C' => 5,
  2312. 'D' => 7,
  2313. 'E' => 9,
  2314. 'F' => 13,
  2315. 'G' => 15,
  2316. 'H' => 17,
  2317. 'I' => 19,
  2318. 'J' => 21,
  2319. 'K' => 2,
  2320. 'L' => 4,
  2321. 'M' => 18,
  2322. 'N' => 20,
  2323. 'O' => 11,
  2324. 'P' => 3,
  2325. 'Q' => 6,
  2326. 'R' => 8,
  2327. 'S' => 12,
  2328. 'T' => 14,
  2329. 'U' => 16,
  2330. 'V' => 10,
  2331. 'W' => 22,
  2332. 'X' => 25,
  2333. 'Y' => 24,
  2334. 'Z' => 23
  2335. );
  2336. protected $_controllo = array(
  2337. '0' => 'A',
  2338. '1' => 'B',
  2339. '2' => 'C',
  2340. '3' => 'D',
  2341. '4' => 'E',
  2342. '5' => 'F',
  2343. '6' => 'G',
  2344. '7' => 'H',
  2345. '8' => 'I',
  2346. '9' => 'J',
  2347. '10' => 'K',
  2348. '11' => 'L',
  2349. '12' => 'M',
  2350. '13' => 'N',
  2351. '14' => 'O',
  2352. '15' => 'P',
  2353. '16' => 'Q',
  2354. '17' => 'R',
  2355. '18' => 'S',
  2356. '19' => 'T',
  2357. '20' => 'U',
  2358. '21' => 'V',
  2359. '22' => 'W',
  2360. '23' => 'X',
  2361. '24' => 'Y',
  2362. '25' => 'Z'
  2363. );
  2364. /**
  2365. * Stringa di errore
  2366. */
  2367. protected $_error = null;
  2368. /**
  2369. * Separatore per la data di nascita
  2370. */
  2371. protected $_dateSeparator = '/';
  2372. /**
  2373. * Percorso del file del database SQLite
  2374. * dei codici catastali
  2375. */
  2376. protected $_dbCatastali = null;
  2377. /**
  2378. * Trasforma la stringa passata in un array di lettere
  2379. * e lo incrocia con un ulteriore array
  2380. */
  2381. protected function _getLettere($string, array $haystack)
  2382. {
  2383. $letters = array();
  2384. foreach (str_split($string) as $needle) {
  2385. if (in_array($needle, $haystack)) {
  2386. $letters[] = $needle;
  2387. }
  2388. }
  2389. return $letters;
  2390. }
  2391. /**
  2392. * Ritorna un array con le vocali di una data stringa
  2393. */
  2394. protected function _getVocali($string)
  2395. {
  2396. return $this->_getLettere($string, $this->_vocali);
  2397. }
  2398. /**
  2399. * Ritorna un array con le consonanti di una data stringa
  2400. */
  2401. protected function _getConsonanti($string)
  2402. {
  2403. return $this->_getLettere($string, $this->_consonanti);
  2404. }
  2405. /**
  2406. * Pulisce la stringa filtrando tutti i caratteri che
  2407. * non sono lettere. Lo switch $toupper se impostato a TRUE
  2408. * converte la stringa risultante in MAIUSCOLO.
  2409. */
  2410. protected function _sanitize($string, $toupper = true)
  2411. {
  2412. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  2413. return ($toupper) ? strtoupper($result) : $result;
  2414. }
  2415. /**
  2416. * Se la stringa passata a funzione e' costituita
  2417. * da meno di 3 caratteri, rimpiazza le lettere
  2418. * mancanti con la lettera X.
  2419. */
  2420. protected function _addMissingX($string)
  2421. {
  2422. $code = $string;
  2423. while (strlen($code) < 3) {
  2424. $code .= 'X';
  2425. }
  2426. return $code;
  2427. }
  2428. /**
  2429. * Ottiene il codice identificativo del nome
  2430. */
  2431. protected function _calcolaNome($string)
  2432. {
  2433. $nome = $this->_sanitize($string);
  2434. $code = '';
  2435. // Se il nome inserito e' piu' corto di 3 lettere
  2436. // si aggiungono tante X quanti sono i caratteri
  2437. // mancanti.
  2438. if (strlen($nome) < 3) {
  2439. return $this->_addMissingX($nome);
  2440. }
  2441. $nome_cons = $this->_getConsonanti($nome);
  2442. // Se le consonanti contenute nel nome sono minori
  2443. // o uguali a 3 vengono considerate nell'ordine in cui
  2444. // compaiono.
  2445. if (count($nome_cons) <= 3) {
  2446. $code = implode('', $nome_cons);
  2447. } else {
  2448. // Se invece abbiamo almeno 4 consonanti, prendiamo
  2449. // la prima, la terza e la quarta.
  2450. for ($i = 0; $i < 4; $i++) {
  2451. if ($i == 1) continue;
  2452. if (!empty($nome_cons[$i])) {
  2453. $code .= $nome_cons[$i];
  2454. }
  2455. }
  2456. }
  2457. // Se compaiono meno di 3 consonanti nel nome, si
  2458. // utilizzano le vocali, nell'ordine in cui compaiono
  2459. // nel nome.
  2460. if (strlen($code) < 3) {
  2461. $nome_voc = $this->_getVocali($nome);
  2462. while (strlen($code) < 3) {
  2463. $code .= array_shift($nome_voc);
  2464. }
  2465. }
  2466. return $code;
  2467. }
  2468. protected function _calcolaCognome($string)
  2469. {
  2470. $cognome = $this->_sanitize($string);
  2471. $code = '';
  2472. // Se il cognome inserito e' piu' corto di 3 lettere
  2473. // si aggiungono tante X quanti sono i caratteri
  2474. // mancanti.
  2475. if (strlen($cognome) < 3) {
  2476. return $this->_addMissingX($cognome);
  2477. }
  2478. $cognome_cons = $this->_getConsonanti($cognome);
  2479. // Per il calcolo del cognome si prendono le prime
  2480. // 3 consonanti.
  2481. for ($i = 0; $i < 3; $i++) {
  2482. if (array_key_exists($i, $cognome_cons)) {
  2483. $code .= $cognome_cons[$i];
  2484. }
  2485. }
  2486. // Se le consonanti non bastano, vengono prese
  2487. // le vocali nell'ordine in cui compaiono.
  2488. if (strlen($code) < 3) {
  2489. $cognome_voc = $this->_getVocali($cognome);
  2490. while (strlen($code) < 3) {
  2491. $code .= array_shift($cognome_voc);
  2492. }
  2493. }
  2494. return $code;
  2495. }
  2496. /**
  2497. * Imposta il separatore di data ( default: / )
  2498. */
  2499. public function setDateSeparator($char)
  2500. {
  2501. $this->_dateSeparator = $char;
  2502. return $this;
  2503. }
  2504. /**
  2505. * Ritorna la parte di codice fiscale corrispondente
  2506. * alla data di nascita del soggetto (Forma: AAMGG)
  2507. */
  2508. protected function _calcolaDataNascita($data, $sesso)
  2509. {
  2510. $dn = explode($this->_dateSeparator, $data);
  2511. $giorno = (int) @$dn[2];
  2512. $mese = (int) @$dn[1];
  2513. $anno = (int) @$dn[0];
  2514. // Le ultime due cifre dell'anno di nascita
  2515. $aa = substr($anno, -2);
  2516. // La lettera corrispondente al mese di nascita
  2517. $mm = $this->_mesi[$mese];
  2518. // Il giorno viene calcolato a seconda del sesso
  2519. // del soggetto di cui si calcola il codice:
  2520. // se e' Maschio si mette il giorno reale, se e'
  2521. // Femmina viene aggiungo 40 a questo numero.
  2522. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  2523. // Bug #1: Thanks to Luca
  2524. if (strlen($gg) < 2) $gg = '0' . $gg;
  2525. return $aa . $mm . $gg;
  2526. }
  2527. /**
  2528. * Ritorna la cifra di controllo sulla base dei
  2529. * 15 caratteri del codice fiscale calcolati.
  2530. */
  2531. protected function _calcolaCifraControllo($codice)
  2532. {
  2533. $code = str_split($codice);
  2534. $sum = 0;
  2535. for ($i = 1; $i <= count($code); $i++) {
  2536. $cifra = $code[$i - 1];
  2537. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  2538. }
  2539. $sum %= 26;
  2540. return $this->_controllo[$sum];
  2541. }
  2542. /**
  2543. * Imposta il messaggio di errore
  2544. */
  2545. protected function _setError($string)
  2546. {
  2547. $this->_error = $string;
  2548. }
  2549. /**
  2550. * Verifica la presenza di un errore.
  2551. * Ritorna TRUE se presente, FALSE altrimenti.
  2552. */
  2553. public function hasError()
  2554. {
  2555. return !is_null($this->_error);
  2556. }
  2557. /**
  2558. * Ritorna la stringa di errore
  2559. */
  2560. public function getError()
  2561. {
  2562. return $this->_error;
  2563. }
  2564. /**
  2565. * Ritorna il codice fiscale utilizzando i parametri
  2566. * passati a funzione. Se si verifica
  2567. */
  2568. public function calcola($nome, $cognome, $data, $sesso, $comune)
  2569. {
  2570. $codice = $this->_calcolaCognome($cognome) .
  2571. $this->_calcolaNome($nome) .
  2572. $this->_calcolaDataNascita($data, $sesso) .
  2573. $comune;
  2574. if ($this->hasError()) {
  2575. return false;
  2576. }
  2577. $codice .= $this->_calcolaCifraControllo($codice);
  2578. if (strlen($codice) != 16) {
  2579. //$this->_setError(self::ERR_GENERIC);
  2580. return 'ERROR';
  2581. }
  2582. return $codice;
  2583. }
  2584. }