All files / src/entities quote.ts

17.82% Statements 18/101
0% Branches 0/54
7.14% Functions 1/14
19.27% Lines 16/83

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141      1x 1x 1x           1x 1x 1x 1x   1x     1x     1x     1x     1x     1x     1x     1x                                                                                                                                                                                                           1x  
import { Assignment } from './assignment.js';
import { QuoteItem } from './quote_item.js';
import { Shipment } from './shipment.js';
import { Invoice } from './invoice.js';
import { Entity } from '../entity.js';
import { kahanSum } from '../util/float.js';
 
interface CalculateOptions {
  strictEmbed?: boolean;
}
 
export class Quote extends Entity {
  protected static resourceName = 'quotes';
  protected static singularName = 'quote';
  protected static pluralName = 'quotes';
 
  @Quote.property({type: Date})
  public archived?: Date | null;
 
  @Quote.property()
  public currency?: string;
 
  @Quote.property()
  public id?: number;
 
  @Quote.property({type: Date})
  public agreedDeadline?: Date | null;
 
  @Quote.property({arrayType: 'Shipment'})
  public shipments?: Shipment[];
 
  @Quote.property({arrayType: 'QuoteItem'})
  public quoteItems?: QuoteItem[];
 
  @Quote.property({arrayType: 'Assignment'})
  public assignments?: Assignment[];
 
  @Quote.property({type: Invoice})
  public invoice?: Invoice | null;
 
  public quoteTotal = (options?: CalculateOptions) => {
    const { strictEmbed = true } = options ? options : {};
    const { quoteItems = [], shipments = [] } = this;
    Iif (strictEmbed && this.quoteItems === undefined) {
      throw new Error('quoteItems is undefined, did you forget to embed it?');
    }
    Iif (strictEmbed && this.shipments === undefined) {
      throw new Error('shipments is undefined, did you forget to embed it?');
    }
    const quoteItemsTotal = kahanSum(quoteItems.map((qI: QuoteItem) =>
      parseFloat(qI.calculateTotal(options)))).toFixed(3);
    const shipmentItemsTotal = kahanSum(shipments.map((s: Shipment) =>
      parseFloat(s.calculateTotal(options)))).toFixed(3);
    return (
      parseFloat(quoteItemsTotal) + parseFloat(shipmentItemsTotal)
    ).toFixed(3);
  };
 
  public calculateTotal = this.quoteTotal;
 
  public calculateSubTotal = (options?: CalculateOptions) => {
    const { strictEmbed = true } = options ? options : {};
    const { quoteItems = [], shipments = [] } = this;
    Iif (strictEmbed && this.quoteItems === undefined) {
      throw new Error('quoteItems is undefined, did you forget to embed it?');
    }
    Iif (strictEmbed && this.shipments === undefined) {
      throw new Error('shipments is undefined, did you forget to embed it?');
    }
    const quoteItemsTotal = kahanSum(quoteItems.map((qI: QuoteItem) =>
      parseFloat(qI.calculateSubTotal(options))));
    const shipmentItemsTotal = kahanSum(shipments.map((s: Shipment) =>
      parseFloat(s.calculateSubTotal(options))));
    return (quoteItemsTotal + shipmentItemsTotal).toFixed(3);
  };
 
  public calculateTaxAmount = (options?: CalculateOptions) => {
    const { strictEmbed = true } = options ? options : {};
    const { quoteItems = [], shipments = [] } = this;
    Iif (strictEmbed && this.quoteItems === undefined) {
      throw new Error('quoteItems is undefined, did you forget to embed it?');
    }
    Iif (strictEmbed && this.shipments === undefined) {
      throw new Error('shipments is undefined, did you forget to embed it?');
    }
    const quoteItemsTotal = kahanSum(quoteItems.map((qI: QuoteItem) =>
      parseFloat(qI.calculateTaxAmount(options))));
    const shipmentItemsTotal = kahanSum(shipments.map((s: Shipment) =>
      parseFloat(s.calculateTaxAmount(options))));
    return (quoteItemsTotal + shipmentItemsTotal).toFixed(3);
  };
 
  public findQuoteItemIndex = (quoteItemId: number) => {
    Iif (this.quoteItems === undefined) {
      throw new Error('quoteItems is undefined, did you forget to embed it?');
    }
    function checkEqualId(quoteItem: QuoteItem) {
      return quoteItem.id === quoteItemId;
    }
    return this.quoteItems.findIndex(checkEqualId);
  };
 
  public removeQuoteItem = (quoteItem: QuoteItem) => {
    Iif (this.quoteItems === undefined) {
      throw new Error('quoteItems is undefined, did you forget to embed it?');
    }
    Iif (quoteItem.id === undefined) {
      throw new Error('quoteItem.id is undefined, did you forget to embed it?');
    }
    const index = this.findQuoteItemIndex(quoteItem.id);
    Iif (index > -1) {
      this.quoteItems.splice(index, 1);
    }
  };
 
  public deadlineTimeDifference = () => {
    Iif (this.agreedDeadline === undefined) {
      const err = 'agreedDeadline is undefined, did you forget to embed it?';
      throw new Error(err);
    }
    Iif (this.assignments === undefined) {
      const err = 'assignments is undefiend, did you forget to embed it?';
      throw new Error(err);
    }
    Iif (this.assignments.length < 1) {
      return null;
    }
    Iif (this.agreedDeadline === null) {
      return null;
    }
    const assignment = this.assignments[0];
    Iif (assignment.productionDeadline === undefined) {
      const err = 'productionDeadline is undefined, did you forget to embed' +
        'it?';
      throw new Error(err);
    }
    const productionDeadline = assignment.productionDeadline;
    return productionDeadline.valueOf() - this.agreedDeadline.valueOf();
  };
}