RecordOUT.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 = 1;
  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. $this->emit('setEdit', true);
  250. }
  251. public function store()
  252. {
  253. $this->emit('refresh');
  254. $this->validate();
  255. try {
  256. $record = \App\Models\Record::create([
  257. 'member_id' => $this->member_id,
  258. 'supplier_id' => $this->supplier_id,
  259. //'causal_id' => $this->causal_id,
  260. 'payment_method_id' => $this->payment_method_id,
  261. 'date' => $this->date,
  262. //'month' => $this->month,
  263. //'year' => $this->year,
  264. //'note' => $this->note,
  265. 'type' => $this->type,
  266. //'amount' => $this->currencyToDouble($this->amount),
  267. 'commercial' => $this->commercial,
  268. ]);
  269. $this->dataId = $record->id;
  270. $tot = 0;
  271. foreach($this->rows as $row)
  272. {
  273. foreach($row["when"] as $x => $y)
  274. {
  275. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  276. }
  277. \App\Models\RecordRow::create([
  278. 'record_id' => $this->dataId,
  279. 'causal_id' => $row["causal_id"],
  280. 'note' => $row["note"],
  281. 'amount' => $this->currencyToDouble($row["amount"]),
  282. 'commercial' => $row["commercial"],
  283. 'when' => json_encode($row["when"])
  284. ]);
  285. $tot += $this->currencyToDouble($row["amount"]);
  286. }
  287. $record->amount = $tot;
  288. $record->save();
  289. session()->flash('success','Movimento creato');
  290. $this->resetFields();
  291. $this->add = false;
  292. $this->emit('setEdit', false);
  293. } catch (\Exception $ex) {
  294. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  295. }
  296. }
  297. public function edit($id){
  298. $this->emit('setEdit', true);
  299. $this->emit('load-select');
  300. //if ($this->hasFilter)
  301. $this->emit('hide-search');
  302. try {
  303. $record = \App\Models\Record::findOrFail($id);
  304. if( !$record) {
  305. session()->flash('error','Movimento non trovato');
  306. } else {
  307. $this->member_id = $record->member_id;
  308. $this->supplier_id = $record->supplier_id;
  309. //$this->causal_id = $record->causal_id;
  310. $this->payment_method_id = $record->payment_method_id;
  311. $this->date = date("Y-m-d", strtotime($record->date));
  312. //$this->month = $record->month;
  313. //$this->year = $record->year;
  314. //$this->note = $record->note;
  315. $this->type = $record->type;
  316. //$this->amount = formatPrice($record->amount);
  317. $this->commercial = $record->commercial;
  318. $this->dataId = $record->id;
  319. $this->update = true;
  320. $this->add = false;
  321. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount')->get()->toArray();
  322. foreach($this->rows as $i => $r)
  323. {
  324. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  325. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  326. }
  327. }
  328. } catch (\Exception $ex) {
  329. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  330. }
  331. }
  332. public function update()
  333. {
  334. $this->emit('refresh');
  335. $this->validate();
  336. try {
  337. \App\Models\Record::whereId($this->dataId)->update([
  338. 'member_id' => $this->member_id,
  339. 'supplier_id' => $this->supplier_id,
  340. //'causal_id' => $this->causal_id,
  341. 'payment_method_id' => $this->payment_method_id,
  342. 'date' => $this->date,
  343. //'month' => $this->month,
  344. //'year' => $this->year,
  345. //'note' => $this->note,
  346. 'type' => $this->type,
  347. //'amount' => $this->currencyToDouble($this->amount),
  348. 'commercial' => $this->commercial,
  349. ]);
  350. $tot = 0;
  351. // Elimino le righe
  352. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  353. // Inserisco le righe
  354. foreach($this->rows as $row)
  355. {
  356. foreach($row["when"] as $x => $y)
  357. {
  358. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  359. }
  360. \App\Models\RecordRow::create([
  361. 'record_id' => $this->dataId,
  362. 'causal_id' => $row["causal_id"],
  363. 'note' => $row["note"],
  364. 'amount' => $this->currencyToDouble($row["amount"]),
  365. 'commercial' => $row["commercial"],
  366. 'when' => json_encode($row["when"])
  367. ]);
  368. $tot += $this->currencyToDouble($row["amount"]);
  369. }
  370. $rec = \App\Models\Record::findOrFail($this->dataId);
  371. $rec->amount = $tot;
  372. $rec->save();
  373. session()->flash('success','Movimento aggiornato');
  374. $this->resetFields();
  375. $this->update = false;
  376. $this->emit('setEdit', false);
  377. } catch (\Exception $ex) {
  378. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  379. }
  380. }
  381. public function cancel()
  382. {
  383. $this->add = false;
  384. $this->update = false;
  385. $this->emit('setEdit', false);
  386. $this->resetFields();
  387. }
  388. public function delete($id)
  389. {
  390. try{
  391. \App\Models\Record::find($id)->delete();
  392. session()->flash('success',"Movimento eliminato");
  393. }catch(\Exception $e){
  394. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  395. }
  396. }
  397. public function multipleDelete()
  398. {
  399. try{
  400. foreach($this->multipleIds as $id)
  401. {
  402. \App\Models\Record::find($id)->delete();
  403. }
  404. }catch(\Exception $e){
  405. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  406. }
  407. $this->multipleAction = '';
  408. }
  409. /*
  410. public function createReceipt()
  411. {
  412. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first();
  413. if ($receipt != null)
  414. {
  415. $receipt->update([
  416. 'member_id' => $this->member_id,
  417. 'supplier_id' => $this->supplier_id,
  418. 'causal_id' => $this->causal_id,
  419. 'payment_method_id' => $this->payment_method_id,
  420. 'date' => date("Y-m-d", strtotime($this->date)),
  421. 'month' => $this->month,
  422. 'year' => $this->year,
  423. 'type' => $this->type,
  424. 'amount' => $this->currencyToDouble($this->amount),
  425. 'commercial' => $this->commercial,
  426. 'status' => 1,
  427. ]);
  428. }
  429. else
  430. {
  431. $number = 1;
  432. $exist = \App\Models\Receipt::where('year', $this->year)->orderBy('number', 'DESC')->first();
  433. if ($exist != null)
  434. $number = $exist->number + 1;
  435. $receipt = \App\Models\Receipt::create([
  436. 'record_id' => $this->dataId,
  437. 'member_id' => $this->member_id,
  438. 'supplier_id' => $this->supplier_id,
  439. 'causal_id' => $this->causal_id,
  440. 'payment_method_id' => $this->payment_method_id,
  441. 'number' => $number,
  442. 'date' => $this->date,
  443. 'month' => $this->month,
  444. 'year' => $this->year,
  445. 'type' => $this->type,
  446. 'amount' => $this->currencyToDouble($this->amount),
  447. 'commercial' => $this->commercial,
  448. 'status' => 1,
  449. ]);
  450. }
  451. }
  452. public function removeReceipt()
  453. {
  454. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  455. $receipt->status = 99;
  456. $receipt->save();
  457. $this->currentReceip = $receipt;
  458. }
  459. */
  460. function currencyToDouble($val)
  461. {
  462. $x = str_replace("€", "", $val);
  463. $x = str_replace(".", "", $x);
  464. $x = str_replace(",", ".", $x);
  465. return floatval(trim($x));
  466. }
  467. public function addRow()
  468. {
  469. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  470. $this->emit('load-select');
  471. }
  472. public function delRow($idx)
  473. {
  474. unset($this->rows[$idx]);
  475. // $this->emit('load-select');
  476. }
  477. public function addPeriod($idx)
  478. {
  479. $x = sizeof($this->rows[$idx]['when']) - 1;
  480. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  481. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  482. //$this->rows[$idx]['when'][] = array('month' => date("n"), 'year' => date("Y"), 'period' => '');
  483. }
  484. public function delPeriod($idx, $xxx)
  485. {
  486. array_splice($this->rows[$idx]['when'], $xxx, 1);
  487. // $this->emit('load-select');
  488. }
  489. public function getTotal()
  490. {
  491. $total = 0.00;
  492. foreach($this->rows as $r)
  493. {
  494. $total += $this->currencyToDouble($r["amount"] != null ? $r["amount"] : 0);
  495. }
  496. return formatPrice($total);
  497. // $this->emit('load-select');
  498. }
  499. public function setCausal($id, $idx)
  500. {
  501. $this->rows[$idx]["causal_id"] = $id;
  502. }
  503. public function multiPeriod()
  504. {
  505. $this->multiP = true;
  506. }
  507. public function multiPeriodCreate($idx)
  508. {
  509. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  510. foreach ($period as $dt) {
  511. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  512. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  513. }
  514. $this->multiP = false;
  515. }
  516. public function multiPeriodCancel()
  517. {
  518. $this->multiP = false;
  519. }
  520. }