RecordOUT.php 18 KB

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