Member.php 104 KB

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