Member.php 97 KB

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