Member.php 103 KB

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