Vat.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Illuminate\Support\Facades\Auth;
  5. class Vat extends Component
  6. {
  7. public $records, $name, $value, $enabled, $dataId, $update = false, $add = false;
  8. protected $rules = [
  9. 'name' => 'required',
  10. 'value' => 'required'
  11. ];
  12. protected $messages = [
  13. 'name.required' => 'Il nome è obbligatorio',
  14. 'value.required' => 'Il valore è obbligatorio',
  15. ];
  16. public $sortField ='name';
  17. public $sortAsc = true;
  18. public function mount(){
  19. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  20. return redirect()->to('/dashboard');
  21. }
  22. public function sortBy($field)
  23. {
  24. if($this->sortField === $field)
  25. {
  26. $this->sortAsc = ! $this->sortAsc;
  27. } else {
  28. $this->sortAsc = true;
  29. }
  30. $this->sortField = $field;
  31. }
  32. public function resetFields(){
  33. $this->name = '';
  34. $this->enabled = true;
  35. $this->emit('load-data-table');
  36. }
  37. public function render()
  38. {
  39. $this->records = \App\Models\Vat::select('id', 'name', 'value', 'enabled')->get();
  40. return view('livewire.vat');
  41. }
  42. public function add()
  43. {
  44. $this->resetFields();
  45. $this->add = true;
  46. $this->update = false;
  47. }
  48. public function store()
  49. {
  50. $this->validate();
  51. try {
  52. \App\Models\Vat::create([
  53. 'name' => $this->name,
  54. 'value' => $this->value,
  55. 'enabled' => $this->enabled
  56. ]);
  57. session()->flash('success','IVA creata');
  58. $this->resetFields();
  59. $this->add = false;
  60. } catch (\Exception $ex) {
  61. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  62. }
  63. }
  64. public function edit($id){
  65. try {
  66. $vat = \App\Models\Vat::findOrFail($id);
  67. if( !$vat) {
  68. session()->flash('error','IVA non trovata');
  69. } else {
  70. $this->name = $vat->name;
  71. $this->value = $vat->value;
  72. $this->enabled = $vat->enabled;
  73. $this->dataId = $vat->id;
  74. $this->update = true;
  75. $this->add = false;
  76. }
  77. } catch (\Exception $ex) {
  78. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  79. }
  80. }
  81. public function update()
  82. {
  83. $this->validate();
  84. try {
  85. \App\Models\Vat::whereId($this->dataId)->update([
  86. 'name' => $this->name,
  87. 'value' => $this->value,
  88. 'enabled' => $this->enabled
  89. ]);
  90. session()->flash('success','IVA aggiornata');
  91. $this->resetFields();
  92. $this->update = false;
  93. } catch (\Exception $ex) {
  94. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  95. }
  96. }
  97. public function cancel()
  98. {
  99. $this->add = false;
  100. $this->update = false;
  101. $this->resetFields();
  102. }
  103. public function delete($id)
  104. {
  105. try{
  106. \App\Models\Vat::find($id)->delete();
  107. session()->flash('success',"IVA eliminata");
  108. return redirect(request()->header('Referer'));
  109. }catch(\Exception $e){
  110. session()->flash('error','Errore (' . $e->getMessage() . ')');
  111. }
  112. }
  113. }