RecordOUT.php 19 KB

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