RecordIN.php 28 KB

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