RecordIN.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. use Barryvdh\DomPDF\Facade\Pdf;
  6. class RecordIN extends Component
  7. {
  8. use WithPagination;
  9. protected $paginationTheme = 'bootstrap';
  10. protected $listeners = ['setCausal' => 'setCausal'];
  11. public $sortField ='date';
  12. public $sortAsc = false;
  13. public $typeIN = "IN";
  14. public $isDuplicate = false;
  15. public $multiP = false;
  16. public $multiMonthTo = 0;
  17. public $multiYearTo = 0;
  18. public $multiMonthFrom = 0;
  19. public $multiYearFrom = 0;
  20. public $createSubscription = 0;
  21. public function sortBy($field)
  22. {
  23. if($this->sortField === $field)
  24. {
  25. $this->sortAsc = ! $this->sortAsc;
  26. } else {
  27. $this->sortAsc = true;
  28. }
  29. $this->sortField = $field;
  30. }
  31. public $courseId = 0;
  32. public $months = array();
  33. public $records, $dataId, $member_id, $supplier_id,
  34. $causal_id,
  35. $payment_method_id,
  36. $date,
  37. $month,
  38. $year,
  39. $type,
  40. $amount,
  41. $vat,
  42. $virtual,
  43. $note,
  44. $parent,
  45. $commercial, $update = false, $add = false;
  46. public $currentReceip;
  47. public $filterMember = 0, $filterPaymentMethod = 0, $filterCausals = [], $filterFrom = '', $filterTo = '', $filterCommercial = 0;
  48. public $hasFilter = false;
  49. public $total = 0;
  50. public $selectedFilter = '';
  51. public $multipleIds = [];
  52. public $multipleAction = '';
  53. public $first = true;
  54. public $selectId = 0;
  55. public $refreshAfter = 0;
  56. public $canSave = true;
  57. public $newMemberFirstName = '';
  58. public $newMemberLastName = '';
  59. public $newMemberFiscalCode = '';
  60. public $newMemberFiscalCodeExist = false;
  61. public $causals = array();
  62. public $payments = array();
  63. public $members = array();
  64. public $vats = array();
  65. public $rows = array();
  66. protected $rules = [
  67. 'member_id' => 'required',
  68. 'payment_method_id' => 'required',
  69. 'rows.*.causal_id' => 'required',
  70. 'rows.*.amount' => 'required'
  71. ];
  72. protected $messages = [
  73. 'member_id.required' => 'La persona è obbligatorio',
  74. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  75. 'rows.*.causal_id.required' => 'La causale è obbligatoria',
  76. 'rows.*.amount.required' => 'L\'importo è obbligatorio',
  77. ];
  78. public function updatedMemberId() {
  79. $this->emit('refresh');
  80. if ($this->member_id > 0)
  81. {
  82. $member = \App\Models\Member::findOrFail($this->member_id);
  83. $this->virtual = $member->getMoney();
  84. $this->newMemberFirstName = '';
  85. $this->newMemberLastName = '';
  86. $this->newMemberFiscalCode = '';
  87. $this->newMemberFiscalCodeExist = false;
  88. }
  89. }
  90. public function updatedCausalId() {
  91. //$this->emit('refresh');
  92. }
  93. public function updatedDate() {
  94. //$this->emit('refresh');
  95. }
  96. public function hydrate()
  97. {
  98. $this->emit('load-select');
  99. }
  100. /*public function updated() {
  101. $this->emit('refresh');
  102. }*/
  103. public function updatedPaymentMethodId() {
  104. //$this->emit('refresh');
  105. $this->canSave = $this->checkCanSave();
  106. }
  107. public function updatedAmount() {
  108. // $this->emit('refresh');
  109. $this->canSave = $this->checkCanSave();
  110. }
  111. public function checkCanSave()
  112. {
  113. $ret = true;
  114. if ($this->payment_method_id != null)
  115. {
  116. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  117. if ($payment_method->money)
  118. {
  119. $ret = $this->virtual >= $this->currencyToDouble($this->amount);
  120. }
  121. }
  122. return $ret;
  123. }
  124. public function resetFields(){
  125. $this->member_id = null;
  126. $this->supplier_id = null;
  127. $this->payment_method_id = null;
  128. $this->commercial = 0;
  129. $this->date = date("Y-m-d");
  130. $this->type = 'IN';
  131. $this->newMemberFirstName = '';
  132. $this->newMemberLastName = '';
  133. $this->newMemberFiscalCode = '';
  134. $this->newMemberFiscalCodeExist = false;
  135. $this->currentReceip = null;
  136. $this->parent = '';
  137. $this->courseId = 0;
  138. $this->months = array();
  139. $this->rows = array();
  140. $this->rows[] = array('causal_id' => isset($_GET["causalId"]) ? $_GET["causalId"] : null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  141. $this->emit('load-data-table');
  142. }
  143. public function getMemberProperty()
  144. {
  145. $ret = null;
  146. if ($this->member_id > 0)
  147. {
  148. $ret = \App\Models\Member::findOrFail($this->member_id);
  149. }
  150. return $ret;
  151. }
  152. public function getCausalProperty()
  153. {
  154. $ret = null;
  155. if ($this->causal_id > 0)
  156. {
  157. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  158. }
  159. return $ret;
  160. }
  161. public function getCausal($causal)
  162. {
  163. $ret = '';
  164. if ($causal > 0)
  165. {
  166. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  167. }
  168. return $ret;
  169. }
  170. function buildTree($records, $parentId = 0) {
  171. $this->causals = array();
  172. foreach ($records as $record)
  173. {
  174. if ($record->parent_id == $parentId)
  175. {
  176. $children = $this->buildTree($record, $record->id);
  177. if ($children) {
  178. $record->children = $children;
  179. }
  180. $this->causals[] = $record;
  181. }
  182. }
  183. return $this->causals;
  184. }
  185. public function getCausale($records, $indentation)
  186. {
  187. foreach($records as $record)
  188. {
  189. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  190. if(count($record->childs))
  191. $this->getCausale($record->childs, $indentation + 1);
  192. }
  193. }
  194. public function mount()
  195. {
  196. $this->causals = array();
  197. $this->multiMonthFrom = date("n");
  198. $this->multiYearFrom = date("Y");
  199. $this->multiMonthTo = date("n");
  200. $this->multiYearTo = date("Y");
  201. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  202. //$this->buildTree(\App\Models\Causal::all(), null);
  203. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  204. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->where('enabled', true)->orderBy('name')->get();
  205. $this->vats = \App\Models\Vat::select('id', 'name', 'value')->orderBy('value')->get();
  206. if ($this->first)
  207. {
  208. if (isset($_GET["new"]))
  209. {
  210. $this->refreshAfter = 1;
  211. $this->add();
  212. }
  213. if (isset($_GET["memberId"]))
  214. {
  215. $this->refreshAfter = 1;
  216. $this->member_id = $_GET["memberId"];
  217. }
  218. if (isset($_GET["causalId"]))
  219. {
  220. $this->refreshAfter = 1;
  221. $this->causal_id = $_GET["causalId"];
  222. }
  223. if (isset($_GET["id"]))
  224. {
  225. $this->refreshAfter = 1;
  226. $this->edit($_GET["id"]);
  227. }
  228. $count = 0;
  229. if (isset($_GET["courseId"]))
  230. {
  231. $this->courseId = $_GET["courseId"];
  232. $mc = \App\Models\MemberCourse::findOrFail($this->courseId);
  233. $course = \App\Models\Course::findOrFail($mc->course_id);
  234. }
  235. if (isset($_GET["months"]))
  236. {
  237. $price = $_GET["price"] / 100 * 100;
  238. $this->refreshAfter = 1;
  239. $desc = "Pagamento";
  240. $months = explode("|", $_GET["months"]);
  241. $this->months = $months;
  242. $this->rows[0]["amount"] = formatPrice($price * sizeof($months));
  243. foreach($months as $idx => $m)
  244. {
  245. $this->rows[0]["when"][$idx]["month"] = $m;
  246. $this->rows[0]["when"][$idx]["year"] = date("Y");
  247. $desc .= " " . $this->getMonth($m);
  248. }
  249. $this->rows[0]["note"] = $desc;
  250. $count += 1;
  251. }
  252. if (isset($_GET["createSubscription"]) && $_GET["createSubscription"] == 1)
  253. {
  254. $this->createSubscription = 1;
  255. $this->courseId = $_GET["courseId"];
  256. $price = $_GET["subscription_price"] / 100 * 100;
  257. $this->refreshAfter = 1;
  258. if ($count == 1)
  259. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  260. $this->rows[$count]["causal_id"] = null;
  261. $this->rows[$count]["amount"] = formatPrice($price);
  262. $this->rows[$count]["note"] = "Pagamento iscrizione " . $course->name;
  263. if (isset($_GET["subCausalId"]))
  264. {
  265. $this->refreshAfter = 1;
  266. $this->rows[$count]["causal_id"] = $_GET["subCausalId"];
  267. }
  268. }
  269. }
  270. $this->first = false;
  271. }
  272. public function search()
  273. {
  274. $this->hasFilter = true;
  275. }
  276. public function disableSearch()
  277. {
  278. $this->filterMember = 0;
  279. $this->filterPaymentMethod = 0;
  280. $this->filterCausals = [];
  281. $this->filterTo = '';
  282. $this->filterFrom = '';
  283. $this->filterCommercial = 0;
  284. $this->hasFilter = false;
  285. $this->total = 0;
  286. }
  287. public function render()
  288. {
  289. $datas = [];
  290. return view('livewire.records_in', ['datas' => $datas]);
  291. }
  292. public function executeMultipleAction(){
  293. if ($this->multipleAction == 'delete')
  294. $this->multipleDelete();
  295. }
  296. public function add()
  297. {
  298. $this->emit('load-select');
  299. //if ($this->hasFilter)
  300. $this->emit('hide-search');
  301. $this->resetFields();
  302. $this->add = true;
  303. $this->update = false;
  304. $this->emit('setEdit', true);
  305. }
  306. public function store($generate)
  307. {
  308. $this->emit('refresh');
  309. $rules = [
  310. 'payment_method_id' => 'required',
  311. 'rows.*.causal_id' => 'required',
  312. 'rows.*.amount' => 'required'
  313. ];
  314. /*$f = false;
  315. foreach($this->rows as $row)
  316. {
  317. if ($row["commercial"]) $f = true;
  318. }
  319. if ($f)
  320. $rules["member_id"] = 'required';*/
  321. if($this->commercial)
  322. $rules["member_id"] = 'required';
  323. //dd($this->getErrorBag());
  324. $this->validate($rules);
  325. //dd("£ASDSDFS");
  326. try {
  327. $record = \App\Models\Record::create([
  328. 'member_id' => $this->member_id,
  329. 'supplier_id' => $this->supplier_id,
  330. 'payment_method_id' => $this->payment_method_id,
  331. 'commercial' => $this->commercial,
  332. 'date' => $this->date,
  333. 'type' => $this->type,
  334. ]);
  335. $this->dataId = $record->id;
  336. // Inserisco le righe
  337. $tot = 0;
  338. $vat = 0;
  339. foreach($this->rows as $row)
  340. {
  341. foreach($row["when"] as $x => $y)
  342. {
  343. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  344. }
  345. \App\Models\RecordRow::create([
  346. 'record_id' => $this->dataId,
  347. 'causal_id' => $row["causal_id"],
  348. 'note' => $row["note"],
  349. 'amount' => $this->currencyToDouble($row["amount"]),
  350. 'vat_id' => $row["vat_id"],
  351. 'commercial' => $this->commercial,
  352. 'when' => json_encode($row["when"])
  353. ]);
  354. $tot += $this->currencyToDouble($row["amount"]);
  355. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  356. }
  357. $record->amount = $tot;
  358. $record->save();
  359. // se sto pagando un corso, aggiorno lo status dei mesi
  360. if ($this->courseId > 0)
  361. {
  362. $record->months = json_encode($this->months);
  363. $record->member_course_id = $this->courseId;
  364. $record->save();
  365. $c = \App\Models\MemberCourse::findOrFail($this->courseId);
  366. $xxx = json_decode($c->months);
  367. foreach($xxx as $idx => $mm)
  368. {
  369. if (in_array($mm->m, $this->months))
  370. {
  371. $xxx[$idx]->status = 1;
  372. }
  373. }
  374. $c->months = json_encode($xxx);
  375. if ($this->createSubscription == 1)
  376. $c->subscribed = true;
  377. $c->save();
  378. }
  379. if ($generate)
  380. $this->createReceipt();
  381. session()->flash('success','Movimento creato');
  382. $this->resetFields();
  383. $this->add = false;
  384. $this->isDuplicate = false;
  385. $this->emit('setEdit', false);
  386. } catch (\Exception $ex) {
  387. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  388. }
  389. }
  390. public function duplicate($id){
  391. $record = \App\Models\Record::findOrFail($id);
  392. $newRecord = $record->replicate();
  393. $newRecord->save();
  394. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  395. foreach($rows as $r)
  396. {
  397. $newRow = $r->replicate();
  398. $newRow->record_id = $newRecord->id;
  399. $newRow->save();
  400. }
  401. $this->isDuplicate = true;
  402. $this->edit($newRecord->id);
  403. }
  404. public function edit($id){
  405. $this->emit('setEdit', true);
  406. //if ($this->hasFilter)
  407. $this->emit('hide-search');
  408. $this->emit('load-select');
  409. try {
  410. $record = \App\Models\Record::findOrFail($id);
  411. if( !$record) {
  412. session()->flash('error','Movimento non trovato');
  413. } else {
  414. $this->member_id = $record->member_id;
  415. $this->supplier_id = $record->supplier_id;
  416. $this->payment_method_id = $record->payment_method_id;
  417. $this->commercial = $record->commercial;
  418. $this->date = date("Y-m-d", strtotime($record->date));
  419. $this->type = $record->type;
  420. $this->dataId = $record->id;
  421. $this->update = true;
  422. $this->add = false;
  423. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  424. foreach($this->rows as $i => $r)
  425. {
  426. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  427. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  428. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  429. }
  430. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  431. if ($exist != null)
  432. {
  433. $this->currentReceip = $exist;
  434. $this->parent = $this->currentReceip->parent;
  435. }
  436. }
  437. } catch (\Exception $ex) {
  438. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  439. }
  440. }
  441. public function update($generate)
  442. {
  443. $this->emit('refresh');
  444. $rules = [
  445. 'payment_method_id' => 'required',
  446. 'rows.*.causal_id' => 'required',
  447. 'rows.*.amount' => 'required'
  448. ];
  449. /*$f = false;
  450. foreach($this->rows as $row)
  451. {
  452. if ($row["commercial"]) $f = true;
  453. }
  454. if ($f)
  455. $rules["member_id"] = 'required';*/
  456. if($this->commercial)
  457. $rules["member_id"] = 'required';
  458. $this->validate($rules);
  459. try {
  460. \App\Models\Record::whereId($this->dataId)->update([
  461. 'member_id' => $this->member_id,
  462. 'supplier_id' => $this->supplier_id,
  463. 'payment_method_id' => $this->payment_method_id,
  464. 'commercial' => $this->commercial,
  465. 'date' => date("Y-m-d", strtotime($this->date)),
  466. 'type' => $this->type,
  467. ]);
  468. $tot = 0;
  469. $vat = 0;
  470. // Elimino le righe
  471. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  472. // Inserisco le righe
  473. foreach($this->rows as $row)
  474. {
  475. foreach($row["when"] as $x => $y)
  476. {
  477. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  478. }
  479. \App\Models\RecordRow::create([
  480. 'record_id' => $this->dataId,
  481. 'causal_id' => $row["causal_id"],
  482. 'note' => $row["note"],
  483. 'vat_id' => $row["vat_id"],
  484. 'amount' => $this->currencyToDouble($row["amount"]),
  485. 'commercial' => $this->commercial,
  486. 'when' => json_encode($row["when"])
  487. ]);
  488. $tot += $this->currencyToDouble($row["amount"]);
  489. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  490. }
  491. $rec = \App\Models\Record::findOrFail($this->dataId);
  492. $rec->amount = $tot;
  493. $rec->save();
  494. if ($generate)
  495. $this->createReceipt();
  496. session()->flash('success','Movimento aggiornato');
  497. $this->resetFields();
  498. $this->update = false;
  499. $this->isDuplicate = false;
  500. $this->emit('setEdit', false);
  501. } catch (\Exception $ex) {
  502. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  503. }
  504. }
  505. public function cancel()
  506. {
  507. // Se arrivo da duplica elimino
  508. if ($this->isDuplicate)
  509. {
  510. try{
  511. \App\Models\Record::find($this->dataId)->delete();
  512. session()->flash('success',"Movimento eliminato");
  513. }catch(\Exception $e){
  514. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  515. }
  516. }
  517. $this->isDuplicate = false;
  518. $this->add = false;
  519. $this->update = false;
  520. $this->resetFields();
  521. $this->emit('setEdit', false);
  522. }
  523. public function delete($id)
  524. {
  525. try{
  526. $r = \App\Models\Record::find($id);
  527. if ($r->member_course_id > 0)
  528. {
  529. $months = json_decode($r->months);
  530. $c = \App\Models\MemberCourse::findOrFail($r->member_course_id);
  531. $xxx = json_decode($c->months);
  532. foreach($xxx as $idx => $mm)
  533. {
  534. if (in_array($mm->m, $months))
  535. {
  536. $xxx[$idx]->status = "";
  537. }
  538. }
  539. $c->months = json_encode($xxx);
  540. $c->save();
  541. }
  542. $r->delete();
  543. session()->flash('success',"Movimento eliminato");
  544. }catch(\Exception $e){
  545. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  546. }
  547. }
  548. public function multipleDelete()
  549. {
  550. try{
  551. foreach($this->multipleIds as $id)
  552. {
  553. \App\Models\Record::find($id)->delete();
  554. }
  555. }catch(\Exception $e){
  556. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  557. }
  558. $this->multipleAction = '';
  559. }
  560. public function createMember()
  561. {
  562. $this->newMemberFiscalCodeExist = false;
  563. $this->validate([
  564. // 'newMemberFiscalCode'=>'required|max:16',
  565. 'newMemberFirstName'=>'required',
  566. 'newMemberLastName'=>'required'
  567. ]);
  568. // Check fiscal code exist
  569. $exist = false;
  570. if ($this->newMemberFiscalCode != '')
  571. {
  572. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  573. $exist = $check->count() > 0;
  574. }
  575. if (!$exist)
  576. {
  577. $member = \App\Models\Member::create([
  578. 'first_name' => $this->newMemberFirstName,
  579. 'last_name' => $this->newMemberLastName,
  580. 'fiscal_code' => $this->newMemberFiscalCode,
  581. 'status' => true
  582. ]);
  583. $this->member_id = $member->id;
  584. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get();
  585. // $this->emit('reloadMembers');
  586. $this->emit('refresh');
  587. $this->newMemberFirstName = '';
  588. $this->newMemberLastName = '';
  589. $this->newMemberFiscalCode = '';
  590. $this->emit('saved');
  591. //$this->validate();
  592. $this->selectId++;
  593. }
  594. else
  595. {
  596. $this->newMemberFiscalCodeExist = true;
  597. }
  598. }
  599. public function createReceipt()
  600. {
  601. $create = false;
  602. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  603. if ($receipt != null)
  604. {
  605. // Controllo lo stato, se 99 ne genero una nuova
  606. if ($receipt->status == 99)
  607. {
  608. $create = true;
  609. }
  610. }
  611. else
  612. {
  613. $create = true;
  614. }
  615. if ($create)
  616. {
  617. $number = 1;
  618. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  619. if ($exist != null)
  620. $number = $exist->number + 1;
  621. $receipt = \App\Models\Receipt::create([
  622. 'record_id' => $this->dataId,
  623. 'member_id' => $this->member_id,
  624. 'supplier_id' => $this->supplier_id,
  625. 'payment_method_id' => $this->payment_method_id,
  626. 'number' => $number,
  627. 'date' => $this->date,
  628. 'year' => date("Y"),
  629. 'type' => $this->type,
  630. 'parent' => $this->parent,
  631. 'status' => 1,
  632. ]);
  633. foreach($this->rows as $row)
  634. {
  635. \App\Models\ReceiptRow::create([
  636. 'receip_id' => $receipt->id,
  637. 'causal_id' => $row["causal_id"],
  638. 'note' => $row["note"],
  639. 'vat_id' => $row["vat_id"],
  640. 'amount' => $this->currencyToDouble($row["amount"]),
  641. 'commercial' => $row["commercial"],
  642. 'when' => json_encode($row["when"])
  643. ]);
  644. }
  645. $this->currentReceip = $receipt;
  646. }
  647. }
  648. public function removeReceipt()
  649. {
  650. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  651. $receipt->status = 99;
  652. $receipt->save();
  653. $this->currentReceip = $receipt;
  654. }
  655. function currencyToDouble($val)
  656. {
  657. $x = str_replace("€", "", $val);
  658. $x = str_replace(".", "", $x);
  659. $x = str_replace(",", ".", $x);
  660. return floatval(trim($x));
  661. }
  662. public function addRow()
  663. {
  664. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  665. $this->emit('load-select');
  666. }
  667. public function delRow($idx)
  668. {
  669. unset($this->rows[$idx]);
  670. // $this->emit('load-select');
  671. }
  672. public function addPeriod($idx)
  673. {
  674. $x = sizeof($this->rows[$idx]['when']) - 1;
  675. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  676. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  677. }
  678. public function delPeriod($idx, $xxx)
  679. {
  680. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  681. //unset($this->rows[$idx]['when'][$xxx]);
  682. // $this->emit('load-select');
  683. }
  684. public function getTotal()
  685. {
  686. $total = 0.00;
  687. foreach($this->rows as $r)
  688. {
  689. if ($r["amount"] != null && $r["amount"] != "")
  690. {
  691. $total += $this->currencyToDouble($r["amount"]);
  692. if ($r["vat_id"] > 0)
  693. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  694. }
  695. }
  696. return formatPrice($total);
  697. // $this->emit('load-select');
  698. }
  699. public function getPrice()
  700. {
  701. $total = 0.00;
  702. foreach($this->rows as $r)
  703. {
  704. if ($r["amount"] != null && $r["amount"] != "")
  705. $total += $this->currencyToDouble($r["amount"]);
  706. }
  707. return formatPrice($total);
  708. // $this->emit('load-select');
  709. }
  710. public function getVat()
  711. {
  712. $total = 0.00;
  713. foreach($this->rows as $r)
  714. {
  715. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  716. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  717. }
  718. return formatPrice($total);
  719. // $this->emit('load-select');
  720. }
  721. public function getVats()
  722. {
  723. $vats = array();
  724. foreach($this->rows as $r)
  725. {
  726. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  727. {
  728. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  729. $vatName = "";
  730. foreach($this->vats as $v)
  731. {
  732. if ($v->id == $r["vat_id"])
  733. $vatName = $v->name;
  734. }
  735. if (isset($vats[$vatName]))
  736. $vats[$vatName] += $vat;
  737. else
  738. $vats[$vatName] = $vat;
  739. }
  740. }
  741. return $vats;
  742. // $this->emit('load-select');
  743. }
  744. public function setCausal($id, $idx)
  745. {
  746. $this->rows[$idx]["causal_id"] = $id;
  747. }
  748. public function printReceipt()
  749. {
  750. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  751. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip))->output();
  752. return response()->streamDownload(
  753. fn () => print($pdf),
  754. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  755. );
  756. /*return response()->streamDownload(function () {
  757. echo $pdf->stream();
  758. }, 'test.pdf');*/
  759. }
  760. public function multiPeriod()
  761. {
  762. $this->multiP = true;
  763. }
  764. public function multiPeriodCreate($idx)
  765. {
  766. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  767. foreach ($period as $dt) {
  768. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  769. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  770. }
  771. $this->multiP = false;
  772. }
  773. public function multiPeriodCancel()
  774. {
  775. $this->multiP = false;
  776. }
  777. public function getMonth($m)
  778. {
  779. $ret = '';
  780. switch ($m) {
  781. case 1:
  782. $ret = 'Gennaio';
  783. break;
  784. case 2:
  785. $ret = 'Febbraio';
  786. break;
  787. case 3:
  788. $ret = 'Marzo';
  789. break;
  790. case 4:
  791. $ret = 'Aprile';
  792. break;
  793. case 5:
  794. $ret = 'Maggio';
  795. break;
  796. case 6:
  797. $ret = 'Giugno';
  798. break;
  799. case 7:
  800. $ret = 'Luglio';
  801. break;
  802. case 8:
  803. $ret = 'Agosto';
  804. break;
  805. case 9:
  806. $ret = 'Settembre';
  807. break;
  808. case 10:
  809. $ret = 'Ottobre';
  810. break;
  811. case 11:
  812. $ret = 'Novembre';
  813. break;
  814. case 12:
  815. $ret = 'Dicembre';
  816. break;
  817. default:
  818. $ret = '';
  819. break;
  820. }
  821. return $ret;
  822. }
  823. }