Member.php 106 KB

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