Member.php 51 KB

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