Member.php 103 KB

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