PaymentMethod.php 5.7 KB

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