PaymentMethod.php 5.1 KB

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