Member.php 104 KB

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