Example 2 - Multiple Facets & References

Extending the Simple Quote Calculator with additional facets

Change the manufacturing cost for all line items by adjusting the manufacturing_cost_per_m2 parameter in the raw_materials facet

Set a minimum Margin Threshold for the quote through the low_margin_threshold parameter in the gross_margin facet

Item Steel m2 Item Cost Unit Price Quantity Line Cost Line Offer Gross Margin
No matching records found.
Profit:

Create and populate the noSheet table:

 1    //
 2    //create and define the table
 3    //        
 4    let new_table = createTable(['item', 'steel_m2', 'unit_offer', 'quantity'], raw_materials, lineitems, gross_margin);
 5
 6    //The raw_materials facet requires two external references; manufacturing_cost_per_m2 and average_steel_cost_m2.
 7    //Here we set average_steel_cost_m2 to be a constant
 8    //manufacturing_cost_per_m2 is set dynamically using Alpine    
 9    new_table.setReferences( function() {
10
11        this.average_steel_cost_m2 = 0.2;
12    });    
13
14    //
15    //populate the table
16    //
17    new_table.load(data);

The raw_materials facet:

1"use strict";
2/**
3Calculate line item cost from raw materials
4*/
5export default function(table, row, refs) {
6
7    row.unit_cost = () => row.steel_m2 * (refs.manufacturing_cost_per_m2 + refs.average_steel_cost_m2);
8};

The lineitems facet:

 1"use strict";
 2/**
 3A simple invoice/quotation calculator
 4*/
 5export default function(table, row) {
 6
 7    row.line_cost     = () => row.quantity * row.unit_cost;    
 8    row.line_offer    = () => row.quantity * row.unit_offer;
 9
10    table.total_quantity = () => SUM(this.column('quantity'));
11    table.total_cost     = () => SUM(this.column('line_cost'));
12    table.total_offer    = () => SUM(this.column('line_offer'));
13    table.profit         = () => table.total_offer - table.total_cost;
14};

The gross_margin facet:

 1"use strict";
 2/**
 3 * Calculate the gross margin for line items and for the whole table
 4 * 
 5 * @param {*} table     Table query object
 6 * @param {*} row       The table row
 7 * @param {*} refs      External references
 8 */
 9export default function(table, row, refs) {
10
11    /**
12     * The row Gross Margin
13     * @returns number
14     */    
15    row.gross_margin  = () => 1 - (row.unit_cost / row.unit_offer);
16
17    /**
18     * The total Gross Margin
19     * @returns number
20     */    
21    table.gross_margin = () => 1 - (table.total_cost / table.total_offer);
22
23    /**
24     * Returns true if the total Gross Margin is below the system threshold
25     * @returns boolean
26     */    
27    table.low_margin_warning = () => table.gross_margin <= refs.low_margin_threshold / 100;
28};