Member.php 106 KB

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