PaymentMethod.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Illuminate\Support\Facades\Auth;
  5. class PaymentMethod extends Component
  6. {
  7. public $records, $name, $enabled, $money, $type, $corrispettivo_fiscale, $dataId, $bank_id, $update = false, $add = false;
  8. public $banks = array();
  9. protected $rules = [
  10. 'name' => 'required'
  11. ];
  12. protected $messages = [
  13. 'name.required' => 'Il nome è obbligatorio'
  14. ];
  15. public $sortField ='name';
  16. public $sortAsc = true;
  17. public function sortBy($field)
  18. {
  19. if($this->sortField === $field)
  20. {
  21. $this->sortAsc = ! $this->sortAsc;
  22. } else {
  23. $this->sortAsc = true;
  24. }
  25. $this->sortField = $field;
  26. }
  27. public function resetFields(){
  28. $this->name = '';
  29. $this->money = false;
  30. $this->type = '';
  31. $this->corrispettivo_fiscale = false;
  32. $this->enabled = true;
  33. $this->emit('load-data-table');
  34. }
  35. public function mount(){
  36. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  37. return redirect()->to('/dashboard');
  38. $this->banks = \App\Models\Bank::select('id', 'name')->get();
  39. }
  40. public function render()
  41. {
  42. $this->records = \App\Models\PaymentMethod::all();
  43. foreach($this->records as $r)
  44. {
  45. $r->bank = $r->bank ? $r->bank->name : '';
  46. }
  47. /*if ($this->sortAsc)
  48. $this->records = $this->records->sortBy($this->sortField);
  49. else
  50. $this->records = $this->records->sortByDesc($this->sortField);*/
  51. return view('livewire.payment_method');
  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\PaymentMethod::create([
  64. 'name' => $this->name,
  65. 'bank_id' => $this->bank_id,
  66. 'money' => $this->money,
  67. 'type' => $this->type,
  68. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  69. 'enabled' => $this->enabled
  70. ]);
  71. session()->flash('success','Metodo pagamento creato');
  72. $this->resetFields();
  73. $this->add = false;
  74. } catch (\Exception $ex) {
  75. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  76. }
  77. }
  78. public function edit($id){
  79. try {
  80. $payment_method = \App\Models\PaymentMethod::findOrFail($id);
  81. if( !$payment_method) {
  82. session()->flash('error','Metodo pagamento non trovato');
  83. } else {
  84. $this->name = $payment_method->name;
  85. $this->enabled = $payment_method->enabled;
  86. $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
  87. $this->money = $payment_method->money;
  88. $this->type = $payment_method->type;
  89. $this->bank_id = $payment_method->bank_id;
  90. $this->dataId = $payment_method->id;
  91. $this->update = true;
  92. $this->add = false;
  93. }
  94. } catch (\Exception $ex) {
  95. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  96. }
  97. }
  98. public function update()
  99. {
  100. $this->validate();
  101. try {
  102. \App\Models\PaymentMethod::whereId($this->dataId)->update([
  103. 'name' => $this->name,
  104. 'bank_id' => $this->bank_id,
  105. 'money' => $this->money,
  106. 'type' => $this->type,
  107. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  108. 'enabled' => $this->enabled
  109. ]);
  110. session()->flash('success','Metodo pagamento aggiornato');
  111. $this->resetFields();
  112. $this->update = false;
  113. } catch (\Exception $ex) {
  114. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  115. }
  116. }
  117. public function cancel()
  118. {
  119. $this->add = false;
  120. $this->update = false;
  121. $this->resetFields();
  122. }
  123. public function delete($id)
  124. {
  125. try{
  126. \App\Models\PaymentMethod::find($id)->delete();
  127. session()->flash('success',"Metodo pagamento eliminato");
  128. return redirect(request()->header('Referer'));
  129. }catch(\Exception $e){
  130. session()->flash('error','Errore (' . $e->getMessage() . ')');
  131. }
  132. }
  133. }