RecordOUT.php 19 KB

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