RecordIN.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. $this->emit('load-data-table');
  357. return view('livewire.records_in', ['datas' => $datas]);
  358. }
  359. public function executeMultipleAction(){
  360. if ($this->multipleAction == 'delete')
  361. $this->multipleDelete();
  362. }
  363. public function add()
  364. {
  365. $this->emit('load-select');
  366. //if ($this->hasFilter)
  367. $this->emit('hide-search');
  368. $this->resetFields();
  369. $this->add = true;
  370. $this->update = false;
  371. }
  372. public function store($generate)
  373. {
  374. $this->emit('refresh');
  375. $rules = [
  376. 'payment_method_id' => 'required',
  377. 'rows.*.causal_id' => 'required',
  378. 'rows.*.amount' => 'required'
  379. ];
  380. /*$f = false;
  381. foreach($this->rows as $row)
  382. {
  383. if ($row["commercial"]) $f = true;
  384. }
  385. if ($f)
  386. $rules["member_id"] = 'required';*/
  387. if($this->commercial)
  388. $rules["member_id"] = 'required';
  389. $this->validate($rules);
  390. try {
  391. $record = \App\Models\Record::create([
  392. 'member_id' => $this->member_id,
  393. 'supplier_id' => $this->supplier_id,
  394. 'payment_method_id' => $this->payment_method_id,
  395. 'commercial' => $this->commercial,
  396. 'date' => $this->date,
  397. 'type' => $this->type,
  398. ]);
  399. $this->dataId = $record->id;
  400. // Inserisco le righe
  401. $tot = 0;
  402. $vat = 0;
  403. foreach($this->rows as $row)
  404. {
  405. foreach($row["when"] as $x => $y)
  406. {
  407. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  408. }
  409. \App\Models\RecordRow::create([
  410. 'record_id' => $this->dataId,
  411. 'causal_id' => $row["causal_id"],
  412. 'note' => $row["note"],
  413. 'amount' => $this->currencyToDouble($row["amount"]),
  414. 'vat_id' => $row["vat_id"],
  415. 'commercial' => $this->commercial,
  416. 'when' => json_encode($row["when"])
  417. ]);
  418. $tot += $this->currencyToDouble($row["amount"]);
  419. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  420. }
  421. $record->amount = $tot;
  422. $record->save();
  423. if ($generate)
  424. $this->createReceipt();
  425. session()->flash('success','Movimento creato');
  426. $this->resetFields();
  427. $this->add = false;
  428. $this->isDuplicate = false;
  429. } catch (\Exception $ex) {
  430. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  431. }
  432. }
  433. public function duplicate($id){
  434. $record = \App\Models\Record::findOrFail($id);
  435. $newRecord = $record->replicate();
  436. $newRecord->save();
  437. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  438. foreach($rows as $r)
  439. {
  440. $newRow = $r->replicate();
  441. $newRow->record_id = $newRecord->id;
  442. $newRow->save();
  443. }
  444. $this->isDuplicate = true;
  445. $this->edit($newRecord->id);
  446. }
  447. public function edit($id){
  448. //if ($this->hasFilter)
  449. $this->emit('hide-search');
  450. $this->emit('load-select');
  451. try {
  452. $record = \App\Models\Record::findOrFail($id);
  453. if( !$record) {
  454. session()->flash('error','Movimento non trovato');
  455. } else {
  456. $this->member_id = $record->member_id;
  457. $this->supplier_id = $record->supplier_id;
  458. $this->payment_method_id = $record->payment_method_id;
  459. $this->commercial = $record->commercial;
  460. $this->date = date("Y-m-d", strtotime($record->date));
  461. $this->type = $record->type;
  462. $this->dataId = $record->id;
  463. $this->update = true;
  464. $this->add = false;
  465. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  466. foreach($this->rows as $i => $r)
  467. {
  468. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  469. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  470. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  471. }
  472. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  473. if ($exist != null)
  474. $this->currentReceip = $exist;
  475. }
  476. } catch (\Exception $ex) {
  477. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  478. }
  479. }
  480. public function update($generate)
  481. {
  482. $this->emit('refresh');
  483. $rules = [
  484. 'payment_method_id' => 'required',
  485. 'rows.*.causal_id' => 'required',
  486. 'rows.*.amount' => 'required'
  487. ];
  488. /*$f = false;
  489. foreach($this->rows as $row)
  490. {
  491. if ($row["commercial"]) $f = true;
  492. }
  493. if ($f)
  494. $rules["member_id"] = 'required';*/
  495. if($this->commercial)
  496. $rules["member_id"] = 'required';
  497. $this->validate($rules);
  498. try {
  499. \App\Models\Record::whereId($this->dataId)->update([
  500. 'member_id' => $this->member_id,
  501. 'supplier_id' => $this->supplier_id,
  502. 'payment_method_id' => $this->payment_method_id,
  503. 'commercial' => $this->commercial,
  504. 'date' => date("Y-m-d", strtotime($this->date)),
  505. 'type' => $this->type,
  506. ]);
  507. $tot = 0;
  508. $vat = 0;
  509. // Elimino le righe
  510. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  511. // Inserisco le righe
  512. foreach($this->rows as $row)
  513. {
  514. foreach($row["when"] as $x => $y)
  515. {
  516. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  517. }
  518. \App\Models\RecordRow::create([
  519. 'record_id' => $this->dataId,
  520. 'causal_id' => $row["causal_id"],
  521. 'note' => $row["note"],
  522. 'vat_id' => $row["vat_id"],
  523. 'amount' => $this->currencyToDouble($row["amount"]),
  524. 'commercial' => $this->commercial,
  525. 'when' => json_encode($row["when"])
  526. ]);
  527. $tot += $this->currencyToDouble($row["amount"]);
  528. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  529. }
  530. $rec = \App\Models\Record::findOrFail($this->dataId);
  531. $rec->amount = $tot;
  532. $rec->save();
  533. if ($generate)
  534. $this->createReceipt();
  535. session()->flash('success','Movimento aggiornato');
  536. $this->resetFields();
  537. $this->update = false;
  538. $this->isDuplicate = false;
  539. } catch (\Exception $ex) {
  540. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  541. }
  542. }
  543. public function cancel()
  544. {
  545. // Se arrivo da duplica elimino
  546. if ($this->isDuplicate)
  547. {
  548. try{
  549. \App\Models\Record::find($this->dataId)->delete();
  550. session()->flash('success',"Movimento eliminato");
  551. }catch(\Exception $e){
  552. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  553. }
  554. }
  555. $this->isDuplicate = false;
  556. $this->add = false;
  557. $this->update = false;
  558. $this->resetFields();
  559. }
  560. public function delete($id)
  561. {
  562. try{
  563. \App\Models\Record::find($id)->delete();
  564. session()->flash('success',"Movimento eliminato");
  565. }catch(\Exception $e){
  566. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  567. }
  568. }
  569. public function multipleDelete()
  570. {
  571. try{
  572. foreach($this->multipleIds as $id)
  573. {
  574. \App\Models\Record::find($id)->delete();
  575. }
  576. }catch(\Exception $e){
  577. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  578. }
  579. $this->multipleAction = '';
  580. }
  581. public function createMember()
  582. {
  583. $this->newMemberFiscalCodeExist = false;
  584. $this->validate([
  585. // 'newMemberFiscalCode'=>'required|max:16',
  586. 'newMemberFirstName'=>'required',
  587. 'newMemberLastName'=>'required'
  588. ]);
  589. // Check fiscal code exist
  590. $exist = false;
  591. if ($this->newMemberFiscalCode != '')
  592. {
  593. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  594. $exist = $check->count() > 0;
  595. }
  596. if (!$exist)
  597. {
  598. $member = \App\Models\Member::create([
  599. 'first_name' => $this->newMemberFirstName,
  600. 'last_name' => $this->newMemberLastName,
  601. 'fiscal_code' => $this->newMemberFiscalCode,
  602. 'status' => true
  603. ]);
  604. $this->member_id = $member->id;
  605. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get();
  606. // $this->emit('reloadMembers');
  607. $this->emit('refresh');
  608. $this->newMemberFirstName = '';
  609. $this->newMemberLastName = '';
  610. $this->newMemberFiscalCode = '';
  611. $this->emit('saved');
  612. //$this->validate();
  613. $this->selectId++;
  614. }
  615. else
  616. {
  617. $this->newMemberFiscalCodeExist = true;
  618. }
  619. }
  620. public function createReceipt()
  621. {
  622. $create = false;
  623. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  624. if ($receipt != null)
  625. {
  626. // Controllo lo stato, se 99 ne genero una nuova
  627. if ($receipt->status == 99)
  628. {
  629. $create = true;
  630. }
  631. }
  632. else
  633. {
  634. $create = true;
  635. }
  636. if ($create)
  637. {
  638. $number = 1;
  639. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  640. if ($exist != null)
  641. $number = $exist->number + 1;
  642. $receipt = \App\Models\Receipt::create([
  643. 'record_id' => $this->dataId,
  644. 'member_id' => $this->member_id,
  645. 'supplier_id' => $this->supplier_id,
  646. 'payment_method_id' => $this->payment_method_id,
  647. 'number' => $number,
  648. 'date' => $this->date,
  649. 'year' => date("Y"),
  650. 'type' => $this->type,
  651. 'status' => 1,
  652. ]);
  653. foreach($this->rows as $row)
  654. {
  655. \App\Models\ReceiptRow::create([
  656. 'receip_id' => $receipt->id,
  657. 'causal_id' => $row["causal_id"],
  658. 'note' => $row["note"],
  659. 'vat_id' => $row["vat_id"],
  660. 'amount' => $this->currencyToDouble($row["amount"]),
  661. 'commercial' => $row["commercial"],
  662. 'when' => json_encode($row["when"])
  663. ]);
  664. }
  665. $this->currentReceip = $receipt;
  666. }
  667. }
  668. public function removeReceipt()
  669. {
  670. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  671. $receipt->status = 99;
  672. $receipt->save();
  673. $this->currentReceip = $receipt;
  674. }
  675. function currencyToDouble($val)
  676. {
  677. $x = str_replace("€", "", $val);
  678. $x = str_replace(".", "", $x);
  679. $x = str_replace(",", ".", $x);
  680. return floatval(trim($x));
  681. }
  682. public function addRow()
  683. {
  684. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  685. $this->emit('load-select');
  686. }
  687. public function delRow($idx)
  688. {
  689. unset($this->rows[$idx]);
  690. // $this->emit('load-select');
  691. }
  692. public function addPeriod($idx)
  693. {
  694. $x = sizeof($this->rows[$idx]['when']) - 1;
  695. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  696. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  697. }
  698. public function delPeriod($idx, $xxx)
  699. {
  700. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  701. //unset($this->rows[$idx]['when'][$xxx]);
  702. // $this->emit('load-select');
  703. }
  704. public function getTotal()
  705. {
  706. $total = 0.00;
  707. foreach($this->rows as $r)
  708. {
  709. if ($r["amount"] != null && $r["amount"] != "")
  710. {
  711. $total += $this->currencyToDouble($r["amount"]);
  712. if ($r["vat_id"] > 0)
  713. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  714. }
  715. }
  716. return formatPrice($total);
  717. // $this->emit('load-select');
  718. }
  719. public function getPrice()
  720. {
  721. $total = 0.00;
  722. foreach($this->rows as $r)
  723. {
  724. if ($r["amount"] != null && $r["amount"] != "")
  725. $total += $this->currencyToDouble($r["amount"]);
  726. }
  727. return formatPrice($total);
  728. // $this->emit('load-select');
  729. }
  730. public function getVat()
  731. {
  732. $total = 0.00;
  733. foreach($this->rows as $r)
  734. {
  735. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  736. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  737. }
  738. return formatPrice($total);
  739. // $this->emit('load-select');
  740. }
  741. public function getVats()
  742. {
  743. $vats = array();
  744. foreach($this->rows as $r)
  745. {
  746. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  747. {
  748. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  749. $vatName = "";
  750. foreach($this->vats as $v)
  751. {
  752. if ($v->id == $r["vat_id"])
  753. $vatName = $v->name;
  754. }
  755. if (isset($vats[$vatName]))
  756. $vats[$vatName] += $vat;
  757. else
  758. $vats[$vatName] = $vat;
  759. }
  760. }
  761. return $vats;
  762. // $this->emit('load-select');
  763. }
  764. public function setCausal($id, $idx)
  765. {
  766. $this->rows[$idx]["causal_id"] = $id;
  767. }
  768. public function printReceipt()
  769. {
  770. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  771. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip))->output();
  772. return response()->streamDownload(
  773. fn () => print($pdf),
  774. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  775. );
  776. /*return response()->streamDownload(function () {
  777. echo $pdf->stream();
  778. }, 'test.pdf');*/
  779. }
  780. public function multiPeriod()
  781. {
  782. $this->multiP = true;
  783. }
  784. public function multiPeriodCreate($idx)
  785. {
  786. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  787. foreach ($period as $dt) {
  788. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  789. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  790. }
  791. $this->multiP = false;
  792. }
  793. public function multiPeriodCancel()
  794. {
  795. $this->multiP = false;
  796. }
  797. }