PaymentMethod.php 4.4 KB

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