RecordIN.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class RecordIN extends Component
  5. {
  6. protected $listeners = ['setCausal' => 'setCausal'];
  7. public $sortField ='date';
  8. public $sortAsc = false;
  9. public $isDuplicate = false;
  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. $virtual,
  29. $note,
  30. $commercial, $update = false, $add = false;
  31. public $currentReceip;
  32. public $filterMember = 0, $filterPaymentMethod = 0, $filterCausals = [], $filterFrom = '', $filterTo = '', $filterCommercial = 0;
  33. public $hasFilter = false;
  34. public $total = 0;
  35. public $selectedFilter = '';
  36. public $multipleIds = [];
  37. public $multipleAction = '';
  38. public $first = true;
  39. public $selectId = 0;
  40. public $refreshAfter = 0;
  41. public $canSave = true;
  42. public $newMemberFirstName = '';
  43. public $newMemberLastName = '';
  44. public $newMemberFiscalCode = '';
  45. public $newMemberFiscalCodeExist = false;
  46. public $causals = array();
  47. public $payments = array();
  48. public $members = array();
  49. public $rows = array();
  50. protected $rules = [
  51. 'member_id' => 'required',
  52. 'payment_method_id' => 'required',
  53. 'rows.*.causal_id' => 'required',
  54. 'rows.*.amount' => 'required'
  55. ];
  56. protected $messages = [
  57. 'member_id.required' => 'La persona è obbligatorio',
  58. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  59. 'causal_id.required' => 'La causale è obbligatoria',
  60. 'amount.required' => 'L\'importo è obbligatorio',
  61. ];
  62. public function updatedMemberId() {
  63. $this->emit('refresh');
  64. if ($this->member_id > 0)
  65. {
  66. $member = \App\Models\Member::findOrFail($this->member_id);
  67. $this->virtual = $member->getMoney();
  68. $this->newMemberFirstName = '';
  69. $this->newMemberLastName = '';
  70. $this->newMemberFiscalCode = '';
  71. $this->newMemberFiscalCodeExist = false;
  72. }
  73. }
  74. public function updatedCausalId() {
  75. //$this->emit('refresh');
  76. }
  77. public function updatedDate() {
  78. //$this->emit('refresh');
  79. }
  80. public function hydrate()
  81. {
  82. $this->emit('load-select');
  83. }
  84. /*public function updated() {
  85. $this->emit('refresh');
  86. }*/
  87. public function updatedPaymentMethodId() {
  88. //$this->emit('refresh');
  89. $this->canSave = $this->checkCanSave();
  90. }
  91. public function updatedAmount() {
  92. // $this->emit('refresh');
  93. $this->canSave = $this->checkCanSave();
  94. }
  95. public function checkCanSave()
  96. {
  97. $ret = true;
  98. if ($this->payment_method_id != null)
  99. {
  100. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  101. if ($payment_method->money)
  102. {
  103. $ret = $this->virtual >= $this->currencyToDouble($this->amount);
  104. }
  105. }
  106. return $ret;
  107. }
  108. public function resetFields(){
  109. $this->member_id = null;
  110. $this->supplier_id = null;
  111. $this->payment_method_id = null;
  112. $this->date = date("Y-m-d");
  113. $this->type = 'IN';
  114. $this->newMemberFirstName = '';
  115. $this->newMemberLastName = '';
  116. $this->newMemberFiscalCode = '';
  117. $this->newMemberFiscalCodeExist = false;
  118. $this->currentReceip = null;
  119. $this->rows = array();
  120. $this->rows[] = array('causal_id' => isset($_GET["causalId"]) ? $_GET["causalId"] : null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  121. }
  122. public function getMemberProperty()
  123. {
  124. $ret = null;
  125. if ($this->member_id > 0)
  126. {
  127. $ret = \App\Models\Member::findOrFail($this->member_id);
  128. }
  129. return $ret;
  130. }
  131. public function getCausalProperty()
  132. {
  133. $ret = null;
  134. if ($this->causal_id > 0)
  135. {
  136. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  137. }
  138. return $ret;
  139. }
  140. public function getCausal($causal)
  141. {
  142. $ret = '';
  143. if ($causal > 0)
  144. {
  145. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  146. }
  147. return $ret;
  148. }
  149. function buildTree($records, $parentId = 0) {
  150. $this->causals = array();
  151. foreach ($records as $record)
  152. {
  153. if ($record->parent_id == $parentId)
  154. {
  155. $children = $this->buildTree($record, $record->id);
  156. if ($children) {
  157. $record->children = $children;
  158. }
  159. $this->causals[] = $record;
  160. }
  161. }
  162. return $this->causals;
  163. }
  164. public function getCausale($records, $indentation)
  165. {
  166. foreach($records as $record)
  167. {
  168. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  169. if(count($record->childs))
  170. $this->getCausale($record->childs, $indentation + 1);
  171. }
  172. }
  173. public function mount()
  174. {
  175. $this->causals = array();
  176. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  177. //$this->buildTree(\App\Models\Causal::all(), null);
  178. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  179. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->orderBy('name')->get();
  180. if ($this->first)
  181. {
  182. if (isset($_GET["new"]))
  183. {
  184. $this->refreshAfter = 1;
  185. $this->add();
  186. }
  187. if (isset($_GET["memberId"]))
  188. {
  189. $this->refreshAfter = 1;
  190. $this->member_id = $_GET["memberId"];
  191. }
  192. if (isset($_GET["causalId"]))
  193. {
  194. $this->refreshAfter = 1;
  195. $this->causal_id = $_GET["causalId"];
  196. }
  197. if (isset($_GET["id"]))
  198. {
  199. $this->refreshAfter = 1;
  200. $this->edit($_GET["id"]);
  201. }
  202. }
  203. $this->first = false;
  204. }
  205. public function search()
  206. {
  207. $this->hasFilter = true;
  208. }
  209. public function disableSearch()
  210. {
  211. $this->filterMember = 0;
  212. $this->filterPaymentMethod = 0;
  213. $this->filterCausals = [];
  214. $this->filterTo = '';
  215. $this->filterFrom = '';
  216. $this->filterCommercial = 0;
  217. $this->hasFilter = false;
  218. $this->total = 0;
  219. }
  220. public function render()
  221. {
  222. if ($this->hasFilter)
  223. {
  224. $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  225. /*if ($this->filterCommercial > 0)
  226. {
  227. $datas = $datas->where('commercial', $this->filterCommercial == 1 ? true : false);
  228. }*/
  229. if ($this->filterMember > 0)
  230. {
  231. $datas = $datas->where('member_id', $this->filterMember);
  232. }
  233. if ($this->filterPaymentMethod > 0)
  234. {
  235. $datas = $datas->where('payment_method_id', $this->filterPaymentMethod);
  236. }
  237. /*if (sizeof($this->filterCausals) > 0)
  238. {
  239. $datas = $datas->whereIn('causal_id', $this->filterCausals);
  240. }*/
  241. if ($this->filterFrom != '')
  242. {
  243. $datas = $datas->where('date', '>=', $this->filterFrom);
  244. }
  245. if ($this->filterTo != '')
  246. {
  247. $datas = $datas->where('date', '<=', $this->filterTo);
  248. }
  249. // $this->records = $datas->orderBy('date', 'DESC')->get();
  250. $this->records = $datas->get();
  251. $this->total = 0;
  252. foreach($this->records as $r)
  253. {
  254. foreach($r->rows as $rr)
  255. {
  256. $this->total += $rr->amount;
  257. }
  258. }
  259. //$this->total = $this->records->sum('amount');
  260. }
  261. else
  262. {
  263. if ($this->selectedFilter == '')
  264. {
  265. $this->records = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method')->limit(20)->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
  266. }
  267. else
  268. {
  269. if ($this->selectedFilter == 0)
  270. {
  271. $fromDate = date("Y-m-d");
  272. $toDate = date("Y-m-d");
  273. }
  274. if ($this->selectedFilter == 1)
  275. {
  276. $fromDate = date("Y-m-01");
  277. $toDate = date("Y-m-t");
  278. }
  279. if ($this->selectedFilter == 2)
  280. {
  281. $fromDate = date("Y-01-01");
  282. $toDate = date("Y-12-31");
  283. }
  284. if ($this->selectedFilter == 3)
  285. {
  286. $fromDate = date("2000-01-01");
  287. $toDate = date("Y-12-31");
  288. }
  289. $this->records = \App\Models\Record::where('type', 'IN')->whereBetween('date', [$fromDate, $toDate])->with('member', 'payment_method')->get();
  290. }
  291. }
  292. foreach($this->records as $r)
  293. {
  294. $r->total = $r->getTotal();
  295. $r->person = $r->member ? ($r->member->first_name . " " . $r->member->last_name) : '';
  296. $r->payment = $r->payment_method ? $r->payment_method->name : '';
  297. }
  298. if ($this->sortField != '')
  299. {
  300. if ($this->sortAsc)
  301. $this->records = $this->records->sortBy($this->sortField);
  302. else
  303. $this->records = $this->records->sortByDesc($this->sortField);
  304. }
  305. else
  306. $this->records = $this->records->sortByDesc('id');
  307. return view('livewire.records_in');
  308. }
  309. public function executeMultipleAction(){
  310. if ($this->multipleAction == 'delete')
  311. $this->multipleDelete();
  312. }
  313. public function add()
  314. {
  315. $this->emit('load-select');
  316. //if ($this->hasFilter)
  317. $this->emit('hide-search');
  318. $this->resetFields();
  319. $this->add = true;
  320. $this->update = false;
  321. }
  322. public function store($generate)
  323. {
  324. $this->emit('refresh');
  325. $rules = [
  326. 'payment_method_id' => 'required',
  327. 'rows.*.causal_id' => 'required',
  328. 'rows.*.amount' => 'required'
  329. ];
  330. $f = false;
  331. foreach($this->rows as $row)
  332. {
  333. if (!$row["commercial"]) $f = true;
  334. }
  335. if ($f)
  336. $rules["member_id"] = 'required';
  337. $this->validate($rules);
  338. try {
  339. $record = \App\Models\Record::create([
  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. $this->dataId = $record->id;
  353. // Inserisco le righe
  354. $tot = 0;
  355. foreach($this->rows as $row)
  356. {
  357. foreach($row["when"] as $x => $y)
  358. {
  359. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  360. }
  361. \App\Models\RecordRow::create([
  362. 'record_id' => $this->dataId,
  363. 'causal_id' => $row["causal_id"],
  364. 'note' => $row["note"],
  365. 'amount' => $this->currencyToDouble($row["amount"]),
  366. 'commercial' => $row["commercial"],
  367. 'when' => json_encode($row["when"])
  368. ]);
  369. $tot += $this->currencyToDouble($row["amount"]);
  370. }
  371. $record->amount = $tot;
  372. $record->save();
  373. if ($generate)
  374. $this->createReceipt();
  375. session()->flash('success','Movimento creato');
  376. $this->resetFields();
  377. $this->add = false;
  378. $this->isDuplicate = false;
  379. } catch (\Exception $ex) {
  380. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  381. }
  382. }
  383. public function duplicate($id){
  384. $record = \App\Models\Record::findOrFail($id);
  385. $newRecord = $record->replicate();
  386. $newRecord->save();
  387. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  388. foreach($rows as $r)
  389. {
  390. $newRow = $r->replicate();
  391. $newRow->record_id = $newRecord->id;
  392. $newRow->save();
  393. }
  394. $this->isDuplicate = true;
  395. $this->edit($newRecord->id);
  396. }
  397. public function edit($id){
  398. //if ($this->hasFilter)
  399. $this->emit('hide-search');
  400. $this->emit('load-select');
  401. try {
  402. $record = \App\Models\Record::findOrFail($id);
  403. if( !$record) {
  404. session()->flash('error','Movimento non trovato');
  405. } else {
  406. $this->member_id = $record->member_id;
  407. $this->supplier_id = $record->supplier_id;
  408. // $this->causal_id = $record->causal_id;
  409. $this->payment_method_id = $record->payment_method_id;
  410. $this->date = date("Y-m-d", strtotime($record->date));
  411. // $this->month = $record->month;
  412. // $this->year = $record->year;
  413. // $this->note = $record->note;
  414. $this->type = $record->type;
  415. // $this->amount = formatPrice($record->amount);
  416. // $this->commercial = $record->commercial;
  417. $this->dataId = $record->id;
  418. $this->update = true;
  419. $this->add = false;
  420. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount')->get()->toArray();
  421. foreach($this->rows as $i => $r)
  422. {
  423. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  424. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  425. }
  426. $exist = \App\Models\Receipt::where('record_id', $id)->first();
  427. if ($exist != null)
  428. $this->currentReceip = $exist;
  429. }
  430. } catch (\Exception $ex) {
  431. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  432. }
  433. }
  434. public function update($generate)
  435. {
  436. $this->emit('refresh');
  437. $rules = [
  438. 'payment_method_id' => 'required',
  439. 'rows.*.causal_id' => 'required',
  440. 'rows.*.amount' => 'required'
  441. ];
  442. $f = false;
  443. foreach($this->rows as $row)
  444. {
  445. if (!$row["commercial"]) $f = true;
  446. }
  447. if ($f)
  448. $rules["member_id"] = 'required';
  449. $this->validate($rules);
  450. try {
  451. \App\Models\Record::whereId($this->dataId)->update([
  452. 'member_id' => $this->member_id,
  453. 'supplier_id' => $this->supplier_id,
  454. // 'causal_id' => $this->causal_id,
  455. 'payment_method_id' => $this->payment_method_id,
  456. 'date' => date("Y-m-d", strtotime($this->date)),
  457. // 'month' => $this->month,
  458. // 'year' => $this->year,
  459. // 'note' => $this->note,
  460. 'type' => $this->type,
  461. // 'amount' => $this->currencyToDouble($this->amount),
  462. //'commercial' => $this->commercial,
  463. ]);
  464. $tot = 0;
  465. // Elimino le righe
  466. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  467. // Inserisco le righe
  468. foreach($this->rows as $row)
  469. {
  470. foreach($row["when"] as $x => $y)
  471. {
  472. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  473. }
  474. \App\Models\RecordRow::create([
  475. 'record_id' => $this->dataId,
  476. 'causal_id' => $row["causal_id"],
  477. 'note' => $row["note"],
  478. 'amount' => $this->currencyToDouble($row["amount"]),
  479. 'commercial' => $row["commercial"],
  480. 'when' => json_encode($row["when"])
  481. ]);
  482. $tot += $this->currencyToDouble($row["amount"]);
  483. }
  484. $rec = \App\Models\Record::findOrFail($this->dataId);
  485. $rec->amount = $tot;
  486. $rec->save();
  487. if ($generate)
  488. $this->createReceipt();
  489. session()->flash('success','Movimento aggiornato');
  490. $this->resetFields();
  491. $this->update = false;
  492. $this->isDuplicate = false;
  493. } catch (\Exception $ex) {
  494. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  495. }
  496. }
  497. public function cancel()
  498. {
  499. // Se arrivo da duplica elimino
  500. if ($this->isDuplicate)
  501. {
  502. try{
  503. \App\Models\Record::find($this->dataId)->delete();
  504. session()->flash('success',"Movimento eliminato");
  505. }catch(\Exception $e){
  506. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  507. }
  508. }
  509. $this->isDuplicate = false;
  510. $this->add = false;
  511. $this->update = false;
  512. $this->resetFields();
  513. }
  514. public function delete($id)
  515. {
  516. try{
  517. \App\Models\Record::find($id)->delete();
  518. session()->flash('success',"Movimento eliminato");
  519. }catch(\Exception $e){
  520. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  521. }
  522. }
  523. public function multipleDelete()
  524. {
  525. try{
  526. foreach($this->multipleIds as $id)
  527. {
  528. \App\Models\Record::find($id)->delete();
  529. }
  530. }catch(\Exception $e){
  531. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  532. }
  533. $this->multipleAction = '';
  534. }
  535. public function createMember()
  536. {
  537. $this->newMemberFiscalCodeExist = false;
  538. $this->validate([
  539. // 'newMemberFiscalCode'=>'required|max:16',
  540. 'newMemberFirstName'=>'required',
  541. 'newMemberLastName'=>'required'
  542. ]);
  543. // Check fiscal code exist
  544. $exist = false;
  545. if ($this->newMemberFiscalCode != '')
  546. {
  547. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  548. $exist = $check->count() > 0;
  549. }
  550. if (!$exist)
  551. {
  552. $member = \App\Models\Member::create([
  553. 'first_name' => $this->newMemberFirstName,
  554. 'last_name' => $this->newMemberLastName,
  555. 'fiscal_code' => $this->newMemberFiscalCode,
  556. 'status' => true
  557. ]);
  558. $this->member_id = $member->id;
  559. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get();
  560. // $this->emit('reloadMembers');
  561. $this->emit('refresh');
  562. $this->newMemberFirstName = '';
  563. $this->newMemberLastName = '';
  564. $this->newMemberFiscalCode = '';
  565. $this->emit('saved');
  566. //$this->validate();
  567. $this->selectId++;
  568. }
  569. else
  570. {
  571. $this->newMemberFiscalCodeExist = true;
  572. }
  573. }
  574. public function createReceipt()
  575. {
  576. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first();
  577. if ($receipt != null)
  578. {
  579. $receipt->update([
  580. 'member_id' => $this->member_id,
  581. 'supplier_id' => $this->supplier_id,
  582. // 'causal_id' => $this->causal_id,
  583. 'payment_method_id' => $this->payment_method_id,
  584. 'date' => date("Y-m-d", strtotime($this->date)),
  585. //'month' => $this->month,
  586. //'year' => $this->year,
  587. 'type' => $this->type,
  588. //'amount' => $this->currencyToDouble($this->amount),
  589. //'commercial' => $this->commercial,
  590. 'status' => 1,
  591. ]);
  592. \App\Models\ReceiptRow::where('receip_id', $receipt->id)->delete();
  593. foreach($this->rows as $row)
  594. {
  595. \App\Models\ReceiptRow::create([
  596. 'receip_id' => $receipt->id,
  597. 'causal_id' => $row["causal_id"],
  598. 'note' => $row["note"],
  599. 'amount' => $this->currencyToDouble($row["amount"]),
  600. 'commercial' => $row["commercial"],
  601. 'when' => json_encode($row["when"])
  602. ]);
  603. }
  604. }
  605. else
  606. {
  607. $number = 1;
  608. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  609. if ($exist != null)
  610. $number = $exist->number + 1;
  611. $receipt = \App\Models\Receipt::create([
  612. 'record_id' => $this->dataId,
  613. 'member_id' => $this->member_id,
  614. 'supplier_id' => $this->supplier_id,
  615. //'causal_id' => $this->causal_id,
  616. 'payment_method_id' => $this->payment_method_id,
  617. 'number' => $number,
  618. 'date' => $this->date,
  619. //'month' => $this->month,
  620. 'year' => date("Y"),
  621. 'type' => $this->type,
  622. //'amount' => $this->currencyToDouble($this->amount),
  623. //'commercial' => $this->commercial,
  624. 'status' => 1,
  625. ]);
  626. foreach($this->rows as $row)
  627. {
  628. \App\Models\ReceiptRow::create([
  629. 'receip_id' => $receipt->id,
  630. 'causal_id' => $row["causal_id"],
  631. 'note' => $row["note"],
  632. 'amount' => $this->currencyToDouble($row["amount"]),
  633. 'commercial' => $row["commercial"],
  634. 'when' => json_encode($row["when"])
  635. ]);
  636. }
  637. $this->currentReceip = $receipt;
  638. }
  639. /*
  640. $data = [
  641. 'member' => $receipt->id
  642. ];
  643. $pdf = \PDF::loadView('partials.pdf_generate_connections', $data)->setPaper('a4', 'landscape')->output(); //
  644. return response()->streamDownload(
  645. fn() => print($pdf), 'export_protocol.pdf'
  646. );
  647. */
  648. }
  649. public function removeReceipt()
  650. {
  651. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  652. $receipt->status = 99;
  653. $receipt->save();
  654. $this->currentReceip = $receipt;
  655. }
  656. function currencyToDouble($val)
  657. {
  658. $x = str_replace("€", "", $val);
  659. $x = str_replace(".", "", $x);
  660. $x = str_replace(",", ".", $x);
  661. return trim($x);
  662. }
  663. public function addRow()
  664. {
  665. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  666. $this->emit('load-select');
  667. }
  668. public function delRow($idx)
  669. {
  670. unset($this->rows[$idx]);
  671. // $this->emit('load-select');
  672. }
  673. public function addPeriod($idx)
  674. {
  675. $this->rows[$idx]['when'][] = array('month' => date("n"), 'year' => date("Y"), 'period' => '');
  676. }
  677. public function delPeriod($idx, $xxx)
  678. {
  679. unset($this->rows[$idx]['when'][$xxx]);
  680. // $this->emit('load-select');
  681. }
  682. public function getTotal()
  683. {
  684. $total = 0.00;
  685. foreach($this->rows as $r)
  686. {
  687. if ($r["amount"] != null && $r["amount"] != "")
  688. $total += $this->currencyToDouble($r["amount"]);
  689. }
  690. return formatPrice($total);
  691. // $this->emit('load-select');
  692. }
  693. public function setCausal($id, $idx)
  694. {
  695. $this->rows[$idx]["causal_id"] = $id;
  696. }
  697. }