Member.php 16 KB

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