Member.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\Attributes\Url;
  5. class Member extends Component
  6. {
  7. public $records, $first_name, $last_name, $status, $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;
  8. public $cards = array();
  9. public $categories = array();
  10. public $nations = array();
  11. public $provinces = array();
  12. public $cities = array();
  13. public $birthNations = array();
  14. public $birthProvinces = array();
  15. public $birthCities = array();
  16. // Card data
  17. public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_status, $addCard, $updateCard, $cardDataId;
  18. // Categories data
  19. public $member_categories = array(), $category_category_id;
  20. // Certificates data
  21. public $member_certificates = array(), $certificate_type, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
  22. protected $rules = [
  23. 'first_name' => 'required',
  24. 'last_name' => 'required',
  25. ];
  26. public function resetFields(){
  27. $this->dataId = -1;
  28. $this->first_name = '';
  29. $this->last_name = '';
  30. $this->status = '';
  31. $this->birth_city_id = null;
  32. $this->birth_province_id = null;
  33. $this->birth_nation_id = null;
  34. $this->birth_date = null;
  35. $this->gender = '';
  36. $this->fiscal_code = '';
  37. $this->address = '';
  38. $this->zip_code = '';
  39. $this->nation_id = null;
  40. $this->province_id = null;
  41. $this->city_id = null;
  42. $this->phone = '';
  43. $this->email = '';
  44. $this->enabled = true;
  45. }
  46. public function resetCardFields(){
  47. $this->card_card_id = null;
  48. $this->card_number = '';
  49. $this->card_date = null;
  50. $this->card_accept_date = null;
  51. $this->card_status = 0;
  52. }
  53. public function resetCertificateFields(){
  54. $this->certificate_type = '';
  55. $this->certificate_filename = '';
  56. $this->certificate_expire_date = null;
  57. $this->certificate_status = 0;
  58. }
  59. public function resetCategoryFields(){
  60. $this->category_category_id = null;
  61. }
  62. public function getCategories($records, $indentation)
  63. {
  64. foreach($records as $record)
  65. {
  66. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  67. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  68. if(count($record->childs))
  69. $this->getCategories($record->childs, $indentation + 1);
  70. }
  71. }
  72. public function mount()
  73. {
  74. if (isset($_GET["new"]))
  75. $this->add();
  76. $this->cards = \App\Models\Card::select('id', 'name')->get();
  77. $this->categories = array();
  78. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  79. $this->nations = \App\Models\Nation::select('id', 'name')->get();
  80. $this->provinces = array();
  81. $this->cities = array();
  82. $this->birthNations = \App\Models\Nation::select('id', 'name')->get();
  83. $this->birthProvinces = array();
  84. $this->birthCities = array();
  85. }
  86. public function loadProvinces()
  87. {
  88. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  89. $this->cities = array();
  90. }
  91. public function loadCities()
  92. {
  93. $this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  94. }
  95. public function loadBirthProvinces()
  96. {
  97. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  98. $this->birthCities = array();
  99. }
  100. public function loadBirthCities()
  101. {
  102. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  103. }
  104. public function render()
  105. {
  106. $this->records = \App\Models\Member::get();
  107. $this->loadMemberCards();
  108. $this->loadMemberCategories();
  109. $this->loadMemberCertificates();
  110. return view('livewire.member');
  111. }
  112. public function loadMemberCards()
  113. {
  114. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  115. // return view('livewire.member');
  116. }
  117. public function loadMemberCategories()
  118. {
  119. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  120. // return view('livewire.member');
  121. }
  122. public function loadMemberCertificates()
  123. {
  124. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->get();
  125. // return view('livewire.member');
  126. }
  127. public function add()
  128. {
  129. $this->resetFields();
  130. $this->add = true;
  131. $this->update = false;
  132. }
  133. public function store($close)
  134. {
  135. $this->validate();
  136. try {
  137. $member = \App\Models\Member::create([
  138. 'first_name' => $this->first_name,
  139. 'last_name' => $this->last_name,
  140. 'status' => $this->status,
  141. 'birth_city_id' => $this->birth_city_id,
  142. 'birth_province_id' => $this->birth_province_id,
  143. 'birth_nation_id' => $this->birth_nation_id,
  144. 'birth_date' => $this->birth_date,
  145. 'gender' => $this->gender,
  146. 'fiscal_code' => $this->fiscal_code,
  147. 'address' => $this->address,
  148. 'zip_code' => $this->zip_code,
  149. 'nation_id' => $this->nation_id,
  150. 'province_id' => $this->province_id,
  151. 'city_id' => $this->city_id,
  152. 'phone' => $this->phone,
  153. 'email' => $this->email,
  154. 'enabled' => $this->enabled
  155. ]);
  156. session()->flash('success, Tesserato creato');
  157. if ($close)
  158. {
  159. $this->resetFields();
  160. $this->add = false;
  161. }
  162. else
  163. {
  164. $this->edit($member->id);
  165. }
  166. } catch (\Exception $ex) {
  167. session()->flash('error','Errore in fase di salvataggio');
  168. }
  169. }
  170. public function edit($id){
  171. try {
  172. $member = \App\Models\Member::findOrFail($id);
  173. if( !$member) {
  174. session()->flash('error','Tesserato non trovato');
  175. } else {
  176. $this->first_name = $member->first_name;
  177. $this->last_name = $member->last_name;
  178. $this->status = $member->status;
  179. $this->birth_city_id = $member->birth_city_id;
  180. $this->birth_province_id = $member->birth_province_id;
  181. $this->birth_nation_id = $member->birth_nation_id;
  182. $this->birth_date = $member->birth_date;
  183. $this->gender = $member->gender;
  184. $this->fiscal_code = $member->fiscal_code;
  185. $this->address = $member->address;
  186. $this->zip_code = $member->zip_code;
  187. $this->nation_id = $member->nation_id;
  188. $this->province_id = $member->province_id;
  189. $this->city_id = $member->city_id;
  190. $this->phone = $member->phone;
  191. $this->email = $member->email;
  192. $this->enabled = $member->enabled;
  193. $this->dataId = $member->id;
  194. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  195. $this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  196. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  197. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  198. $this->update = true;
  199. $this->add = false;
  200. }
  201. } catch (\Exception $ex) {
  202. session()->flash('error','Errore');
  203. }
  204. }
  205. public function update()
  206. {
  207. $this->validate();
  208. try {
  209. \App\Models\Member::whereId($this->dataId)->update([
  210. 'first_name' => $this->first_name,
  211. 'last_name' => $this->last_name,
  212. 'status' => $this->status,
  213. 'birth_city_id' => $this->birth_city_id,
  214. 'birth_province_id' => $this->birth_province_id,
  215. 'birth_nation_id' => $this->birth_nation_id,
  216. 'birth_date' => $this->birth_date,
  217. 'gender' => $this->gender,
  218. 'fiscal_code' => $this->fiscal_code,
  219. 'address' => $this->address,
  220. 'zip_code' => $this->zip_code,
  221. 'nation_id' => $this->nation_id,
  222. 'province_id' => $this->province_id,
  223. 'city_id' => $this->city_id,
  224. 'phone' => $this->phone,
  225. 'email' => $this->email,
  226. 'enabled' => $this->enabled
  227. ]);
  228. session()->flash('success','Tesserato aggiornato');
  229. $this->resetFields();
  230. $this->update = false;
  231. } catch (\Exception $ex) {
  232. session()->flash('success','Errore');
  233. }
  234. }
  235. public function cancel()
  236. {
  237. $this->add = false;
  238. $this->update = false;
  239. $this->resetFields();
  240. }
  241. public function delete($id)
  242. {
  243. try{
  244. \App\Models\Member::find($id)->delete();
  245. session()->flash('success',"Tesserato eliminato");
  246. }catch(\Exception $e){
  247. session()->flash('error',"Errore");
  248. }
  249. }
  250. // Card
  251. public function addCard()
  252. {
  253. $this->resetCardFields();
  254. $this->addCard = true;
  255. $this->updateCard = false;
  256. }
  257. public function storeCard()
  258. {
  259. $this->validate(['card_card_id' => 'required']);
  260. try {
  261. // in base alla card selezionata calcolo la scadenza
  262. $expire_date = null;
  263. if ($this->card_date != '')
  264. {
  265. $card = \App\Models\Card::findOrFail($this->card_card_id);
  266. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  267. {
  268. $next_exp = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire);
  269. if ($next_exp > $this->card_date)
  270. $expire_date = $next_exp;
  271. else
  272. $expire_date = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  273. }
  274. }
  275. \App\Models\MemberCard::create([
  276. 'member_id' => $this->dataId,
  277. 'card_id' => $this->card_card_id,
  278. 'number' => $this->card_number,
  279. 'date' => $this->card_date,
  280. 'accept_date' => $this->card_accept_date,
  281. 'expire_date' => $expire_date,
  282. 'status' => $this->card_status
  283. ]);
  284. session()->flash('success, Tesserato creato');
  285. $this->resetCardFields();
  286. $this->addCard = false;
  287. } catch (\Exception $ex) {
  288. session()->flash('error','Errore in fase di salvataggio');
  289. }
  290. }
  291. public function editCard($id){
  292. try {
  293. $memberCard = \App\Models\MemberCard::findOrFail($id);
  294. if( !$memberCard) {
  295. session()->flash('error','Tesserato non trovato');
  296. } else {
  297. $this->card_card_id = $memberCard->card_id;
  298. $this->card_number = $memberCard->number;
  299. $this->card_date = $memberCard->date;
  300. $this->card_accept_date = $memberCard->accept_date;
  301. $this->card_status = $memberCard->status;
  302. $this->cardDataId = $memberCard->id;
  303. $this->updateCard = true;
  304. $this->addCard = false;
  305. }
  306. } catch (\Exception $ex) {
  307. session()->flash('error','Errore');
  308. }
  309. }
  310. public function updateCard()
  311. {
  312. //$this->validate();
  313. $this->validate(['card_card_id' => 'required']);
  314. try {
  315. // in base alla card selezionata calcolo la scadenza
  316. $expire_date = null;
  317. if ($this->card_date != '')
  318. {
  319. $card = \App\Models\Card::findOrFail($this->card_card_id);
  320. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  321. {
  322. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  323. $next_exp = date("Y-" . $m . "-" . $card->next_day_expire);
  324. if ($next_exp > $this->card_date)
  325. {
  326. $expire_date = $next_exp;
  327. }
  328. else
  329. $expire_date = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  330. }
  331. }
  332. \App\Models\MemberCard::whereId($this->dataId)->update([
  333. 'member_id' => $this->dataId,
  334. 'card_id' => $this->card_card_id,
  335. 'number' => $this->card_number,
  336. 'date' => $this->card_date,
  337. 'accept_date' => $this->card_accept_date,
  338. 'expire_date' => $expire_date,
  339. 'status' => $this->card_status
  340. ]);
  341. session()->flash('success','Tesserato aggiornato');
  342. $this->resetCardFields();
  343. $this->updateCard = false;
  344. } catch (\Exception $ex) {
  345. session()->flash('success','Errore');
  346. }
  347. }
  348. public function cancelCard()
  349. {
  350. $this->addCard = false;
  351. $this->updateCard = false;
  352. $this->resetCardFields();
  353. }
  354. public function deleteCard($id)
  355. {
  356. try{
  357. \App\Models\MemberCard::find($id)->delete();
  358. session()->flash('success',"Tesserato eliminato");
  359. }catch(\Exception $e){
  360. session()->flash('error',"Errore");
  361. }
  362. }
  363. // Certificates
  364. public function addCertificate()
  365. {
  366. $this->resetCertificateFields();
  367. $this->addCertificate = true;
  368. $this->updateCertificate = false;
  369. }
  370. public function storeCertificate()
  371. {
  372. $this->validate(['certificate_expire_date' => 'required']);
  373. // $this->validate();
  374. try {
  375. \App\Models\MemberCertificate::create([
  376. 'member_id' => $this->dataId,
  377. 'type' => $this->certificate_type,
  378. 'filename' => $this->certificate_filename,
  379. 'expire_date' => $this->certificate_expire_date,
  380. 'status' => $this->certificate_status
  381. ]);
  382. session()->flash('success, Tesserato creato');
  383. $this->resetCertificateFields();
  384. $this->addCertificate = false;
  385. } catch (\Exception $ex) {
  386. session()->flash('error','Errore in fase di salvataggio');
  387. }
  388. }
  389. public function editCertificate($id){
  390. try {
  391. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  392. if( !$memberCertificate) {
  393. session()->flash('error','Tesserato non trovato');
  394. } else {
  395. $this->certificate_type = $memberCertificate->type;
  396. $this->certificate_filename = $memberCertificate->filename;
  397. $this->certificate_expire_date = $memberCertificate->expire_date;
  398. $this->certificate_status = $memberCertificate->status;
  399. $this->cardCertificateId = $memberCertificate->id;
  400. $this->updateCertificate = true;
  401. $this->addCertificate = false;
  402. }
  403. } catch (\Exception $ex) {
  404. session()->flash('error','Errore');
  405. }
  406. }
  407. public function updateCertificate()
  408. {
  409. $this->validate(['certificate_expire_date' => 'required']);
  410. try {
  411. \App\Models\MemberCertificate::whereId($this->dataId)->update([
  412. 'member_id' => $this->dataId,
  413. 'type' => $this->certificate_type,
  414. 'filename' => $this->certificate_filename,
  415. 'expire_date' => $this->certificate_expire_date,
  416. 'status' => $this->certificate_status
  417. ]);
  418. session()->flash('success','Tesserato aggiornato');
  419. $this->resetCertificateFields();
  420. $this->updateCertificate = false;
  421. } catch (\Exception $ex) {
  422. session()->flash('success','Errore');
  423. }
  424. }
  425. public function cancelCertificate()
  426. {
  427. $this->addCertificate = false;
  428. $this->updateCertificate = false;
  429. $this->resetCertificateFields();
  430. }
  431. public function deleteCertificate($id)
  432. {
  433. try{
  434. \App\Models\MemberCertificate::find($id)->delete();
  435. session()->flash('success',"Tesserato eliminato");
  436. }catch(\Exception $e){
  437. session()->flash('error',"Errore");
  438. }
  439. }
  440. // Gruppi di appartenenza
  441. public function storeCategory()
  442. {
  443. $this->validate(['category_category_id' => 'required']);
  444. try {
  445. \App\Models\MemberCategory::create([
  446. 'member_id' => $this->dataId,
  447. 'category_id' => $this->category_category_id,
  448. 'date' => \Carbon\Carbon::now()
  449. ]);
  450. session()->flash('success, Associazione creato');
  451. $this->resetCategoryFields();
  452. $this->addCard = false;
  453. } catch (\Exception $ex) {
  454. session()->flash('error','Errore in fase di salvataggio');
  455. }
  456. }
  457. public function deleteCategory($id)
  458. {
  459. try{
  460. \App\Models\MemberCategory::find($id)->delete();
  461. session()->flash('success',"Associazione eliminata");
  462. }catch(\Exception $e){
  463. session()->flash('error',"Errore");
  464. }
  465. }
  466. }