Member.php 103 KB

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