RecordOUT.php 17 KB

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