Member.php 109 KB

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