RecordOUT.php 17 KB

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