Member.php 91 KB

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