FinancialMovements.php 8.6 KB

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