Member.php 107 KB

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