Advertisement
Guest User

Untitled

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