RecordOUT.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class RecordOUT extends Component
  5. {
  6. protected $listeners = ['setCausal' => 'setCausal'];
  7. public $sortField ='date';
  8. public $sortAsc = false;
  9. public $typeOUT = "OUT";
  10. public $fromPage = '';
  11. public $multiP = false;
  12. public $multiMonthTo = 0;
  13. public $multiYearTo = 0;
  14. public $multiMonthFrom = 0;
  15. public $multiYearFrom = 0;
  16. public function sortBy($field)
  17. {
  18. if($this->sortField === $field)
  19. {
  20. $this->sortAsc = ! $this->sortAsc;
  21. } else {
  22. $this->sortAsc = true;
  23. }
  24. $this->sortField = $field;
  25. }
  26. public $records, $dataId, $member_id, $supplier_id,
  27. $causal_id,
  28. $payment_method_id,
  29. $origin_id,
  30. $date,
  31. $month,
  32. $year,
  33. $type,
  34. $amount,
  35. $note,
  36. $commercial, $update = false, $add = false;
  37. public $filterSupplier = 0, $filterPaymentMethod = 0, $filterCausals = [], $filterFrom = '', $filterTo = '', $filterCommercial = 0;
  38. public $hasFilter = false;
  39. public $total = 0;
  40. public $selectedFilter = '';
  41. public $multipleIds = [];
  42. public $multipleAction = '';
  43. public $selectId = 0;
  44. public $causals = array();
  45. public $payments = array();
  46. public $banks = array();
  47. public $suppliers = array();
  48. public $rows = array();
  49. protected $rules = [
  50. 'supplier_id' => 'required',
  51. 'payment_method_id' => 'required',
  52. 'rows.*.causal_id' => 'required',
  53. 'rows.*.amount' => 'required'
  54. //'causal_id' => 'required',
  55. //'amount' => 'required'
  56. ];
  57. protected $messages = [
  58. 'supplier_id.required' => 'Il fornitore è obbligatorio',
  59. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  60. 'rows.*.amount.required' => 'L\'importo è obbligatorio',
  61. 'rows.*.causal_id.required' => 'La causale è obbligatoria'
  62. ];
  63. public function getSupplierProperty()
  64. {
  65. $ret = null;
  66. if ($this->supplier_id > 0)
  67. {
  68. $ret = \App\Models\Supplier::findOrFail($this->supplier_id);
  69. }
  70. return $ret;
  71. }
  72. public function getCausalProperty()
  73. {
  74. $ret = null;
  75. if ($this->causal_id > 0)
  76. {
  77. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  78. }
  79. return $ret;
  80. }
  81. public function resetFields(){
  82. $this->member_id = null;
  83. $this->supplier_id = null;
  84. //$this->causal_id = null;
  85. $this->payment_method_id = null;
  86. $this->origin_id = null;
  87. $this->date = date("Y-m-d");
  88. //$this->month = date("n");
  89. //$this->year = date("Y");
  90. $this->type = 'OUT';
  91. //$this->note = '';
  92. //$this->amount = null;
  93. $this->commercial = 1;
  94. $this->rows = array();
  95. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  96. $this->emit('load-data-table');
  97. }
  98. public function getCausale($records, $indentation)
  99. {
  100. foreach($records as $record)
  101. {
  102. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree());
  103. if(count($record->childs))
  104. $this->getCausale($record->childs, $indentation + 1);
  105. }
  106. }
  107. public function hydrate()
  108. {
  109. $this->emit('load-select');
  110. }
  111. public function mount()
  112. {
  113. if (isset($_GET["from"]))
  114. {
  115. $this->fromPage = $_GET["from"];
  116. }
  117. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  118. return redirect()->to('/dashboard');
  119. $this->multiMonthFrom = date("n");
  120. $this->multiYearFrom = date("Y");
  121. $this->multiMonthTo = date("n");
  122. $this->multiYearTo = date("Y");
  123. if (isset($_GET["new"]))
  124. $this->add();
  125. $this->causals = array();
  126. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->orderBy('name')->get(), 0);
  127. $this->suppliers = \App\Models\Supplier::select('name','id')->orderBy('name')->get();
  128. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->whereIn('type', array('ALL', 'OUT'))->where('enabled', true)->orderBy('name')->get();
  129. $this->banks = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', array('ALL', 'OUT'))->orderBy('name')->get();
  130. }
  131. public function updatedPaymentMethodId() {
  132. if ($this->payment_method_id) {
  133. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  134. $this->origin_id = $payment_method->origin_id ?? null;
  135. }
  136. }
  137. public function getCausal($causal)
  138. {
  139. $ret = '';
  140. if ($causal > 0)
  141. {
  142. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  143. }
  144. return $ret;
  145. }
  146. public function search()
  147. {
  148. $this->hasFilter = true;
  149. }
  150. public function disableSearch()
  151. {
  152. $this->filterSupplier = 0;
  153. $this->filterPaymentMethod = 0;
  154. $this->filterCausals = [];
  155. $this->filterTo = '';
  156. $this->filterFrom = '';
  157. $this->filterCommercial = 0;
  158. $this->hasFilter = false;
  159. $this->hasFilter = false;
  160. }
  161. public function render()
  162. {
  163. $datas = [];
  164. if (false)
  165. {
  166. if ($this->hasFilter)
  167. {
  168. $datas = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  169. /*if ($this->filterCommercial > 0)
  170. {
  171. $datas = $datas->where('commercial', $this->filterCommercial == 1 ? true : false);
  172. }*/
  173. if ($this->filterSupplier > 0)
  174. {
  175. $datas = $datas->where('supplier_id', $this->filterSupplier);
  176. }
  177. if ($this->filterPaymentMethod > 0)
  178. {
  179. $datas = $datas->where('payment_method_id', $this->filterPaymentMethod);
  180. }
  181. /*if (sizeof($this->filterCausals) > 0)
  182. {
  183. $datas = $datas->whereIn('causal_id', $this->filterCausals);
  184. }*/
  185. if ($this->filterFrom != '')
  186. {
  187. $datas = $datas->where('date', '>=', $this->filterFrom);
  188. }
  189. if ($this->filterTo != '')
  190. {
  191. $datas = $datas->where('date', '<=', $this->filterTo);
  192. }
  193. //$this->records = $datas->orderBy('date', 'DESC')->get();
  194. $this->records = $datas->get();
  195. //$this->total = $this->records->sum('amount');
  196. $this->total = 0;
  197. foreach($this->records as $r)
  198. {
  199. foreach($r->rows as $rr)
  200. {
  201. $this->total += $rr->amount;
  202. }
  203. }
  204. }
  205. else
  206. {
  207. if ($this->selectedFilter == '')
  208. {
  209. $this->records = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method')->limit(20)->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
  210. }
  211. else
  212. {
  213. if ($this->selectedFilter == 0)
  214. {
  215. $fromDate = date("Y-m-d");
  216. $toDate = date("Y-m-d");
  217. }
  218. if ($this->selectedFilter == 1)
  219. {
  220. $fromDate = date("Y-m-01");
  221. $toDate = date("Y-m-t");
  222. }
  223. if ($this->selectedFilter == 2)
  224. {
  225. $fromDate = date("Y-01-01");
  226. $toDate = date("Y-12-31");
  227. }
  228. if ($this->selectedFilter == 3)
  229. {
  230. $fromDate = date("2000-01-01");
  231. $toDate = date("Y-12-31");
  232. }
  233. $this->records = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->with('supplier', 'payment_method')->get();
  234. }
  235. }
  236. foreach($this->records as $r)
  237. {
  238. $r->total = $r->getTotal();
  239. $r->supplier = $r->supplier ? $r->supplier->name : '';
  240. $r->payment = $r->payment_method ? $r->payment_method->name : '';
  241. }
  242. }
  243. /*if ($this->sortField != '')
  244. {
  245. if ($this->sortAsc)
  246. $this->records = $this->records->sortBy($this->sortField);
  247. else
  248. $this->records = $this->records->sortByDesc($this->sortField);
  249. }
  250. else
  251. $this->records = $this->records->sortByDesc('id');*/
  252. return view('livewire.records_out');
  253. }
  254. public function executeMultipleAction(){
  255. if ($this->multipleAction == 'delete')
  256. $this->multipleDelete();
  257. }
  258. public function add()
  259. {
  260. $this->emit('load-select');
  261. //if ($this->hasFilter)
  262. $this->emit('hide-search');
  263. $this->resetFields();
  264. $this->add = true;
  265. $this->update = false;
  266. $this->emit('setEdit', true);
  267. }
  268. public function store()
  269. {
  270. $this->emit('refresh');
  271. $this->validate();
  272. try {
  273. $record = \App\Models\Record::create([
  274. 'member_id' => $this->member_id,
  275. 'supplier_id' => $this->supplier_id,
  276. //'causal_id' => $this->causal_id,
  277. 'payment_method_id' => $this->payment_method_id,
  278. 'origin_id' => $this->origin_id,
  279. 'date' => $this->date,
  280. //'month' => $this->month,
  281. //'year' => $this->year,
  282. //'note' => $this->note,
  283. 'type' => $this->type,
  284. //'amount' => $this->currencyToDouble($this->amount),
  285. 'commercial' => $this->commercial,
  286. ]);
  287. $this->dataId = $record->id;
  288. $tot = 0;
  289. foreach($this->rows as $row)
  290. {
  291. foreach($row["when"] as $x => $y)
  292. {
  293. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  294. }
  295. \App\Models\RecordRow::create([
  296. 'record_id' => $this->dataId,
  297. 'causal_id' => $row["causal_id"],
  298. 'note' => $row["note"],
  299. 'amount' => $this->currencyToDouble($row["amount"]),
  300. 'commercial' => $row["commercial"],
  301. 'when' => json_encode($row["when"])
  302. ]);
  303. $tot += $this->currencyToDouble($row["amount"]);
  304. }
  305. $record->amount = $tot;
  306. $record->save();
  307. session()->flash('success','Movimento creato');
  308. $this->resetFields();
  309. $this->add = false;
  310. $this->emit('setEdit', false);
  311. } catch (\Exception $ex) {
  312. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  313. }
  314. }
  315. public function edit($id){
  316. $this->resetFields();
  317. if (!isset($_GET["from"]) && $this->fromPage == '')
  318. $this->fromPage = 'out';
  319. $this->emit('setEdit', true);
  320. $this->emit('load-select');
  321. //if ($this->hasFilter)
  322. $this->emit('hide-search');
  323. try {
  324. $record = \App\Models\Record::findOrFail($id);
  325. if( !$record) {
  326. session()->flash('error','Movimento non trovato');
  327. } else {
  328. $this->member_id = $record->member_id;
  329. $this->supplier_id = $record->supplier_id;
  330. //$this->causal_id = $record->causal_id;
  331. $this->payment_method_id = $record->payment_method_id;
  332. $this->origin_id = $record->origin_id;
  333. $this->date = date("Y-m-d", strtotime($record->date));
  334. //$this->month = $record->month;
  335. //$this->year = $record->year;
  336. //$this->note = $record->note;
  337. $this->type = $record->type;
  338. //$this->amount = formatPrice($record->amount);
  339. $this->commercial = $record->commercial;
  340. $this->dataId = $record->id;
  341. $this->update = true;
  342. $this->add = false;
  343. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount')->get()->toArray();
  344. foreach($this->rows as $i => $r)
  345. {
  346. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  347. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  348. }
  349. }
  350. } catch (\Exception $ex) {
  351. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  352. }
  353. }
  354. public function update()
  355. {
  356. $this->emit('refresh');
  357. $this->validate();
  358. try {
  359. \App\Models\Record::whereId($this->dataId)->update([
  360. 'member_id' => $this->member_id,
  361. 'supplier_id' => $this->supplier_id,
  362. //'causal_id' => $this->causal_id,
  363. 'payment_method_id' => $this->payment_method_id,
  364. 'origin_id' => $this->origin_id,
  365. 'date' => $this->date,
  366. //'month' => $this->month,
  367. //'year' => $this->year,
  368. //'note' => $this->note,
  369. 'type' => $this->type,
  370. //'amount' => $this->currencyToDouble($this->amount),
  371. 'commercial' => $this->commercial,
  372. ]);
  373. $tot = 0;
  374. // Elimino le righe
  375. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  376. // Inserisco le righe
  377. foreach($this->rows as $row)
  378. {
  379. foreach($row["when"] as $x => $y)
  380. {
  381. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  382. }
  383. \App\Models\RecordRow::create([
  384. 'record_id' => $this->dataId,
  385. 'causal_id' => $row["causal_id"],
  386. 'note' => $row["note"],
  387. 'amount' => $this->currencyToDouble($row["amount"]),
  388. 'commercial' => $row["commercial"],
  389. 'when' => json_encode($row["when"])
  390. ]);
  391. $tot += $this->currencyToDouble($row["amount"]);
  392. }
  393. $rec = \App\Models\Record::findOrFail($this->dataId);
  394. $rec->amount = $tot;
  395. $rec->save();
  396. session()->flash('success','Movimento aggiornato');
  397. $this->resetFields();
  398. $this->update = false;
  399. $this->emit('setEdit', false);
  400. } catch (\Exception $ex) {
  401. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  402. }
  403. }
  404. public function cancel()
  405. {
  406. $this->add = false;
  407. $this->update = false;
  408. $this->emit('setEdit', false);
  409. $this->resetFields();
  410. }
  411. public function delete($id)
  412. {
  413. try{
  414. \App\Models\Record::find($id)->delete();
  415. session()->flash('success',"Movimento eliminato");
  416. }catch(\Exception $e){
  417. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  418. }
  419. }
  420. public function multipleDelete()
  421. {
  422. try{
  423. foreach($this->multipleIds as $id)
  424. {
  425. \App\Models\Record::find($id)->delete();
  426. }
  427. }catch(\Exception $e){
  428. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  429. }
  430. $this->multipleAction = '';
  431. }
  432. /*
  433. public function createReceipt()
  434. {
  435. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first();
  436. if ($receipt != null)
  437. {
  438. $receipt->update([
  439. 'member_id' => $this->member_id,
  440. 'supplier_id' => $this->supplier_id,
  441. 'causal_id' => $this->causal_id,
  442. 'payment_method_id' => $this->payment_method_id,
  443. 'date' => date("Y-m-d", strtotime($this->date)),
  444. 'month' => $this->month,
  445. 'year' => $this->year,
  446. 'type' => $this->type,
  447. 'amount' => $this->currencyToDouble($this->amount),
  448. 'commercial' => $this->commercial,
  449. 'status' => 1,
  450. ]);
  451. }
  452. else
  453. {
  454. $number = 1;
  455. $exist = \App\Models\Receipt::where('year', $this->year)->orderBy('number', 'DESC')->first();
  456. if ($exist != null)
  457. $number = $exist->number + 1;
  458. $receipt = \App\Models\Receipt::create([
  459. 'record_id' => $this->dataId,
  460. 'member_id' => $this->member_id,
  461. 'supplier_id' => $this->supplier_id,
  462. 'causal_id' => $this->causal_id,
  463. 'payment_method_id' => $this->payment_method_id,
  464. 'number' => $number,
  465. 'date' => $this->date,
  466. 'month' => $this->month,
  467. 'year' => $this->year,
  468. 'type' => $this->type,
  469. 'amount' => $this->currencyToDouble($this->amount),
  470. 'commercial' => $this->commercial,
  471. 'status' => 1,
  472. ]);
  473. }
  474. }
  475. public function removeReceipt()
  476. {
  477. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  478. $receipt->status = 99;
  479. $receipt->save();
  480. $this->currentReceip = $receipt;
  481. }
  482. */
  483. function currencyToDouble($val)
  484. {
  485. $x = str_replace("€", "", $val);
  486. $x = str_replace(".", "", $x);
  487. $x = str_replace(",", ".", $x);
  488. return floatval(trim($x));
  489. }
  490. public function addRow()
  491. {
  492. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  493. $this->emit('load-select');
  494. }
  495. public function delRow($idx)
  496. {
  497. unset($this->rows[$idx]);
  498. // $this->emit('load-select');
  499. }
  500. public function addPeriod($idx)
  501. {
  502. $x = sizeof($this->rows[$idx]['when']) - 1;
  503. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  504. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  505. //$this->rows[$idx]['when'][] = array('month' => date("n"), 'year' => date("Y"), 'period' => '');
  506. }
  507. public function delPeriod($idx, $xxx)
  508. {
  509. array_splice($this->rows[$idx]['when'], $xxx, 1);
  510. // $this->emit('load-select');
  511. }
  512. public function getTotal()
  513. {
  514. $total = 0.00;
  515. foreach($this->rows as $r)
  516. {
  517. $total += $this->currencyToDouble($r["amount"] != null ? $r["amount"] : 0);
  518. }
  519. return formatPrice($total);
  520. // $this->emit('load-select');
  521. }
  522. public function setCausal($id, $idx)
  523. {
  524. $this->rows[$idx]["causal_id"] = $id;
  525. }
  526. public function multiPeriod()
  527. {
  528. $this->multiP = true;
  529. }
  530. public function multiPeriodCreate($idx)
  531. {
  532. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  533. $this->rows[$idx]['when'] = [];
  534. foreach ($period as $dt) {
  535. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  536. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  537. }
  538. $this->multiP = false;
  539. }
  540. public function multiPeriodCancel()
  541. {
  542. $this->multiP = false;
  543. }
  544. }