PaymentMethod.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Http\Livewire\Auth;
  4. use Livewire\Component;
  5. class PaymentMethod extends Component
  6. {
  7. public $records, $name, $enabled, $money, $type, $corrispettivo_fiscale, $dataId, $bank_id, $update = false, $add = false;
  8. public $paymentMethods = [];
  9. public $banks = array();
  10. protected $rules = [
  11. 'name' => 'required'
  12. ];
  13. protected $messages = [
  14. 'name.required' => 'Il nome è obbligatorio'
  15. ];
  16. public $sortField ='name';
  17. public $sortAsc = true;
  18. public function sortBy($field)
  19. {
  20. if($this->sortField === $field)
  21. {
  22. $this->sortAsc = ! $this->sortAsc;
  23. } else {
  24. $this->sortAsc = true;
  25. }
  26. $this->sortField = $field;
  27. }
  28. public function resetFields(){
  29. $this->name = '';
  30. $this->money = false;
  31. $this->type = '';
  32. $this->corrispettivo_fiscale = false;
  33. $this->enabled = true;
  34. $this->emit('load-data-table');
  35. }
  36. public function mount(){
  37. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  38. return redirect()->to('/dashboard');
  39. $this->banks = \App\Models\Bank::select('id', 'name')->get();
  40. // Load predefined payment methods from database
  41. $this->loadPaymentMethodOptions(); }
  42. public function render()
  43. {
  44. $this->records = \App\Models\PaymentMethod::all();
  45. foreach($this->records as $r)
  46. {
  47. $r->bank = $r->bank ? $r->bank->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. 'bank_id' => $this->bank_id,
  68. 'money' => $this->money,
  69. 'type' => $this->type,
  70. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  71. 'enabled' => $this->enabled
  72. ]);
  73. session()->flash('success','Metodo pagamento creato');
  74. $this->resetFields();
  75. $this->add = false;
  76. } catch (\Exception $ex) {
  77. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  78. }
  79. }
  80. public function edit($id){
  81. try {
  82. $payment_method = \App\Models\PaymentMethod::findOrFail($id);
  83. if( !$payment_method) {
  84. session()->flash('error','Metodo pagamento non trovato');
  85. } else {
  86. $this->name = $payment_method->name;
  87. $this->enabled = $payment_method->enabled;
  88. $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
  89. $this->money = $payment_method->money;
  90. $this->type = $payment_method->type;
  91. $this->bank_id = $payment_method->bank_id;
  92. $this->dataId = $payment_method->id;
  93. $this->update = true;
  94. $this->add = false;
  95. }
  96. } catch (\Exception $ex) {
  97. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  98. }
  99. }
  100. public function update()
  101. {
  102. $this->validate();
  103. try {
  104. \App\Models\PaymentMethod::whereId($this->dataId)->update([
  105. 'name' => $this->name,
  106. 'bank_id' => $this->bank_id,
  107. 'money' => $this->money,
  108. 'type' => $this->type,
  109. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  110. 'enabled' => $this->enabled
  111. ]);
  112. session()->flash('success','Metodo pagamento aggiornato');
  113. $this->resetFields();
  114. $this->update = false;
  115. } catch (\Exception $ex) {
  116. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  117. }
  118. }
  119. public function cancel()
  120. {
  121. $this->add = false;
  122. $this->update = false;
  123. $this->resetFields();
  124. }
  125. public function delete($id)
  126. {
  127. try{
  128. \App\Models\PaymentMethod::find($id)->delete();
  129. session()->flash('success',"Metodo pagamento eliminato");
  130. return redirect(request()->header('Referer'));
  131. }catch(\Exception $e){
  132. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  133. }
  134. }
  135. protected function loadPaymentMethodOptions()
  136. {
  137. $this->paymentMethods = [
  138. ['name' => 'Contanti', 'code' => 'MP01'],
  139. ['name' => 'Assegno', 'code' => 'MP02'],
  140. ['name' => 'Assegno circolare', 'code' => 'MP03'],
  141. ['name' => 'Contanti presso Tesoreria', 'code' => 'MP04'],
  142. ['name' => 'Bonifico', 'code' => 'MP05'],
  143. ['name' => 'Vaglia cambiario', 'code' => 'MP06'],
  144. ['name' => 'Bollettino bancario', 'code' => 'MP07'],
  145. ['name' => 'Carta di credito', 'code' => 'MP08'],
  146. ['name' => 'RID', 'code' => 'MP09'],
  147. ['name' => 'RID utenze', 'code' => 'MP10'],
  148. ['name' => 'RID veloce', 'code' => 'MP11'],
  149. ['name' => 'Riba', 'code' => 'MP12'],
  150. ['name' => 'MAV', 'code' => 'MP13'],
  151. ['name' => 'Quietanza erario stato', 'code' => 'MP14'],
  152. ['name' => 'Giroconto su conti di contabilità speciale', 'code' => 'MP15'],
  153. ['name' => 'Domiciliazione bancaria', 'code' => 'MP16'],
  154. ['name' => 'Domiciliazione postale', 'code' => 'MP17']
  155. ];
  156. }
  157. }