RecordIN.php 30 KB

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