Member.php 15 KB

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