PaymentMethod.php 6.2 KB

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