RecordIN.php 36 KB

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