RecordIN.php 33 KB

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