RecordIN.php 30 KB

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