RecordIN.php 29 KB

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