RecordOUT.php 19 KB

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