Supplier.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Supplier extends Component
  5. {
  6. public $records, $name, $fiscal_code, $vat, $address, $zip_code,$nation_id,$province_id,$city_id,$referent,$website,$phone,$email,$enabled, $dataId, $update = false, $add = false;
  7. public $nations = array();
  8. public $provinces = array();
  9. public $cities = array();
  10. protected $rules = [
  11. 'name' => 'required'
  12. ];
  13. protected $messages = [
  14. 'name.required' => 'Il nome è obbligatorio'
  15. ];
  16. public function resetFields(){
  17. $this->name = '';
  18. $this->fiscal_code = '';
  19. $this->vat = '';
  20. $this->address = '';
  21. $this->zip_code = '';
  22. $this->nation_id = null;
  23. $this->province_id = null;
  24. $this->city_id = null;
  25. $this->referent = '';
  26. $this->website = '';
  27. $this->phone = '';
  28. $this->email = '';
  29. $this->enabled = true;
  30. }
  31. public function mount()
  32. {
  33. $this->nations = \App\Models\Nation::select('id', 'name')->get();
  34. $this->provinces = array();
  35. $this->cities = array();
  36. }
  37. public function loadProvinces()
  38. {
  39. $this->provinces = \App\Models\Province::select('id', 'name')->where('nation_id', $this->nation_id)->get();
  40. $this->cities = array();\App\Models\City::select('id', 'name')->get();
  41. }
  42. public function loadCities()
  43. {
  44. $this->cities = \App\Models\City::select('id', 'name')->where('province_id', $this->province_id)->get();
  45. }
  46. public function render()
  47. {
  48. $this->records = \App\Models\Supplier::with('nation')->get();
  49. return view('livewire.supplier');
  50. }
  51. public function add()
  52. {
  53. $this->resetFields();
  54. $this->add = true;
  55. $this->update = false;
  56. }
  57. public function store()
  58. {
  59. $this->validate();
  60. try {
  61. \App\Models\Supplier::create([
  62. 'name' => $this->name,
  63. 'fiscal_code' => $this->fiscal_code,
  64. 'vat' => $this->vat,
  65. 'address' => $this->address,
  66. 'zip_code' => $this->zip_code,
  67. 'nation_id' => $this->nation_id,
  68. 'province_id' => $this->province_id,
  69. 'city_id' => $this->city_id,
  70. 'referent' => $this->referent,
  71. 'website' => $this->website,
  72. 'phone' => $this->phone,
  73. 'email' => $this->email,
  74. 'enabled' => $this->enabled
  75. ]);
  76. session()->flash('success','Fornitore creato');
  77. $this->resetFields();
  78. $this->add = false;
  79. } catch (\Exception $ex) {
  80. session()->flash('error','Errore in fase di salvataggio');
  81. }
  82. }
  83. public function edit($id){
  84. try {
  85. $supplier = \App\Models\Supplier::findOrFail($id);
  86. if( !$supplier) {
  87. session()->flash('error','Fornitore non trovato');
  88. } else {
  89. $this->name = $supplier->name;
  90. $this->fiscal_code = $supplier->fiscal_code;
  91. $this->vat = $supplier->vat;
  92. $this->address = $supplier->address;
  93. $this->zip_code = $supplier->zip_code;
  94. $this->nation_id = $supplier->nation_id;
  95. $this->province_id = $supplier->province_id;
  96. $this->city_id = $supplier->city_id;
  97. $this->referent = $supplier->referent;
  98. $this->website = $supplier->website;
  99. $this->phone = $supplier->phone;
  100. $this->email = $supplier->email;
  101. $this->dataId = $supplier->id;
  102. $this->update = true;
  103. $this->add = false;
  104. }
  105. } catch (\Exception $ex) {
  106. session()->flash('error','Errore');
  107. }
  108. }
  109. public function update()
  110. {
  111. $this->validate();
  112. try {
  113. \App\Models\Supplier::whereId($this->dataId)->update([
  114. 'name' => $this->name,
  115. 'fiscal_code' => $this->fiscal_code,
  116. 'vat' => $this->vat,
  117. 'address' => $this->address,
  118. 'zip_code' => $this->zip_code,
  119. 'nation_id' => $this->nation_id,
  120. 'province_id' => $this->province_id,
  121. 'city_id' => $this->city_id,
  122. 'referent' => $this->referent,
  123. 'website' => $this->website,
  124. 'phone' => $this->phone,
  125. 'email' => $this->email,
  126. 'enabled' => $this->enabled
  127. ]);
  128. session()->flash('success','Fornitore aggiornato');
  129. $this->resetFields();
  130. $this->update = false;
  131. } catch (\Exception $ex) {
  132. session()->flash('success','Errore');
  133. }
  134. }
  135. public function cancel()
  136. {
  137. $this->add = false;
  138. $this->update = false;
  139. $this->resetFields();
  140. }
  141. public function delete($id)
  142. {
  143. try{
  144. \App\Models\Supplier::find($id)->delete();
  145. session()->flash('success',"Fornitore eliminato");
  146. }catch(\Exception $e){
  147. session()->flash('error',"Errore");
  148. }
  149. }
  150. }