Member.php 76 KB

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