RecordIN.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. $this->courseId = $_GET["courseId"];
  230. $mc = \App\Models\MemberCourse::findOrFail($this->courseId);
  231. $course = \App\Models\Course::findOrFail($mc->course_id);
  232. if (isset($_GET["months"]))
  233. {
  234. $price = $_GET["price"] / 100 * 100;
  235. $this->refreshAfter = 1;
  236. $desc = "Pagamento";
  237. $months = explode("|", $_GET["months"]);
  238. $this->months = $months;
  239. $this->rows[0]["amount"] = formatPrice($price * sizeof($months));
  240. foreach($months as $idx => $m)
  241. {
  242. $this->rows[0]["when"][$idx]["month"] = $m;
  243. $this->rows[0]["when"][$idx]["year"] = date("Y");
  244. $desc .= " " . $this->getMonth($m);
  245. }
  246. $this->rows[0]["note"] = $desc;
  247. $count += 1;
  248. }
  249. if (isset($_GET["createSubscription"]) && $_GET["createSubscription"] == 1)
  250. {
  251. $this->createSubscription = 1;
  252. $this->courseId = $_GET["courseId"];
  253. $price = $_GET["subscription_price"] / 100 * 100;
  254. $this->refreshAfter = 1;
  255. if ($count == 1)
  256. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  257. $this->rows[$count]["causal_id"] = null;
  258. $this->rows[$count]["amount"] = formatPrice($price);
  259. $this->rows[$count]["note"] = "Pagamento iscrizione " . $course->name;
  260. }
  261. }
  262. $this->first = false;
  263. }
  264. public function search()
  265. {
  266. $this->hasFilter = true;
  267. }
  268. public function disableSearch()
  269. {
  270. $this->filterMember = 0;
  271. $this->filterPaymentMethod = 0;
  272. $this->filterCausals = [];
  273. $this->filterTo = '';
  274. $this->filterFrom = '';
  275. $this->filterCommercial = 0;
  276. $this->hasFilter = false;
  277. $this->total = 0;
  278. }
  279. public function render()
  280. {
  281. $datas = [];
  282. return view('livewire.records_in', ['datas' => $datas]);
  283. }
  284. public function executeMultipleAction(){
  285. if ($this->multipleAction == 'delete')
  286. $this->multipleDelete();
  287. }
  288. public function add()
  289. {
  290. $this->emit('load-select');
  291. //if ($this->hasFilter)
  292. $this->emit('hide-search');
  293. $this->resetFields();
  294. $this->add = true;
  295. $this->update = false;
  296. }
  297. public function store($generate)
  298. {
  299. $this->emit('refresh');
  300. $rules = [
  301. 'payment_method_id' => 'required',
  302. 'rows.*.causal_id' => 'required',
  303. 'rows.*.amount' => 'required'
  304. ];
  305. /*$f = false;
  306. foreach($this->rows as $row)
  307. {
  308. if ($row["commercial"]) $f = true;
  309. }
  310. if ($f)
  311. $rules["member_id"] = 'required';*/
  312. if($this->commercial)
  313. $rules["member_id"] = 'required';
  314. //dd($this->getErrorBag());
  315. $this->validate($rules);
  316. //dd("£ASDSDFS");
  317. try {
  318. $record = \App\Models\Record::create([
  319. 'member_id' => $this->member_id,
  320. 'supplier_id' => $this->supplier_id,
  321. 'payment_method_id' => $this->payment_method_id,
  322. 'commercial' => $this->commercial,
  323. 'date' => $this->date,
  324. 'type' => $this->type,
  325. ]);
  326. $this->dataId = $record->id;
  327. // Inserisco le righe
  328. $tot = 0;
  329. $vat = 0;
  330. foreach($this->rows as $row)
  331. {
  332. foreach($row["when"] as $x => $y)
  333. {
  334. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  335. }
  336. \App\Models\RecordRow::create([
  337. 'record_id' => $this->dataId,
  338. 'causal_id' => $row["causal_id"],
  339. 'note' => $row["note"],
  340. 'amount' => $this->currencyToDouble($row["amount"]),
  341. 'vat_id' => $row["vat_id"],
  342. 'commercial' => $this->commercial,
  343. 'when' => json_encode($row["when"])
  344. ]);
  345. $tot += $this->currencyToDouble($row["amount"]);
  346. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  347. }
  348. $record->amount = $tot;
  349. $record->save();
  350. // se sto pagando un corso, aggiorno lo status dei mesi
  351. if ($this->courseId > 0)
  352. {
  353. $record->months = json_encode($this->months);
  354. $record->member_course_id = $this->courseId;
  355. $record->save();
  356. $c = \App\Models\MemberCourse::findOrFail($this->courseId);
  357. $xxx = json_decode($c->months);
  358. foreach($xxx as $idx => $mm)
  359. {
  360. if (in_array($mm->m, $this->months))
  361. {
  362. $xxx[$idx]->status = 1;
  363. }
  364. }
  365. $c->months = json_encode($xxx);
  366. if ($this->createSubscription == 1)
  367. $c->subscribed = true;
  368. $c->save();
  369. }
  370. if ($generate)
  371. $this->createReceipt();
  372. session()->flash('success','Movimento creato');
  373. $this->resetFields();
  374. $this->add = false;
  375. $this->isDuplicate = false;
  376. } catch (\Exception $ex) {
  377. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  378. }
  379. }
  380. public function duplicate($id){
  381. $record = \App\Models\Record::findOrFail($id);
  382. $newRecord = $record->replicate();
  383. $newRecord->save();
  384. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  385. foreach($rows as $r)
  386. {
  387. $newRow = $r->replicate();
  388. $newRow->record_id = $newRecord->id;
  389. $newRow->save();
  390. }
  391. $this->isDuplicate = true;
  392. $this->edit($newRecord->id);
  393. }
  394. public function edit($id){
  395. //if ($this->hasFilter)
  396. $this->emit('hide-search');
  397. $this->emit('load-select');
  398. try {
  399. $record = \App\Models\Record::findOrFail($id);
  400. if( !$record) {
  401. session()->flash('error','Movimento non trovato');
  402. } else {
  403. $this->member_id = $record->member_id;
  404. $this->supplier_id = $record->supplier_id;
  405. $this->payment_method_id = $record->payment_method_id;
  406. $this->commercial = $record->commercial;
  407. $this->date = date("Y-m-d", strtotime($record->date));
  408. $this->type = $record->type;
  409. $this->dataId = $record->id;
  410. $this->update = true;
  411. $this->add = false;
  412. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  413. foreach($this->rows as $i => $r)
  414. {
  415. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  416. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  417. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  418. }
  419. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  420. if ($exist != null)
  421. {
  422. $this->currentReceip = $exist;
  423. $this->parent = $this->currentReceip->parent;
  424. }
  425. }
  426. } catch (\Exception $ex) {
  427. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  428. }
  429. }
  430. public function update($generate)
  431. {
  432. $this->emit('refresh');
  433. $rules = [
  434. 'payment_method_id' => 'required',
  435. 'rows.*.causal_id' => 'required',
  436. 'rows.*.amount' => 'required'
  437. ];
  438. /*$f = false;
  439. foreach($this->rows as $row)
  440. {
  441. if ($row["commercial"]) $f = true;
  442. }
  443. if ($f)
  444. $rules["member_id"] = 'required';*/
  445. if($this->commercial)
  446. $rules["member_id"] = 'required';
  447. $this->validate($rules);
  448. try {
  449. \App\Models\Record::whereId($this->dataId)->update([
  450. 'member_id' => $this->member_id,
  451. 'supplier_id' => $this->supplier_id,
  452. 'payment_method_id' => $this->payment_method_id,
  453. 'commercial' => $this->commercial,
  454. 'date' => date("Y-m-d", strtotime($this->date)),
  455. 'type' => $this->type,
  456. ]);
  457. $tot = 0;
  458. $vat = 0;
  459. // Elimino le righe
  460. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  461. // Inserisco le righe
  462. foreach($this->rows as $row)
  463. {
  464. foreach($row["when"] as $x => $y)
  465. {
  466. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  467. }
  468. \App\Models\RecordRow::create([
  469. 'record_id' => $this->dataId,
  470. 'causal_id' => $row["causal_id"],
  471. 'note' => $row["note"],
  472. 'vat_id' => $row["vat_id"],
  473. 'amount' => $this->currencyToDouble($row["amount"]),
  474. 'commercial' => $this->commercial,
  475. 'when' => json_encode($row["when"])
  476. ]);
  477. $tot += $this->currencyToDouble($row["amount"]);
  478. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  479. }
  480. $rec = \App\Models\Record::findOrFail($this->dataId);
  481. $rec->amount = $tot;
  482. $rec->save();
  483. if ($generate)
  484. $this->createReceipt();
  485. session()->flash('success','Movimento aggiornato');
  486. $this->resetFields();
  487. $this->update = false;
  488. $this->isDuplicate = false;
  489. } catch (\Exception $ex) {
  490. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  491. }
  492. }
  493. public function cancel()
  494. {
  495. // Se arrivo da duplica elimino
  496. if ($this->isDuplicate)
  497. {
  498. try{
  499. \App\Models\Record::find($this->dataId)->delete();
  500. session()->flash('success',"Movimento eliminato");
  501. }catch(\Exception $e){
  502. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  503. }
  504. }
  505. $this->isDuplicate = false;
  506. $this->add = false;
  507. $this->update = false;
  508. $this->resetFields();
  509. }
  510. public function delete($id)
  511. {
  512. try{
  513. $r = \App\Models\Record::find($id);
  514. if ($r->member_course_id > 0)
  515. {
  516. $months = json_decode($r->months);
  517. $c = \App\Models\MemberCourse::findOrFail($r->member_course_id);
  518. $xxx = json_decode($c->months);
  519. foreach($xxx as $idx => $mm)
  520. {
  521. if (in_array($mm->m, $months))
  522. {
  523. $xxx[$idx]->status = "";
  524. }
  525. }
  526. $c->months = json_encode($xxx);
  527. $c->save();
  528. }
  529. $r->delete();
  530. session()->flash('success',"Movimento eliminato");
  531. }catch(\Exception $e){
  532. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  533. }
  534. }
  535. public function multipleDelete()
  536. {
  537. try{
  538. foreach($this->multipleIds as $id)
  539. {
  540. \App\Models\Record::find($id)->delete();
  541. }
  542. }catch(\Exception $e){
  543. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  544. }
  545. $this->multipleAction = '';
  546. }
  547. public function createMember()
  548. {
  549. $this->newMemberFiscalCodeExist = false;
  550. $this->validate([
  551. // 'newMemberFiscalCode'=>'required|max:16',
  552. 'newMemberFirstName'=>'required',
  553. 'newMemberLastName'=>'required'
  554. ]);
  555. // Check fiscal code exist
  556. $exist = false;
  557. if ($this->newMemberFiscalCode != '')
  558. {
  559. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  560. $exist = $check->count() > 0;
  561. }
  562. if (!$exist)
  563. {
  564. $member = \App\Models\Member::create([
  565. 'first_name' => $this->newMemberFirstName,
  566. 'last_name' => $this->newMemberLastName,
  567. 'fiscal_code' => $this->newMemberFiscalCode,
  568. 'status' => true
  569. ]);
  570. $this->member_id = $member->id;
  571. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get();
  572. // $this->emit('reloadMembers');
  573. $this->emit('refresh');
  574. $this->newMemberFirstName = '';
  575. $this->newMemberLastName = '';
  576. $this->newMemberFiscalCode = '';
  577. $this->emit('saved');
  578. //$this->validate();
  579. $this->selectId++;
  580. }
  581. else
  582. {
  583. $this->newMemberFiscalCodeExist = true;
  584. }
  585. }
  586. public function createReceipt()
  587. {
  588. $create = false;
  589. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  590. if ($receipt != null)
  591. {
  592. // Controllo lo stato, se 99 ne genero una nuova
  593. if ($receipt->status == 99)
  594. {
  595. $create = true;
  596. }
  597. }
  598. else
  599. {
  600. $create = true;
  601. }
  602. if ($create)
  603. {
  604. $number = 1;
  605. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  606. if ($exist != null)
  607. $number = $exist->number + 1;
  608. $receipt = \App\Models\Receipt::create([
  609. 'record_id' => $this->dataId,
  610. 'member_id' => $this->member_id,
  611. 'supplier_id' => $this->supplier_id,
  612. 'payment_method_id' => $this->payment_method_id,
  613. 'number' => $number,
  614. 'date' => $this->date,
  615. 'year' => date("Y"),
  616. 'type' => $this->type,
  617. 'parent' => $this->parent,
  618. 'status' => 1,
  619. ]);
  620. foreach($this->rows as $row)
  621. {
  622. \App\Models\ReceiptRow::create([
  623. 'receip_id' => $receipt->id,
  624. 'causal_id' => $row["causal_id"],
  625. 'note' => $row["note"],
  626. 'vat_id' => $row["vat_id"],
  627. 'amount' => $this->currencyToDouble($row["amount"]),
  628. 'commercial' => $row["commercial"],
  629. 'when' => json_encode($row["when"])
  630. ]);
  631. }
  632. $this->currentReceip = $receipt;
  633. }
  634. }
  635. public function removeReceipt()
  636. {
  637. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  638. $receipt->status = 99;
  639. $receipt->save();
  640. $this->currentReceip = $receipt;
  641. }
  642. function currencyToDouble($val)
  643. {
  644. $x = str_replace("€", "", $val);
  645. $x = str_replace(".", "", $x);
  646. $x = str_replace(",", ".", $x);
  647. return floatval(trim($x));
  648. }
  649. public function addRow()
  650. {
  651. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  652. $this->emit('load-select');
  653. }
  654. public function delRow($idx)
  655. {
  656. unset($this->rows[$idx]);
  657. // $this->emit('load-select');
  658. }
  659. public function addPeriod($idx)
  660. {
  661. $x = sizeof($this->rows[$idx]['when']) - 1;
  662. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  663. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  664. }
  665. public function delPeriod($idx, $xxx)
  666. {
  667. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  668. //unset($this->rows[$idx]['when'][$xxx]);
  669. // $this->emit('load-select');
  670. }
  671. public function getTotal()
  672. {
  673. $total = 0.00;
  674. foreach($this->rows as $r)
  675. {
  676. if ($r["amount"] != null && $r["amount"] != "")
  677. {
  678. $total += $this->currencyToDouble($r["amount"]);
  679. if ($r["vat_id"] > 0)
  680. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  681. }
  682. }
  683. return formatPrice($total);
  684. // $this->emit('load-select');
  685. }
  686. public function getPrice()
  687. {
  688. $total = 0.00;
  689. foreach($this->rows as $r)
  690. {
  691. if ($r["amount"] != null && $r["amount"] != "")
  692. $total += $this->currencyToDouble($r["amount"]);
  693. }
  694. return formatPrice($total);
  695. // $this->emit('load-select');
  696. }
  697. public function getVat()
  698. {
  699. $total = 0.00;
  700. foreach($this->rows as $r)
  701. {
  702. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  703. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  704. }
  705. return formatPrice($total);
  706. // $this->emit('load-select');
  707. }
  708. public function getVats()
  709. {
  710. $vats = array();
  711. foreach($this->rows as $r)
  712. {
  713. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  714. {
  715. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  716. $vatName = "";
  717. foreach($this->vats as $v)
  718. {
  719. if ($v->id == $r["vat_id"])
  720. $vatName = $v->name;
  721. }
  722. if (isset($vats[$vatName]))
  723. $vats[$vatName] += $vat;
  724. else
  725. $vats[$vatName] = $vat;
  726. }
  727. }
  728. return $vats;
  729. // $this->emit('load-select');
  730. }
  731. public function setCausal($id, $idx)
  732. {
  733. $this->rows[$idx]["causal_id"] = $id;
  734. }
  735. public function printReceipt()
  736. {
  737. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  738. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip))->output();
  739. return response()->streamDownload(
  740. fn () => print($pdf),
  741. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  742. );
  743. /*return response()->streamDownload(function () {
  744. echo $pdf->stream();
  745. }, 'test.pdf');*/
  746. }
  747. public function multiPeriod()
  748. {
  749. $this->multiP = true;
  750. }
  751. public function multiPeriodCreate($idx)
  752. {
  753. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  754. foreach ($period as $dt) {
  755. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  756. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  757. }
  758. $this->multiP = false;
  759. }
  760. public function multiPeriodCancel()
  761. {
  762. $this->multiP = false;
  763. }
  764. public function getMonth($m)
  765. {
  766. $ret = '';
  767. switch ($m) {
  768. case 1:
  769. $ret = 'Gennaio';
  770. break;
  771. case 2:
  772. $ret = 'Febbraio';
  773. break;
  774. case 3:
  775. $ret = 'Marzo';
  776. break;
  777. case 4:
  778. $ret = 'Aprile';
  779. break;
  780. case 5:
  781. $ret = 'Maggio';
  782. break;
  783. case 6:
  784. $ret = 'Giugno';
  785. break;
  786. case 7:
  787. $ret = 'Luglio';
  788. break;
  789. case 8:
  790. $ret = 'Agosto';
  791. break;
  792. case 9:
  793. $ret = 'Settembre';
  794. break;
  795. case 10:
  796. $ret = 'Ottobre';
  797. break;
  798. case 11:
  799. $ret = 'Novembre';
  800. break;
  801. case 12:
  802. $ret = 'Dicembre';
  803. break;
  804. default:
  805. $ret = '';
  806. break;
  807. }
  808. return $ret;
  809. }
  810. }