RecordOUT.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 getCausal($causal)
  132. {
  133. $ret = '';
  134. if ($causal > 0)
  135. {
  136. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  137. }
  138. return $ret;
  139. }
  140. public function search()
  141. {
  142. $this->hasFilter = true;
  143. }
  144. public function disableSearch()
  145. {
  146. $this->filterSupplier = 0;
  147. $this->filterPaymentMethod = 0;
  148. $this->filterCausals = [];
  149. $this->filterTo = '';
  150. $this->filterFrom = '';
  151. $this->filterCommercial = 0;
  152. $this->hasFilter = false;
  153. $this->hasFilter = false;
  154. }
  155. public function render()
  156. {
  157. $datas = [];
  158. if (false)
  159. {
  160. if ($this->hasFilter)
  161. {
  162. $datas = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  163. /*if ($this->filterCommercial > 0)
  164. {
  165. $datas = $datas->where('commercial', $this->filterCommercial == 1 ? true : false);
  166. }*/
  167. if ($this->filterSupplier > 0)
  168. {
  169. $datas = $datas->where('supplier_id', $this->filterSupplier);
  170. }
  171. if ($this->filterPaymentMethod > 0)
  172. {
  173. $datas = $datas->where('payment_method_id', $this->filterPaymentMethod);
  174. }
  175. /*if (sizeof($this->filterCausals) > 0)
  176. {
  177. $datas = $datas->whereIn('causal_id', $this->filterCausals);
  178. }*/
  179. if ($this->filterFrom != '')
  180. {
  181. $datas = $datas->where('date', '>=', $this->filterFrom);
  182. }
  183. if ($this->filterTo != '')
  184. {
  185. $datas = $datas->where('date', '<=', $this->filterTo);
  186. }
  187. //$this->records = $datas->orderBy('date', 'DESC')->get();
  188. $this->records = $datas->get();
  189. //$this->total = $this->records->sum('amount');
  190. $this->total = 0;
  191. foreach($this->records as $r)
  192. {
  193. foreach($r->rows as $rr)
  194. {
  195. $this->total += $rr->amount;
  196. }
  197. }
  198. }
  199. else
  200. {
  201. if ($this->selectedFilter == '')
  202. {
  203. $this->records = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method')->limit(20)->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
  204. }
  205. else
  206. {
  207. if ($this->selectedFilter == 0)
  208. {
  209. $fromDate = date("Y-m-d");
  210. $toDate = date("Y-m-d");
  211. }
  212. if ($this->selectedFilter == 1)
  213. {
  214. $fromDate = date("Y-m-01");
  215. $toDate = date("Y-m-t");
  216. }
  217. if ($this->selectedFilter == 2)
  218. {
  219. $fromDate = date("Y-01-01");
  220. $toDate = date("Y-12-31");
  221. }
  222. if ($this->selectedFilter == 3)
  223. {
  224. $fromDate = date("2000-01-01");
  225. $toDate = date("Y-12-31");
  226. }
  227. $this->records = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->with('supplier', 'payment_method')->get();
  228. }
  229. }
  230. foreach($this->records as $r)
  231. {
  232. $r->total = $r->getTotal();
  233. $r->supplier = $r->supplier ? $r->supplier->name : '';
  234. $r->payment = $r->payment_method ? $r->payment_method->name : '';
  235. }
  236. }
  237. /*if ($this->sortField != '')
  238. {
  239. if ($this->sortAsc)
  240. $this->records = $this->records->sortBy($this->sortField);
  241. else
  242. $this->records = $this->records->sortByDesc($this->sortField);
  243. }
  244. else
  245. $this->records = $this->records->sortByDesc('id');*/
  246. return view('livewire.records_out');
  247. }
  248. public function executeMultipleAction(){
  249. if ($this->multipleAction == 'delete')
  250. $this->multipleDelete();
  251. }
  252. public function add()
  253. {
  254. $this->emit('load-select');
  255. //if ($this->hasFilter)
  256. $this->emit('hide-search');
  257. $this->resetFields();
  258. $this->add = true;
  259. $this->update = false;
  260. $this->emit('setEdit', true);
  261. }
  262. public function store()
  263. {
  264. $this->emit('refresh');
  265. $this->validate();
  266. try {
  267. $record = \App\Models\Record::create([
  268. 'member_id' => $this->member_id,
  269. 'supplier_id' => $this->supplier_id,
  270. //'causal_id' => $this->causal_id,
  271. 'payment_method_id' => $this->payment_method_id,
  272. 'origin_id' => $this->origin_id,
  273. 'date' => $this->date,
  274. //'month' => $this->month,
  275. //'year' => $this->year,
  276. //'note' => $this->note,
  277. 'type' => $this->type,
  278. //'amount' => $this->currencyToDouble($this->amount),
  279. 'commercial' => $this->commercial,
  280. ]);
  281. $this->dataId = $record->id;
  282. $tot = 0;
  283. foreach($this->rows as $row)
  284. {
  285. foreach($row["when"] as $x => $y)
  286. {
  287. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  288. }
  289. \App\Models\RecordRow::create([
  290. 'record_id' => $this->dataId,
  291. 'causal_id' => $row["causal_id"],
  292. 'note' => $row["note"],
  293. 'amount' => $this->currencyToDouble($row["amount"]),
  294. 'commercial' => $row["commercial"],
  295. 'when' => json_encode($row["when"])
  296. ]);
  297. $tot += $this->currencyToDouble($row["amount"]);
  298. }
  299. $record->amount = $tot;
  300. $record->save();
  301. session()->flash('success','Movimento creato');
  302. $this->resetFields();
  303. $this->add = false;
  304. $this->emit('setEdit', false);
  305. } catch (\Exception $ex) {
  306. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  307. }
  308. }
  309. public function edit($id){
  310. $this->resetFields();
  311. if (!isset($_GET["from"]) && $this->fromPage == '')
  312. $this->fromPage = 'out';
  313. $this->emit('setEdit', true);
  314. $this->emit('load-select');
  315. //if ($this->hasFilter)
  316. $this->emit('hide-search');
  317. try {
  318. $record = \App\Models\Record::findOrFail($id);
  319. if( !$record) {
  320. session()->flash('error','Movimento non trovato');
  321. } else {
  322. $this->member_id = $record->member_id;
  323. $this->supplier_id = $record->supplier_id;
  324. //$this->causal_id = $record->causal_id;
  325. $this->payment_method_id = $record->payment_method_id;
  326. $this->origin_id = $record->origin_id;
  327. $this->date = date("Y-m-d", strtotime($record->date));
  328. //$this->month = $record->month;
  329. //$this->year = $record->year;
  330. //$this->note = $record->note;
  331. $this->type = $record->type;
  332. //$this->amount = formatPrice($record->amount);
  333. $this->commercial = $record->commercial;
  334. $this->dataId = $record->id;
  335. $this->update = true;
  336. $this->add = false;
  337. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount')->get()->toArray();
  338. foreach($this->rows as $i => $r)
  339. {
  340. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  341. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  342. }
  343. }
  344. } catch (\Exception $ex) {
  345. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  346. }
  347. }
  348. public function update()
  349. {
  350. $this->emit('refresh');
  351. $this->validate();
  352. try {
  353. \App\Models\Record::whereId($this->dataId)->update([
  354. 'member_id' => $this->member_id,
  355. 'supplier_id' => $this->supplier_id,
  356. //'causal_id' => $this->causal_id,
  357. 'payment_method_id' => $this->payment_method_id,
  358. 'origin_id' => $this->origin_id,
  359. 'date' => $this->date,
  360. //'month' => $this->month,
  361. //'year' => $this->year,
  362. //'note' => $this->note,
  363. 'type' => $this->type,
  364. //'amount' => $this->currencyToDouble($this->amount),
  365. 'commercial' => $this->commercial,
  366. ]);
  367. $tot = 0;
  368. // Elimino le righe
  369. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  370. // Inserisco le righe
  371. foreach($this->rows as $row)
  372. {
  373. foreach($row["when"] as $x => $y)
  374. {
  375. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  376. }
  377. \App\Models\RecordRow::create([
  378. 'record_id' => $this->dataId,
  379. 'causal_id' => $row["causal_id"],
  380. 'note' => $row["note"],
  381. 'amount' => $this->currencyToDouble($row["amount"]),
  382. 'commercial' => $row["commercial"],
  383. 'when' => json_encode($row["when"])
  384. ]);
  385. $tot += $this->currencyToDouble($row["amount"]);
  386. }
  387. $rec = \App\Models\Record::findOrFail($this->dataId);
  388. $rec->amount = $tot;
  389. $rec->save();
  390. session()->flash('success','Movimento aggiornato');
  391. $this->resetFields();
  392. $this->update = false;
  393. $this->emit('setEdit', false);
  394. } catch (\Exception $ex) {
  395. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  396. }
  397. }
  398. public function cancel()
  399. {
  400. $this->add = false;
  401. $this->update = false;
  402. $this->emit('setEdit', false);
  403. $this->resetFields();
  404. }
  405. public function delete($id)
  406. {
  407. try{
  408. \App\Models\Record::find($id)->delete();
  409. session()->flash('success',"Movimento eliminato");
  410. }catch(\Exception $e){
  411. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  412. }
  413. }
  414. public function multipleDelete()
  415. {
  416. try{
  417. foreach($this->multipleIds as $id)
  418. {
  419. \App\Models\Record::find($id)->delete();
  420. }
  421. }catch(\Exception $e){
  422. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  423. }
  424. $this->multipleAction = '';
  425. }
  426. /*
  427. public function createReceipt()
  428. {
  429. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first();
  430. if ($receipt != null)
  431. {
  432. $receipt->update([
  433. 'member_id' => $this->member_id,
  434. 'supplier_id' => $this->supplier_id,
  435. 'causal_id' => $this->causal_id,
  436. 'payment_method_id' => $this->payment_method_id,
  437. 'date' => date("Y-m-d", strtotime($this->date)),
  438. 'month' => $this->month,
  439. 'year' => $this->year,
  440. 'type' => $this->type,
  441. 'amount' => $this->currencyToDouble($this->amount),
  442. 'commercial' => $this->commercial,
  443. 'status' => 1,
  444. ]);
  445. }
  446. else
  447. {
  448. $number = 1;
  449. $exist = \App\Models\Receipt::where('year', $this->year)->orderBy('number', 'DESC')->first();
  450. if ($exist != null)
  451. $number = $exist->number + 1;
  452. $receipt = \App\Models\Receipt::create([
  453. 'record_id' => $this->dataId,
  454. 'member_id' => $this->member_id,
  455. 'supplier_id' => $this->supplier_id,
  456. 'causal_id' => $this->causal_id,
  457. 'payment_method_id' => $this->payment_method_id,
  458. 'number' => $number,
  459. 'date' => $this->date,
  460. 'month' => $this->month,
  461. 'year' => $this->year,
  462. 'type' => $this->type,
  463. 'amount' => $this->currencyToDouble($this->amount),
  464. 'commercial' => $this->commercial,
  465. 'status' => 1,
  466. ]);
  467. }
  468. }
  469. public function removeReceipt()
  470. {
  471. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  472. $receipt->status = 99;
  473. $receipt->save();
  474. $this->currentReceip = $receipt;
  475. }
  476. */
  477. function currencyToDouble($val)
  478. {
  479. $x = str_replace("€", "", $val);
  480. $x = str_replace(".", "", $x);
  481. $x = str_replace(",", ".", $x);
  482. return floatval(trim($x));
  483. }
  484. public function addRow()
  485. {
  486. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  487. $this->emit('load-select');
  488. }
  489. public function delRow($idx)
  490. {
  491. unset($this->rows[$idx]);
  492. // $this->emit('load-select');
  493. }
  494. public function addPeriod($idx)
  495. {
  496. $x = sizeof($this->rows[$idx]['when']) - 1;
  497. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  498. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  499. //$this->rows[$idx]['when'][] = array('month' => date("n"), 'year' => date("Y"), 'period' => '');
  500. }
  501. public function delPeriod($idx, $xxx)
  502. {
  503. array_splice($this->rows[$idx]['when'], $xxx, 1);
  504. // $this->emit('load-select');
  505. }
  506. public function getTotal()
  507. {
  508. $total = 0.00;
  509. foreach($this->rows as $r)
  510. {
  511. $total += $this->currencyToDouble($r["amount"] != null ? $r["amount"] : 0);
  512. }
  513. return formatPrice($total);
  514. // $this->emit('load-select');
  515. }
  516. public function setCausal($id, $idx)
  517. {
  518. $this->rows[$idx]["causal_id"] = $id;
  519. }
  520. public function multiPeriod()
  521. {
  522. $this->multiP = true;
  523. }
  524. public function multiPeriodCreate($idx)
  525. {
  526. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  527. $this->rows[$idx]['when'] = [];
  528. foreach ($period as $dt) {
  529. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  530. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  531. }
  532. $this->multiP = false;
  533. }
  534. public function multiPeriodCancel()
  535. {
  536. $this->multiP = false;
  537. }
  538. }