All files / src/entities quote_item.ts

51.35% Statements 19/37
0% Branches 0/20
25% Functions 1/4
51.51% Lines 17/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x           1x 1x 1x 1x   1x     1x     1x     1x     1x     1x     1x     1x     1x                                                                 1x  
import { CountryTax } from './country_tax.js';
import { Entity } from '../entity.js';
import { Quote } from './quote.js';
 
interface CalculateOptions {
  strictEmbed?: boolean;
}
 
export class QuoteItem extends Entity {
  protected static resourceName = 'quote_items';
  protected static singularName = 'quoteItem';
  protected static pluralName = 'quoteItems';
 
  @QuoteItem.property({type: Date})
  public archived?: Date | null;
 
  @QuoteItem.property()
  public id?: number;
 
  @QuoteItem.property()
  public type?: number;
 
  @QuoteItem.property()
  public quantity?: number;
 
  @QuoteItem.property({type: String})
  public description?: string | null;
 
  @QuoteItem.property({type: Number})
  public unitPrice?: number | null;
 
  @QuoteItem.property({type: Number})
  public taxAmount?: number | null;
 
  @QuoteItem.property({type: CountryTax})
  public taxType?: CountryTax;
 
  @QuoteItem.property()
  public quote?: Quote;
 
  public calculateSubTotal = (options?: CalculateOptions) => {
    const { strictEmbed = true } = options ? options : {};
    Iif (strictEmbed){
      Iif (this.unitPrice === undefined) {
        throw new Error('unitPrice is undefined, did you forget to embed it?');
      }
      Iif (this.quantity === undefined) {
        throw new Error('quantity is undefined, did you forget to embed it?');
      }
    }
    const quant = this.quantity ? this.quantity : 0;
    const unitPrice = this.unitPrice ? this.unitPrice : 0;
    return (quant * unitPrice).toFixed(3);
  };
 
  public calculateTaxAmount = (options?: CalculateOptions) => {
    const taxPercent = this.taxType && this.taxType.taxPercent ?
      this.taxType.taxPercent : 0;
    const taxRate = taxPercent ? Number(taxPercent) / 100 : 0;
    return (parseFloat(
      this.calculateSubTotal(options)) * taxRate).toFixed(3);
  };
 
  public calculateTotal = (options?: CalculateOptions) => {
    return (
      parseFloat(this.calculateSubTotal(options)) +
      parseFloat(this.calculateTaxAmount(options))
    ).toFixed(3);
  };
 
}