Member.php 75 KB

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