Member.php 104 KB

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