Member.php 47 KB

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