Member.php 101 KB

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