Member.php 61 KB

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