FinancialMovements.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Http\Middleware\TenantMiddleware;
  4. use Livewire\Component;
  5. class FinancialMovements extends Component
  6. {
  7. public $origins = [];
  8. public $destinations = [];
  9. public $date;
  10. public $origin_id;
  11. public $destination_id;
  12. public $causal_id;
  13. public $amount;
  14. public $amountFormatted;
  15. public $notes;
  16. public $causal_name;
  17. public $add = false;
  18. public $update = false;
  19. public $isDuplicate = false;
  20. public $dataId;
  21. public $multipleIds = [];
  22. public $multipleAction = '';
  23. protected function rules()
  24. {
  25. return [
  26. 'date' => ['required', 'date'],
  27. 'origin_id' => ['required', 'integer', 'exists:banks,id'],
  28. 'destination_id' => ['required', 'integer', 'exists:banks,id', 'different:origin_id',],
  29. 'amount' => ['required', 'numeric', 'gt:0'],
  30. 'notes' => ['nullable', 'string', 'max:5000'],
  31. ];
  32. }
  33. protected $messages = [
  34. 'date.required' => "La data dell'operazione è obbligatoria.",
  35. 'origin_id.required' => "Seleziona un conto di origine.",
  36. 'origin_id.exists' => "Il conto di origine selezionato non è valido.",
  37. 'destination_id.required' => "Seleziona un conto di destinazione.",
  38. 'destination_id.exists' => "Il conto di destinazione selezionato non è valido.",
  39. 'destination_id.different' => "Origine e destinazione devono essere diversi.",
  40. 'amount.required' => "Inserisci un importo.",
  41. 'amount.numeric' => "L'importo deve essere un numero.",
  42. 'amount.gt' => "L'importo deve essere maggiore di zero.",
  43. ];
  44. public function boot()
  45. {
  46. app(TenantMiddleware::class)->setupTenantConnection();
  47. }
  48. public function hydrate()
  49. {
  50. $this->emit('load-select');
  51. }
  52. public function resetFields()
  53. {
  54. $this->date = date("Y-m-d");
  55. $this->origin_id = null;
  56. $this->destination_id = null;
  57. $this->amount = null;
  58. $this->amountFormatted = null;
  59. $this->notes = null;
  60. $this->emit('load-data-table');
  61. }
  62. public function mount()
  63. {
  64. $this->origins = \App\Models\Bank::where('enabled', true)->whereIn('visibility', ['ALL', 'OUT'])->orderBy('name')->get();
  65. $this->destinations = \App\Models\Bank::where('enabled', true)->whereIn('visibility', ['ALL', 'IN'])->orderBy('name')->get();
  66. $this->causal_name = "Movimento finanziario";
  67. $this->causal_id = \App\Models\Causal::where('name', $this->causal_name)->value('id');
  68. if (!$this->causal_id) {
  69. $causal = new \App\Models\Causal();
  70. $causal->name = $this->causal_name;
  71. $causal->type = "IN";
  72. $causal->parent_id = null;
  73. $causal->money = false;
  74. $causal->corrispettivo_fiscale = false;
  75. $causal->no_receipt = false;
  76. $causal->user_status = false;
  77. $causal->no_first = false;
  78. $causal->no_records = true;
  79. $causal->no_reports = true;
  80. $causal->enabled = true;
  81. $causal->save();
  82. $this->causal_id = $causal->id;
  83. }
  84. }
  85. public function render()
  86. {
  87. return view('livewire.financial_movements');
  88. }
  89. public function add()
  90. {
  91. $this->emit('load-select');
  92. $this->resetFields();
  93. $this->dataId = 0;
  94. $this->add = true;
  95. $this->update = false;
  96. $this->emit('setEdit', true);
  97. }
  98. public function store()
  99. {
  100. if ($this->validate()) {
  101. try {
  102. $financial_movement = \App\Models\FinancialMovement::create([
  103. 'date' => $this->date,
  104. 'origin_id' => $this->origin_id,
  105. 'destination_id' => $this->destination_id,
  106. 'causal_id' => $this->causal_id,
  107. 'amount' => $this->amount,
  108. 'notes' => $this->notes,
  109. ]);
  110. $this->dataId = $financial_movement->id;
  111. session()->flash('success', 'Movimento creato');
  112. $this->resetFields();
  113. $this->add = false;
  114. $this->isDuplicate = false;
  115. $this->emit('setEdit', false);
  116. } catch (\Exception $ex) {
  117. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  118. }
  119. }
  120. }
  121. public function duplicate($id)
  122. {
  123. $financial_movement = \App\Models\FinancialMovement::findOrFail($id);
  124. $new_financial_movement = $financial_movement->replicate();
  125. $new_financial_movement->save();
  126. $this->edit($new_financial_movement->id);
  127. $this->isDuplicate = true;
  128. }
  129. public function edit($id)
  130. {
  131. $this->emit('setEdit', true);
  132. $this->emit('load-select');
  133. try {
  134. $financial_movement = \App\Models\FinancialMovement::findOrFail($id);
  135. if (!$financial_movement) {
  136. session()->flash('error', 'Movimento non trovato');
  137. } else {
  138. $this->date = date("Y-m-d", strtotime($financial_movement->date));;
  139. $this->origin_id = $financial_movement->origin_id;
  140. $this->destination_id = $financial_movement->destination_id;
  141. $this->amount = $financial_movement->amount;
  142. $this->amountFormatted = formatPrice($this->amount);
  143. $this->notes = $financial_movement->notes;
  144. $this->dataId = $financial_movement->id;
  145. $this->update = true;
  146. $this->add = false;
  147. }
  148. } catch (\Exception $ex) {
  149. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  150. }
  151. }
  152. public function update()
  153. {
  154. if ($this->validate()) {
  155. try {
  156. \App\Models\FinancialMovement::whereId($this->dataId)->update([
  157. 'date' => $this->date,
  158. 'origin_id' => $this->origin_id,
  159. 'destination_id' => $this->destination_id,
  160. 'causal_id' => $this->causal_id,
  161. 'amount' => $this->amount,
  162. 'notes' => $this->notes,
  163. ]);
  164. session()->flash('success', 'Movimento aggiornato');
  165. $this->resetFields();
  166. $this->update = false;
  167. $this->isDuplicate = false;
  168. $this->emit('setEdit', false);
  169. } catch (\Exception $ex) {
  170. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  171. }
  172. }
  173. }
  174. public function cancel()
  175. {
  176. if ($this->isDuplicate) {
  177. try {
  178. \App\Models\FinancialMovement::find($this->dataId)->delete();
  179. session()->flash('success', "Movimento eliminato");
  180. } catch (\Exception $e) {
  181. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  182. }
  183. }
  184. $this->isDuplicate = false;
  185. $this->add = false;
  186. $this->update = false;
  187. $this->resetFields();
  188. $this->emit('setEdit', false);
  189. return redirect()->to('/financial_movements');
  190. }
  191. public function delete($id)
  192. {
  193. try {
  194. $fm = \App\Models\FinancialMovement::find($id);
  195. $fm->deleted = true;
  196. $fm->save();
  197. session()->flash('success', 'Movimento eliminato');
  198. $this->resetFields();
  199. $this->update = false;
  200. $this->isDuplicate = false;
  201. $this->emit('setEdit', false);
  202. $this->emit('reload');
  203. } catch (\Exception $ex) {
  204. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  205. }
  206. }
  207. public function multipleDelete()
  208. {
  209. try {
  210. foreach ($this->multipleIds as $id) {
  211. $fm = \App\Models\FinancialMovement::find($id);
  212. $fm->deleted = true;
  213. $fm->save();
  214. }
  215. } catch (\Exception $e) {
  216. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  217. }
  218. $this->multipleAction = '';
  219. }
  220. public function updatedAmountFormatted($value)
  221. {
  222. $clean = str_replace(['€', '.', ' '], '', $value);
  223. $clean = str_replace(',', '.', $clean);
  224. $this->amount = is_numeric($clean) ? (float) $clean : null;
  225. }
  226. public function updatedOriginId()
  227. {
  228. if ($this->origin_id && $this->destination_id && $this->origin_id == $this->destination_id) {
  229. $this->destination_id = null;
  230. }
  231. }
  232. public function updatedDestinationId()
  233. {
  234. if ($this->origin_id && $this->destination_id && $this->origin_id == $this->destination_id) {
  235. $this->destination_id = null;
  236. }
  237. }
  238. }