Member.php 105 KB

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