Member.php 106 KB

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