PaymentMethod.php 5.9 KB

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