Member.php 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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. protected $messages = [
  18. 'birth_nation_id.required' => 'birth_nation_id',
  19. 'birth_province_id.required' => 'birth_province_id',
  20. 'birth_city_id.required' => 'birth_city_id',
  21. 'nation_id.required' => 'nation_id',
  22. 'province_id.required' => 'province_id',
  23. 'city_id.required' => 'city_id',
  24. 'gender' => 'Obbligatorio'
  25. ];
  26. public function sortBy($field)
  27. {
  28. if($this->sortField === $field)
  29. {
  30. $this->sortAsc = ! $this->sortAsc;
  31. } else {
  32. $this->sortAsc = true;
  33. }
  34. $this->sortField = $field;
  35. }
  36. 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;
  37. public $isSaving = false;
  38. public $father_name, $mother_name, $father_email, $mother_email, $father_phone, $mother_phone, $father_fiscal_code, $mother_fiscal_code, $father_doc_number, $father_doc_type, $mother_doc_number, $mother_doc_type;
  39. public $document_type, $document_number, $document_from, $document_expire_date, $document_files, $document_files_old;
  40. public $selectId = 0;
  41. public $causalId = 0;
  42. public $error_fc = false;
  43. public $under18 = false;
  44. public $refreshAfter = 0;
  45. public $checkedAll = false;
  46. public $multipleIds = [];
  47. public $multipleAction = '';
  48. public $isItaly = true;
  49. public $isBirthItaly = true;
  50. public $showDetail = false;
  51. public $currentMember;
  52. public $currentStatus;
  53. public $age = '';
  54. public $disciplines = array();
  55. public $cards = array();
  56. public $categories = array();
  57. public $courses = array();
  58. public $course_names = array();
  59. public $course_levels = array();
  60. public $course_types = array();
  61. public $course_frequencies = array();
  62. public $course_subscriptions = array();
  63. public $createSubscription = false;
  64. public $payMonths = array();
  65. public $showCourse = array();
  66. public $searchTxt;
  67. public $search;
  68. public $showReset = false;
  69. public $advanced = false;
  70. public $birthNations = array();
  71. public $birthProvinces = array();
  72. public $birthCities = array();
  73. public $active;
  74. // Card data
  75. public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_status, $card_discipline1_id, $card_discipline2_id, $card_discipline3_id, $addCard, $updateCard, $cardDataId;
  76. // Categories data
  77. public $member_categories = array(), $category_category_id;
  78. // Courses data
  79. public $member_courses = array(), $course_course_id, $course_date_from, $course_date_to, $course_when = array(), $addCourse, $updateCourse, $courseDataId, $course_course_subscription_id, $course_status, $course_note, $course_price, $course_subscription_price, $course_subscribed, $course_months = array(), $course_exist;
  80. public $course_name, $course_level_id, $course_type_id, $course_frequency_id;
  81. // Certificates data
  82. public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
  83. public $filterCard = [];
  84. public $filterCategory = [];
  85. public $filterCertNormal = 0;
  86. public $filterCertAgonistic = 0;
  87. public $filterCertScaduto = 0;
  88. public $filterCertInScadenza = 0;
  89. protected $rules = [
  90. 'first_name' => 'required',
  91. 'last_name' => 'required',
  92. 'email' => 'required',
  93. 'phone' => 'required',
  94. 'birth_date' => 'before_or_equal:today'
  95. ];
  96. public function resetFields(){
  97. $this->dataId = -1;
  98. $this->first_name = '';
  99. $this->last_name = '';
  100. $this->status = '';
  101. $this->birth_city_id = null;
  102. $this->birth_province_id = null;
  103. $this->birth_nation_id = null;
  104. $this->birth_date = null;
  105. $this->birth_place = '';
  106. $this->father_name = '';
  107. $this->mother_name = '';
  108. $this->father_email = '';
  109. $this->mother_email = '';
  110. $this->father_phone = '';
  111. $this->mother_phone = '';
  112. $this->father_fiscal_code = '';
  113. $this->mother_fiscal_code = '';
  114. $this->father_doc_number = '';
  115. $this->mother_doc_number = '';
  116. $this->father_doc_type = '';
  117. $this->mother_doc_type = '';
  118. $this->gender = null;
  119. $this->fiscal_code = '';
  120. $this->address = '';
  121. $this->zip_code = '';
  122. $this->document_type = '';
  123. $this->document_number = '';
  124. $this->document_from = '';
  125. $this->document_expire_date = null;
  126. $this->document_files = array();
  127. //$this->nation_id = null;
  128. //$this->province_id = null;
  129. $this->nation_id = null;
  130. $this->province_id = null;
  131. $this->city_id = null;
  132. $this->phone = '';
  133. $this->phone2 = '';
  134. $this->phone3 = '';
  135. $this->email = '';
  136. $this->enabled = true;
  137. $this->under18 = false;
  138. $this->image = null;
  139. $this->error_fc = false;
  140. $this->isSaving = false;
  141. $this->emit('load-data-table');
  142. }
  143. public function resetCardFields(){
  144. $this->card_card_id = null;
  145. $this->card_number = '';
  146. $this->card_date = null;
  147. $this->card_accept_date = null;
  148. $this->card_status = 0;
  149. $this->card_discipline1_id = null;
  150. $this->card_discipline2_id = null;
  151. $this->card_discipline3_id = null;
  152. }
  153. public function resetCertificateFields(){
  154. $this->certificate_type = 'N';
  155. $this->certificate_filename = '';
  156. $this->certificate_filename_old = '';
  157. $this->certificate_expire_date = null;
  158. $this->certificate_status = 0;
  159. }
  160. public function resetCourseFields(){
  161. $this->course_course_id = null;
  162. $this->course_when = array();
  163. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  164. $this->course_date_from = null;
  165. $this->course_date_to = null;
  166. $this->course_course_subscription_id = null;
  167. $this->course_status = 0;
  168. $this->course_price = 0;
  169. $this->course_subscription_price = 0;
  170. $this->course_exist = false;
  171. $this->course_subscribed = false;
  172. $this->course_months = array();
  173. $this->course_note = '';
  174. $this->course_name = '';
  175. $this->course_level_id = '';
  176. $this->course_type_id = '';
  177. $this->course_frequency_id = '';
  178. }
  179. public function executeMultipleAction(){
  180. if ($this->multipleAction == 'delete')
  181. $this->multipleDelete();
  182. }
  183. public function updatedImage()
  184. {
  185. $this->validate([
  186. 'image' => 'image|max:1024',
  187. ]);
  188. $this->image_old = '';
  189. }
  190. public function updatedCourseName()
  191. {
  192. $this->course_course_id = null;
  193. $this->course_level_id = '';
  194. $this->course_type_id = '';
  195. $this->course_frequency_id = '';
  196. $levels_ids = [];
  197. if ($this->course_name != '')
  198. {
  199. $all = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->get();
  200. foreach($all as $a)
  201. {
  202. $levels_ids[] = $a->course_level_id;
  203. }
  204. }
  205. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  206. $this->course_types = [];
  207. $this->course_frequencies = [];
  208. }
  209. public function updatedCourseLevelId()
  210. {
  211. $this->course_course_id = null;
  212. $this->course_type_id = '';
  213. $this->course_frequency_id = '';
  214. $types_ids = [];
  215. if ($this->course_level_id != '')
  216. {
  217. $all = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->where('course_level_id', $this->course_level_id)->get();
  218. foreach($all as $a)
  219. {
  220. $types_ids[] = $a->course_type_id;
  221. }
  222. }
  223. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  224. $this->course_frequencies = [];
  225. }
  226. public function updatedCourseTypeId()
  227. {
  228. $this->course_course_id = null;
  229. $this->course_frequency_id = '';
  230. $frequencies_ids = [];
  231. if ($this->course_type_id != '')
  232. {
  233. $all = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->get();
  234. foreach($all as $a)
  235. {
  236. $frequencies_ids[] = $a->course_frequency_id;
  237. }
  238. }
  239. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  240. }
  241. public function updatedCourseFrequencyId()
  242. {
  243. $this->course_course_id = null;
  244. if ($this->course_frequency_id != '')
  245. {
  246. $this->course_course_id = \App\Models\Course::where('name', 'like', '%' . $this->course_name . "%")->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->where('course_frequency_id', $this->course_frequency_id)->first()->id;
  247. if ($this->course_course_id > 0)
  248. {
  249. $c = \App\Models\Course::findOrFail($this->course_course_id);
  250. $this->course_price = formatPrice($c->price);
  251. $this->course_subscription_price = formatPrice($c->subscription_price);
  252. // Controllo se sono già iscritto la corso
  253. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  254. }
  255. else
  256. {
  257. $this->course_price = 0;
  258. $this->course_subscription_price = 0;
  259. $this->course_exist = false;
  260. }
  261. }
  262. else
  263. {
  264. $this->course_price = 0;
  265. $this->course_subscription_price = 0;
  266. $this->course_exist = false;
  267. }
  268. }
  269. public $documents = [];
  270. public function removeDocument($idx)
  271. {
  272. unset($this->document_files[$idx]);
  273. }
  274. public function updatedDocuments()
  275. {
  276. foreach ($this->documents as $document)
  277. {
  278. $name = $document->getClientOriginalName(); // . '.'.$allegato->extension();
  279. $document->storeAs('public', $name);
  280. $this->document_files[] = $name;
  281. }
  282. $this->documents = [];
  283. }
  284. public function resetCategoryFields(){
  285. $this->category_category_id = null;
  286. }
  287. public function getCategories($records, $indentation)
  288. {
  289. foreach($records as $record)
  290. {
  291. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  292. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  293. if(count($record->childs))
  294. $this->getCategories($record->childs, $indentation + 1);
  295. }
  296. }
  297. public function getCourses($records, $indentation)
  298. {
  299. foreach($records as $record)
  300. {
  301. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  302. $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
  303. if(count($record->childs))
  304. $this->getCourses($record->childs, $indentation + 1);
  305. }
  306. }
  307. public function updatedBirthDate()
  308. {
  309. $this->validateOnly('birth_date');
  310. $date1 = new DateTime($this->birth_date);
  311. $date2 = new DateTime("now");
  312. $interval = $date1->diff($date2);
  313. $this->age = $interval->y . " anni";
  314. $this->under18 = $interval->y < 18;
  315. }
  316. public function mount()
  317. {
  318. if (isset($_GET["new"]))
  319. {
  320. $this->refreshAfter = 1;
  321. $this->add();
  322. }
  323. $this->cards = \App\Models\Card::select('id', 'name')->get();
  324. $this->disciplines = \App\Models\Discipline::select('id', 'name')->get();
  325. $this->categories = array();
  326. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  327. $this->courses = \App\Models\Course::select('id', 'name')->where('parent_id', null)->get();
  328. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  329. // $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->get(), 0);
  330. /*$this->nations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  331. $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  332. $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  333. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  334. if ($c)
  335. $this->causalId = $c->id;
  336. if (isset($_GET["member_detail"]))
  337. {
  338. $this->showDetailF($_GET["member_detail"]);
  339. $this->refreshAfter = 1;
  340. }
  341. /*$this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  342. $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  343. $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  344. }
  345. public function updated()
  346. {
  347. if ($this->isSaving)
  348. {
  349. $zzz = [];
  350. if ($this->birth_nation_id == null)
  351. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  352. if ($this->isBirthItaly)
  353. {
  354. if ($this->birth_province_id == null)
  355. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  356. if ($this->birth_city_id == null)
  357. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  358. }
  359. if ($this->nation_id == null)
  360. $zzz[] = 'nation_id='.$this->nation_id;
  361. if ($this->isItaly)
  362. {
  363. if ($this->province_id == null)
  364. $zzz[] = 'province_id='.$this->province_id;
  365. if ($this->city_id == null)
  366. $zzz[] = 'city_id='.$this->city_id;
  367. }
  368. $this->emit('setErrorMsg', $zzz);
  369. }
  370. // $this->emit('load-select');
  371. }
  372. public function hydrate()
  373. {
  374. if ($this->isSaving)
  375. {
  376. $zzz = [];
  377. if ($this->birth_nation_id == null)
  378. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  379. if ($this->isBirthItaly)
  380. {
  381. if ($this->birth_province_id == null)
  382. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  383. if ($this->birth_city_id == null)
  384. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  385. }
  386. if ($this->nation_id == null)
  387. $zzz[] = 'nation_id='.$this->nation_id;
  388. if ($this->isItaly)
  389. {
  390. if ($this->province_id == null)
  391. $zzz[] = 'province_id='.$this->province_id;
  392. if ($this->city_id == null)
  393. $zzz[] = 'city_id='.$this->city_id;
  394. }
  395. $this->emit('setErrorMsg', $zzz);
  396. }
  397. $this->emit('load-select');
  398. // $this->emit('destroy-data-table');
  399. }
  400. public function checkIsItaly()
  401. {
  402. $n = \App\Models\Nation::findOrFail($this->nation_id);
  403. $this->isItaly = $n->is_italy;
  404. }
  405. public function checkIsBirthItaly()
  406. {
  407. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  408. $this->isBirthItaly = $n->is_italy;
  409. }
  410. /*public function loadProvinces()
  411. {
  412. $n = \App\Models\Nation::findOrFail($this->nation_id);
  413. $this->isItaly = $n->is_italy;
  414. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
  415. $this->cities = array();
  416. $this->selectId++;
  417. }
  418. public function loadCities()
  419. {
  420. $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
  421. $this->selectId++;
  422. }
  423. public function loadBirthProvinces()
  424. {
  425. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  426. $this->isBirthItaly = $n->is_italy;
  427. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
  428. $this->birthCities = array();
  429. $this->selectId++;
  430. }
  431. public function loadBirthCities()
  432. {
  433. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  434. $this->selectId++;
  435. }
  436. */
  437. public function search()
  438. {
  439. if ($this->searchTxt != '')
  440. {
  441. $this->search = $this->searchTxt;
  442. $this->showReset = true;
  443. }
  444. }
  445. public function resetSearch()
  446. {
  447. $this->showReset = false;
  448. $this->searchTxt = '';
  449. $this->search = $this->searchTxt;
  450. }
  451. public function advancedSearch()
  452. {
  453. $this->advanced = true;
  454. }
  455. public function advancedSearchCancel()
  456. {
  457. $this->filterCard = [];
  458. $this->filterCategory = [];
  459. $this->filterCertNormal = 0;
  460. $this->filterCertAgonistic = 0;
  461. $this->filterCertScaduto = 0;
  462. $this->filterCertInScadenza = 0;
  463. $this->advanced = false;
  464. }
  465. public function render()
  466. {
  467. $this->course_names = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  468. $datas = [];
  469. // $this->emit('destroy-data-table');
  470. if (false)
  471. {
  472. if (!$this->advanced)
  473. {
  474. /*if ($this->search != '')
  475. $datas = \App\Models\Member::select('members.*') // , \DB::raw('SUM(records.id) As total'))
  476. ->where('first_name', 'LIKE', '%' . $this->search . '%')
  477. ->orWhere('last_name', 'LIKE', '%' . $this->search . '%')
  478. ->orWhere('email', 'LIKE', '%' . $this->search . '%');
  479. $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
  480. else*/
  481. $this->records = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone')->get();
  482. //$datas = \App\Models\Member::select('members.*');
  483. }
  484. else
  485. {
  486. //$this->records = \App\Models\Member::where('id', '>', 0);
  487. $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  488. if (sizeof($this->filterCard) > 0)
  489. {
  490. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->filterCard)->pluck('member_id');
  491. //$this->records->whereIn('id', $card_ids);
  492. $datas = $datas->whereIn('id', $card_ids);
  493. }
  494. if (sizeof($this->filterCategory) > 0)
  495. {
  496. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', $this->filterCategory)->pluck('member_id');
  497. //$this->records->whereIn('id', $cats_ids);
  498. $datas = $datas->whereIn('id', $cats_ids);
  499. }
  500. $certs = [];
  501. if ($this->filterCertNormal > 0)
  502. {
  503. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  504. //$this->records->whereIn('id', $normal);
  505. $datas = $datas->whereIn('id', $normal);;
  506. }
  507. if ($this->filterCertAgonistic > 0)
  508. {
  509. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  510. //$this->records->whereIn('id', $agonistic);
  511. $datas = $datas->whereIn('id', $agonistic);
  512. }
  513. if ($this->filterCertScaduto > 0)
  514. {
  515. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  516. //$this->records->whereIn('id', $scaduto);
  517. $datas = $datas->whereIn('id', $scaduto);
  518. }
  519. if ($this->filterCertInScadenza > 0)
  520. {
  521. $scaduto = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  522. //$this->records->whereIn('id', $scaduto);
  523. $datas = $datas->whereIn('id', $scaduto);
  524. }
  525. if (sizeof($certs) > 0)
  526. {
  527. $datas = $datas->whereIn('id', $certs);
  528. //$this->records->whereIn('id', $certs);
  529. }
  530. $this->records = $datas->get();
  531. }
  532. foreach($this->records as $r)
  533. {
  534. $r->age = $r->getAge();
  535. $active = $r->isActive();
  536. $r->status = $active["status"];
  537. $r->date = $active["date"];
  538. $r->certificate = $r->hasCertificate()["date"];
  539. $r->state = $r->getStatus()["status"];
  540. }
  541. /*
  542. if ($this->sortAsc)
  543. $this->records = $this->records->sortBy($this->sortField);
  544. else
  545. $this->records = $this->records->sortByDesc($this->sortField);
  546. */
  547. //$datas = $datas->get(); // ->orderBy($this->sortField, $this->sortAsc ? 'ASC' : 'DESC')->paginate(10);
  548. //$this->records = $this->records->get();
  549. $this->emit('load-data-table');
  550. }
  551. $this->loadMemberCards();
  552. $this->loadMemberCourses();
  553. $this->loadMemberCategories();
  554. $this->loadMemberCertificates();
  555. return view('livewire.member', ['datas' => $datas]);
  556. }
  557. public function updatedCourseCourseId()
  558. {
  559. if ($this->course_course_id > 0)
  560. {
  561. $c = \App\Models\Course::findOrFail($this->course_course_id);
  562. $this->course_price = formatPrice($c->price);
  563. $this->course_subscription_price = formatPrice($c->subscription_price);
  564. // Controllo se sono già iscritto la corso
  565. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  566. }
  567. else
  568. {
  569. $this->course_price = 0;
  570. $this->course_subscription_price = 0;
  571. $this->course_exist = false;
  572. }
  573. }
  574. public function loadMemberCards()
  575. {
  576. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  577. // return view('livewire.member');
  578. }
  579. public function loadMemberCourses()
  580. {
  581. $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
  582. // return view('livewire.member');
  583. }
  584. public function loadMemberCategories()
  585. {
  586. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  587. // return view('livewire.member');
  588. }
  589. public function loadMemberCertificates()
  590. {
  591. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->orderBy('expire_date', 'DESC')->get();
  592. // return view('livewire.member');
  593. }
  594. public function showDetailF($id)
  595. {
  596. $this->currentMember = \App\Models\Member::findOrFail($id);
  597. $this->currentStatus = $this->currentMember->getStatus();
  598. $this->showDetail = true;
  599. }
  600. public function add()
  601. {
  602. $this->emit('load-select');
  603. $this->emit('hide-search');
  604. $this->showDetail = false;
  605. $this->resetFields();
  606. $this->add = true;
  607. $this->update = false;
  608. $this->emit('setEdit', true);
  609. }
  610. public function store($close)
  611. {
  612. $this->isSaving = false;
  613. $this->emit('load-select');
  614. $rules = [
  615. 'first_name' => 'required',
  616. 'last_name' => 'required',
  617. 'email' => 'required',
  618. 'phone' => 'required',
  619. 'birth_nation_id' => 'required',
  620. //'birth_place' => 'required',
  621. 'address' => 'required',
  622. 'zip_code' => 'required',
  623. 'nation_id' => 'required',
  624. 'fiscal_code' => 'required',
  625. 'birth_date' => 'before_or_equal:today',
  626. 'gender' => 'required'
  627. ];
  628. if ($this->isBirthItaly)
  629. {
  630. $rules['birth_province_id'] = 'required';
  631. $rules['birth_city_id'] = 'required';
  632. }
  633. else
  634. {
  635. $rules['birth_place'] = 'required';
  636. }
  637. if ($this->isItaly)
  638. {
  639. $rules['province_id'] = 'required';
  640. $rules['city_id'] = 'required';
  641. }
  642. $zzz = [];
  643. if ($this->birth_nation_id == null)
  644. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  645. if ($this->isBirthItaly)
  646. {
  647. if ($this->birth_province_id == null)
  648. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  649. if ($this->birth_city_id == null)
  650. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  651. }
  652. if ($this->nation_id == null)
  653. $zzz[] = 'nation_id='.$this->nation_id;
  654. if ($this->isItaly)
  655. {
  656. if ($this->province_id == null)
  657. $zzz[] = 'province_id='.$this->province_id;
  658. if ($this->city_id == null)
  659. $zzz[] = 'city_id='.$this->city_id;
  660. }
  661. $this->emit('setErrorMsg', $zzz);
  662. $this->validate($rules);
  663. //$this->emit('setErrors');
  664. try {
  665. $name = '';
  666. if ($this->image)
  667. {
  668. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  669. $this->image->storeAs('public', $name);
  670. }
  671. $docs = implode("|", $this->document_files);
  672. $member = \App\Models\Member::create([
  673. 'first_name' => strtoupper($this->first_name),
  674. 'last_name' => strtoupper($this->last_name),
  675. 'status' => $this->status,
  676. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  677. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  678. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  679. 'birth_date' => $this->birth_date,
  680. 'birth_place' => $this->birth_place,
  681. 'father_name' => $this->father_name,
  682. 'mother_name' => $this->mother_name,
  683. 'father_email' => strtolower($this->father_email),
  684. 'mother_email' => strtolower($this->mother_email),
  685. 'father_phone' => $this->father_phone,
  686. 'mother_phone' => $this->mother_phone,
  687. 'father_fiscal_code' => $this->father_fiscal_code,
  688. 'mother_fiscal_code' => $this->mother_fiscal_code,
  689. 'father_doc_number' => $this->father_doc_number,
  690. 'father_doc_type' => $this->father_doc_type,
  691. 'mother_doc_number' => $this->mother_doc_number,
  692. 'mother_doc_type' => $this->mother_doc_type,
  693. 'document_type' => $this->document_type,
  694. 'document_number' => $this->document_number,
  695. 'document_from' => $this->document_from,
  696. 'document_expire_date' => $this->document_expire_date,
  697. 'document_files' => $docs,
  698. 'gender' => $this->gender,
  699. 'fiscal_code' => $this->fiscal_code,
  700. 'address' => $this->address,
  701. 'zip_code' => $this->zip_code,
  702. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  703. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  704. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  705. 'phone' => $this->phone,
  706. 'phone2' => $this->phone2,
  707. 'phone3' => $this->phone3,
  708. 'email' => strtolower($this->email),
  709. 'image' => $name,
  710. 'enabled' => $this->enabled
  711. ]);
  712. session()->flash('success, Tesserato creato');
  713. $this->resetFields();
  714. if ($close)
  715. {
  716. $this->add = false;
  717. }
  718. else
  719. {
  720. $this->edit($member->id);
  721. }
  722. $this->emit('setEdit', false);
  723. } catch (\Exception $ex) {
  724. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  725. }
  726. }
  727. public function duplicate($id){
  728. $member = \App\Models\Member::findOrFail($id);
  729. $newMember = $member->replicate();
  730. $newMember->save();
  731. $this->edit($newMember->id);
  732. }
  733. public function edit($id){
  734. $this->showDetail = false;
  735. $this->emit('setEdit', true);
  736. $this->emit('hide-search');
  737. try {
  738. $member = \App\Models\Member::findOrFail($id);
  739. if( !$member) {
  740. session()->flash('error','Tesserato non trovato');
  741. } else {
  742. $this->first_name = $member->first_name;
  743. $this->last_name = $member->last_name;
  744. $this->status = $member->status;
  745. $this->birth_city_id = $member->birth_city_id;
  746. $this->birth_province_id = $member->birth_province_id;
  747. $this->birth_nation_id = $member->birth_nation_id;
  748. $this->birth_date = $member->birth_date;
  749. $this->birth_place = $member->birth_place;
  750. $this->father_name = $member->father_name;
  751. $this->mother_name = $member->mother_name;
  752. $this->father_email = strtolower($member->father_email);
  753. $this->mother_email = strtolower($member->mother_email);
  754. $this->father_phone = $member->father_phone;
  755. $this->mother_phone = $member->mother_phone;
  756. $this->father_fiscal_code = $member->father_fiscal_code;
  757. $this->mother_fiscal_code = $member->mother_fiscal_code;
  758. $this->father_doc_number = $member->father_doc_number;
  759. $this->mother_doc_number = $member->mother_doc_number;
  760. $this->father_doc_type = $member->father_doc_type;
  761. $this->mother_doc_type = $member->mother_doc_type;
  762. $this->document_type = $member->document_type;
  763. $this->document_number = $member->document_number;
  764. $this->document_from = $member->document_from;
  765. $this->document_expire_date = $member->document_expire_date;
  766. $this->document_files = explode("|", $member->document_files);
  767. $this->gender = $member->gender;
  768. $this->fiscal_code = $member->fiscal_code;
  769. $this->address = $member->address;
  770. $this->zip_code = $member->zip_code;
  771. $this->nation_id = $member->nation_id;
  772. $this->province_id = $member->province_id;
  773. $this->city_id = $member->city_id;
  774. $this->phone = $member->phone;
  775. $this->phone2 = $member->phone2;
  776. $this->phone3 = $member->phone3;
  777. $this->email = strtolower($member->email);
  778. $this->image_old = $member->image;
  779. $this->enabled = $member->enabled;
  780. $this->dataId = $member->id;
  781. $this->active = $member->getStatus();
  782. $date1 = new DateTime($this->birth_date);
  783. $date2 = new DateTime("now");
  784. $interval = $date1->diff($date2);
  785. $this->age = $interval->y . " anni";
  786. $this->under18 = $interval->y < 18;
  787. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  788. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  789. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  790. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  791. $this->update = true;
  792. $this->add = false;
  793. $this->emit('load-select');
  794. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  795. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  796. $this->emit('load-cities', $this->province_id, 'cityClass');
  797. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  798. }
  799. } catch (\Exception $ex) {
  800. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  801. }
  802. }
  803. public function update($close)
  804. {
  805. $this->isSaving = false;
  806. $this->emit('load-select');
  807. $rules = [
  808. 'first_name' => 'required',
  809. 'last_name' => 'required',
  810. 'email' => 'required',
  811. 'phone' => 'required',
  812. 'birth_nation_id' => 'required',
  813. //'birth_place' => 'required',
  814. 'address' => 'required',
  815. 'zip_code' => 'required',
  816. 'nation_id' => 'required',
  817. 'fiscal_code' => 'required',
  818. 'birth_date' => 'before_or_equal:today'
  819. ];
  820. if ($this->isBirthItaly)
  821. {
  822. $rules['birth_province_id'] = 'required';
  823. $rules['birth_city_id'] = 'required';
  824. }
  825. else
  826. {
  827. $rules['birth_place'] = 'required';
  828. }
  829. if ($this->isItaly)
  830. {
  831. $rules['province_id'] = 'required';
  832. $rules['city_id'] = 'required';
  833. }
  834. $zzz = [];
  835. if ($this->birth_nation_id == null)
  836. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  837. if ($this->isBirthItaly)
  838. {
  839. if ($this->birth_province_id == null)
  840. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  841. if ($this->birth_city_id == null)
  842. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  843. }
  844. if ($this->nation_id == null)
  845. $zzz[] = 'nation_id='.$this->nation_id;
  846. if ($this->isItaly)
  847. {
  848. if ($this->province_id == null)
  849. $zzz[] = 'province_id='.$this->province_id;
  850. if ($this->city_id == null)
  851. $zzz[] = 'city_id='.$this->city_id;
  852. }
  853. $this->emit('setErrorMsg', $zzz);
  854. $this->validate($rules);
  855. try {
  856. $name = '';
  857. if ($this->image)
  858. {
  859. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  860. $this->image->storeAs('public', $name);
  861. }
  862. $docs = implode("|", $this->document_files);
  863. \App\Models\Member::whereId($this->dataId)->update([
  864. 'first_name' => strtoupper($this->first_name),
  865. 'last_name' => strtoupper($this->last_name),
  866. 'status' => $this->status,
  867. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  868. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  869. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  870. 'birth_date' => $this->birth_date,
  871. 'birth_place' => $this->birth_place,
  872. 'father_name' => $this->father_name,
  873. 'mother_name' => $this->mother_name,
  874. 'father_email' => strtolower($this->father_email),
  875. 'mother_email' => strtolower($this->mother_email),
  876. 'father_phone' => $this->father_phone,
  877. 'mother_phone' => $this->mother_phone,
  878. 'father_fiscal_code' => $this->father_fiscal_code,
  879. 'mother_fiscal_code' => $this->mother_fiscal_code,
  880. 'father_doc_number' => $this->father_doc_number,
  881. 'father_doc_type' => $this->father_doc_type,
  882. 'mother_doc_number' => $this->mother_doc_number,
  883. 'mother_doc_type' => $this->mother_doc_type,
  884. 'document_type' => $this->document_type,
  885. 'document_number' => $this->document_number,
  886. 'document_from' => $this->document_from,
  887. 'document_expire_date' => $this->document_expire_date,
  888. 'document_files' => $docs,
  889. 'gender' => $this->gender,
  890. 'fiscal_code' => $this->fiscal_code,
  891. 'address' => $this->address,
  892. 'zip_code' => $this->zip_code,
  893. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  894. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  895. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  896. 'image' => $name != '' ? $name : $this->image_old,
  897. 'phone' => $this->phone,
  898. 'phone2' => $this->phone2,
  899. 'phone3' => $this->phone3,
  900. 'email' => strtolower($this->email),
  901. 'enabled' => $this->enabled
  902. ]);
  903. session()->flash('success','Tesserato aggiornato');
  904. if ($close)
  905. {
  906. $this->resetFields();
  907. $this->update = false;
  908. }
  909. $this->emit('setEdit', false);
  910. } catch (\Exception $ex) {
  911. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  912. }
  913. }
  914. public function cancel()
  915. {
  916. $this->add = false;
  917. $this->update = false;
  918. $this->showDetail = false;
  919. $this->resetFields();
  920. $this->emit('setEdit', false);
  921. }
  922. public function delete($id)
  923. {
  924. try{
  925. \App\Models\Member::find($id)->delete();
  926. session()->flash('success',"Tesserato eliminato");
  927. }catch(\Exception $e){
  928. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  929. }
  930. }
  931. public function checkUncheckAll()
  932. {
  933. if (!$this->checkedAll)
  934. {
  935. $this->multipleIds = array();
  936. }
  937. else
  938. {
  939. foreach($this->records as $r)
  940. {
  941. $this->multipleIds[] = $r->id;
  942. }
  943. }
  944. }
  945. public function multipleDelete()
  946. {
  947. try{
  948. foreach($this->multipleIds as $id)
  949. {
  950. \App\Models\Member::find($id)->delete();
  951. }
  952. }catch(\Exception $e){
  953. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  954. }
  955. $this->multipleAction = '';
  956. }
  957. // Card
  958. public function addCard()
  959. {
  960. $this->resetCardFields();
  961. $this->addCard = true;
  962. $this->updateCard = false;
  963. }
  964. public function storeCard()
  965. {
  966. $this->validate(['card_card_id' => 'required']);
  967. try {
  968. // in base alla card selezionata calcolo la scadenza
  969. $expire_date = null;
  970. if ($this->card_date != '')
  971. {
  972. $y = date("Y", strtotime($this->card_date));
  973. $card = \App\Models\Card::findOrFail($this->card_card_id);
  974. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  975. {
  976. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  977. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  978. $y += 1;
  979. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  980. if ($next_exp > $this->card_date)
  981. {
  982. $expire_date = $next_exp;
  983. }
  984. else
  985. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  986. }
  987. else
  988. {
  989. if ($card->one_year_expire)
  990. {
  991. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  992. }
  993. }
  994. }
  995. \App\Models\MemberCard::create([
  996. 'member_id' => $this->dataId,
  997. 'card_id' => $this->card_card_id,
  998. 'number' => $this->card_number,
  999. 'date' => $this->card_date,
  1000. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1001. 'expire_date' => $expire_date,
  1002. 'status' => $this->card_status,
  1003. 'discipline1_id' => $this->card_discipline1_id,
  1004. 'discipline2_id' => $this->card_discipline2_id,
  1005. 'discipline3_id' => $this->card_discipline3_id,
  1006. ]);
  1007. session()->flash('success, Tesserato creato');
  1008. $this->resetCardFields();
  1009. $this->addCard = false;
  1010. } catch (\Exception $ex) {
  1011. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1012. }
  1013. }
  1014. public function editCard($id){
  1015. try {
  1016. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1017. if( !$memberCard) {
  1018. session()->flash('error','Tesserato non trovato');
  1019. } else {
  1020. $this->card_card_id = $memberCard->card_id;
  1021. $this->card_number = $memberCard->number;
  1022. $this->card_date = $memberCard->date;
  1023. $this->card_accept_date = $memberCard->accept_date;
  1024. $this->card_status = $memberCard->status;
  1025. $this->card_discipline1_id = $memberCard->discipline1_id;
  1026. $this->card_discipline2_id = $memberCard->discipline2_id;
  1027. $this->card_discipline3_id = $memberCard->discipline3_id;
  1028. $this->cardDataId = $memberCard->id;
  1029. $this->updateCard = true;
  1030. $this->addCard = false;
  1031. }
  1032. } catch (\Exception $ex) {
  1033. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1034. }
  1035. }
  1036. public function updateCard()
  1037. {
  1038. //$this->validate();
  1039. $this->validate(['card_card_id' => 'required']);
  1040. try {
  1041. // in base alla card selezionata calcolo la scadenza
  1042. $expire_date = null;
  1043. if ($this->card_date != '')
  1044. {
  1045. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1046. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  1047. {
  1048. $y = date("Y", strtotime($this->card_date));
  1049. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1050. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1051. if ($next_exp > $this->card_date)
  1052. {
  1053. $expire_date = $next_exp;
  1054. }
  1055. else
  1056. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1057. }
  1058. else
  1059. {
  1060. if ($card->one_year_expire)
  1061. {
  1062. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1063. }
  1064. }
  1065. }
  1066. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1067. 'member_id' => $this->dataId,
  1068. 'card_id' => $this->card_card_id,
  1069. 'number' => $this->card_number,
  1070. 'date' => $this->card_date,
  1071. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1072. 'expire_date' => $expire_date,
  1073. 'status' => $this->card_status,
  1074. 'discipline1_id' => $this->card_discipline1_id,
  1075. 'discipline2_id' => $this->card_discipline2_id,
  1076. 'discipline3_id' => $this->card_discipline3_id,
  1077. ]);
  1078. session()->flash('success','Tesserato aggiornato');
  1079. $this->resetCardFields();
  1080. $this->updateCard = false;
  1081. } catch (\Exception $ex) {
  1082. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1083. }
  1084. }
  1085. public function cancelCard()
  1086. {
  1087. $this->addCard = false;
  1088. $this->updateCard = false;
  1089. $this->resetCardFields();
  1090. }
  1091. public function deleteCard($id)
  1092. {
  1093. try{
  1094. \App\Models\MemberCard::find($id)->delete();
  1095. session()->flash('success',"Tesserato eliminato");
  1096. }catch(\Exception $e){
  1097. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1098. }
  1099. }
  1100. // Courses
  1101. public function addCourse()
  1102. {
  1103. $this->resetCourseFields();
  1104. $this->addCourse = true;
  1105. $this->updateCourse = false;
  1106. }
  1107. public function storeCourse()
  1108. {
  1109. $this->validate(['course_course_id' => 'required']);
  1110. try {
  1111. \App\Models\MemberCourse::create([
  1112. 'member_id' => $this->dataId,
  1113. 'course_id' => $this->course_course_id,
  1114. 'date_from' => $this->course_date_from,
  1115. 'date_to' => $this->course_date_to,
  1116. 'course_subscription_id' => $this->course_course_subscription_id,
  1117. 'status' => $this->course_status,
  1118. 'subscribed' => false, // default non iscritto
  1119. 'price' => currencyToDouble($this->course_price),
  1120. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1121. 'notes' => $this->course_note,
  1122. 'months' => json_encode($this->course_months),
  1123. 'when' => json_encode($this->course_when)
  1124. ]);
  1125. session()->flash('success, Corso creato');
  1126. $this->resetCourseFields();
  1127. $this->addCourse = false;
  1128. } catch (\Exception $ex) {
  1129. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1130. }
  1131. }
  1132. public function editCourse($id){
  1133. try {
  1134. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1135. if( !$memberCourse) {
  1136. session()->flash('error','Corso non trovato');
  1137. } else {
  1138. $this->course_course_id = $memberCourse->course_id;
  1139. // Carico i dati relativi alla struttura ad albero
  1140. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1141. $this->course_name = $c->name;
  1142. $this->course_level_id = $c->course_level_id;
  1143. $this->course_type_id = $c->course_type_id;
  1144. $this->course_frequency_id = $c->course_frequency_id;
  1145. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->get();
  1146. foreach($all as $a)
  1147. {
  1148. $levels_ids[] = $a->course_level_id;
  1149. }
  1150. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1151. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('course_level_id', $this->course_level_id)->get();
  1152. foreach($all as $a)
  1153. {
  1154. $types_ids[] = $a->course_type_id;
  1155. }
  1156. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1157. $frequencies_ids = [];
  1158. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->get();
  1159. foreach($all as $a)
  1160. {
  1161. $frequencies_ids[] = $a->course_frequency_id;
  1162. }
  1163. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1164. $this->course_date_from = $memberCourse->date_from;
  1165. $this->course_date_to = $memberCourse->date_to;
  1166. foreach(json_decode($memberCourse->when) as $z)
  1167. {
  1168. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1169. }
  1170. //$this->course_when = json_decode($memberCourse->when);
  1171. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1172. $this->course_status = $memberCourse->status;
  1173. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1174. $this->course_price = formatPrice($memberCourse->price);
  1175. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1176. $this->course_note = $memberCourse->notes;
  1177. foreach(json_decode($memberCourse->months) as $z)
  1178. {
  1179. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1180. }
  1181. $this->courseDataId = $memberCourse->id;
  1182. $this->updateCourse = true;
  1183. $this->addCourse = false;
  1184. }
  1185. } catch (\Exception $ex) {
  1186. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1187. }
  1188. }
  1189. public function updateCourse()
  1190. {
  1191. //$this->validate();
  1192. $this->validate(['course_course_id' => 'required']);
  1193. try {
  1194. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1195. 'member_id' => $this->dataId,
  1196. 'course_id' => $this->course_course_id,
  1197. 'date_from' => $this->course_date_from,
  1198. 'date_to' => $this->course_date_to,
  1199. 'course_subscription_id' => $this->course_course_subscription_id,
  1200. 'status' => $this->course_status,
  1201. 'price' => currencyToDouble($this->course_price),
  1202. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1203. 'notes' => $this->course_note,
  1204. 'months' => json_encode($this->course_months),
  1205. 'when' => json_encode($this->course_when)
  1206. ]);
  1207. session()->flash('success','Corso aggiornato');
  1208. $this->resetCourseFields();
  1209. $this->updateCourse = false;
  1210. } catch (\Exception $ex) {
  1211. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1212. }
  1213. }
  1214. public function cancelCourse()
  1215. {
  1216. $this->addCourse = false;
  1217. $this->updateCourse = false;
  1218. $this->resetCourseFields();
  1219. }
  1220. public function deleteCourse($id)
  1221. {
  1222. try{
  1223. \App\Models\MemberCourse::find($id)->delete();
  1224. session()->flash('success',"Corso eliminato");
  1225. }catch(\Exception $e){
  1226. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1227. }
  1228. }
  1229. public function addRow()
  1230. {
  1231. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1232. }
  1233. public function delRow($idx)
  1234. {
  1235. unset($this->course_when[$idx]);
  1236. }
  1237. // Certificates
  1238. public function addCertificate()
  1239. {
  1240. $this->resetCertificateFields();
  1241. $this->addCertificate = true;
  1242. $this->updateCertificate = false;
  1243. }
  1244. public function storeCertificate()
  1245. {
  1246. $this->validate(['certificate_expire_date' => 'required']);
  1247. // $this->validate();
  1248. try {
  1249. $name = '';
  1250. try{
  1251. if ($this->certificate_filename)
  1252. {
  1253. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1254. $this->certificate_filename->storeAs('public', $name);
  1255. }
  1256. } catch (\Exception $ex) {
  1257. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1258. }
  1259. if ($this->dataId > -1)
  1260. {
  1261. \App\Models\MemberCertificate::create([
  1262. 'member_id' => $this->dataId,
  1263. 'type' => $this->certificate_type,
  1264. 'filename' => $name,
  1265. 'expire_date' => $this->certificate_expire_date,
  1266. 'status' => $this->certificate_status
  1267. ]);
  1268. }
  1269. /*else
  1270. {
  1271. $this->certificateTmp = new \App\Models\MemberCertificate();
  1272. $this->certificateTmp->type = $this->certificate_type;
  1273. $this->certificateTmp->filename = $name;
  1274. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1275. $this->certificateTmp->status = $this->certificate_status;
  1276. $this->certificateTmp->status = $this->certificate_status;
  1277. // s $this->member_certificates[] = $certificateTmp;
  1278. }*/
  1279. session()->flash('success, Tesserato creato');
  1280. $this->resetCertificateFields();
  1281. $this->addCertificate = false;
  1282. } catch (\Exception $ex) {
  1283. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1284. }
  1285. }
  1286. public function editCertificate($id){
  1287. try {
  1288. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1289. if( !$memberCertificate) {
  1290. session()->flash('error','Tesserato non trovato');
  1291. } else {
  1292. $this->certificate_type = $memberCertificate->type;
  1293. $this->certificate_filename_old = $memberCertificate->filename;
  1294. $this->certificate_expire_date = $memberCertificate->expire_date;
  1295. $this->certificate_status = $memberCertificate->status;
  1296. $this->cardCertificateId = $memberCertificate->id;
  1297. $this->updateCertificate = true;
  1298. $this->addCertificate = false;
  1299. }
  1300. } catch (\Exception $ex) {
  1301. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1302. }
  1303. }
  1304. public function updateCertificate()
  1305. {
  1306. $this->validate(['certificate_expire_date' => 'required']);
  1307. try {
  1308. $name = '';
  1309. try{
  1310. if ($this->certificate_filename)
  1311. {
  1312. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1313. $this->certificate_filename->storeAs('public', $name);
  1314. }
  1315. } catch (\Exception $ex) {
  1316. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1317. }
  1318. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1319. 'member_id' => $this->dataId,
  1320. 'type' => $this->certificate_type,
  1321. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1322. 'expire_date' => $this->certificate_expire_date,
  1323. 'status' => $this->certificate_status
  1324. ]);
  1325. session()->flash('success','Tesserato aggiornato');
  1326. $this->resetCertificateFields();
  1327. $this->updateCertificate = false;
  1328. } catch (\Exception $ex) {
  1329. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1330. }
  1331. }
  1332. public function cancelCertificate()
  1333. {
  1334. $this->addCertificate = false;
  1335. $this->updateCertificate = false;
  1336. $this->resetCertificateFields();
  1337. }
  1338. public function deleteCertificate($id)
  1339. {
  1340. try{
  1341. \App\Models\MemberCertificate::find($id)->delete();
  1342. session()->flash('success',"Tesserato eliminato");
  1343. }catch(\Exception $e){
  1344. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1345. }
  1346. }
  1347. // Gruppi di appartenenza
  1348. public function storeCategory()
  1349. {
  1350. $this->validate(['category_category_id' => 'required']);
  1351. try {
  1352. \App\Models\MemberCategory::create([
  1353. 'member_id' => $this->dataId,
  1354. 'category_id' => $this->category_category_id,
  1355. 'date' => \Carbon\Carbon::now()
  1356. ]);
  1357. session()->flash('success, Associazione creato');
  1358. $this->resetCategoryFields();
  1359. $this->addCard = false;
  1360. } catch (\Exception $ex) {
  1361. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1362. }
  1363. }
  1364. public function hideMsg()
  1365. {
  1366. $this->groupMsg = '';
  1367. }
  1368. public function storeCategoryWithID($id)
  1369. {
  1370. $this->groupMsg = '';
  1371. try {
  1372. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first())
  1373. {
  1374. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1375. }
  1376. else
  1377. {
  1378. \App\Models\MemberCategory::create([
  1379. 'member_id' => $this->dataId,
  1380. 'category_id' => $id,
  1381. 'date' => \Carbon\Carbon::now()
  1382. ]);
  1383. session()->flash('success, Associazione creato');
  1384. $this->resetCategoryFields();
  1385. $this->addCard = false;
  1386. }
  1387. } catch (\Exception $ex) {
  1388. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1389. }
  1390. }
  1391. public function deleteCategory($id)
  1392. {
  1393. try{
  1394. \App\Models\MemberCategory::find($id)->delete();
  1395. session()->flash('success',"Associazione eliminata");
  1396. }catch(\Exception $e){
  1397. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1398. }
  1399. }
  1400. public function setCourse($id)
  1401. {
  1402. $this->course_course_id = $id;
  1403. }
  1404. public function getNation($nation)
  1405. {
  1406. if ($nation > 0)
  1407. {
  1408. $ret = \App\Models\Nation::findOrFail($nation);
  1409. return $ret->name;
  1410. }
  1411. return "";
  1412. }
  1413. public function getProvince($province)
  1414. {
  1415. if ($province > 0)
  1416. {
  1417. $ret = \App\Models\Province::findOrFail($province);
  1418. return $ret->name;
  1419. }
  1420. return "";
  1421. }
  1422. public function getCity($city)
  1423. {
  1424. if ($city > 0)
  1425. {
  1426. $ret = \App\Models\City::findOrFail($city);
  1427. return $ret->name;
  1428. }
  1429. return "";
  1430. }
  1431. public function getFiscalCode()
  1432. {
  1433. $this->error_fc = false;
  1434. $cf = new codicefiscale();
  1435. $cf->setDateSeparator('-');
  1436. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0)
  1437. {
  1438. $code = '';
  1439. if ($this->birth_city_id > 0)
  1440. {
  1441. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1442. }
  1443. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1444. $this->fiscal_code = $codice;
  1445. }
  1446. else
  1447. $this->error_fc = true;
  1448. }
  1449. public function setMonth($m)
  1450. {
  1451. $exist = -1;
  1452. foreach($this->course_months as $idx => $x)
  1453. {
  1454. if ($x["m"] == $m)
  1455. {
  1456. $exist = $idx;
  1457. break;
  1458. }
  1459. }
  1460. if ($exist > -1)
  1461. {
  1462. array_splice($this->course_months, $exist, 1);
  1463. }
  1464. else
  1465. {
  1466. $this->course_months[] = array("m" => $m, "status" => "");
  1467. }
  1468. /*
  1469. if (in_array($m, $this->course_months))
  1470. {
  1471. $i = array_search($m, $this->course_months);
  1472. array_splice($this->course_months, $i, 1);
  1473. }
  1474. else
  1475. {
  1476. $this->course_months[] = $m;
  1477. }*/
  1478. }
  1479. public function newPayment($course)
  1480. {
  1481. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1482. return redirect()->to('/in?new=1&memberId=' . $this->dataId . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=' . $this->createSubscription . (sizeof($this->payMonths) > 0 ? '&months=' . implode("|", $this->payMonths) : "") . '&price=' . $course["price"] . '&subscription_price=' . $course["subscription_price"] . "&courseId=" . $course["id"]);
  1483. }
  1484. public function block($course)
  1485. {
  1486. $xxx = json_decode($course["months"]);
  1487. foreach($xxx as $idx => $mm)
  1488. {
  1489. if (in_array($mm->m, $this->payMonths))
  1490. {
  1491. $xxx[$idx]->status = 2;
  1492. }
  1493. }
  1494. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1495. $c->months = json_encode($xxx);
  1496. $c->save();
  1497. $this->payMonths = array();
  1498. }
  1499. public function setPayMonth($m, $months)
  1500. {
  1501. if (in_array($m, $this->payMonths))
  1502. {
  1503. $i = array_search($m, $this->payMonths);
  1504. array_splice($this->payMonths, $i, 1);
  1505. }
  1506. else
  1507. {
  1508. foreach(json_decode($months) as $mm)
  1509. {
  1510. if ($mm->m == $m)
  1511. {
  1512. if ($mm->status == "")
  1513. $this->payMonths[] = $m;
  1514. }
  1515. }
  1516. }
  1517. }
  1518. public function setCreateSubscription($subscribed)
  1519. {
  1520. if (!$subscribed)
  1521. $this->createSubscription = !$this->createSubscription;
  1522. }
  1523. public function showHideCourse($id)
  1524. {
  1525. if (in_array($id, $this->showCourse))
  1526. {
  1527. $i = array_search($id, $this->showCourse);
  1528. array_splice($this->showCourse, $i, 1);
  1529. }
  1530. else
  1531. {
  1532. $this->showCourse[] = $id;
  1533. }
  1534. }
  1535. public function getMonthStatus($m, $months)
  1536. {
  1537. $class = "grey";
  1538. foreach(json_decode($months) as $mm)
  1539. {
  1540. if ($mm->m == $m)
  1541. {
  1542. if ($mm->status == "")
  1543. {
  1544. if (in_array($m, $this->payMonths))
  1545. $class = "blue";
  1546. else
  1547. $class = "orange";
  1548. }
  1549. if ($mm->status == "1")
  1550. {
  1551. $class = "green";
  1552. }
  1553. if ($mm->status == "2")
  1554. {
  1555. $class = "yellow";
  1556. }
  1557. }
  1558. }
  1559. return $class;
  1560. }
  1561. public function getSubscriptionStatus($subscribed)
  1562. {
  1563. $class = "grey";
  1564. if ($this->createSubscription)
  1565. $class = "blue";
  1566. else
  1567. $class = "orange";
  1568. if ($subscribed)
  1569. $class = "green";
  1570. return $class;
  1571. }
  1572. public function checkMonth($m)
  1573. {
  1574. $ret = false;
  1575. foreach($this->course_months as $idx => $x)
  1576. {
  1577. if ($x["m"] == $m)
  1578. {
  1579. $ret = true;
  1580. break;
  1581. }
  1582. }
  1583. return $ret;
  1584. }
  1585. public function setDay($idx, $d)
  1586. {
  1587. if (in_array($d, $this->course_when[$idx]["day"]))
  1588. {
  1589. $i = array_search($d, $this->course_when[$idx]["day"]);
  1590. array_splice($this->course_when[$idx]["day"], $i, 1);
  1591. }
  1592. else
  1593. {
  1594. $this->course_when[$idx]["day"][] = $d;
  1595. }
  1596. }
  1597. public function getMonth($m)
  1598. {
  1599. $ret = '';
  1600. switch ($m) {
  1601. case 1:
  1602. $ret = 'Gennaio';
  1603. break;
  1604. case 2:
  1605. $ret = 'Febbraio';
  1606. break;
  1607. case 3:
  1608. $ret = 'Marzo';
  1609. break;
  1610. case 4:
  1611. $ret = 'Aprile';
  1612. break;
  1613. case 5:
  1614. $ret = 'Maggio';
  1615. break;
  1616. case 6:
  1617. $ret = 'Giugno';
  1618. break;
  1619. case 7:
  1620. $ret = 'Luglio';
  1621. break;
  1622. case 8:
  1623. $ret = 'Agosto';
  1624. break;
  1625. case 9:
  1626. $ret = 'Settembre';
  1627. break;
  1628. case 10:
  1629. $ret = 'Ottobre';
  1630. break;
  1631. case 11:
  1632. $ret = 'Novembre';
  1633. break;
  1634. case 12:
  1635. $ret = 'Dicembre';
  1636. break;
  1637. default:
  1638. $ret = '';
  1639. break;
  1640. }
  1641. return $ret;
  1642. }
  1643. }
  1644. class codicefiscale {
  1645. /**
  1646. * Array delle consonanti
  1647. */
  1648. protected $_consonanti = array(
  1649. 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K',
  1650. 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
  1651. 'V', 'W', 'X', 'Y', 'Z'
  1652. );
  1653. /**
  1654. * Array delle vocali
  1655. */
  1656. protected $_vocali = array(
  1657. 'A', 'E', 'I', 'O', 'U'
  1658. );
  1659. /**
  1660. * Array per il calcolo della lettera del mese
  1661. * Al numero del mese corrisponde una lettera
  1662. */
  1663. protected $_mesi = array(
  1664. 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
  1665. 6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
  1666. 11 => 'S', 12 => 'T'
  1667. );
  1668. protected $_pari = array(
  1669. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
  1670. '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
  1671. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4,
  1672. 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9,
  1673. 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14,
  1674. 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19,
  1675. 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24,
  1676. 'Z' => 25
  1677. );
  1678. protected $_dispari = array(
  1679. '0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9,
  1680. '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21,
  1681. 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9,
  1682. 'F' => 13, 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21,
  1683. 'K' => 2, 'L' => 4, 'M' => 18, 'N' => 20, 'O' => 11,
  1684. 'P' => 3, 'Q' => 6, 'R' => 8, 'S' => 12, 'T' => 14,
  1685. 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25, 'Y' => 24,
  1686. 'Z' => 23
  1687. );
  1688. protected $_controllo = array(
  1689. '0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
  1690. '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
  1691. '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
  1692. '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P',
  1693. '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T',
  1694. '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X',
  1695. '24' => 'Y', '25' => 'Z'
  1696. );
  1697. /**
  1698. * Stringa di errore
  1699. */
  1700. protected $_error = null;
  1701. /**
  1702. * Separatore per la data di nascita
  1703. */
  1704. protected $_dateSeparator = '/';
  1705. /**
  1706. * Percorso del file del database SQLite
  1707. * dei codici catastali
  1708. */
  1709. protected $_dbCatastali = null;
  1710. /**
  1711. * Trasforma la stringa passata in un array di lettere
  1712. * e lo incrocia con un ulteriore array
  1713. */
  1714. protected function _getLettere($string, array $haystack) {
  1715. $letters = array();
  1716. foreach(str_split($string) as $needle) {
  1717. if (in_array($needle, $haystack)) {
  1718. $letters[] = $needle;
  1719. }
  1720. }
  1721. return $letters;
  1722. }
  1723. /**
  1724. * Ritorna un array con le vocali di una data stringa
  1725. */
  1726. protected function _getVocali($string) {
  1727. return $this->_getLettere($string, $this->_vocali);
  1728. }
  1729. /**
  1730. * Ritorna un array con le consonanti di una data stringa
  1731. */
  1732. protected function _getConsonanti($string) {
  1733. return $this->_getLettere($string, $this->_consonanti);
  1734. }
  1735. /**
  1736. * Pulisce la stringa filtrando tutti i caratteri che
  1737. * non sono lettere. Lo switch $toupper se impostato a TRUE
  1738. * converte la stringa risultante in MAIUSCOLO.
  1739. */
  1740. protected function _sanitize($string, $toupper = true) {
  1741. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  1742. return ($toupper) ? strtoupper($result) : $result;
  1743. }
  1744. /**
  1745. * Se la stringa passata a funzione e' costituita
  1746. * da meno di 3 caratteri, rimpiazza le lettere
  1747. * mancanti con la lettera X.
  1748. */
  1749. protected function _addMissingX($string) {
  1750. $code = $string;
  1751. while(strlen($code) < 3) {
  1752. $code .= 'X';
  1753. }
  1754. return $code;
  1755. }
  1756. /**
  1757. * Ottiene il codice identificativo del nome
  1758. */
  1759. protected function _calcolaNome($string) {
  1760. $nome = $this->_sanitize($string);
  1761. $code = '';
  1762. // Se il nome inserito e' piu' corto di 3 lettere
  1763. // si aggiungono tante X quanti sono i caratteri
  1764. // mancanti.
  1765. if (strlen($nome) < 3) {
  1766. return $this->_addMissingX($nome);
  1767. }
  1768. $nome_cons = $this->_getConsonanti($nome);
  1769. // Se le consonanti contenute nel nome sono minori
  1770. // o uguali a 3 vengono considerate nell'ordine in cui
  1771. // compaiono.
  1772. if (count($nome_cons) <= 3) {
  1773. $code = implode('', $nome_cons);
  1774. } else {
  1775. // Se invece abbiamo almeno 4 consonanti, prendiamo
  1776. // la prima, la terza e la quarta.
  1777. for($i=0; $i<4; $i++) {
  1778. if ($i == 1) continue;
  1779. if (!empty($nome_cons[$i])) {
  1780. $code .= $nome_cons[$i];
  1781. }
  1782. }
  1783. }
  1784. // Se compaiono meno di 3 consonanti nel nome, si
  1785. // utilizzano le vocali, nell'ordine in cui compaiono
  1786. // nel nome.
  1787. if (strlen($code) < 3) {
  1788. $nome_voc = $this->_getVocali($nome);
  1789. while (strlen($code) < 3) {
  1790. $code .= array_shift($nome_voc);
  1791. }
  1792. }
  1793. return $code;
  1794. }
  1795. protected function _calcolaCognome($string) {
  1796. $cognome = $this->_sanitize($string);
  1797. $code = '';
  1798. // Se il cognome inserito e' piu' corto di 3 lettere
  1799. // si aggiungono tante X quanti sono i caratteri
  1800. // mancanti.
  1801. if (strlen($cognome) < 3) {
  1802. return $this->_addMissingX($cognome);
  1803. }
  1804. $cognome_cons = $this->_getConsonanti($cognome);
  1805. // Per il calcolo del cognome si prendono le prime
  1806. // 3 consonanti.
  1807. for ($i=0; $i<3; $i++) {
  1808. if (array_key_exists($i, $cognome_cons)) {
  1809. $code .= $cognome_cons[$i];
  1810. }
  1811. }
  1812. // Se le consonanti non bastano, vengono prese
  1813. // le vocali nell'ordine in cui compaiono.
  1814. if (strlen($code) < 3) {
  1815. $cognome_voc = $this->_getVocali($cognome);
  1816. while (strlen($code) < 3) {
  1817. $code .= array_shift($cognome_voc);
  1818. }
  1819. }
  1820. return $code;
  1821. }
  1822. /**
  1823. * Imposta il separatore di data ( default: / )
  1824. */
  1825. public function setDateSeparator($char) {
  1826. $this->_dateSeparator = $char;
  1827. return $this;
  1828. }
  1829. /**
  1830. * Ritorna la parte di codice fiscale corrispondente
  1831. * alla data di nascita del soggetto (Forma: AAMGG)
  1832. */
  1833. protected function _calcolaDataNascita($data, $sesso) {
  1834. $dn = explode($this->_dateSeparator, $data);
  1835. $giorno = (int) @$dn[2];
  1836. $mese = (int) @$dn[1];
  1837. $anno = (int) @$dn[0];
  1838. // Le ultime due cifre dell'anno di nascita
  1839. $aa = substr($anno, -2);
  1840. // La lettera corrispondente al mese di nascita
  1841. $mm = $this->_mesi[$mese];
  1842. // Il giorno viene calcolato a seconda del sesso
  1843. // del soggetto di cui si calcola il codice:
  1844. // se e' Maschio si mette il giorno reale, se e'
  1845. // Femmina viene aggiungo 40 a questo numero.
  1846. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  1847. // Bug #1: Thanks to Luca
  1848. if (strlen($gg) < 2) $gg = '0' . $gg;
  1849. return $aa . $mm . $gg;
  1850. }
  1851. /**
  1852. * Ritorna la cifra di controllo sulla base dei
  1853. * 15 caratteri del codice fiscale calcolati.
  1854. */
  1855. protected function _calcolaCifraControllo($codice) {
  1856. $code = str_split($codice);
  1857. $sum = 0;
  1858. for($i=1; $i <= count($code); $i++) {
  1859. $cifra = $code[$i-1];
  1860. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  1861. }
  1862. $sum %= 26;
  1863. return $this->_controllo[$sum];
  1864. }
  1865. /**
  1866. * Imposta il messaggio di errore
  1867. */
  1868. protected function _setError($string) {
  1869. $this->_error = $string;
  1870. }
  1871. /**
  1872. * Verifica la presenza di un errore.
  1873. * Ritorna TRUE se presente, FALSE altrimenti.
  1874. */
  1875. public function hasError() {
  1876. return !is_null($this->_error);
  1877. }
  1878. /**
  1879. * Ritorna la stringa di errore
  1880. */
  1881. public function getError() {
  1882. return $this->_error;
  1883. }
  1884. /**
  1885. * Ritorna il codice fiscale utilizzando i parametri
  1886. * passati a funzione. Se si verifica
  1887. */
  1888. public function calcola($nome, $cognome, $data, $sesso, $comune) {
  1889. $codice = $this->_calcolaCognome($cognome) .
  1890. $this->_calcolaNome($nome) .
  1891. $this->_calcolaDataNascita($data, $sesso) .
  1892. $comune;
  1893. if ($this->hasError()) {
  1894. return false;
  1895. }
  1896. $codice .= $this->_calcolaCifraControllo($codice);
  1897. if (strlen($codice) != 16) {
  1898. //$this->_setError(self::ERR_GENERIC);
  1899. return 'ERROR';
  1900. }
  1901. return $codice;
  1902. }
  1903. }