Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.90 KB | None | 0 0
  1. import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
  2. import { FormBuilder, FormGroup, FormArray, FormControl, Validators } from '@angular/forms';
  3. import { Location } from '@angular/common';
  4. import { MatSnackBar } from '@angular/material';
  5. import { Subject, Observable, pipe } from 'rxjs';
  6. import { takeUntil, take, delay, startWith, map, debounceTime } from 'rxjs/operators';
  7.  
  8. import { Router } from '@angular/router';
  9.  
  10. import { fuseAnimations } from '@layout/animations';
  11. import { FuseUtils } from '@layout/utils';
  12.  
  13. import { Category } from 'app/main/apps/inventory/category/category.model';
  14. import { InventoryCategoryService } from 'app/main/apps/inventory/category/category.service';
  15.  
  16. import { ImageCroppedEvent } from 'ngx-image-cropper';
  17.  
  18. import { MatDialog } from '@angular/material/dialog';
  19. import { PhotoPickerComponent } from 'app/photo-picker/photo-picker.component';
  20. import { InvoiceCorrectionService } from './correction.service';
  21. import { Invoice, Client, InvoiceProduct } from '../new/invoice.model';
  22. import { ClientComponent } from '../client/client.component';
  23. import { searchProducts } from 'app/db-queries';
  24. import * as _ from 'lodash';
  25. import { by } from 'protractor';
  26.  
  27. @Component({
  28. selector : 'new-invoice',
  29. templateUrl : './correction.component.html',
  30. styleUrls : ['./correction.component.scss'],
  31. encapsulation: ViewEncapsulation.None,
  32. animations : fuseAnimations
  33. })
  34. export class InvoiceCorrectionComponent implements OnInit, OnDestroy
  35. {
  36.  
  37.  
  38. //products before, products correction ( netto, brutto, tax, tax_by_vat)
  39.  
  40. //todo: dodać pole całość, przy zaliczce wyłączyć pole termin płatności, zmienić label data wykonnaie usługi, na płatność
  41. invoice: any;
  42. saleInvoice: any;
  43. searching: boolean;
  44. searchingProduct: boolean;
  45. invoiceForm: FormGroup;
  46. clients: Client[]
  47. filteredClients: Client[];
  48. searchedProducts: Client[];
  49. before_correction: any;
  50. correction: any;
  51. // orders: any;
  52.  
  53. // Private
  54. private _unsubscribeAll: Subject<any>;
  55.  
  56. /**
  57. * Constructor
  58. *
  59. * @param {CategoryService} _categoryService
  60. * @param {FormBuilder} _formBuilder
  61. * @param {Location} _location
  62. * @param {MatSnackBar} _matSnackBar
  63. */
  64. constructor(
  65. private _invoiceNewService: InvoiceCorrectionService,
  66. private _formBuilder: FormBuilder,
  67. private _location: Location,
  68. private _matSnackBar: MatSnackBar,
  69. public dialog: MatDialog,
  70. private router: Router
  71. )
  72. {
  73.  
  74. this.invoice = new Invoice();
  75. this.invoice.creation_date = new Date();
  76. this.searching = false;
  77. this.searchingProduct = false;
  78. this.searchedProducts = [];
  79. console.log('construct');
  80. // Set the private defaults
  81. this._unsubscribeAll = new Subject();
  82.  
  83.  
  84. this.filteredClients = [];
  85. }
  86.  
  87.  
  88. // -----------------------------------------------------------------------------------------------------
  89. // @ Lifecycle hooks
  90. // -----------------------------------------------------------------------------------------------------
  91.  
  92. /**
  93. * On init
  94. */
  95. ngOnInit(): void
  96. {
  97.  
  98.  
  99. this.invoiceForm = this.createForm();
  100. this.invoiceForm.get('invoice_type').setValue('4');
  101. this.invoiceForm.get('invoice_type').disable();
  102.  
  103. //this.addProduct();
  104.  
  105. console.log('init');
  106.  
  107. this._invoiceNewService.onSaleInvoiceChange
  108. .pipe(takeUntil(this._unsubscribeAll))
  109. .subscribe(invoice => {
  110. this.saleInvoice = invoice;
  111. this.invoice.reference_invoice = this.saleInvoice.id;
  112.  
  113. if(this.saleInvoice.id){
  114. this.saleInvoice.products.forEach(product => {
  115. this.addCorrectionProducts(product);
  116. });
  117.  
  118. this.invoiceForm.get('client').setValue(this.saleInvoice.client);
  119. this.invoice.seller = this.saleInvoice.seller;
  120.  
  121. //kopie produktow i ceny przed korekta
  122. this.before_correction = {
  123. products: this.saleInvoice.products,
  124. total_brutto: this.saleInvoice.total_brutto,
  125. total_netto: this.saleInvoice.total_netto,
  126. total_tax: this.saleInvoice.total_tax,
  127. total_by_vat: this.saleInvoice.total_by_vat
  128. }
  129.  
  130. }
  131. }
  132. );
  133.  
  134.  
  135. this._invoiceNewService.onAdvancedPayments
  136. .pipe(takeUntil(this._unsubscribeAll))
  137. .subscribe((advancedPayments) => {
  138. this.invoice.advanced_payments = advancedPayments;
  139.  
  140. this.invoiceForm.get('payment_brutto').setValidators([Validators.max(this.calculateOutstandingAmount(this.invoice.total_brutto, 0))]);
  141.  
  142.  
  143. });
  144.  
  145.  
  146. this._invoiceNewService.onSellerChange
  147. .pipe(takeUntil(this._unsubscribeAll))
  148. .subscribe((seller) => {
  149. this.invoice.seller = seller.company_info;
  150. this.invoice.seller.logo = seller.logo
  151. console.log(this.invoice.seller, 'seller');
  152.  
  153. });
  154.  
  155. //todo: zmienić na nasłuchiwanie tylko jednego pola
  156. this.invoiceForm.valueChanges.
  157. pipe(
  158.  
  159. startWith(''),
  160. debounceTime(300),
  161. map((value: any) => {
  162. console.log(value, 'value filtered');
  163.  
  164. console.log(value, 'kekeke');
  165.  
  166. return typeof value.client === 'string' ? value.client : value.name
  167.  
  168.  
  169. }),
  170. map(text => text && text.length >= 2 ? this._searchClients(text) : this.filteredClients = [])
  171. ).subscribe();
  172.  
  173.  
  174.  
  175.  
  176.  
  177. this.searchProduct.valueChanges
  178. .pipe(
  179. startWith(''),
  180. map(value => value && value.length >= 2 ? this._searchProducts(value): this.searchedProducts = [])
  181. ).subscribe();
  182.  
  183.  
  184. this.invoiceForm.get("creation_date").valueChanges.subscribe(()=>{
  185. this.getInvoiceNumber();
  186. })
  187.  
  188. this.invoiceForm.get("payment_brutto").valueChanges.subscribe(()=>{
  189. this.invoice.outstanding_amount = this.calculateOutstandingAmount(this.invoice.total_brutto, this.invoiceForm.get('payment_brutto').value);
  190.  
  191. let payment_brutto = this.invoiceForm.get('payment_brutto').value;
  192. let payment_by_vat = [];
  193. if(this.saleInvoice.id){
  194. let total_payment_vat = 0;
  195. let total_payment_netto = 0
  196. let total_payment_brutto = 0
  197.  
  198. this.saleInvoice.total_by_vat.forEach(byVat => {
  199.  
  200. //vat /netto /brutto /tax
  201.  
  202. console.log(byVat, 'byvat');
  203.  
  204. let brutto = (Math.round((byVat.brutto / this.saleInvoice.total_brutto * payment_brutto) * 100) / 100);
  205. let netto = this.calculateTotalNetto(1, brutto, byVat.vat );
  206. let tax = this.calculateTotalTax(brutto, byVat.vat)
  207.  
  208.  
  209. total_payment_vat += tax;
  210. total_payment_netto += netto;
  211. total_payment_brutto += brutto;
  212. payment_by_vat.push({vat: byVat.vat, brutto: brutto, netto: netto, tax: tax});
  213. })
  214.  
  215. this.invoice.payment_netto = Math.round(total_payment_netto *100)/100;
  216. this.invoice.payment_tax = Math.round(total_payment_vat *100)/100;
  217. this.invoice.payment_by_vat = payment_by_vat;
  218. }
  219. })
  220.  
  221. this.getInvoiceNumber();
  222.  
  223. }
  224.  
  225.  
  226. get searchProduct(): FormControl {
  227. //return this.productForm.get('name') as FormArray;
  228. return this.invoiceForm.get('searchProduct') as FormControl
  229. }
  230.  
  231.  
  232. get products(): FormArray {
  233.  
  234. return this.invoiceForm.get('products') as FormArray;
  235. }
  236.  
  237. getInvoiceNumber() {
  238. let type = parseInt(this.invoiceForm.get('invoice_type').value);
  239. let date = new Date(this.invoiceForm.get('creation_date').value).getTime();
  240.  
  241. console.log(type, date);
  242.  
  243. this._invoiceNewService.getInvoiceNumber(date,type).then(data => {
  244. this.invoiceForm.get('prefix').setValue(data.prefix);
  245. this.invoiceForm.get('number').setValue(data.number);
  246. this.invoice.inv_number = data.inv_number;
  247. })
  248. }
  249.  
  250.  
  251. disablePaymentField() {
  252. console.log('disable');
  253. this.invoiceForm.get('payment_brutto').setValue(0);
  254. this.invoiceForm.get('payment_brutto').disable();
  255. }
  256.  
  257.  
  258.  
  259. enablePaymentField(){
  260. this.invoiceForm.get('payment_brutto').enable();
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267. addCorrectionProducts = (product) => {
  268. this.products.push(this.createInvoiceProduct(product));
  269. this.calculateSummaries(this.products);
  270. this.invoiceForm.get('products');
  271.  
  272. //todo: skopiować do before_correction.products
  273. }
  274.  
  275. addProduct = (product = null) => {
  276. let newInvoiceProduct = {id: new Date().getTime(), name: '', count: 1, brutto: 0, vat: 0, total_netto: 0, total_brutto: 0, total_tax: 0};
  277.  
  278.  
  279.  
  280.  
  281. newInvoiceProduct.name = this.productNameWithAttribute(product);
  282. newInvoiceProduct.vat = product.vat / 100;
  283. newInvoiceProduct.brutto = product.price
  284. newInvoiceProduct.total_brutto = this.calculateTotalBrutto(newInvoiceProduct.count, newInvoiceProduct.brutto );
  285. newInvoiceProduct.total_netto = this.calculateTotalNetto(1, newInvoiceProduct.brutto, newInvoiceProduct.vat );
  286. newInvoiceProduct.total_tax = this.calculateTotalTax(newInvoiceProduct.total_brutto, newInvoiceProduct.vat);
  287.  
  288.  
  289.  
  290.  
  291. this.products.push(this.createInvoiceProduct(newInvoiceProduct));
  292.  
  293. //naslcuhiwanie zmian i obliczanie cen netto, brutto, vat,tax
  294.  
  295. this.products.controls.forEach((control, index) => {
  296. control.get('brutto').valueChanges.subscribe((data)=> {
  297.  
  298. control.get('total_brutto').setValue(this.calculateTotalBrutto(control.get('count').value, control.get('brutto').value));
  299. control.get('total_netto').setValue(this.calculateTotalNetto(control.get('count').value, control.get('brutto').value,control.get('vat').value));
  300. control.get('total_tax').setValue(this.calculateTotalTax(control.get('total_brutto').value,control.get('vat').value))
  301.  
  302. this.calculateSummaries(this.products);
  303. })
  304.  
  305. control.get('vat').valueChanges.subscribe((data)=> {
  306.  
  307. control.get('total_brutto').setValue(this.calculateTotalBrutto(control.get('count').value, control.get('brutto').value));
  308. control.get('total_netto').setValue(this.calculateTotalNetto(control.get('count').value, control.get('brutto').value,control.get('vat').value));
  309. control.get('total_tax').setValue(this.calculateTotalTax(control.get('total_brutto').value,control.get('vat').value));
  310.  
  311. this.calculateSummaries(this.products);
  312. })
  313.  
  314.  
  315. control.get('count').valueChanges.subscribe((data)=> {
  316.  
  317. control.get('total_brutto').setValue(this.calculateTotalBrutto(control.get('count').value, control.get('brutto').value));
  318. control.get('total_netto').setValue(this.calculateTotalNetto(control.get('count').value, control.get('brutto').value,control.get('vat').value));
  319. control.get('total_tax').setValue(this.calculateTotalTax(control.get('total_brutto').value,control.get('vat').value))
  320.  
  321. this.calculateSummaries(this.products);
  322. })
  323. })
  324.  
  325.  
  326. this.calculateSummaries(this.products);
  327. this.cleanProductSearch();
  328. }
  329.  
  330. createInvoiceProduct(product : InvoiceProduct): FormGroup {
  331.  
  332. let group = this._formBuilder.group({
  333. id: product.id || '',
  334. name: product.name || '',
  335. count: product.count || 0,
  336. brutto: product.brutto || 0,
  337. vat: product.vat || 0,
  338. total_netto: {value:product.total_netto || 0, disabled: true},
  339. total_brutto: {value: product.total_brutto || 0,disabled: true },
  340. total_tax: {value:product.total_tax || 0, disabled: true}
  341. })
  342.  
  343.  
  344. return group;
  345. }
  346.  
  347.  
  348.  
  349. /**
  350. * On destroy
  351. */
  352. ngOnDestroy(): void
  353. {
  354. console.log('dESTROOOOOY');
  355. // Unsubscribe from all subscriptions
  356. this._unsubscribeAll.next();
  357. this._unsubscribeAll.complete();
  358. }
  359.  
  360.  
  361. compareState = (val1: number, val2: number) => {
  362.  
  363. return val1 === val2;
  364. }
  365.  
  366. clientDisplayWith(client?: Client): string | undefined {
  367. return client ? client.name : undefined;
  368. }
  369.  
  370.  
  371. productDisplayWith(product?): string | undefined {
  372. const name = product? product.name[0].text : undefined;
  373. const attr = product? product.attr_name[0].text || undefined : undefined;
  374.  
  375. return name? name + (attr? ' ' + attr : '') : undefined;
  376. }
  377.  
  378. openClientDialog() {
  379. const dialogRef = this.dialog.open(ClientComponent, {
  380. maxWidth: '80vw',
  381. });
  382.  
  383. dialogRef.afterClosed().subscribe(result => {
  384. console.log(result);
  385.  
  386. if(result){
  387. this.filteredClients.push(result)
  388. this.invoiceForm.get('client').setValue(result);
  389. }
  390. });
  391. }
  392.  
  393. cleanProductSearch(){
  394. this.searchProduct.setValue('');
  395. this.searchedProducts = [];
  396. }
  397.  
  398.  
  399. removeProduct = (index) => {
  400. this.products.removeAt(index);
  401. this.calculateSummaries(this.products);
  402. }
  403.  
  404.  
  405.  
  406. private _searchClients(text: string): void {
  407. console.log('searchiiiiiing');
  408. this.searching = true;
  409. const filterValue = text.toLowerCase();
  410.  
  411. console.log(filterValue, 'filterValue')
  412.  
  413.  
  414. this._invoiceNewService.getClients(filterValue).then((clients: Client[]) => {
  415. console.log(clients, 'klients');
  416. this.searching = false;
  417. this.filteredClients = clients;
  418. return clients;
  419. });
  420. }
  421.  
  422.  
  423.  
  424. //todo przenieść getProducts do modelu produktu
  425. private _searchProducts(text: string): void {
  426. console.log('searching product', text);
  427. this.searchingProduct = true;
  428. const filterValue = text.toLowerCase();
  429.  
  430. console.log(filterValue, 'filterValue')
  431.  
  432.  
  433. this._invoiceNewService.getProducts(filterValue).then((products: any) => {
  434.  
  435. this.searchingProduct = false;
  436. this.searchedProducts = products;
  437.  
  438. console.log(this.searchedProducts, 'searchedProducts');
  439. return products;
  440. });
  441. }
  442.  
  443.  
  444.  
  445.  
  446. /**
  447. * Create product form
  448. *
  449. * @returns {FormGroup}
  450. */
  451. createForm(): FormGroup
  452. {
  453. let form = this._formBuilder.group({
  454. invoice_type : [this.invoice.invoice_type.toString()],
  455. client: [this.invoice.client],
  456. products: this._formBuilder.array([
  457. ]),
  458. creation_date: [this.invoice.creation_date],
  459. creation_place: [this.invoice.creation_place],
  460. execution_date: [this.invoice.execution_date],
  461. payment_type: [this.invoice.payment_type],
  462. payment_deadline: [this.invoice.payment_deadline],
  463. payment_brutto: [this.invoice.payment_brutto],
  464. payment_deadline_days: [this.invoice.payment_deadline_days],
  465. prefix: [this.invoice.prefix],
  466. number: [this.invoice.number],
  467. note: [this.invoice.note],
  468. searchProduct: [''],
  469. });
  470.  
  471. return form;
  472. }
  473.  
  474.  
  475.  
  476.  
  477.  
  478. productNameWithAttribute(product) : string | undefined{
  479. const name = product? product.name[0].text : undefined;
  480. const attr = product? product.attr_name[0].text || undefined : undefined;
  481.  
  482. return name? name + (attr? ' ' + attr : '') : undefined;
  483. }
  484.  
  485. //calculations
  486.  
  487.  
  488. //KP = WARTOŚĆ BRUTTO * SP / 100 + SP
  489.  
  490. calculateTotalNetto(quantity, brutto, vat){
  491.  
  492. const total_brutto = this.calculateTotalBrutto(quantity , brutto);
  493. const total_vat = this.calculateTotalTax(total_brutto, vat);
  494.  
  495. return Math.round((total_brutto - total_vat) * 100) / 100
  496. }
  497.  
  498. calculateTotalTax(total_brutto, vat){
  499. return Math.round((total_brutto * vat) / (100 + vat) * 100) / 100;
  500. }
  501.  
  502. calculateTotalBrutto(quantity, brutto){
  503. return quantity * brutto;
  504. }
  505.  
  506.  
  507. calculateSummaryBrutto(products){
  508. return parseFloat(products.map(a => a.total_brutto).reduce((a,b) => a + b, 0).toFixed(2));
  509. }
  510.  
  511. calculateSummaryNetto(products){
  512. return parseFloat(products.map(a=> a.total_netto).reduce((a,b) => a + b, 0).toFixed(2));
  513. }
  514.  
  515.  
  516. calculateSummaryTax(products){
  517. return parseFloat(products.map(product => product.total_brutto - product.total_netto).reduce((a,b) => a + b, 0).toFixed(2));
  518. }
  519.  
  520.  
  521. calculateSummaries(products){
  522. let vatSummaryBrutto = []
  523.  
  524. console.log(products, 'wwa');
  525.  
  526.  
  527. products.getRawValue().forEach(product => {
  528.  
  529.  
  530. if(!vatSummaryBrutto.length){
  531. vatSummaryBrutto.push({vat: product.vat , sum: product.total_brutto});
  532.  
  533. } else {
  534. let index= _.findIndex(vatSummaryBrutto, {'vat': product.vat});
  535.  
  536. console.log(index, 'index');
  537.  
  538. if (index >=0){
  539. vatSummaryBrutto[index].sum += product.total_brutto;
  540. } else {
  541. vatSummaryBrutto.push({vat: product.vat , sum: product.total_brutto});
  542. }
  543. }
  544.  
  545. });
  546.  
  547. let total_vat = 0;
  548. let total_netto = 0;
  549. let total_brutto = 0;
  550. let total_by_vat = [];
  551.  
  552.  
  553. console.log(total_vat, total_netto, total_brutto);
  554.  
  555. vatSummaryBrutto.forEach(prod => {
  556. let vat = Math.round((prod.sum * prod.vat) / (100 + prod.vat) * 100) / 100;
  557. let netto = Math.round((prod.sum - vat) * 100) / 100
  558.  
  559.  
  560. console.log(vat, netto, 'vatnetto');
  561.  
  562.  
  563. total_vat += vat;
  564. total_netto += netto;
  565. total_brutto += prod.sum;
  566. total_by_vat.push({vat: prod.vat, netto: netto, brutto: prod.sum, tax: vat });
  567.  
  568.  
  569.  
  570. });
  571.  
  572.  
  573.  
  574.  
  575.  
  576. this.invoice.total_brutto = Math.round(total_brutto *100)/100;
  577. this.invoice.total_netto = Math.round(total_netto *100)/100;
  578. this.invoice.total_tax = Math.round(total_vat * 100)/100;
  579. this.invoice.total_by_vat = total_by_vat;
  580. this.invoice.outstanding_amount = this.calculateOutstandingAmount(this.invoice.total_brutto, this.invoiceForm.get('payment_brutto').value);
  581.  
  582. }
  583.  
  584.  
  585.  
  586. calculateFinalSummaries(total_by_vat, advancedPayments){
  587.  
  588. const vatFinalSummaryBrutto = total_by_vat.map(byVat => {
  589. advancedPayments.forEach(advance => {
  590. advance.payment_by_vat.forEach(pbv => {
  591. console.log(pbv, byVat);
  592. if(byVat.vat == pbv.vat){
  593. byVat.brutto -= pbv.brutto
  594. }
  595. });
  596.  
  597. });
  598.  
  599. return {vat: byVat.vat, brutto: Math.round(byVat.brutto*100)/100}
  600. })
  601.  
  602.  
  603.  
  604. let total_vat = 0;
  605. let total_netto = 0;
  606. let total_brutto = 0;
  607. total_by_vat = [];
  608.  
  609. vatFinalSummaryBrutto.forEach(prod => {
  610. let vat = Math.round((prod.brutto * prod.vat) / (100 + prod.vat) * 100) / 100;
  611. let netto = Math.round((prod.brutto - vat) * 100) / 100
  612.  
  613.  
  614. console.log(vat, netto, 'vatnetto');
  615.  
  616.  
  617. total_vat += vat;
  618. total_netto += netto;
  619. total_brutto += prod.brutto;
  620. total_by_vat.push({vat: prod.vat, netto: netto, brutto: prod.brutto, tax: vat });
  621.  
  622. });
  623.  
  624.  
  625. this.invoice.total_brutto = Math.round(total_brutto *100)/100;
  626. this.invoice.total_netto = Math.round(total_netto *100)/100;
  627. this.invoice.total_tax = Math.round(total_vat * 100)/100;
  628. this.invoice.total_by_vat = total_by_vat;
  629.  
  630.  
  631. }
  632.  
  633. calculateOutstandingAmount(total, total_payment){
  634. let advancedTotal = 0;
  635.  
  636. this.invoice.advanced_payments.forEach(advanced => {
  637. advancedTotal+=advanced.payment_brutto
  638. });
  639.  
  640. console.log(Math.round((total-advancedTotal-total_payment)*100)/100, 'outstanding amount');
  641. return Math.round((total - advancedTotal - total_payment) * 100) / 100
  642. }
  643.  
  644. calculateCorrection(beforeCorrection){
  645.  
  646. let tempCorrection = this.invoice.total_by_vat.map(totalbyVat => {
  647.  
  648. let vat = totalbyVat.vat;
  649.  
  650. beforeCorrection.forEach(bC => {
  651. totalbyVat.brutto -= bC.brutto;
  652. })
  653.  
  654. return { vat: vat, brutto: totalbyVat.brutto }
  655.  
  656. })
  657.  
  658. console.log(tempCorrection, 'temp correction grouped by vat')
  659.  
  660. let correction_by_vat = [];
  661. let correction_netto = 0;
  662. let correction_brutto = 0;
  663. let correction_tax = 0;
  664.  
  665.  
  666. tempCorrection.forEach(prod => {
  667. let vat = Math.round((prod.brutto * prod.vat) / (100 + prod.vat) * 100) / 100;
  668. let netto = Math.round((prod.brutto - vat) * 100) / 100
  669.  
  670.  
  671. console.log(vat, netto, 'vatnetto');
  672.  
  673.  
  674. correction_tax += vat;
  675. correction_netto += netto;
  676. correction_brutto += prod.brutto;
  677. correction_by_vat.push({vat: prod.vat, netto: netto, brutto: prod.brutto, tax: vat });
  678.  
  679. });
  680.  
  681.  
  682. this.correction.total_brutto = Math.round(correction_brutto *100)/100;
  683. this.correction.total_netto = Math.round(correction_netto *100)/100;
  684. this.correction.total_tax = Math.round(correction_tax * 100)/100;
  685. this.correction.total_by_vat = correction_by_vat;
  686.  
  687.  
  688.  
  689. console.log(this.correction.total_by_vat, 'correction grouped by vat, result')
  690. }
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697. /**
  698. * Add category
  699. */
  700. addInvoice(): void {
  701. let data = this.invoiceForm.getRawValue();
  702.  
  703.  
  704. this.calculateFinalSummaries(this.invoice.total_by_vat, this.invoice.advanced_payments);
  705.  
  706. //const oldValues = parsedClient;
  707. let newValues = {...this.invoice, ...data};
  708.  
  709.  
  710. newValues.execution_date = new Date(newValues.execution_date ).getTime();
  711. newValues.creation_date = new Date(newValues.creation_date ).getTime();
  712. newValues.invoice_type = parseInt(newValues.invoice_type);
  713.  
  714.  
  715.  
  716.  
  717. newValues.client.__typename = undefined;
  718. newValues.client.addressObj.__typename = undefined;
  719. newValues.seller.__typename = undefined;
  720. newValues.seller.addressObj.__typename = undefined;
  721. newValues.payment_deadline = new Date(newValues.payment_deadline ).getTime();
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729. console.log(newValues, 'new invoice');
  730.  
  731. this._invoiceNewService.addInvoice(newValues)
  732. .then((d) => {
  733.  
  734. // Trigger the subscription with new data
  735. this._invoiceNewService.onInvoiceChanged.next(d.data.createInvoice);
  736.  
  737. // Show the success message
  738. this._matSnackBar.open('Faktura zapisana', 'OK', {
  739. verticalPosition: 'top',
  740. duration: 2000
  741. });
  742.  
  743. // Change the location with new one
  744. this._location.go('apps/invoices/details/' + d.data.createInvoice.id);
  745. this.router.navigate(['/invoices/details/' + d.data.createInvoice.id]);
  746. });
  747. }
  748.  
  749. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement