Supplier.php 8.0 KB

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