PaymentMethod.php 4.3 KB

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