Supplier.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (isset($_GET["new"]))
  34. $this->add();
  35. $this->nations = \App\Models\Nation::select('id', 'name')->get();
  36. $this->provinces = array();
  37. $this->cities = array();
  38. }
  39. public function loadProvinces()
  40. {
  41. $this->provinces = \App\Models\Province::select('id', 'name')->where('nation_id', $this->nation_id)->get();
  42. $this->cities = array();\App\Models\City::select('id', 'name')->get();
  43. }
  44. public function loadCities()
  45. {
  46. $this->cities = \App\Models\City::select('id', 'name')->where('province_id', $this->province_id)->get();
  47. }
  48. public function render()
  49. {
  50. $this->records = \App\Models\Supplier::with('nation')->get();
  51. return view('livewire.supplier');
  52. }
  53. public function add()
  54. {
  55. $this->resetFields();
  56. $this->add = true;
  57. $this->update = false;
  58. }
  59. public function store()
  60. {
  61. $this->validate();
  62. try {
  63. \App\Models\Supplier::create([
  64. 'name' => $this->name,
  65. 'fiscal_code' => $this->fiscal_code,
  66. 'vat' => $this->vat,
  67. 'address' => $this->address,
  68. 'zip_code' => $this->zip_code,
  69. 'nation_id' => $this->nation_id,
  70. 'province_id' => $this->province_id,
  71. 'city_id' => $this->city_id,
  72. 'referent' => $this->referent,
  73. 'website' => $this->website,
  74. 'phone' => $this->phone,
  75. 'email' => $this->email,
  76. 'enabled' => $this->enabled
  77. ]);
  78. session()->flash('success','Fornitore creato');
  79. $this->resetFields();
  80. $this->add = false;
  81. } catch (\Exception $ex) {
  82. session()->flash('error','Errore in fase di salvataggio');
  83. }
  84. }
  85. public function edit($id){
  86. try {
  87. $supplier = \App\Models\Supplier::findOrFail($id);
  88. if( !$supplier) {
  89. session()->flash('error','Fornitore non trovato');
  90. } else {
  91. $this->name = $supplier->name;
  92. $this->fiscal_code = $supplier->fiscal_code;
  93. $this->vat = $supplier->vat;
  94. $this->address = $supplier->address;
  95. $this->zip_code = $supplier->zip_code;
  96. $this->nation_id = $supplier->nation_id;
  97. $this->province_id = $supplier->province_id;
  98. $this->city_id = $supplier->city_id;
  99. $this->referent = $supplier->referent;
  100. $this->website = $supplier->website;
  101. $this->phone = $supplier->phone;
  102. $this->email = $supplier->email;
  103. $this->dataId = $supplier->id;
  104. $this->update = true;
  105. $this->add = false;
  106. }
  107. } catch (\Exception $ex) {
  108. session()->flash('error','Errore');
  109. }
  110. }
  111. public function update()
  112. {
  113. $this->validate();
  114. try {
  115. \App\Models\Supplier::whereId($this->dataId)->update([
  116. 'name' => $this->name,
  117. 'fiscal_code' => $this->fiscal_code,
  118. 'vat' => $this->vat,
  119. 'address' => $this->address,
  120. 'zip_code' => $this->zip_code,
  121. 'nation_id' => $this->nation_id,
  122. 'province_id' => $this->province_id,
  123. 'city_id' => $this->city_id,
  124. 'referent' => $this->referent,
  125. 'website' => $this->website,
  126. 'phone' => $this->phone,
  127. 'email' => $this->email,
  128. 'enabled' => $this->enabled
  129. ]);
  130. session()->flash('success','Fornitore aggiornato');
  131. $this->resetFields();
  132. $this->update = false;
  133. } catch (\Exception $ex) {
  134. session()->flash('success','Errore');
  135. }
  136. }
  137. public function cancel()
  138. {
  139. $this->add = false;
  140. $this->update = false;
  141. $this->resetFields();
  142. }
  143. public function delete($id)
  144. {
  145. try{
  146. \App\Models\Supplier::find($id)->delete();
  147. session()->flash('success',"Fornitore eliminato");
  148. }catch(\Exception $e){
  149. session()->flash('error',"Errore");
  150. }
  151. }
  152. }