Member.php 104 KB

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