Member.php 109 KB

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