Member.php 101 KB

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