Supplier.php 8.0 KB

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