RecordOUT.php 18 KB

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