PaymentMethod.php 3.8 KB

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