Member.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\Attributes\Url;
  5. use Livewire\WithFileUploads;
  6. use DateTime;
  7. class Member extends Component
  8. {
  9. use WithFileUploads;
  10. 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, $email, $enabled, $dataId, $update = false, $add = false;
  11. public $father_name, $mother_name, $father_email, $mother_email, $father_phone, $mother_phone, $father_fiscal_code, $mother_fiscal_code;
  12. public $selectId = 0;
  13. public $causalId = 0;
  14. public $under18 = false;
  15. public $multipleIds = [];
  16. public $multipleAction = '';
  17. public $isItaly = true;
  18. public $isBirthItaly = true;
  19. public $showDetail = false;
  20. public $currentMember;
  21. public $age = '';
  22. public $cards = array();
  23. public $categories = array();
  24. public $nations = array();
  25. public $provinces = array();
  26. public $cities = array();
  27. public $searchTxt;
  28. public $search;
  29. public $showReset = false;
  30. public $advanced = false;
  31. public $birthNations = array();
  32. public $birthProvinces = array();
  33. public $birthCities = array();
  34. public $active;
  35. // Card data
  36. public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_status, $addCard, $updateCard, $cardDataId;
  37. // Categories data
  38. public $member_categories = array(), $category_category_id;
  39. // Certificates data
  40. public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
  41. protected $rules = [
  42. 'first_name' => 'required',
  43. 'last_name' => 'required',
  44. ];
  45. public function resetFields(){
  46. $this->dataId = -1;
  47. $this->first_name = '';
  48. $this->last_name = '';
  49. $this->status = '';
  50. $this->birth_city_id = null;
  51. //$this->birth_province_id = null;
  52. //$this->birth_nation_id = null;
  53. $this->birth_province_id = 178;
  54. $this->birth_nation_id = 1;
  55. $this->birth_date = null;
  56. $this->birth_place = '';
  57. $this->father_name = '';
  58. $this->mother_name = '';
  59. $this->father_email = '';
  60. $this->mother_email = '';
  61. $this->father_phone = '';
  62. $this->mother_phone = '';
  63. $this->father_fiscal_code = '';
  64. $this->mother_fiscal_code = '';
  65. $this->gender = '';
  66. $this->fiscal_code = '';
  67. $this->address = '';
  68. $this->zip_code = '';
  69. //$this->nation_id = null;
  70. //$this->province_id = null;
  71. $this->nation_id = 1;
  72. $this->province_id = 178;
  73. $this->city_id = null;
  74. $this->phone = '';
  75. $this->email = '';
  76. $this->enabled = true;
  77. $this->under18 = false;
  78. $this->image = null;
  79. }
  80. public function resetCardFields(){
  81. $this->card_card_id = null;
  82. $this->card_number = '';
  83. $this->card_date = null;
  84. $this->card_accept_date = null;
  85. $this->card_status = 0;
  86. }
  87. public function resetCertificateFields(){
  88. $this->certificate_type = '';
  89. $this->certificate_filename = '';
  90. $this->certificate_filename_old = '';
  91. $this->certificate_expire_date = null;
  92. $this->certificate_status = 0;
  93. }
  94. public function executeMultipleAction(){
  95. if ($this->multipleAction == 'delete')
  96. $this->multipleDelete();
  97. }
  98. public function updatedImage()
  99. {
  100. $this->validate([
  101. 'image' => 'image|max:1024',
  102. ]);
  103. $this->image_old = '';
  104. }
  105. public function resetCategoryFields(){
  106. $this->category_category_id = null;
  107. }
  108. public function getCategories($records, $indentation)
  109. {
  110. foreach($records as $record)
  111. {
  112. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  113. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  114. if(count($record->childs))
  115. $this->getCategories($record->childs, $indentation + 1);
  116. }
  117. }
  118. public function updatedBirthDate()
  119. {
  120. $date1 = new DateTime($this->birth_date);
  121. $date2 = new DateTime("now");
  122. $interval = $date1->diff($date2);
  123. $this->age = $interval->y . " anni";
  124. $this->under18 = $interval->y < 18;
  125. }
  126. public function mount()
  127. {
  128. if (isset($_GET["new"]))
  129. $this->add();
  130. $this->cards = \App\Models\Card::select('id', 'name')->get();
  131. $this->categories = array();
  132. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  133. $this->nations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  134. $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  135. $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();
  136. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  137. if ($c)
  138. $this->causalId = $c->id;
  139. $this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  140. //$this->birthProvinces = array();
  141. //$this->birthCities = array();
  142. $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  143. $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();
  144. }
  145. public function updated()
  146. {
  147. $this->emit('load-select');
  148. }
  149. public function hydrate()
  150. {
  151. $this->emit('load-select');
  152. }
  153. public function loadProvinces()
  154. {
  155. $n = \App\Models\Nation::findOrFail($this->nation_id);
  156. $this->isItaly = $n->is_italy;
  157. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
  158. $this->cities = array();
  159. $this->selectId++;
  160. }
  161. public function loadCities()
  162. {
  163. $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
  164. $this->selectId++;
  165. }
  166. public function loadBirthProvinces()
  167. {
  168. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  169. $this->isBirthItaly = $n->is_italy;
  170. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
  171. $this->birthCities = array();
  172. $this->selectId++;
  173. }
  174. public function loadBirthCities()
  175. {
  176. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  177. $this->selectId++;
  178. }
  179. public function search()
  180. {
  181. if ($this->searchTxt != '')
  182. {
  183. $this->search = $this->searchTxt;
  184. $this->showReset = true;
  185. }
  186. }
  187. public function resetSearch()
  188. {
  189. $this->showReset = false;
  190. $this->searchTxt = '';
  191. $this->search = $this->searchTxt;
  192. }
  193. public function advancedSearch()
  194. {
  195. $this->avanced = true;
  196. }
  197. public function render()
  198. {
  199. if (!$this->advanced)
  200. {
  201. if ($this->search != '')
  202. $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
  203. else
  204. $this->records = \App\Models\Member::get();
  205. }
  206. else
  207. {
  208. $this->records = \App\Models\Member::where('id', '>', 0);
  209. $this->records = $this->records->get();
  210. }
  211. //$this->records = $this->records->get();
  212. $this->loadMemberCards();
  213. $this->loadMemberCategories();
  214. $this->loadMemberCertificates();
  215. return view('livewire.member');
  216. }
  217. public function loadMemberCards()
  218. {
  219. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  220. // return view('livewire.member');
  221. }
  222. public function loadMemberCategories()
  223. {
  224. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  225. // return view('livewire.member');
  226. }
  227. public function loadMemberCertificates()
  228. {
  229. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->get();
  230. // return view('livewire.member');
  231. }
  232. public function showDetail($id)
  233. {
  234. $this->currentMember = \App\Models\Member::findOrFail($id);
  235. $this->showDetail = true;
  236. }
  237. public function add()
  238. {
  239. $this->emit('load-select');
  240. $this->showDetail = false;
  241. $this->resetFields();
  242. $this->add = true;
  243. $this->update = false;
  244. }
  245. public function store($close)
  246. {
  247. $this->emit('load-select');
  248. $this->validate();
  249. try {
  250. $name = '';
  251. if ($this->image)
  252. {
  253. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  254. $this->image->storeAs('public', $name);
  255. }
  256. $member = \App\Models\Member::create([
  257. 'first_name' => $this->first_name,
  258. 'last_name' => $this->last_name,
  259. 'status' => $this->status,
  260. 'birth_city_id' => $this->birth_city_id,
  261. 'birth_province_id' => $this->birth_province_id,
  262. 'birth_nation_id' => $this->birth_nation_id,
  263. 'birth_date' => $this->birth_date,
  264. 'birth_place' => $this->birth_place,
  265. 'father_name' => $this->father_name,
  266. 'mother_name' => $this->mother_name,
  267. 'father_email' => $this->father_email,
  268. 'mother_email' => $this->mother_email,
  269. 'father_phone' => $this->father_phone,
  270. 'mother_phone' => $this->mother_phone,
  271. 'father_fiscal_code' => $this->father_fiscal_code,
  272. 'mother_fiscal_code' => $this->mother_fiscal_code,
  273. 'gender' => $this->gender,
  274. 'fiscal_code' => $this->fiscal_code,
  275. 'address' => $this->address,
  276. 'zip_code' => $this->zip_code,
  277. 'nation_id' => $this->nation_id,
  278. 'province_id' => $this->province_id,
  279. 'city_id' => $this->city_id,
  280. 'phone' => $this->phone,
  281. 'email' => $this->email,
  282. 'image' => $name,
  283. 'enabled' => $this->enabled
  284. ]);
  285. session()->flash('success, Tesserato creato');
  286. $this->resetFields();
  287. if ($close)
  288. {
  289. $this->add = false;
  290. }
  291. else
  292. {
  293. $this->edit($member->id);
  294. }
  295. } catch (\Exception $ex) {
  296. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  297. }
  298. }
  299. public function duplicate($id){
  300. $member = \App\Models\Member::findOrFail($id);
  301. $newMember = $member->replicate();
  302. $newMember->save();
  303. $this->edit($newMember->id);
  304. }
  305. public function edit($id){
  306. $this->showDetail = false;
  307. $this->emit('load-select');
  308. try {
  309. $member = \App\Models\Member::findOrFail($id);
  310. if( !$member) {
  311. session()->flash('error','Tesserato non trovato');
  312. } else {
  313. $this->first_name = $member->first_name;
  314. $this->last_name = $member->last_name;
  315. $this->status = $member->status;
  316. $this->birth_city_id = $member->birth_city_id;
  317. $this->birth_province_id = $member->birth_province_id;
  318. $this->birth_nation_id = $member->birth_nation_id;
  319. $this->birth_date = $member->birth_date;
  320. $this->birth_place = $member->birth_place;
  321. $this->father_name = $member->father_name;
  322. $this->mother_name = $member->mother_name;
  323. $this->father_email = $member->father_email;
  324. $this->mother_email = $member->mother_email;
  325. $this->father_phone = $member->father_phone;
  326. $this->mother_phone = $member->mother_phone;
  327. $this->father_fiscal_code = $member->father_fiscal_code;
  328. $this->mother_fiscal_code = $member->mother_fiscal_code;
  329. $this->gender = $member->gender;
  330. $this->fiscal_code = $member->fiscal_code;
  331. $this->address = $member->address;
  332. $this->zip_code = $member->zip_code;
  333. $this->nation_id = $member->nation_id;
  334. $this->province_id = $member->province_id;
  335. $this->city_id = $member->city_id;
  336. $this->phone = $member->phone;
  337. $this->email = $member->email;
  338. $this->image_old = $member->image;
  339. $this->enabled = $member->enabled;
  340. $this->dataId = $member->id;
  341. $this->active = $member->isActive();
  342. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  343. $this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  344. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  345. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  346. $this->update = true;
  347. $this->add = false;
  348. }
  349. } catch (\Exception $ex) {
  350. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  351. }
  352. }
  353. public function update($close)
  354. {
  355. $this->emit('load-select');
  356. $this->validate();
  357. try {
  358. $name = '';
  359. if ($this->image)
  360. {
  361. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  362. $this->image->storeAs('public', $name);
  363. }
  364. \App\Models\Member::whereId($this->dataId)->update([
  365. 'first_name' => $this->first_name,
  366. 'last_name' => $this->last_name,
  367. 'status' => $this->status,
  368. 'birth_city_id' => $this->birth_city_id,
  369. 'birth_province_id' => $this->birth_province_id,
  370. 'birth_nation_id' => $this->birth_nation_id,
  371. 'birth_date' => $this->birth_date,
  372. 'birth_place' => $this->birth_place,
  373. 'father_name' => $this->father_name,
  374. 'mother_name' => $this->mother_name,
  375. 'father_email' => $this->father_email,
  376. 'mother_email' => $this->mother_email,
  377. 'father_phone' => $this->father_phone,
  378. 'mother_phone' => $this->mother_phone,
  379. 'father_fiscal_code' => $this->father_fiscal_code,
  380. 'mother_fiscal_code' => $this->mother_fiscal_code,
  381. 'gender' => $this->gender,
  382. 'fiscal_code' => $this->fiscal_code,
  383. 'address' => $this->address,
  384. 'zip_code' => $this->zip_code,
  385. 'nation_id' => $this->nation_id,
  386. 'province_id' => $this->province_id,
  387. 'city_id' => $this->city_id,
  388. 'image' => $name != '' ? $name : $this->image_old,
  389. 'phone' => $this->phone,
  390. 'email' => $this->email,
  391. 'enabled' => $this->enabled
  392. ]);
  393. session()->flash('success','Tesserato aggiornato');
  394. if ($close)
  395. {
  396. $this->resetFields();
  397. $this->update = false;
  398. }
  399. } catch (\Exception $ex) {
  400. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  401. }
  402. }
  403. public function cancel()
  404. {
  405. $this->add = false;
  406. $this->update = false;
  407. $this->showDetail = false;
  408. $this->resetFields();
  409. }
  410. public function delete($id)
  411. {
  412. try{
  413. \App\Models\Member::find($id)->delete();
  414. session()->flash('success',"Tesserato eliminato");
  415. }catch(\Exception $e){
  416. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  417. }
  418. }
  419. public function multipleDelete()
  420. {
  421. try{
  422. foreach($this->multipleIds as $id)
  423. {
  424. \App\Models\Member::find($id)->delete();
  425. }
  426. }catch(\Exception $e){
  427. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  428. }
  429. $this->multipleAction = '';
  430. }
  431. // Card
  432. public function addCard()
  433. {
  434. $this->resetCardFields();
  435. $this->addCard = true;
  436. $this->updateCard = false;
  437. }
  438. public function storeCard()
  439. {
  440. $this->validate(['card_card_id' => 'required']);
  441. try {
  442. // in base alla card selezionata calcolo la scadenza
  443. $expire_date = null;
  444. if ($this->card_date != '')
  445. {
  446. $card = \App\Models\Card::findOrFail($this->card_card_id);
  447. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  448. {
  449. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  450. $next_exp = date("Y-" . $m . "-" . $card->next_day_expire);
  451. if ($next_exp > $this->card_date)
  452. {
  453. $expire_date = $next_exp;
  454. }
  455. else
  456. $expire_date = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  457. }
  458. }
  459. \App\Models\MemberCard::create([
  460. 'member_id' => $this->dataId,
  461. 'card_id' => $this->card_card_id,
  462. 'number' => $this->card_number,
  463. 'date' => $this->card_date,
  464. 'accept_date' => $this->card_accept_date,
  465. 'expire_date' => $expire_date,
  466. 'status' => $this->card_status
  467. ]);
  468. session()->flash('success, Tesserato creato');
  469. $this->resetCardFields();
  470. $this->addCard = false;
  471. } catch (\Exception $ex) {
  472. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  473. }
  474. }
  475. public function editCard($id){
  476. try {
  477. $memberCard = \App\Models\MemberCard::findOrFail($id);
  478. if( !$memberCard) {
  479. session()->flash('error','Tesserato non trovato');
  480. } else {
  481. $this->card_card_id = $memberCard->card_id;
  482. $this->card_number = $memberCard->number;
  483. $this->card_date = $memberCard->date;
  484. $this->card_accept_date = $memberCard->accept_date;
  485. $this->card_status = $memberCard->status;
  486. $this->cardDataId = $memberCard->id;
  487. $this->updateCard = true;
  488. $this->addCard = false;
  489. }
  490. } catch (\Exception $ex) {
  491. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  492. }
  493. }
  494. public function updateCard()
  495. {
  496. //$this->validate();
  497. $this->validate(['card_card_id' => 'required']);
  498. try {
  499. // in base alla card selezionata calcolo la scadenza
  500. $expire_date = null;
  501. if ($this->card_date != '')
  502. {
  503. $card = \App\Models\Card::findOrFail($this->card_card_id);
  504. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  505. {
  506. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  507. $next_exp = date("Y-" . $m . "-" . $card->next_day_expire);
  508. if ($next_exp > $this->card_date)
  509. {
  510. $expire_date = $next_exp;
  511. }
  512. else
  513. $expire_date = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  514. }
  515. }
  516. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  517. 'member_id' => $this->dataId,
  518. 'card_id' => $this->card_card_id,
  519. 'number' => $this->card_number,
  520. 'date' => $this->card_date,
  521. 'accept_date' => $this->card_accept_date,
  522. 'expire_date' => $expire_date,
  523. 'status' => $this->card_status
  524. ]);
  525. session()->flash('success','Tesserato aggiornato');
  526. $this->resetCardFields();
  527. $this->updateCard = false;
  528. } catch (\Exception $ex) {
  529. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  530. }
  531. }
  532. public function cancelCard()
  533. {
  534. $this->addCard = false;
  535. $this->updateCard = false;
  536. $this->resetCardFields();
  537. }
  538. public function deleteCard($id)
  539. {
  540. try{
  541. \App\Models\MemberCard::find($id)->delete();
  542. session()->flash('success',"Tesserato eliminato");
  543. }catch(\Exception $e){
  544. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  545. }
  546. }
  547. // Certificates
  548. public function addCertificate()
  549. {
  550. $this->resetCertificateFields();
  551. $this->addCertificate = true;
  552. $this->updateCertificate = false;
  553. }
  554. public function storeCertificate()
  555. {
  556. $this->validate(['certificate_expire_date' => 'required']);
  557. // $this->validate();
  558. try {
  559. $name = '';
  560. try{
  561. if ($this->certificate_filename)
  562. {
  563. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  564. $this->certificate_filename->storeAs('public', $name);
  565. }
  566. } catch (\Exception $ex) {
  567. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  568. }
  569. if ($this->dataId > -1)
  570. {
  571. \App\Models\MemberCertificate::create([
  572. 'member_id' => $this->dataId,
  573. 'type' => $this->certificate_type,
  574. 'filename' => $name,
  575. 'expire_date' => $this->certificate_expire_date,
  576. 'status' => $this->certificate_status
  577. ]);
  578. }
  579. /*else
  580. {
  581. $this->certificateTmp = new \App\Models\MemberCertificate();
  582. $this->certificateTmp->type = $this->certificate_type;
  583. $this->certificateTmp->filename = $name;
  584. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  585. $this->certificateTmp->status = $this->certificate_status;
  586. $this->certificateTmp->status = $this->certificate_status;
  587. // s $this->member_certificates[] = $certificateTmp;
  588. }*/
  589. session()->flash('success, Tesserato creato');
  590. $this->resetCertificateFields();
  591. $this->addCertificate = false;
  592. } catch (\Exception $ex) {
  593. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  594. }
  595. }
  596. public function editCertificate($id){
  597. try {
  598. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  599. if( !$memberCertificate) {
  600. session()->flash('error','Tesserato non trovato');
  601. } else {
  602. $this->certificate_type = $memberCertificate->type;
  603. $this->certificate_filename_old = $memberCertificate->filename;
  604. $this->certificate_expire_date = $memberCertificate->expire_date;
  605. $this->certificate_status = $memberCertificate->status;
  606. $this->cardCertificateId = $memberCertificate->id;
  607. $this->updateCertificate = true;
  608. $this->addCertificate = false;
  609. }
  610. } catch (\Exception $ex) {
  611. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  612. }
  613. }
  614. public function updateCertificate()
  615. {
  616. $this->validate(['certificate_expire_date' => 'required']);
  617. try {
  618. $name = '';
  619. try{
  620. if ($this->certificate_filename)
  621. {
  622. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  623. $this->certificate_filename->storeAs('public', $name);
  624. }
  625. } catch (\Exception $ex) {
  626. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  627. }
  628. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  629. 'member_id' => $this->dataId,
  630. 'type' => $this->certificate_type,
  631. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  632. 'expire_date' => $this->certificate_expire_date,
  633. 'status' => $this->certificate_status
  634. ]);
  635. session()->flash('success','Tesserato aggiornato');
  636. $this->resetCertificateFields();
  637. $this->updateCertificate = false;
  638. } catch (\Exception $ex) {
  639. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  640. }
  641. }
  642. public function cancelCertificate()
  643. {
  644. $this->addCertificate = false;
  645. $this->updateCertificate = false;
  646. $this->resetCertificateFields();
  647. }
  648. public function deleteCertificate($id)
  649. {
  650. try{
  651. \App\Models\MemberCertificate::find($id)->delete();
  652. session()->flash('success',"Tesserato eliminato");
  653. }catch(\Exception $e){
  654. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  655. }
  656. }
  657. // Gruppi di appartenenza
  658. public function storeCategory()
  659. {
  660. $this->validate(['category_category_id' => 'required']);
  661. try {
  662. \App\Models\MemberCategory::create([
  663. 'member_id' => $this->dataId,
  664. 'category_id' => $this->category_category_id,
  665. 'date' => \Carbon\Carbon::now()
  666. ]);
  667. session()->flash('success, Associazione creato');
  668. $this->resetCategoryFields();
  669. $this->addCard = false;
  670. } catch (\Exception $ex) {
  671. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  672. }
  673. }
  674. public function deleteCategory($id)
  675. {
  676. try{
  677. \App\Models\MemberCategory::find($id)->delete();
  678. session()->flash('success',"Associazione eliminata");
  679. }catch(\Exception $e){
  680. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  681. }
  682. }
  683. }