RecordIN.php 34 KB

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