Member.php 105 KB

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