Member.php 51 KB

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