PaymentMethod.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class PaymentMethod extends Component
  5. {
  6. public $records, $name, $enabled, $money, $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->corrispettivo_fiscale = false;
  30. $this->enabled = true;
  31. $this->emit('load-data-table');
  32. }
  33. public function mount()
  34. {
  35. $this->banks = \App\Models\Bank::select('id', 'name')->get();
  36. }
  37. public function render()
  38. {
  39. $this->records = \App\Models\PaymentMethod::all();
  40. foreach($this->records as $r)
  41. {
  42. $r->bank = $r->bank ? $r->bank->name : '';
  43. }
  44. /*if ($this->sortAsc)
  45. $this->records = $this->records->sortBy($this->sortField);
  46. else
  47. $this->records = $this->records->sortByDesc($this->sortField);*/
  48. return view('livewire.payment_method');
  49. }
  50. public function add()
  51. {
  52. $this->resetFields();
  53. $this->add = true;
  54. $this->update = false;
  55. }
  56. public function store()
  57. {
  58. $this->validate();
  59. try {
  60. \App\Models\PaymentMethod::create([
  61. 'name' => $this->name,
  62. 'bank_id' => $this->bank_id,
  63. 'money' => $this->money,
  64. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  65. 'enabled' => $this->enabled
  66. ]);
  67. session()->flash('success','Metodo pagamento creato');
  68. $this->resetFields();
  69. $this->add = false;
  70. } catch (\Exception $ex) {
  71. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  72. }
  73. }
  74. public function edit($id){
  75. try {
  76. $payment_method = \App\Models\PaymentMethod::findOrFail($id);
  77. if( !$payment_method) {
  78. session()->flash('error','Metodo pagamento non trovato');
  79. } else {
  80. $this->name = $payment_method->name;
  81. $this->enabled = $payment_method->enabled;
  82. $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
  83. $this->money = $payment_method->money;
  84. $this->bank_id = $payment_method->bank_id;
  85. $this->dataId = $payment_method->id;
  86. $this->update = true;
  87. $this->add = false;
  88. }
  89. } catch (\Exception $ex) {
  90. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  91. }
  92. }
  93. public function update()
  94. {
  95. $this->validate();
  96. try {
  97. \App\Models\PaymentMethod::whereId($this->dataId)->update([
  98. 'name' => $this->name,
  99. 'bank_id' => $this->bank_id,
  100. 'money' => $this->money,
  101. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  102. 'enabled' => $this->enabled
  103. ]);
  104. session()->flash('success','Metodo pagamento aggiornato');
  105. $this->resetFields();
  106. $this->update = false;
  107. } catch (\Exception $ex) {
  108. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  109. }
  110. }
  111. public function cancel()
  112. {
  113. $this->add = false;
  114. $this->update = false;
  115. $this->resetFields();
  116. }
  117. public function delete($id)
  118. {
  119. try{
  120. \App\Models\PaymentMethod::find($id)->delete();
  121. session()->flash('success',"Metodo pagamento eliminato");
  122. }catch(\Exception $e){
  123. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  124. }
  125. }
  126. }