Member.php 106 KB

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