Member.php 109 KB

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