Member.php 53 KB

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