Supplier.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 $referent_first_name, $referent_last_name, $referent_email, $referent_phone, $referent_mobile;
  8. public $isItaly = true;
  9. public $searchTxt;
  10. public $search;
  11. protected $rules = [
  12. 'name' => 'required'
  13. ];
  14. protected $messages = [
  15. 'name.required' => 'Il nome è obbligatorio'
  16. ];
  17. public function resetFields(){
  18. $this->name = '';
  19. $this->fiscal_code = '';
  20. $this->vat = '';
  21. $this->address = '';
  22. $this->zip_code = '';
  23. $this->nation_id = null;
  24. $this->province_id = null;
  25. $this->city_id = null;
  26. $this->referent = '';
  27. $this->website = '';
  28. $this->phone = '';
  29. $this->email = '';
  30. $this->enabled = true;
  31. $this->referent_first_name = '';
  32. $this->referent_last_name = '';
  33. $this->referent_email = '';
  34. $this->referent_phone = '';
  35. $this->referent_mobile = '';
  36. $this->emit('load-data-table');
  37. }
  38. public $sortField ='name';
  39. public $sortAsc = true;
  40. public function sortBy($field)
  41. {
  42. if($this->sortField === $field)
  43. {
  44. $this->sortAsc = ! $this->sortAsc;
  45. } else {
  46. $this->sortAsc = true;
  47. }
  48. $this->sortField = $field;
  49. }
  50. public function mount()
  51. {
  52. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  53. return redirect()->to('/dashboard');
  54. if (isset($_GET["new"]))
  55. $this->add();
  56. }
  57. public function search()
  58. {
  59. if ($this->searchTxt != '')
  60. {
  61. $this->search = $this->searchTxt;
  62. $this->showReset = true;
  63. }
  64. }
  65. public function resetSearch()
  66. {
  67. $this->showReset = false;
  68. $this->searchTxt = '';
  69. $this->search = $this->searchTxt;
  70. }
  71. public function render()
  72. {
  73. /*if ($this->search != '')
  74. $this->records = \App\Models\Supplier::with('nation')->where('name', 'LIKE', '%' . $this->search . '%')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
  75. else*/
  76. $this->records = \App\Models\Supplier::with('nation')->get();
  77. return view('livewire.supplier');
  78. }
  79. public function hydrate()
  80. {
  81. $this->emit('load-select');
  82. }
  83. public function checkIsItaly()
  84. {
  85. $n = \App\Models\Nation::findOrFail($this->nation_id);
  86. $this->isItaly = $n->is_italy;
  87. }
  88. public function add()
  89. {
  90. $this->resetFields();
  91. $this->emit('load-select');
  92. $this->add = true;
  93. $this->update = false;
  94. }
  95. public function store()
  96. {
  97. $this->validate();
  98. try {
  99. \App\Models\Supplier::create([
  100. 'name' => $this->name,
  101. 'fiscal_code' => $this->fiscal_code,
  102. 'vat' => $this->vat,
  103. 'address' => $this->address,
  104. 'zip_code' => $this->zip_code,
  105. 'nation_id' => $this->nation_id,
  106. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  107. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  108. 'referent' => $this->referent,
  109. 'website' => $this->website,
  110. 'phone' => $this->phone,
  111. 'email' => $this->email,
  112. 'referent_first_name' => $this->referent_first_name,
  113. 'referent_last_name' => $this->referent_last_name,
  114. 'referent_email' => $this->referent_email,
  115. 'referent_phone' => $this->referent_phone,
  116. 'referent_mobile' => $this->referent_mobile,
  117. 'enabled' => $this->enabled
  118. ]);
  119. session()->flash('success','Fornitore creato');
  120. $this->resetFields();
  121. $this->add = false;
  122. } catch (\Exception $ex) {
  123. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  124. }
  125. }
  126. public function edit($id){
  127. $this->emit('load-select');
  128. try {
  129. $supplier = \App\Models\Supplier::findOrFail($id);
  130. if( !$supplier) {
  131. session()->flash('error','Fornitore non trovato');
  132. } else {
  133. $this->name = $supplier->name;
  134. $this->fiscal_code = $supplier->fiscal_code;
  135. $this->vat = $supplier->vat;
  136. $this->address = $supplier->address;
  137. $this->zip_code = $supplier->zip_code;
  138. $this->nation_id = $supplier->nation_id;
  139. $this->province_id = $supplier->province_id;
  140. $this->city_id = $supplier->city_id;
  141. $this->referent = $supplier->referent;
  142. $this->website = $supplier->website;
  143. $this->phone = $supplier->phone;
  144. $this->email = $supplier->email;
  145. $this->referent_first_name = $supplier->referent_first_name;
  146. $this->referent_last_name = $supplier->referent_last_name;
  147. $this->referent_email = $supplier->referent_email;
  148. $this->referent_phone = $supplier->referent_phone;
  149. $this->referent_mobile = $supplier->referent_mobile;
  150. $this->enabled = $supplier->enabled;
  151. $this->dataId = $supplier->id;
  152. $this->update = true;
  153. $this->add = false;
  154. $this->checkIsItaly();
  155. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  156. $this->emit('load-cities', $this->province_id, 'cityClass');
  157. }
  158. } catch (\Exception $ex) {
  159. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  160. }
  161. }
  162. public function update()
  163. {
  164. $this->validate();
  165. try {
  166. \App\Models\Supplier::whereId($this->dataId)->update([
  167. 'name' => $this->name,
  168. 'fiscal_code' => $this->fiscal_code,
  169. 'vat' => $this->vat,
  170. 'address' => $this->address,
  171. 'zip_code' => $this->zip_code,
  172. 'nation_id' => $this->nation_id,
  173. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  174. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  175. 'referent' => $this->referent,
  176. 'website' => $this->website,
  177. 'phone' => $this->phone,
  178. 'email' => $this->email,
  179. 'referent_first_name' => $this->referent_first_name,
  180. 'referent_last_name' => $this->referent_last_name,
  181. 'referent_email' => $this->referent_email,
  182. 'referent_phone' => $this->referent_phone,
  183. 'referent_mobile' => $this->referent_mobile,
  184. 'enabled' => $this->enabled
  185. ]);
  186. session()->flash('success','Fornitore aggiornato');
  187. $this->resetFields();
  188. $this->update = false;
  189. } catch (\Exception $ex) {
  190. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  191. }
  192. }
  193. public function cancel()
  194. {
  195. $this->add = false;
  196. $this->update = false;
  197. $this->resetFields();
  198. }
  199. public function delete($id)
  200. {
  201. try{
  202. \App\Models\Supplier::find($id)->delete();
  203. session()->flash('success',"Fornitore eliminato");
  204. return redirect(request()->header('Referer'));
  205. }catch(\Exception $e){
  206. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  207. }
  208. }
  209. public function getNation($nation)
  210. {
  211. if ($nation > 0)
  212. {
  213. $ret = \App\Models\Nation::findOrFail($nation);
  214. return $ret->name;
  215. }
  216. return "";
  217. }
  218. public function getProvince($province)
  219. {
  220. if ($province > 0)
  221. {
  222. $ret = \App\Models\Province::findOrFail($province);
  223. return $ret->name;
  224. }
  225. return "";
  226. }
  227. public function getCity($city)
  228. {
  229. if ($city > 0)
  230. {
  231. $ret = \App\Models\City::findOrFail($city);
  232. return $ret->name;
  233. }
  234. return "";
  235. }
  236. }