RecordIN.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. }
  260. if (isset($_GET["causalId"]))
  261. {
  262. $this->refreshAfter = 1;
  263. $this->causal_id = $_GET["causalId"];
  264. }
  265. if (isset($_GET["id"]))
  266. {
  267. $this->refreshAfter = 1;
  268. $this->edit($_GET["id"]);
  269. }
  270. $count = 0;
  271. if (isset($_GET["courseId"]))
  272. {
  273. $this->courseId = $_GET["courseId"];
  274. $mc = \App\Models\MemberCourse::findOrFail($this->courseId);
  275. $course = \App\Models\Course::findOrFail($mc->course_id);
  276. }
  277. if (isset($_GET["months"]))
  278. {
  279. $price = $_GET["price"] / 100 * 100;
  280. $this->refreshAfter = 1;
  281. $desc = "Pagamento";
  282. $months = explode("|", $_GET["months"]);
  283. $this->months = $months;
  284. $this->rows[0]["amount"] = formatPrice($price * sizeof($months));
  285. foreach($months as $idx => $m)
  286. {
  287. $this->rows[0]["when"][$idx]["month"] = $m;
  288. $this->rows[0]["when"][$idx]["year"] = date("Y");
  289. $desc .= " " . $this->getMonth($m);
  290. }
  291. $this->rows[0]["note"] = $desc;
  292. $count += 1;
  293. }
  294. if (isset($_GET["createSubscription"]) && $_GET["createSubscription"] == 1)
  295. {
  296. $this->createSubscription = 1;
  297. $this->courseId = $_GET["courseId"];
  298. $price = $_GET["subscription_price"] / 100 * 100;
  299. $this->refreshAfter = 1;
  300. if ($count == 1)
  301. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  302. $this->rows[$count]["causal_id"] = null;
  303. $this->rows[$count]["amount"] = formatPrice($price);
  304. $this->rows[$count]["note"] = "Pagamento iscrizione " . $course->name;
  305. if (isset($_GET["subCausalId"]))
  306. {
  307. $this->refreshAfter = 1;
  308. $this->rows[$count]["causal_id"] = $_GET["subCausalId"];
  309. }
  310. }
  311. }
  312. $this->first = false;
  313. }
  314. public function search()
  315. {
  316. $this->hasFilter = true;
  317. }
  318. public function disableSearch()
  319. {
  320. $this->filterMember = 0;
  321. $this->filterPaymentMethod = 0;
  322. $this->filterCausals = 0;
  323. $this->filterTo = '';
  324. $this->filterFrom = '';
  325. $this->filterCommercial = 0;
  326. $this->hasFilter = false;
  327. $this->total = 0;
  328. }
  329. public function render()
  330. {
  331. $datas = [];
  332. return view('livewire.records_in', ['datas' => $datas]);
  333. }
  334. public function executeMultipleAction(){
  335. if ($this->multipleAction == 'delete')
  336. $this->multipleDelete();
  337. }
  338. public function add()
  339. {
  340. $this->emit('load-select');
  341. //if ($this->hasFilter)
  342. $this->emit('hide-search');
  343. $this->resetFields();
  344. $this->dataId = 0;
  345. $this->add = true;
  346. $this->update = false;
  347. $this->emit('setEdit', true);
  348. }
  349. public function storeCorrispettivo()
  350. {
  351. $this->emit('refresh');
  352. $rules = [
  353. 'member_id' => 'required'
  354. ];
  355. $this->validate($rules);
  356. foreach($this->payments as $p)
  357. {
  358. if ($p->corrispettivo_fiscale)
  359. {
  360. $price = isset($this->corrispettivo[$p->id]) ? $this->currencyToDouble($this->corrispettivo[$p->id]) : 0;
  361. if ($price > 0)
  362. {
  363. $record = \App\Models\Record::create([
  364. 'member_id' => $this->member_id,
  365. 'supplier_id' => null,
  366. 'payment_method_id' => $p->id,
  367. 'commercial' => $this->commercial,
  368. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  369. 'date' => $this->date,
  370. 'type' => $this->type,
  371. 'amount' => $price,
  372. 'financial_movement' => $this->financial_movement,
  373. 'deleted' => $this->deleted
  374. ]);
  375. \App\Models\RecordRow::create([
  376. 'record_id' => $record->id,
  377. 'causal_id' => $this->corrispettivo_causal_id,
  378. 'note' => '',
  379. 'amount' => $price,
  380. 'vat_id' => null,
  381. 'commercial' => $this->commercial,
  382. 'when' => json_encode([])
  383. ]);
  384. }
  385. }
  386. }
  387. $this->resetFields();
  388. $this->add = false;
  389. $this->isDuplicate = false;
  390. $this->emit('setEdit', false);
  391. }
  392. public function store($generate)
  393. {
  394. $this->emit('refresh');
  395. $rules = [
  396. 'payment_method_id' => 'required',
  397. 'rows.*.causal_id' => 'required',
  398. 'rows.*.amount' => 'required'
  399. ];
  400. /*$f = false;
  401. foreach($this->rows as $row)
  402. {
  403. if ($row["commercial"]) $f = true;
  404. }
  405. if ($f)
  406. $rules["member_id"] = 'required';*/
  407. if(!$this->commercial)
  408. $rules["member_id"] = 'required';
  409. //dd($this->getErrorBag());
  410. $this->validate($rules);
  411. //dd("£ASDSDFS");
  412. try {
  413. $record = \App\Models\Record::create([
  414. 'member_id' => $this->member_id,
  415. 'supplier_id' => $this->supplier_id,
  416. 'payment_method_id' => $this->payment_method_id,
  417. 'commercial' => $this->commercial,
  418. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  419. 'date' => $this->date,
  420. 'type' => $this->type,
  421. 'financial_movement' => $this->financial_movement,
  422. 'deleted' => $this->deleted
  423. ]);
  424. $this->dataId = $record->id;
  425. // Inserisco le righe
  426. $tot = 0;
  427. $vat = 0;
  428. foreach($this->rows as $row)
  429. {
  430. foreach($row["when"] as $x => $y)
  431. {
  432. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  433. }
  434. \App\Models\RecordRow::create([
  435. 'record_id' => $this->dataId,
  436. 'causal_id' => $row["causal_id"],
  437. 'note' => $row["note"],
  438. 'amount' => $this->currencyToDouble($row["amount"]),
  439. 'vat_id' => $row["vat_id"],
  440. 'commercial' => $this->commercial,
  441. 'when' => json_encode($row["when"])
  442. ]);
  443. $tot += $this->currencyToDouble($row["amount"]);
  444. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  445. }
  446. $record->amount = $tot;
  447. $record->save();
  448. // se sto pagando un corso, aggiorno lo status dei mesi
  449. if ($this->courseId > 0)
  450. {
  451. $record->months = json_encode($this->months);
  452. $record->member_course_id = $this->courseId;
  453. $record->save();
  454. $c = \App\Models\MemberCourse::findOrFail($this->courseId);
  455. $xxx = json_decode($c->months);
  456. foreach($xxx as $idx => $mm)
  457. {
  458. if (in_array($mm->m, $this->months))
  459. {
  460. $xxx[$idx]->status = 1;
  461. }
  462. }
  463. $c->months = json_encode($xxx);
  464. if ($this->createSubscription == 1)
  465. $c->subscribed = true;
  466. $c->save();
  467. }
  468. if (!$this->financial_movement)
  469. {
  470. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  471. if (!$payment_method->money)
  472. {
  473. if ($generate)
  474. $this->createReceipt();
  475. }
  476. }
  477. session()->flash('success','Movimento creato');
  478. $this->resetFields();
  479. $this->add = false;
  480. $this->isDuplicate = false;
  481. $this->emit('setEdit', false);
  482. } catch (\Exception $ex) {
  483. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  484. }
  485. }
  486. public function duplicate($id){
  487. $record = \App\Models\Record::findOrFail($id);
  488. $newRecord = $record->replicate();
  489. $newRecord->save();
  490. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  491. foreach($rows as $r)
  492. {
  493. $newRow = $r->replicate();
  494. $newRow->record_id = $newRecord->id;
  495. $newRow->save();
  496. }
  497. $this->edit($newRecord->id);
  498. $this->isDuplicate = true;
  499. }
  500. public function edit($id){
  501. $this->emit('setEdit', true);
  502. //if ($this->hasFilter)
  503. $this->emit('hide-search');
  504. $this->emit('load-select');
  505. try {
  506. $record = \App\Models\Record::findOrFail($id);
  507. if( !$record) {
  508. session()->flash('error','Movimento non trovato');
  509. } else {
  510. $this->member_id = $record->member_id;
  511. $this->supplier_id = $record->supplier_id;
  512. $this->payment_method_id = $record->payment_method_id;
  513. $this->amount = $record->amount;
  514. $this->commercial = $record->commercial;
  515. $this->corrispettivo_fiscale = $record->corrispettivo_fiscale;
  516. $this->date = date("Y-m-d", strtotime($record->date));
  517. $this->type = $record->type;
  518. $this->deleted = $record->deleted;
  519. $this->financial_movement = $record->financial_movement;
  520. $this->dataId = $record->id;
  521. $this->update = true;
  522. $this->add = false;
  523. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  524. foreach($this->rows as $i => $r)
  525. {
  526. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  527. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  528. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  529. }
  530. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  531. if ($exist != null)
  532. {
  533. $this->currentReceip = $exist;
  534. $this->parent = $this->currentReceip->parent;
  535. }
  536. }
  537. } catch (\Exception $ex) {
  538. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  539. }
  540. }
  541. public function update($generate = false)
  542. {
  543. $this->emit('refresh');
  544. $rules = [
  545. 'payment_method_id' => 'required',
  546. 'rows.*.causal_id' => 'required',
  547. 'rows.*.amount' => 'required'
  548. ];
  549. if(!$this->commercial)
  550. $rules["member_id"] = 'required';
  551. $this->validate($rules);
  552. try {
  553. \App\Models\Record::whereId($this->dataId)->update([
  554. 'member_id' => $this->member_id,
  555. 'supplier_id' => $this->supplier_id,
  556. 'payment_method_id' => $this->payment_method_id,
  557. 'commercial' => $this->commercial,
  558. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  559. 'date' => date("Y-m-d", strtotime($this->date)),
  560. 'type' => $this->type,
  561. 'financial_movement' => $this->financial_movement,
  562. 'deleted' => $this->deleted,
  563. ]);
  564. $tot = 0;
  565. $vat = 0;
  566. // Elimino le righe
  567. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  568. // Inserisco le righe
  569. foreach($this->rows as $row)
  570. {
  571. foreach($row["when"] as $x => $y)
  572. {
  573. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  574. }
  575. \App\Models\RecordRow::create([
  576. 'record_id' => $this->dataId,
  577. 'causal_id' => $row["causal_id"],
  578. 'note' => $row["note"],
  579. 'vat_id' => $row["vat_id"],
  580. 'amount' => $this->currencyToDouble($row["amount"]),
  581. 'commercial' => $this->commercial,
  582. 'when' => json_encode($row["when"])
  583. ]);
  584. $tot += $this->currencyToDouble($row["amount"]);
  585. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  586. }
  587. $rec = \App\Models\Record::findOrFail($this->dataId);
  588. $rec->amount = $tot;
  589. $rec->save();
  590. if (!$this->financial_movement)
  591. {
  592. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  593. if (!$payment_method->money)
  594. {
  595. if ($generate)
  596. $this->createReceipt();
  597. }
  598. }
  599. session()->flash('success','Movimento aggiornato');
  600. $this->resetFields();
  601. $this->update = false;
  602. $this->isDuplicate = false;
  603. $this->emit('setEdit', false);
  604. } catch (\Exception $ex) {
  605. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  606. }
  607. }
  608. public function cancel()
  609. {
  610. // Se arrivo da duplica elimino
  611. if ($this->isDuplicate)
  612. {
  613. try{
  614. \App\Models\Record::find($this->dataId)->delete();
  615. session()->flash('success',"Movimento eliminato");
  616. }catch(\Exception $e){
  617. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  618. }
  619. }
  620. $this->isDuplicate = false;
  621. $this->add = false;
  622. $this->update = false;
  623. $this->resetFields();
  624. $this->emit('setEdit', false);
  625. }
  626. public function delete($id)
  627. {
  628. try{
  629. $r = \App\Models\Record::find($id);
  630. // Annullo la ricevuta
  631. $receipt = \App\Models\Receipt::where('record_id', $r->id)->first();
  632. if($receipt)
  633. {
  634. $receipt->status = 99;
  635. sendReceiptDeleteEmail($receipt);
  636. $receipt->save();
  637. }
  638. if ($r->member_course_id > 0)
  639. {
  640. $months = json_decode($r->months);
  641. $c = \App\Models\MemberCourse::findOrFail($r->member_course_id);
  642. $xxx = json_decode($c->months);
  643. foreach($xxx as $idx => $mm)
  644. {
  645. if (in_array($mm->m, $months))
  646. {
  647. $xxx[$idx]->status = "";
  648. }
  649. }
  650. $c->months = json_encode($xxx);
  651. $c->save();
  652. }
  653. $r->deleted = true;
  654. $r->save();
  655. session()->flash('success','Movimento aggiornato');
  656. $this->resetFields();
  657. $this->update = false;
  658. $this->isDuplicate = false;
  659. $this->emit('setEdit', false);
  660. $this->emit('reload');
  661. //$this->emit('load-data-table');
  662. }catch(\Exception $ex){
  663. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  664. }
  665. }
  666. public function multipleDelete()
  667. {
  668. try{
  669. foreach($this->multipleIds as $id)
  670. {
  671. \App\Models\Record::find($id)->delete();
  672. }
  673. }catch(\Exception $e){
  674. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  675. }
  676. $this->multipleAction = '';
  677. }
  678. public function createMember()
  679. {
  680. $this->newMemberFiscalCodeExist = false;
  681. $this->validate([
  682. // 'newMemberFiscalCode'=>'required|max:16',
  683. 'newMemberFirstName'=>'required',
  684. 'newMemberLastName'=>'required'
  685. ]);
  686. // Check fiscal code exist
  687. $exist = false;
  688. if ($this->newMemberFiscalCode != '')
  689. {
  690. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  691. $exist = $check->count() > 0;
  692. }
  693. if (!$exist)
  694. {
  695. $member = \App\Models\Member::create([
  696. 'first_name' => strtoupper($this->newMemberFirstName),
  697. 'last_name' => strtoupper($this->newMemberLastName),
  698. 'fiscal_code' => $this->newMemberFiscalCode,
  699. 'status' => true
  700. ]);
  701. $this->member_id = $member->id;
  702. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get();
  703. // $this->emit('reloadMembers');
  704. $this->emit('refresh');
  705. $this->newMemberFirstName = '';
  706. $this->newMemberLastName = '';
  707. $this->newMemberFiscalCode = '';
  708. $this->emit('saved');
  709. //$this->validate();
  710. $this->selectId++;
  711. }
  712. else
  713. {
  714. $this->newMemberFiscalCodeExist = true;
  715. }
  716. }
  717. public function createReceipt()
  718. {
  719. // Ulteriore controllo commerciale/non commerciale
  720. if (!$this->commercial)
  721. {
  722. $create = false;
  723. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  724. if ($receipt != null)
  725. {
  726. // Controllo lo stato, se 99 ne genero una nuova
  727. if ($receipt->status == 99)
  728. {
  729. $create = true;
  730. }
  731. }
  732. else
  733. {
  734. $create = true;
  735. }
  736. if ($create)
  737. {
  738. $number = 1;
  739. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  740. if ($exist != null)
  741. $number = $exist->number + 1;
  742. $receipt = \App\Models\Receipt::create([
  743. 'record_id' => $this->dataId,
  744. 'member_id' => $this->member_id,
  745. 'supplier_id' => $this->supplier_id,
  746. 'payment_method_id' => $this->payment_method_id,
  747. 'number' => $number,
  748. 'date' => $this->date,
  749. 'year' => date("Y"),
  750. 'type' => $this->type,
  751. 'parent' => $this->parent,
  752. 'status' => 1,
  753. ]);
  754. foreach($this->rows as $row)
  755. {
  756. \App\Models\ReceiptRow::create([
  757. 'receip_id' => $receipt->id,
  758. 'causal_id' => $row["causal_id"],
  759. 'note' => $row["note"],
  760. 'vat_id' => $row["vat_id"],
  761. 'amount' => $this->currencyToDouble($row["amount"]),
  762. 'commercial' => $row["commercial"],
  763. 'when' => json_encode($row["when"])
  764. ]);
  765. }
  766. $this->currentReceip = $receipt;
  767. sendReceiptEmail($receipt);
  768. /*
  769. // Invio mail
  770. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  771. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  772. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  773. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  774. if ($email != '')
  775. {
  776. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  777. 'pdf' => 'public/pdf/' . $pdfName,
  778. 'number' => $receipt->number . "/" . $receipt->year
  779. ]));
  780. }
  781. */
  782. session()->flash('receipt',"Ricevuta " . $number . "/" . date("Y") . " creata correttamente");
  783. // Apro la ricevuta
  784. $this->emit('showReceipt', $this->currentReceip->id);
  785. }
  786. }
  787. }
  788. public function removeReceipt()
  789. {
  790. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  791. $receipt->status = 99;
  792. $receipt->save();
  793. sendReceiptDeleteEmail($receipt);
  794. $this->currentReceip = $receipt;
  795. }
  796. function currencyToDouble($val)
  797. {
  798. $x = str_replace("€", "", $val);
  799. $x = str_replace(".", "", $x);
  800. $x = str_replace(",", ".", $x);
  801. return floatval(trim($x));
  802. }
  803. public function addRow()
  804. {
  805. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  806. $this->emit('load-select');
  807. }
  808. public function delRow($idx)
  809. {
  810. unset($this->rows[$idx]);
  811. // $this->emit('load-select');
  812. }
  813. public function addPeriod($idx)
  814. {
  815. $x = sizeof($this->rows[$idx]['when']) - 1;
  816. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  817. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  818. }
  819. public function delPeriod($idx, $xxx)
  820. {
  821. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  822. //unset($this->rows[$idx]['when'][$xxx]);
  823. // $this->emit('load-select');
  824. }
  825. public function getTotal()
  826. {
  827. $total = 0.00;
  828. foreach($this->rows as $r)
  829. {
  830. if ($r["amount"] != null && $r["amount"] != "")
  831. {
  832. $total += $this->currencyToDouble($r["amount"]);
  833. if ($r["vat_id"] > 0)
  834. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  835. }
  836. }
  837. return formatPrice($total);
  838. // $this->emit('load-select');
  839. }
  840. public function getTotalCorrispettivo()
  841. {
  842. $total = 0.00;
  843. foreach($this->corrispettivo as $r)
  844. {
  845. $total += $this->currencyToDouble($r);
  846. }
  847. return formatPrice($total);
  848. // $this->emit('load-select');
  849. }
  850. public function getPrice()
  851. {
  852. $total = 0.00;
  853. foreach($this->rows as $r)
  854. {
  855. if ($r["amount"] != null && $r["amount"] != "")
  856. $total += $this->currencyToDouble($r["amount"]);
  857. }
  858. return formatPrice($total);
  859. // $this->emit('load-select');
  860. }
  861. public function getVat()
  862. {
  863. $total = 0.00;
  864. foreach($this->rows as $r)
  865. {
  866. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  867. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  868. }
  869. return formatPrice($total);
  870. // $this->emit('load-select');
  871. }
  872. public function getVats()
  873. {
  874. $vats = array();
  875. foreach($this->rows as $r)
  876. {
  877. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  878. {
  879. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  880. $vatName = "";
  881. foreach($this->vats as $v)
  882. {
  883. if ($v->id == $r["vat_id"])
  884. $vatName = $v->name;
  885. }
  886. if (isset($vats[$vatName]))
  887. $vats[$vatName] += $vat;
  888. else
  889. $vats[$vatName] = $vat;
  890. }
  891. }
  892. return $vats;
  893. // $this->emit('load-select');
  894. }
  895. public function setCausal($id, $idx)
  896. {
  897. $this->rows[$idx]["causal_id"] = $id;
  898. }
  899. public function printReceipt()
  900. {
  901. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  902. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip));//->output();
  903. return $pdf->stream();
  904. /*return response()->streamDownload(
  905. fn () => print($pdf),
  906. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  907. );*/
  908. /*return response()->streamDownload(function () {
  909. echo $pdf->stream();
  910. }, 'test.pdf');*/
  911. }
  912. public function multiPeriod()
  913. {
  914. $this->multiP = true;
  915. }
  916. public function multiPeriodCreate($idx)
  917. {
  918. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  919. foreach ($period as $dt) {
  920. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  921. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  922. }
  923. $this->multiP = false;
  924. }
  925. public function multiPeriodCancel()
  926. {
  927. $this->multiP = false;
  928. }
  929. public function getMonth($m)
  930. {
  931. $ret = '';
  932. switch ($m) {
  933. case 1:
  934. $ret = 'Gennaio';
  935. break;
  936. case 2:
  937. $ret = 'Febbraio';
  938. break;
  939. case 3:
  940. $ret = 'Marzo';
  941. break;
  942. case 4:
  943. $ret = 'Aprile';
  944. break;
  945. case 5:
  946. $ret = 'Maggio';
  947. break;
  948. case 6:
  949. $ret = 'Giugno';
  950. break;
  951. case 7:
  952. $ret = 'Luglio';
  953. break;
  954. case 8:
  955. $ret = 'Agosto';
  956. break;
  957. case 9:
  958. $ret = 'Settembre';
  959. break;
  960. case 10:
  961. $ret = 'Ottobre';
  962. break;
  963. case 11:
  964. $ret = 'Novembre';
  965. break;
  966. case 12:
  967. $ret = 'Dicembre';
  968. break;
  969. default:
  970. $ret = '';
  971. break;
  972. }
  973. return $ret;
  974. }
  975. }