PaymentMethod.php 3.8 KB

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