PaymentMethod.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, $origin_id, $destination_id, $update = false, $add = false;
  9. public $paymentMethods = [];
  10. public $showHidden = false;
  11. public $origins = [];
  12. public $destinations = [];
  13. protected $rules = [
  14. 'name' => 'required'
  15. ];
  16. protected $messages = [
  17. 'name.required' => 'Il nome è obbligatorio'
  18. ];
  19. public $sortField = 'name';
  20. public $sortAsc = true;
  21. public function boot()
  22. {
  23. app(TenantMiddleware::class)->setupTenantConnection();
  24. }
  25. public function sortBy($field)
  26. {
  27. if ($this->sortField === $field) {
  28. $this->sortAsc = ! $this->sortAsc;
  29. } else {
  30. $this->sortAsc = true;
  31. }
  32. $this->sortField = $field;
  33. }
  34. public function resetFields()
  35. {
  36. $this->name = '';
  37. $this->money = false;
  38. $this->type = 'ALL';
  39. $this->corrispettivo_fiscale = false;
  40. $this->enabled = true;
  41. $this->emit('load-data-table');
  42. }
  43. public function mount()
  44. {
  45. if (Auth::user()->level != env('LEVEL_ADMIN', 0))
  46. return redirect()->to('/dashboard');
  47. $this->origins = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['OUT', 'ALL'])->get();
  48. $this->destinations = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['IN', 'ALL'])->get();
  49. $this->loadPaymentMethodOptions();
  50. }
  51. public function hide($id)
  52. {
  53. try {
  54. \App\Models\PaymentMethod::whereId($id)->update(['hidden' => true]);
  55. session()->flash('success', 'Metodo di pagamento nascosto');
  56. } catch (\Exception $ex) {
  57. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  58. }
  59. }
  60. public function show($id)
  61. {
  62. try {
  63. \App\Models\PaymentMethod::whereId($id)->update(['hidden' => false]);
  64. session()->flash('success', 'Metodo di pagamento ripristinato');
  65. } catch (\Exception $ex) {
  66. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  67. }
  68. }
  69. public function toggleShowHidden()
  70. {
  71. $this->showHidden = !$this->showHidden;
  72. }
  73. public function render()
  74. {
  75. if ($this->showHidden) {
  76. $this->records = \App\Models\PaymentMethod::all();
  77. } else {
  78. $this->records = \App\Models\PaymentMethod::where(function ($query) {
  79. $query->where('hidden', false)->orWhereNull('hidden');
  80. })->get();
  81. }
  82. // foreach ($this->records as $r) {
  83. // dd($r, $r->origin);
  84. // $r->origin = $r->origin ? $r->origin->name : '';
  85. // $r->destination = $r->destination ? $r->destination->name : '';
  86. // }
  87. return view('livewire.payment_method');
  88. }
  89. public function add()
  90. {
  91. $this->resetFields();
  92. $this->add = true;
  93. $this->update = false;
  94. }
  95. public function store()
  96. {
  97. $this->validate();
  98. try {
  99. \App\Models\PaymentMethod::create([
  100. 'name' => $this->name,
  101. 'origin_id' => $this->origin_id != "" ? $this->origin_id : null,
  102. 'destination_id' => $this->destination_id != "" ? $this->destination_id : null,
  103. 'money' => $this->money,
  104. 'type' => $this->type,
  105. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  106. 'enabled' => $this->enabled
  107. ]);
  108. session()->flash('success', 'Metodo di pagamento creato');
  109. $this->resetFields();
  110. $this->add = false;
  111. } catch (\Exception $ex) {
  112. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  113. }
  114. }
  115. public function edit($id)
  116. {
  117. try {
  118. $payment_method = \App\Models\PaymentMethod::findOrFail($id);
  119. if (!$payment_method) {
  120. session()->flash('error', 'Metodo di pagamento non trovato');
  121. } else {
  122. $this->name = $payment_method->name;
  123. $this->enabled = $payment_method->enabled;
  124. $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
  125. $this->money = $payment_method->money;
  126. $this->type = $payment_method->type;
  127. $this->origin_id = $payment_method->origin_id;
  128. $this->destination_id = $payment_method->destination_id;
  129. $this->dataId = $payment_method->id;
  130. $this->update = true;
  131. $this->add = false;
  132. }
  133. } catch (\Exception $ex) {
  134. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  135. }
  136. }
  137. public function update()
  138. {
  139. $this->validate();
  140. try {
  141. \App\Models\PaymentMethod::whereId($this->dataId)->update([
  142. 'name' => $this->name,
  143. 'origin_id' => $this->origin_id != "" ? $this->origin_id : null,
  144. 'destination_id' => $this->destination_id != "" ? $this->destination_id : null,
  145. 'money' => $this->money,
  146. 'type' => $this->type,
  147. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  148. 'enabled' => $this->enabled
  149. ]);
  150. session()->flash('success', 'Metodo di pagamento aggiornato');
  151. $this->resetFields();
  152. $this->update = false;
  153. } catch (\Exception $ex) {
  154. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  155. }
  156. }
  157. public function cancel()
  158. {
  159. $this->add = false;
  160. $this->update = false;
  161. $this->resetFields();
  162. }
  163. protected function loadPaymentMethodOptions()
  164. {
  165. $this->paymentMethods = [
  166. ['name' => 'Contanti', 'code' => 'MP01'],
  167. ['name' => 'Assegno', 'code' => 'MP02'],
  168. ['name' => 'Assegno circolare', 'code' => 'MP03'],
  169. ['name' => 'Contanti presso Tesoreria', 'code' => 'MP04'],
  170. ['name' => 'Bonifico', 'code' => 'MP05'],
  171. ['name' => 'Vaglia cambiario', 'code' => 'MP06'],
  172. ['name' => 'Bollettino bancario', 'code' => 'MP07'],
  173. ['name' => 'Carta di credito', 'code' => 'MP08'],
  174. ['name' => 'RID', 'code' => 'MP09'],
  175. ['name' => 'RID utenze', 'code' => 'MP10'],
  176. ['name' => 'RID veloce', 'code' => 'MP11'],
  177. ['name' => 'Riba', 'code' => 'MP12'],
  178. ['name' => 'MAV', 'code' => 'MP13'],
  179. ['name' => 'Quietanza erario stato', 'code' => 'MP14'],
  180. ['name' => 'Giroconto su conti di contabilità speciale', 'code' => 'MP15'],
  181. ['name' => 'Domiciliazione bancaria', 'code' => 'MP16'],
  182. ['name' => 'Domiciliazione postale', 'code' => 'MP17']
  183. ];
  184. }
  185. }