Member.php 92 KB

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