Member.php 92 KB

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