Member.php 108 KB

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