Member.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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 function sortBy($field)
  18. {
  19. if($this->sortField === $field)
  20. {
  21. $this->sortAsc = ! $this->sortAsc;
  22. } else {
  23. $this->sortAsc = true;
  24. }
  25. $this->sortField = $field;
  26. }
  27. public $records, $first_name, $last_name, $image, $image_old, $status, $birth_place, $birth_city_id, $birth_province_id, $birth_nation_id, $birth_date, $gender, $fiscal_code, $address, $zip_code, $nation_id, $province_id, $city_id, $phone, $phone2, $phone3, $email, $enabled, $dataId, $update = false, $add = false;
  28. 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;
  29. public $document_type, $document_number, $document_from, $document_expire_date, $document_files, $document_files_old;
  30. public $selectId = 0;
  31. public $causalId = 0;
  32. public $error_fc = false;
  33. public $under18 = false;
  34. public $refreshAfter = 0;
  35. public $checkedAll = false;
  36. public $multipleIds = [];
  37. public $multipleAction = '';
  38. public $isItaly = true;
  39. public $isBirthItaly = true;
  40. public $showDetail = false;
  41. public $currentMember;
  42. public $currentStatus;
  43. public $age = '';
  44. public $cards = array();
  45. public $categories = array();
  46. public $courses = array();
  47. public $searchTxt;
  48. public $search;
  49. public $showReset = false;
  50. public $advanced = false;
  51. public $birthNations = array();
  52. public $birthProvinces = array();
  53. public $birthCities = array();
  54. public $active;
  55. // Card data
  56. public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_status, $addCard, $updateCard, $cardDataId;
  57. // Categories data
  58. public $member_categories = array(), $category_category_id;
  59. // Courses data
  60. public $member_courses = array(), $course_course_id, $course_date_from, $course_date_to, $course_when = array(), $addCourse, $updateCourse, $courseDataId;
  61. // Certificates data
  62. public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
  63. public $filterCard = [];
  64. public $filterCategory = [];
  65. public $filterCertNormal = 0;
  66. public $filterCertAgonistic = 0;
  67. public $filterCertScaduto = 0;
  68. public $filterCertInScadenza = 0;
  69. protected $rules = [
  70. 'first_name' => 'required',
  71. 'last_name' => 'required',
  72. ];
  73. public function resetFields(){
  74. $this->dataId = -1;
  75. $this->first_name = '';
  76. $this->last_name = '';
  77. $this->status = '';
  78. $this->birth_city_id = null;
  79. $this->birth_province_id = null;
  80. $this->birth_nation_id = null;
  81. $this->birth_date = null;
  82. $this->birth_place = '';
  83. $this->father_name = '';
  84. $this->mother_name = '';
  85. $this->father_email = '';
  86. $this->mother_email = '';
  87. $this->father_phone = '';
  88. $this->mother_phone = '';
  89. $this->father_fiscal_code = '';
  90. $this->mother_fiscal_code = '';
  91. $this->father_doc_number = '';
  92. $this->mother_doc_number = '';
  93. $this->father_doc_type = '';
  94. $this->mother_doc_type = '';
  95. $this->gender = '';
  96. $this->fiscal_code = '';
  97. $this->address = '';
  98. $this->zip_code = '';
  99. $this->document_type = '';
  100. $this->document_number = '';
  101. $this->document_from = '';
  102. $this->document_expire_date = null;
  103. $this->document_files = array();
  104. //$this->nation_id = null;
  105. //$this->province_id = null;
  106. $this->nation_id = null;
  107. $this->province_id = null;
  108. $this->city_id = null;
  109. $this->phone = '';
  110. $this->phone2 = '';
  111. $this->phone3 = '';
  112. $this->email = '';
  113. $this->enabled = true;
  114. $this->under18 = false;
  115. $this->image = null;
  116. $this->error_fc = false;
  117. $this->emit('load-data-table');
  118. }
  119. public function resetCardFields(){
  120. $this->card_card_id = null;
  121. $this->card_number = '';
  122. $this->card_date = null;
  123. $this->card_accept_date = null;
  124. $this->card_status = 0;
  125. }
  126. public function resetCertificateFields(){
  127. $this->certificate_type = 'N';
  128. $this->certificate_filename = '';
  129. $this->certificate_filename_old = '';
  130. $this->certificate_expire_date = null;
  131. $this->certificate_status = 0;
  132. }
  133. public function resetCourseFields(){
  134. $this->course_course_id = null;
  135. $this->course_when = array();
  136. $this->course_when[] = array('day' => '', 'from' => '', 'to' => '');
  137. $this->course_date_from = null;
  138. $this->course_date_to = null;
  139. }
  140. public function executeMultipleAction(){
  141. if ($this->multipleAction == 'delete')
  142. $this->multipleDelete();
  143. }
  144. public function updatedImage()
  145. {
  146. $this->validate([
  147. 'image' => 'image|max:1024',
  148. ]);
  149. $this->image_old = '';
  150. }
  151. public $documents = [];
  152. public function removeDocument($idx)
  153. {
  154. unset($this->document_files[$idx]);
  155. }
  156. public function updatedDocuments()
  157. {
  158. foreach ($this->documents as $document)
  159. {
  160. $name = $document->getClientOriginalName(); // . '.'.$allegato->extension();
  161. $document->storeAs('public', $name);
  162. $this->document_files[] = $name;
  163. }
  164. $this->documents = [];
  165. }
  166. public function resetCategoryFields(){
  167. $this->category_category_id = null;
  168. }
  169. public function getCategories($records, $indentation)
  170. {
  171. foreach($records as $record)
  172. {
  173. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  174. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  175. if(count($record->childs))
  176. $this->getCategories($record->childs, $indentation + 1);
  177. }
  178. }
  179. public function getCourses($records, $indentation)
  180. {
  181. foreach($records as $record)
  182. {
  183. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  184. $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
  185. if(count($record->childs))
  186. $this->getCourses($record->childs, $indentation + 1);
  187. }
  188. }
  189. public function updatedBirthDate()
  190. {
  191. $date1 = new DateTime($this->birth_date);
  192. $date2 = new DateTime("now");
  193. $interval = $date1->diff($date2);
  194. $this->age = $interval->y . " anni";
  195. $this->under18 = $interval->y < 18;
  196. }
  197. public function mount()
  198. {
  199. if (isset($_GET["new"]))
  200. {
  201. $this->refreshAfter = 1;
  202. $this->add();
  203. }
  204. $this->cards = \App\Models\Card::select('id', 'name')->get();
  205. $this->categories = array();
  206. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  207. $this->courses = array();
  208. $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->get(), 0);
  209. /*$this->nations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  210. $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  211. $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  212. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  213. if ($c)
  214. $this->causalId = $c->id;
  215. /*$this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  216. $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  217. $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  218. }
  219. public function updated()
  220. {
  221. // $this->emit('load-select');
  222. }
  223. public function hydrate()
  224. {
  225. $this->emit('load-select');
  226. // $this->emit('destroy-data-table');
  227. }
  228. public function checkIsItaly()
  229. {
  230. $n = \App\Models\Nation::findOrFail($this->nation_id);
  231. $this->isItaly = $n->is_italy;
  232. }
  233. public function checkIsBirthItaly()
  234. {
  235. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  236. $this->isBirthItaly = $n->is_italy;
  237. }
  238. /*public function loadProvinces()
  239. {
  240. $n = \App\Models\Nation::findOrFail($this->nation_id);
  241. $this->isItaly = $n->is_italy;
  242. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
  243. $this->cities = array();
  244. $this->selectId++;
  245. }
  246. public function loadCities()
  247. {
  248. $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
  249. $this->selectId++;
  250. }
  251. public function loadBirthProvinces()
  252. {
  253. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  254. $this->isBirthItaly = $n->is_italy;
  255. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
  256. $this->birthCities = array();
  257. $this->selectId++;
  258. }
  259. public function loadBirthCities()
  260. {
  261. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  262. $this->selectId++;
  263. }
  264. */
  265. public function search()
  266. {
  267. if ($this->searchTxt != '')
  268. {
  269. $this->search = $this->searchTxt;
  270. $this->showReset = true;
  271. }
  272. }
  273. public function resetSearch()
  274. {
  275. $this->showReset = false;
  276. $this->searchTxt = '';
  277. $this->search = $this->searchTxt;
  278. }
  279. public function advancedSearch()
  280. {
  281. $this->advanced = true;
  282. }
  283. public function advancedSearchCancel()
  284. {
  285. $this->filterCard = [];
  286. $this->filterCategory = [];
  287. $this->filterCertNormal = 0;
  288. $this->filterCertAgonistic = 0;
  289. $this->filterCertScaduto = 0;
  290. $this->filterCertInScadenza = 0;
  291. $this->advanced = false;
  292. }
  293. public function render()
  294. {
  295. // $this->emit('destroy-data-table');
  296. $datas = [];
  297. if (!$this->advanced)
  298. {
  299. /*if ($this->search != '')
  300. $datas = \App\Models\Member::select('members.*') // , \DB::raw('SUM(records.id) As total'))
  301. ->where('first_name', 'LIKE', '%' . $this->search . '%')
  302. ->orWhere('last_name', 'LIKE', '%' . $this->search . '%')
  303. ->orWhere('email', 'LIKE', '%' . $this->search . '%');
  304. $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
  305. else*/
  306. $this->records = \App\Models\Member::get();
  307. //$datas = \App\Models\Member::select('members.*');
  308. }
  309. else
  310. {
  311. //$this->records = \App\Models\Member::where('id', '>', 0);
  312. $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  313. if (sizeof($this->filterCard) > 0)
  314. {
  315. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->filterCard)->pluck('member_id');
  316. //$this->records->whereIn('id', $card_ids);
  317. $datas = $datas->whereIn('id', $card_ids);
  318. }
  319. if (sizeof($this->filterCategory) > 0)
  320. {
  321. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', $this->filterCategory)->pluck('member_id');
  322. //$this->records->whereIn('id', $cats_ids);
  323. $datas = $datas->whereIn('id', $cats_ids);
  324. }
  325. $certs = [];
  326. if ($this->filterCertNormal > 0)
  327. {
  328. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  329. //$this->records->whereIn('id', $normal);
  330. $datas = $datas->whereIn('id', $normal);;
  331. }
  332. if ($this->filterCertAgonistic > 0)
  333. {
  334. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  335. //$this->records->whereIn('id', $agonistic);
  336. $datas = $datas->whereIn('id', $agonistic);
  337. }
  338. if ($this->filterCertScaduto > 0)
  339. {
  340. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  341. //$this->records->whereIn('id', $scaduto);
  342. $datas = $datas->whereIn('id', $scaduto);
  343. }
  344. if ($this->filterCertInScadenza > 0)
  345. {
  346. $scaduto = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  347. //$this->records->whereIn('id', $scaduto);
  348. $datas = $datas->whereIn('id', $scaduto);
  349. }
  350. if (sizeof($certs) > 0)
  351. {
  352. $datas = $datas->whereIn('id', $certs);
  353. //$this->records->whereIn('id', $certs);
  354. }
  355. $this->records = $datas->get();
  356. }
  357. /*
  358. foreach($this->records as $r)
  359. {
  360. $r->age = $r->getAge();
  361. $r->status = $r->isActive()["status"];
  362. $r->certificate = $r->hasCertificate()["status"];
  363. $r->state = $r->getStatus()["status"];
  364. }
  365. if ($this->sortAsc)
  366. $this->records = $this->records->sortBy($this->sortField);
  367. else
  368. $this->records = $this->records->sortByDesc($this->sortField);
  369. */
  370. //$datas = $datas->get(); // ->orderBy($this->sortField, $this->sortAsc ? 'ASC' : 'DESC')->paginate(10);
  371. //$this->records = $this->records->get();
  372. $this->loadMemberCards();
  373. $this->loadMemberCourses();
  374. $this->loadMemberCategories();
  375. $this->loadMemberCertificates();
  376. $this->emit('load-data-table');
  377. return view('livewire.member', ['datas' => $datas]);
  378. }
  379. public function loadMemberCards()
  380. {
  381. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  382. // return view('livewire.member');
  383. }
  384. public function loadMemberCourses()
  385. {
  386. $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
  387. // return view('livewire.member');
  388. }
  389. public function loadMemberCategories()
  390. {
  391. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  392. // return view('livewire.member');
  393. }
  394. public function loadMemberCertificates()
  395. {
  396. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->orderBy('expire_date', 'DESC')->get();
  397. // return view('livewire.member');
  398. }
  399. public function showDetail($id)
  400. {
  401. $this->currentMember = \App\Models\Member::findOrFail($id);
  402. $this->currentStatus = $this->currentMember->getStatus();
  403. $this->showDetail = true;
  404. }
  405. public function add()
  406. {
  407. $this->emit('load-select');
  408. $this->emit('hide-search');
  409. $this->showDetail = false;
  410. $this->resetFields();
  411. $this->add = true;
  412. $this->update = false;
  413. }
  414. public function store($close)
  415. {
  416. $this->emit('load-select');
  417. $this->validate();
  418. try {
  419. $name = '';
  420. if ($this->image)
  421. {
  422. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  423. $this->image->storeAs('public', $name);
  424. }
  425. $docs = implode("|", $this->document_files);
  426. $member = \App\Models\Member::create([
  427. 'first_name' => strtoupper($this->first_name),
  428. 'last_name' => strtoupper($this->last_name),
  429. 'status' => $this->status,
  430. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  431. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  432. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  433. 'birth_date' => $this->birth_date,
  434. 'birth_place' => $this->birth_place,
  435. 'father_name' => $this->father_name,
  436. 'mother_name' => $this->mother_name,
  437. 'father_email' => strtolower($this->father_email),
  438. 'mother_email' => strtolower($this->mother_email),
  439. 'father_phone' => $this->father_phone,
  440. 'mother_phone' => $this->mother_phone,
  441. 'father_fiscal_code' => $this->father_fiscal_code,
  442. 'mother_fiscal_code' => $this->mother_fiscal_code,
  443. 'father_doc_number' => $this->father_doc_number,
  444. 'father_doc_type' => $this->father_doc_type,
  445. 'mother_doc_number' => $this->mother_doc_number,
  446. 'mother_doc_type' => $this->mother_doc_type,
  447. 'document_type' => $this->document_type,
  448. 'document_number' => $this->document_number,
  449. 'document_from' => $this->document_from,
  450. 'document_expire_date' => $this->document_expire_date,
  451. 'document_files' => $docs,
  452. 'gender' => $this->gender,
  453. 'fiscal_code' => $this->fiscal_code,
  454. 'address' => $this->address,
  455. 'zip_code' => $this->zip_code,
  456. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  457. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  458. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  459. 'phone' => $this->phone,
  460. 'phone2' => $this->phone2,
  461. 'phone3' => $this->phone3,
  462. 'email' => strtolower($this->email),
  463. 'image' => $name,
  464. 'enabled' => $this->enabled
  465. ]);
  466. session()->flash('success, Tesserato creato');
  467. $this->resetFields();
  468. if ($close)
  469. {
  470. $this->add = false;
  471. }
  472. else
  473. {
  474. $this->edit($member->id);
  475. }
  476. } catch (\Exception $ex) {
  477. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  478. }
  479. }
  480. public function duplicate($id){
  481. $member = \App\Models\Member::findOrFail($id);
  482. $newMember = $member->replicate();
  483. $newMember->save();
  484. $this->edit($newMember->id);
  485. }
  486. public function edit($id){
  487. $this->showDetail = false;
  488. $this->emit('hide-search');
  489. try {
  490. $member = \App\Models\Member::findOrFail($id);
  491. if( !$member) {
  492. session()->flash('error','Tesserato non trovato');
  493. } else {
  494. $this->first_name = $member->first_name;
  495. $this->last_name = $member->last_name;
  496. $this->status = $member->status;
  497. $this->birth_city_id = $member->birth_city_id;
  498. $this->birth_province_id = $member->birth_province_id;
  499. $this->birth_nation_id = $member->birth_nation_id;
  500. $this->birth_date = $member->birth_date;
  501. $this->birth_place = $member->birth_place;
  502. $this->father_name = $member->father_name;
  503. $this->mother_name = $member->mother_name;
  504. $this->father_email = strtolower($member->father_email);
  505. $this->mother_email = strtolower($member->mother_email);
  506. $this->father_phone = $member->father_phone;
  507. $this->mother_phone = $member->mother_phone;
  508. $this->father_fiscal_code = $member->father_fiscal_code;
  509. $this->mother_fiscal_code = $member->mother_fiscal_code;
  510. $this->father_doc_number = $member->father_doc_number;
  511. $this->mother_doc_number = $member->mother_doc_number;
  512. $this->father_doc_type = $member->father_doc_type;
  513. $this->mother_doc_type = $member->mother_doc_type;
  514. $this->document_type = $member->document_type;
  515. $this->document_number = $member->document_number;
  516. $this->document_from = $member->document_from;
  517. $this->document_expire_date = $member->document_expire_date;
  518. $this->document_files = explode("|", $member->document_files);
  519. $this->gender = $member->gender;
  520. $this->fiscal_code = $member->fiscal_code;
  521. $this->address = $member->address;
  522. $this->zip_code = $member->zip_code;
  523. $this->nation_id = $member->nation_id;
  524. $this->province_id = $member->province_id;
  525. $this->city_id = $member->city_id;
  526. $this->phone = $member->phone;
  527. $this->phone2 = $member->phone2;
  528. $this->phone3 = $member->phone3;
  529. $this->email = strtolower($member->email);
  530. $this->image_old = $member->image;
  531. $this->enabled = $member->enabled;
  532. $this->dataId = $member->id;
  533. $this->active = $member->getStatus();
  534. $date1 = new DateTime($this->birth_date);
  535. $date2 = new DateTime("now");
  536. $interval = $date1->diff($date2);
  537. $this->age = $interval->y . " anni";
  538. $this->under18 = $interval->y < 18;
  539. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  540. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  541. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  542. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  543. $this->update = true;
  544. $this->add = false;
  545. $this->emit('load-select');
  546. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  547. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  548. $this->emit('load-cities', $this->province_id, 'cityClass');
  549. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  550. }
  551. } catch (\Exception $ex) {
  552. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  553. }
  554. }
  555. public function update($close)
  556. {
  557. $this->emit('load-select');
  558. $this->validate();
  559. try {
  560. $name = '';
  561. if ($this->image)
  562. {
  563. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  564. $this->image->storeAs('public', $name);
  565. }
  566. $docs = implode("|", $this->document_files);
  567. \App\Models\Member::whereId($this->dataId)->update([
  568. 'first_name' => strtoupper($this->first_name),
  569. 'last_name' => strtoupper($this->last_name),
  570. 'status' => $this->status,
  571. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  572. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  573. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  574. 'birth_date' => $this->birth_date,
  575. 'birth_place' => $this->birth_place,
  576. 'father_name' => $this->father_name,
  577. 'mother_name' => $this->mother_name,
  578. 'father_email' => strtolower($this->father_email),
  579. 'mother_email' => strtolower($this->mother_email),
  580. 'father_phone' => $this->father_phone,
  581. 'mother_phone' => $this->mother_phone,
  582. 'father_fiscal_code' => $this->father_fiscal_code,
  583. 'mother_fiscal_code' => $this->mother_fiscal_code,
  584. 'father_doc_number' => $this->father_doc_number,
  585. 'father_doc_type' => $this->father_doc_type,
  586. 'mother_doc_number' => $this->mother_doc_number,
  587. 'mother_doc_type' => $this->mother_doc_type,
  588. 'document_type' => $this->document_type,
  589. 'document_number' => $this->document_number,
  590. 'document_from' => $this->document_from,
  591. 'document_expire_date' => $this->document_expire_date,
  592. 'document_files' => $docs,
  593. 'gender' => $this->gender,
  594. 'fiscal_code' => $this->fiscal_code,
  595. 'address' => $this->address,
  596. 'zip_code' => $this->zip_code,
  597. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  598. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  599. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  600. 'image' => $name != '' ? $name : $this->image_old,
  601. 'phone' => $this->phone,
  602. 'phone2' => $this->phone2,
  603. 'phone3' => $this->phone3,
  604. 'email' => strtolower($this->email),
  605. 'enabled' => $this->enabled
  606. ]);
  607. session()->flash('success','Tesserato aggiornato');
  608. if ($close)
  609. {
  610. $this->resetFields();
  611. $this->update = false;
  612. }
  613. } catch (\Exception $ex) {
  614. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  615. }
  616. }
  617. public function cancel()
  618. {
  619. $this->add = false;
  620. $this->update = false;
  621. $this->showDetail = false;
  622. $this->resetFields();
  623. }
  624. public function delete($id)
  625. {
  626. try{
  627. \App\Models\Member::find($id)->delete();
  628. session()->flash('success',"Tesserato eliminato");
  629. }catch(\Exception $e){
  630. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  631. }
  632. }
  633. public function checkUncheckAll()
  634. {
  635. if (!$this->checkedAll)
  636. {
  637. $this->multipleIds = array();
  638. }
  639. else
  640. {
  641. foreach($this->records as $r)
  642. {
  643. $this->multipleIds[] = $r->id;
  644. }
  645. }
  646. }
  647. public function multipleDelete()
  648. {
  649. try{
  650. foreach($this->multipleIds as $id)
  651. {
  652. \App\Models\Member::find($id)->delete();
  653. }
  654. }catch(\Exception $e){
  655. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  656. }
  657. $this->multipleAction = '';
  658. }
  659. // Card
  660. public function addCard()
  661. {
  662. $this->resetCardFields();
  663. $this->addCard = true;
  664. $this->updateCard = false;
  665. }
  666. public function storeCard()
  667. {
  668. $this->validate(['card_card_id' => 'required']);
  669. try {
  670. // in base alla card selezionata calcolo la scadenza
  671. $expire_date = null;
  672. if ($this->card_date != '')
  673. {
  674. $y = date("Y", strtotime($this->card_date));
  675. $card = \App\Models\Card::findOrFail($this->card_card_id);
  676. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  677. {
  678. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  679. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  680. $y += 1;
  681. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  682. if ($next_exp > $this->card_date)
  683. {
  684. $expire_date = $next_exp;
  685. }
  686. else
  687. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  688. }
  689. else
  690. {
  691. if ($card->one_year_expire)
  692. {
  693. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  694. }
  695. }
  696. }
  697. \App\Models\MemberCard::create([
  698. 'member_id' => $this->dataId,
  699. 'card_id' => $this->card_card_id,
  700. 'number' => $this->card_number,
  701. 'date' => $this->card_date,
  702. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  703. 'expire_date' => $expire_date,
  704. 'status' => $this->card_status
  705. ]);
  706. session()->flash('success, Tesserato creato');
  707. $this->resetCardFields();
  708. $this->addCard = false;
  709. } catch (\Exception $ex) {
  710. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  711. }
  712. }
  713. public function editCard($id){
  714. try {
  715. $memberCard = \App\Models\MemberCard::findOrFail($id);
  716. if( !$memberCard) {
  717. session()->flash('error','Tesserato non trovato');
  718. } else {
  719. $this->card_card_id = $memberCard->card_id;
  720. $this->card_number = $memberCard->number;
  721. $this->card_date = $memberCard->date;
  722. $this->card_accept_date = $memberCard->accept_date;
  723. $this->card_status = $memberCard->status;
  724. $this->cardDataId = $memberCard->id;
  725. $this->updateCard = true;
  726. $this->addCard = false;
  727. }
  728. } catch (\Exception $ex) {
  729. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  730. }
  731. }
  732. public function updateCard()
  733. {
  734. //$this->validate();
  735. $this->validate(['card_card_id' => 'required']);
  736. try {
  737. // in base alla card selezionata calcolo la scadenza
  738. $expire_date = null;
  739. if ($this->card_date != '')
  740. {
  741. $card = \App\Models\Card::findOrFail($this->card_card_id);
  742. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  743. {
  744. $y = date("Y", strtotime($this->card_date));
  745. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  746. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  747. if ($next_exp > $this->card_date)
  748. {
  749. $expire_date = $next_exp;
  750. }
  751. else
  752. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  753. }
  754. else
  755. {
  756. if ($card->one_year_expire)
  757. {
  758. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  759. }
  760. }
  761. }
  762. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  763. 'member_id' => $this->dataId,
  764. 'card_id' => $this->card_card_id,
  765. 'number' => $this->card_number,
  766. 'date' => $this->card_date,
  767. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  768. 'expire_date' => $expire_date,
  769. 'status' => $this->card_status
  770. ]);
  771. session()->flash('success','Tesserato aggiornato');
  772. $this->resetCardFields();
  773. $this->updateCard = false;
  774. } catch (\Exception $ex) {
  775. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  776. }
  777. }
  778. public function cancelCard()
  779. {
  780. $this->addCard = false;
  781. $this->updateCard = false;
  782. $this->resetCardFields();
  783. }
  784. public function deleteCard($id)
  785. {
  786. try{
  787. \App\Models\MemberCard::find($id)->delete();
  788. session()->flash('success',"Tesserato eliminato");
  789. }catch(\Exception $e){
  790. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  791. }
  792. }
  793. // Courses
  794. public function addCourse()
  795. {
  796. $this->resetCourseFields();
  797. $this->addCourse = true;
  798. $this->updateCourse = false;
  799. }
  800. public function storeCourse()
  801. {
  802. $this->validate(['course_course_id' => 'required']);
  803. try {
  804. \App\Models\MemberCourse::create([
  805. 'member_id' => $this->dataId,
  806. 'course_id' => $this->course_course_id,
  807. 'date_from' => $this->course_date_from,
  808. 'date_to' => $this->course_date_to,
  809. 'when' => json_encode($this->course_when)
  810. ]);
  811. session()->flash('success, Corso creato');
  812. $this->resetCourseFields();
  813. $this->addCourse = false;
  814. } catch (\Exception $ex) {
  815. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  816. }
  817. }
  818. public function editCourse($id){
  819. try {
  820. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  821. if( !$memberCourse) {
  822. session()->flash('error','Corso non trovato');
  823. } else {
  824. $this->course_course_id = $memberCourse->course_id;
  825. $this->course_date_from = $memberCourse->date_from;
  826. $this->course_date_to = $memberCourse->date_to;
  827. $this->course_when = json_decode($memberCourse->when);
  828. $this->courseDataId = $memberCourse->id;
  829. $this->updateCourse = true;
  830. $this->addCourse = false;
  831. }
  832. } catch (\Exception $ex) {
  833. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  834. }
  835. }
  836. public function updateCourse()
  837. {
  838. //$this->validate();
  839. $this->validate(['course_course_id' => 'required']);
  840. try {
  841. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  842. 'member_id' => $this->dataId,
  843. 'course_id' => $this->course_course_id,
  844. 'date_from' => $this->course_date_from,
  845. 'date_to' => $this->course_date_to,
  846. 'when' => json_encode($this->course_when)
  847. ]);
  848. session()->flash('success','Corso aggiornato');
  849. $this->resetCardFields();
  850. $this->updateCourse = false;
  851. } catch (\Exception $ex) {
  852. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  853. }
  854. }
  855. public function cancelCourse()
  856. {
  857. $this->addCourse = false;
  858. $this->updateCourse = false;
  859. $this->resetCourseFields();
  860. }
  861. public function deleteCourse($id)
  862. {
  863. try{
  864. \App\Models\MemberCourse::find($id)->delete();
  865. session()->flash('success',"Corso eliminato");
  866. }catch(\Exception $e){
  867. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  868. }
  869. }
  870. public function addRow()
  871. {
  872. $this->course_when[] = array('day' => '', 'from' => '', 'to' => '');
  873. }
  874. public function delRow($idx)
  875. {
  876. unset($this->course_when[$idx]);
  877. }
  878. // Certificates
  879. public function addCertificate()
  880. {
  881. $this->resetCertificateFields();
  882. $this->addCertificate = true;
  883. $this->updateCertificate = false;
  884. }
  885. public function storeCertificate()
  886. {
  887. $this->validate(['certificate_expire_date' => 'required']);
  888. // $this->validate();
  889. try {
  890. $name = '';
  891. try{
  892. if ($this->certificate_filename)
  893. {
  894. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  895. $this->certificate_filename->storeAs('public', $name);
  896. }
  897. } catch (\Exception $ex) {
  898. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  899. }
  900. if ($this->dataId > -1)
  901. {
  902. \App\Models\MemberCertificate::create([
  903. 'member_id' => $this->dataId,
  904. 'type' => $this->certificate_type,
  905. 'filename' => $name,
  906. 'expire_date' => $this->certificate_expire_date,
  907. 'status' => $this->certificate_status
  908. ]);
  909. }
  910. /*else
  911. {
  912. $this->certificateTmp = new \App\Models\MemberCertificate();
  913. $this->certificateTmp->type = $this->certificate_type;
  914. $this->certificateTmp->filename = $name;
  915. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  916. $this->certificateTmp->status = $this->certificate_status;
  917. $this->certificateTmp->status = $this->certificate_status;
  918. // s $this->member_certificates[] = $certificateTmp;
  919. }*/
  920. session()->flash('success, Tesserato creato');
  921. $this->resetCertificateFields();
  922. $this->addCertificate = false;
  923. } catch (\Exception $ex) {
  924. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  925. }
  926. }
  927. public function editCertificate($id){
  928. try {
  929. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  930. if( !$memberCertificate) {
  931. session()->flash('error','Tesserato non trovato');
  932. } else {
  933. $this->certificate_type = $memberCertificate->type;
  934. $this->certificate_filename_old = $memberCertificate->filename;
  935. $this->certificate_expire_date = $memberCertificate->expire_date;
  936. $this->certificate_status = $memberCertificate->status;
  937. $this->cardCertificateId = $memberCertificate->id;
  938. $this->updateCertificate = true;
  939. $this->addCertificate = false;
  940. }
  941. } catch (\Exception $ex) {
  942. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  943. }
  944. }
  945. public function updateCertificate()
  946. {
  947. $this->validate(['certificate_expire_date' => 'required']);
  948. try {
  949. $name = '';
  950. try{
  951. if ($this->certificate_filename)
  952. {
  953. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  954. $this->certificate_filename->storeAs('public', $name);
  955. }
  956. } catch (\Exception $ex) {
  957. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  958. }
  959. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  960. 'member_id' => $this->dataId,
  961. 'type' => $this->certificate_type,
  962. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  963. 'expire_date' => $this->certificate_expire_date,
  964. 'status' => $this->certificate_status
  965. ]);
  966. session()->flash('success','Tesserato aggiornato');
  967. $this->resetCertificateFields();
  968. $this->updateCertificate = false;
  969. } catch (\Exception $ex) {
  970. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  971. }
  972. }
  973. public function cancelCertificate()
  974. {
  975. $this->addCertificate = false;
  976. $this->updateCertificate = false;
  977. $this->resetCertificateFields();
  978. }
  979. public function deleteCertificate($id)
  980. {
  981. try{
  982. \App\Models\MemberCertificate::find($id)->delete();
  983. session()->flash('success',"Tesserato eliminato");
  984. }catch(\Exception $e){
  985. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  986. }
  987. }
  988. // Gruppi di appartenenza
  989. public function storeCategory()
  990. {
  991. $this->validate(['category_category_id' => 'required']);
  992. try {
  993. \App\Models\MemberCategory::create([
  994. 'member_id' => $this->dataId,
  995. 'category_id' => $this->category_category_id,
  996. 'date' => \Carbon\Carbon::now()
  997. ]);
  998. session()->flash('success, Associazione creato');
  999. $this->resetCategoryFields();
  1000. $this->addCard = false;
  1001. } catch (\Exception $ex) {
  1002. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1003. }
  1004. }
  1005. public function hideMsg()
  1006. {
  1007. $this->groupMsg = '';
  1008. }
  1009. public function storeCategoryWithID($id)
  1010. {
  1011. $this->groupMsg = '';
  1012. try {
  1013. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first())
  1014. {
  1015. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1016. }
  1017. else
  1018. {
  1019. \App\Models\MemberCategory::create([
  1020. 'member_id' => $this->dataId,
  1021. 'category_id' => $id,
  1022. 'date' => \Carbon\Carbon::now()
  1023. ]);
  1024. session()->flash('success, Associazione creato');
  1025. $this->resetCategoryFields();
  1026. $this->addCard = false;
  1027. }
  1028. } catch (\Exception $ex) {
  1029. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1030. }
  1031. }
  1032. public function deleteCategory($id)
  1033. {
  1034. try{
  1035. \App\Models\MemberCategory::find($id)->delete();
  1036. session()->flash('success',"Associazione eliminata");
  1037. }catch(\Exception $e){
  1038. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1039. }
  1040. }
  1041. public function setCourse($id)
  1042. {
  1043. $this->course_course_id = $id;
  1044. }
  1045. public function getNation($nation)
  1046. {
  1047. if ($nation > 0)
  1048. {
  1049. $ret = \App\Models\Nation::findOrFail($nation);
  1050. return $ret->name;
  1051. }
  1052. return "";
  1053. }
  1054. public function getProvince($province)
  1055. {
  1056. if ($province > 0)
  1057. {
  1058. $ret = \App\Models\Province::findOrFail($province);
  1059. return $ret->name;
  1060. }
  1061. return "";
  1062. }
  1063. public function getCity($city)
  1064. {
  1065. if ($city > 0)
  1066. {
  1067. $ret = \App\Models\City::findOrFail($city);
  1068. return $ret->name;
  1069. }
  1070. return "";
  1071. }
  1072. public function getFiscalCode()
  1073. {
  1074. $this->error_fc = false;
  1075. $cf = new codicefiscale();
  1076. $cf->setDateSeparator('-');
  1077. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0)
  1078. {
  1079. $code = '';
  1080. if ($this->birth_city_id > 0)
  1081. {
  1082. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1083. }
  1084. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1085. $this->fiscal_code = $codice;
  1086. }
  1087. else
  1088. $this->error_fc = true;
  1089. }
  1090. }
  1091. class codicefiscale {
  1092. /**
  1093. * Array delle consonanti
  1094. */
  1095. protected $_consonanti = array(
  1096. 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K',
  1097. 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
  1098. 'V', 'W', 'X', 'Y', 'Z'
  1099. );
  1100. /**
  1101. * Array delle vocali
  1102. */
  1103. protected $_vocali = array(
  1104. 'A', 'E', 'I', 'O', 'U'
  1105. );
  1106. /**
  1107. * Array per il calcolo della lettera del mese
  1108. * Al numero del mese corrisponde una lettera
  1109. */
  1110. protected $_mesi = array(
  1111. 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
  1112. 6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
  1113. 11 => 'S', 12 => 'T'
  1114. );
  1115. protected $_pari = array(
  1116. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
  1117. '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
  1118. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4,
  1119. 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9,
  1120. 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14,
  1121. 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19,
  1122. 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24,
  1123. 'Z' => 25
  1124. );
  1125. protected $_dispari = array(
  1126. '0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9,
  1127. '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21,
  1128. 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9,
  1129. 'F' => 13, 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21,
  1130. 'K' => 2, 'L' => 4, 'M' => 18, 'N' => 20, 'O' => 11,
  1131. 'P' => 3, 'Q' => 6, 'R' => 8, 'S' => 12, 'T' => 14,
  1132. 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25, 'Y' => 24,
  1133. 'Z' => 23
  1134. );
  1135. protected $_controllo = array(
  1136. '0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
  1137. '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
  1138. '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
  1139. '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P',
  1140. '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T',
  1141. '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X',
  1142. '24' => 'Y', '25' => 'Z'
  1143. );
  1144. /**
  1145. * Stringa di errore
  1146. */
  1147. protected $_error = null;
  1148. /**
  1149. * Separatore per la data di nascita
  1150. */
  1151. protected $_dateSeparator = '/';
  1152. /**
  1153. * Percorso del file del database SQLite
  1154. * dei codici catastali
  1155. */
  1156. protected $_dbCatastali = null;
  1157. /**
  1158. * Trasforma la stringa passata in un array di lettere
  1159. * e lo incrocia con un ulteriore array
  1160. */
  1161. protected function _getLettere($string, array $haystack) {
  1162. $letters = array();
  1163. foreach(str_split($string) as $needle) {
  1164. if (in_array($needle, $haystack)) {
  1165. $letters[] = $needle;
  1166. }
  1167. }
  1168. return $letters;
  1169. }
  1170. /**
  1171. * Ritorna un array con le vocali di una data stringa
  1172. */
  1173. protected function _getVocali($string) {
  1174. return $this->_getLettere($string, $this->_vocali);
  1175. }
  1176. /**
  1177. * Ritorna un array con le consonanti di una data stringa
  1178. */
  1179. protected function _getConsonanti($string) {
  1180. return $this->_getLettere($string, $this->_consonanti);
  1181. }
  1182. /**
  1183. * Pulisce la stringa filtrando tutti i caratteri che
  1184. * non sono lettere. Lo switch $toupper se impostato a TRUE
  1185. * converte la stringa risultante in MAIUSCOLO.
  1186. */
  1187. protected function _sanitize($string, $toupper = true) {
  1188. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  1189. return ($toupper) ? strtoupper($result) : $result;
  1190. }
  1191. /**
  1192. * Se la stringa passata a funzione e' costituita
  1193. * da meno di 3 caratteri, rimpiazza le lettere
  1194. * mancanti con la lettera X.
  1195. */
  1196. protected function _addMissingX($string) {
  1197. $code = $string;
  1198. while(strlen($code) < 3) {
  1199. $code .= 'X';
  1200. }
  1201. return $code;
  1202. }
  1203. /**
  1204. * Ottiene il codice identificativo del nome
  1205. */
  1206. protected function _calcolaNome($string) {
  1207. $nome = $this->_sanitize($string);
  1208. $code = '';
  1209. // Se il nome inserito e' piu' corto di 3 lettere
  1210. // si aggiungono tante X quanti sono i caratteri
  1211. // mancanti.
  1212. if (strlen($nome) < 3) {
  1213. return $this->_addMissingX($nome);
  1214. }
  1215. $nome_cons = $this->_getConsonanti($nome);
  1216. // Se le consonanti contenute nel nome sono minori
  1217. // o uguali a 3 vengono considerate nell'ordine in cui
  1218. // compaiono.
  1219. if (count($nome_cons) <= 3) {
  1220. $code = implode('', $nome_cons);
  1221. } else {
  1222. // Se invece abbiamo almeno 4 consonanti, prendiamo
  1223. // la prima, la terza e la quarta.
  1224. for($i=0; $i<4; $i++) {
  1225. if ($i == 1) continue;
  1226. if (!empty($nome_cons[$i])) {
  1227. $code .= $nome_cons[$i];
  1228. }
  1229. }
  1230. }
  1231. // Se compaiono meno di 3 consonanti nel nome, si
  1232. // utilizzano le vocali, nell'ordine in cui compaiono
  1233. // nel nome.
  1234. if (strlen($code) < 3) {
  1235. $nome_voc = $this->_getVocali($nome);
  1236. while (strlen($code) < 3) {
  1237. $code .= array_shift($nome_voc);
  1238. }
  1239. }
  1240. return $code;
  1241. }
  1242. protected function _calcolaCognome($string) {
  1243. $cognome = $this->_sanitize($string);
  1244. $code = '';
  1245. // Se il cognome inserito e' piu' corto di 3 lettere
  1246. // si aggiungono tante X quanti sono i caratteri
  1247. // mancanti.
  1248. if (strlen($cognome) < 3) {
  1249. return $this->_addMissingX($cognome);
  1250. }
  1251. $cognome_cons = $this->_getConsonanti($cognome);
  1252. // Per il calcolo del cognome si prendono le prime
  1253. // 3 consonanti.
  1254. for ($i=0; $i<3; $i++) {
  1255. if (array_key_exists($i, $cognome_cons)) {
  1256. $code .= $cognome_cons[$i];
  1257. }
  1258. }
  1259. // Se le consonanti non bastano, vengono prese
  1260. // le vocali nell'ordine in cui compaiono.
  1261. if (strlen($code) < 3) {
  1262. $cognome_voc = $this->_getVocali($cognome);
  1263. while (strlen($code) < 3) {
  1264. $code .= array_shift($cognome_voc);
  1265. }
  1266. }
  1267. return $code;
  1268. }
  1269. /**
  1270. * Imposta il separatore di data ( default: / )
  1271. */
  1272. public function setDateSeparator($char) {
  1273. $this->_dateSeparator = $char;
  1274. return $this;
  1275. }
  1276. /**
  1277. * Ritorna la parte di codice fiscale corrispondente
  1278. * alla data di nascita del soggetto (Forma: AAMGG)
  1279. */
  1280. protected function _calcolaDataNascita($data, $sesso) {
  1281. $dn = explode($this->_dateSeparator, $data);
  1282. $giorno = (int) @$dn[2];
  1283. $mese = (int) @$dn[1];
  1284. $anno = (int) @$dn[0];
  1285. // Le ultime due cifre dell'anno di nascita
  1286. $aa = substr($anno, -2);
  1287. // La lettera corrispondente al mese di nascita
  1288. $mm = $this->_mesi[$mese];
  1289. // Il giorno viene calcolato a seconda del sesso
  1290. // del soggetto di cui si calcola il codice:
  1291. // se e' Maschio si mette il giorno reale, se e'
  1292. // Femmina viene aggiungo 40 a questo numero.
  1293. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  1294. // Bug #1: Thanks to Luca
  1295. if (strlen($gg) < 2) $gg = '0' . $gg;
  1296. return $aa . $mm . $gg;
  1297. }
  1298. /**
  1299. * Ritorna la cifra di controllo sulla base dei
  1300. * 15 caratteri del codice fiscale calcolati.
  1301. */
  1302. protected function _calcolaCifraControllo($codice) {
  1303. $code = str_split($codice);
  1304. $sum = 0;
  1305. for($i=1; $i <= count($code); $i++) {
  1306. $cifra = $code[$i-1];
  1307. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  1308. }
  1309. $sum %= 26;
  1310. return $this->_controllo[$sum];
  1311. }
  1312. /**
  1313. * Imposta il messaggio di errore
  1314. */
  1315. protected function _setError($string) {
  1316. $this->_error = $string;
  1317. }
  1318. /**
  1319. * Verifica la presenza di un errore.
  1320. * Ritorna TRUE se presente, FALSE altrimenti.
  1321. */
  1322. public function hasError() {
  1323. return !is_null($this->_error);
  1324. }
  1325. /**
  1326. * Ritorna la stringa di errore
  1327. */
  1328. public function getError() {
  1329. return $this->_error;
  1330. }
  1331. /**
  1332. * Ritorna il codice fiscale utilizzando i parametri
  1333. * passati a funzione. Se si verifica
  1334. */
  1335. public function calcola($nome, $cognome, $data, $sesso, $comune) {
  1336. $codice = $this->_calcolaCognome($cognome) .
  1337. $this->_calcolaNome($nome) .
  1338. $this->_calcolaDataNascita($data, $sesso) .
  1339. $comune;
  1340. if ($this->hasError()) {
  1341. return false;
  1342. }
  1343. $codice .= $this->_calcolaCifraControllo($codice);
  1344. if (strlen($codice) != 16) {
  1345. //$this->_setError(self::ERR_GENERIC);
  1346. return 'ERROR';
  1347. }
  1348. return $codice;
  1349. }
  1350. }