RecordIN.php 27 KB

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