Member.php 75 KB

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