Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import _ from 'lodash';
- /**
- * Declare our private methods
- * @type {symbol}
- */
- const replaceVariables = Symbol( 'replaceVariables' );
- const replaceOptions = Symbol( 'replaceOptions' );
- const replaceTakeoffs = Symbol( 'replaceTakeoffs' );
- const configureClass = Symbol( 'configureClass' );
- class CalculationSolver{
- constructor(){
- this[ configureClass ]();
- }
- [ configureClass ](){
- this.debug = false;
- this.processSell = false;
- this.markup = 0;
- this.commission = 0;
- this.tax = 0;
- this.variables = {};
- this.takeoffs = {};
- this.options = {};
- this.material = false;
- this.calculation = "";
- this.material_cost = {
- original: 0,
- price: 0,
- tax: 0,
- commission: 0,
- markup: 0,
- };
- this.result = 0;
- this.is_removal = false;
- window.roundup = function( number, place = 0 ){
- let power = Math.pow( 10, place );
- return Math.ceil( number * power ) / power;
- };
- window.rounddown = function( number, place = 0 ){
- let power = Math.pow( 10, place );
- return Math.floor( number * power ) / power;
- };
- }
- //-- These are our setters
- /**
- * Enable or Disable Debug (Printed to console)
- * @param value
- */
- setDebug( value = false ){
- this.debug = value;
- }
- setIsRemoval( value ){
- this.is_removal = value;
- }
- /**
- * Tell the parse if it is going to process markup/commision/tax
- * @param value
- */
- setProcessSell( value = false ){
- this.processSell = value;
- }
- /**
- * This will set the markup's value (normally something like 0.1 *10%*)
- * @param value
- */
- setMarkup( value ){
- this.markup = value;
- }
- /**
- * This will set the commission value (normally something like 0.1 *10%*)
- * @param value
- */
- setCommission( value ){
- this.commission = value;
- }
- /**
- * This will set the tax rate of the system.
- * @param value
- */
- setTax( value ){
- this.tax = value;
- }
- /**
- * Set the variables object ({variable_id:parsed_calculation})
- * @param variables
- */
- setVariables( variables ){
- this.variables = variables;
- }
- /**
- * Set the takeoffs {takeoff_id: value}
- * @param takeoffs
- */
- setTakeoffs( takeoffs ){
- this.takeoffs = takeoffs;
- }
- /**
- * Set the options {option_id: quantity}
- * @param options
- */
- setOptions( options ){
- this.options = options;
- }
- /**
- * Sets the calculations material (if one exists... this is up to the user
- * @param material
- */
- setMaterial( material ){
- this.material = material;
- }
- /**
- * Set the calculation
- * @param calculation
- */
- setCalculation( calculation ){
- this.calculation = calculation;
- }
- //--Actions
- processCalculation(){
- let breakout = 1;
- while( this.calculation.includes( "{{" ) && breakout < 101 ){
- if( this.debug ){
- console.log( "Starting Run #" + breakout );
- console.log( "Calculation before variable replacement", this.calculation );
- }
- this.calculation = this[ replaceVariables ]( this.calculation );
- if( this.debug ){
- console.log( "Calculation after variable replacement", this.calculation );
- console.log( "Calculation before takeoff replacement", this.calculation );
- }
- this.calculation = this[ replaceTakeoffs ]( this.calculation );
- if( this.debug ){
- console.log( "Calculation after takeoff replacement", this.calculation );
- console.log( "Calculation before option replacement", this.calculation );
- }
- this.calculation = this[ replaceOptions ]( this.calculation );
- this.calculation = this.calculation.replace( /<brcontenteditable="false">/g, "" );
- if( this.debug ){
- console.log( "Calculation after option replacement", this.calculation );
- console.log( "Ending Run #" + breakout );
- console.log( "Calculation at end of run:", this.calculation );
- }
- breakout++;
- //Sometimes we have //<brcontenteditable="false">// and we need to kill it.
- }
- if( this.calculation && this.calculation !== '' ){
- try{
- let parser = new Function( 'calculation', 'return eval(calculation)' );
- this.result = parser( this.calculation );
- }
- catch( e ){
- console.warn( e );
- console.error( this.calculation, 'This calculation could not be parsed properly' );
- this.result = 0;
- }
- }
- if( this.is_removal ){
- this.result *= -1;
- }
- return this.result;
- }
- processComplexResults(){
- let result = {
- quantity: this.result,
- cost: this.material_cost.original / 100,
- total_cost: ( this.material_cost.original / 100 ) * this.result,
- tax: ( this.material_cost.tax / 100 ) * this.result,
- mark_up: ( this.material_cost.markup / 100 ) * this.result,
- commission: ( this.material_cost.commission / 100 ) * this.result,
- };
- result.up_charge_total = result.tax + result.commission + result.mark_up;
- result.total_sell_price = result.up_charge_total + result.total_cost;
- return result;
- }
- configureMaterialCosts(){
- this.material_cost.original = parseInt( this.material.price.amount );
- let base = this.material_cost.original;
- //We need to handle each markup type
- switch( parseInt( this.material.material_type_id ) ){
- case 1:
- //We first do the tax
- this.material_cost.calculated_tax = this.material_cost.original * this.tax;
- this.material_cost.tax += this.material_cost.calculated_tax;
- base += this.material_cost.calculated_tax;
- //Next comes the markup
- this.material_cost.calculated_markup = base * this.markup;
- this.material_cost.markup += this.material_cost.calculated_markup;
- this.material_cost.price += this.material_cost.calculated_markup;
- base += this.material_cost.calculated_markup;
- //Finally We deal with commission
- this.material_cost.calculated_commission = base * this.commission;
- this.material_cost.commission += this.material_cost.calculated_commission;
- base += this.material_cost.calculated_commission;
- if( this.debug ){
- console.log( "Adding in the required material costs", this.material_cost );
- }
- break;
- case 2:
- //First We will with the Markup
- this.material_cost.calculated_markup = base * this.markup;
- this.material_cost.markup += this.material_cost.calculated_markup;
- this.material_cost.price += this.material_cost.calculated_markup;
- base += this.material_cost.calculated_markup;
- //We take tax after markup
- this.material_cost.calculated_tax = this.material_cost.original * this.tax;
- this.material_cost.tax += this.material_cost.calculated_tax;
- base += this.material_cost.calculated_tax;
- //Last we deal with commission
- this.material_cost.calculated_commission = base * this.commission;
- this.material_cost.commission += this.material_cost.calculated_commission;
- base += this.material_cost.calculated_commission;
- if( this.debug ){
- console.log( "Adding in the required material costs", this.material_cost );
- }
- break;
- case 3:
- default:
- //First comes markup
- this.material_cost.calculated_markup = base * this.markup;
- this.material_cost.markup += this.material_cost.calculated_markup;
- this.material_cost.price += this.material_cost.calculated_markup;
- base += this.material_cost.calculated_markup;
- //Next is commission
- this.material_cost.calculated_commission = base * this.commission;
- this.material_cost.commission += this.material_cost.calculated_commission;
- base += this.material_cost.calculated_commission;
- if( this.debug ){
- console.log( "Adding in the required material costs", this.material_cost );
- }
- //We do NOT take tax here...
- break;
- }
- this.material_cost.price = base;
- if( this.debug ){
- console.log( this.material_cost );
- }
- }
- //Private Helpers
- [ replaceVariables ]( calculation ){
- let matches = [ ...new Set( calculation.match( /{{variables\.[0-9]+}}/gm ) ) ];
- if( matches ){
- matches.forEach( match => {
- let match_id = parseInt( match.replace( "{{variables.", "" ).replace( "}}", "" ) );
- let replacement = 0;
- if( this.variables[ match_id ] ){
- replacement = this.variables[ match_id ];
- }
- let regex_replacement = new RegExp( match, "g" );
- calculation = calculation.replace( regex_replacement, replacement );
- } );
- }
- return calculation;
- }
- [ replaceOptions ]( calculation ){
- let matches = [ ...new Set( calculation.match( /{{selected_options\.[0-9]+}}/gm ) ) ];
- if( matches ){
- matches.forEach( match => {
- // console.log( this.option.name + ' has selected options' );
- let match_id = parseInt( match.replace( "{{selected_options.", "" ).replace( "}}", "" ) );
- let replacement = 0;
- if( this.options[ match_id ] ){
- replacement = this.options[ match_id ];
- }
- let regex_replacement = new RegExp( match, "g" );
- calculation = calculation.replace( regex_replacement, replacement );
- } );
- }
- return calculation;
- }
- [ replaceTakeoffs ]( calculation ){
- let matches = [ ...new Set( calculation.match( /{{takeoffs\.[0-9]+}}/gm ) ) ];
- if( matches ){
- matches.forEach( match => {
- let match_id = parseInt( match.replace( "{{takeoffs.", "" ).replace( "}}", "" ) );
- let replacement = 0;
- if( this.takeoffs[ match_id ] ){
- replacement = parseInt( this.takeoffs[ match_id ] );
- }
- let regex_replacement = new RegExp( match, "g" );
- calculation = calculation.replace( regex_replacement, replacement );
- } );
- }
- return calculation;
- }
- showClass(){
- let table = {
- 'Debug': this.debug,
- 'Process Sell': this.processSell,
- 'Mark Up': this.markup,
- 'Commission': this.commission,
- 'Tax': this.tax,
- 'Variables': this.variables,
- 'Options': this.options,
- 'Takeoffs': this.takeoffs,
- "Material": this.material,
- };
- console.table( table );
- }
- }
- export default CalculationSolver;
Add Comment
Please, Sign In to add comment