Question

How would you go about coding this?

I have 2 input fields on a CRM 2011 Form - Company Size / Number of Documents. The documents have a set price - ie If you Company Size is 0-100 - and you purchase 1-10 documents the price is 260.00 if you purchase 11-50 its 400 if you purchase 51-200 = 650 if you purchase200 + 1000

Each company size has a different pricing structure

example

Size of Company || 1-10 documents || 11-50 documents || 51-200 documents ||200+ documents

I current have the follow - Which is super massive

if (EMP < 101) // Company Size 1 to 100
{
    if (DOC < 11) 
            {
                CV3 = 260.39;
            }
        else if (DOC < 51)
            {
                CV3 = 433.99;
            }
        else if (DOC < 201) 
            {
                CV3 = 694.38;
            }
       else if (DOC >  201)
            {
                CV3 = 1041.57;
            }
}

else if (EMP < 201) // Company Size 101 to 200
{
    if (DOC < 11) 
            {
                CV3 = 328.12;
            }
        else if (DOC < 51)
            {
                CV3 = 546.86;
            }
        else if (DOC < 201) 
            {
                CV3 = 874.98;
            }
       else if (DOC >  201)
            {
                CV3 = 1312.47;
            }
}
Was it helpful?

Solution

Picking up on Pointy's idea of a table the following jsfiddle first of all sets up a table which is then used to lookup costs. (Code at bottom)

The given price ranges are split into groups of 10, with repeated values to give the group ranges, ie the costs for groups (11-20),(21-30),(31-40) and (41-50) are all set to the price range for (11-50)

Taking 1 from the number of documents, dividing by 10 and taking the integer part put you into the correct part of the table for that number of documents.

The table is done for company sizes of 1-100 and 101-200

provided that company sizes go up in 100s it would be straight forward to add further companies. To get to the company part of the table take 1 from size divide by 100 and take integer part. If the ranges of company sizes varied in size then the set up table code would need to be amended into equal grouping as for the number of documents.

Hopefully the data for costs can easily be maintained.

var DOC; //number of documents;
var COMP; //size of company;

numComps=2; //Number of companies in table


/*price range for each company falls into four bands
for company size
priceRange[size][0] is for 1-10 docs
priceRange[size][1] is for 11-50 docs
priceRange[size][2] is for 51-200 docs
price range[size][4] is for 200+ docs
priceRange */

priceRange=[]
//company size 1-100
priceRange[0]=[260.39,433.99,694.38,1041.57];

//company size 101-200
priceRange[1]=[382.12,546.86,874.98,1312.47];

//expand through different company sizes as needed

/*split price ranges into groups of ten and equate to form groups for document sizes and form table for companies and document sizes*/

//set up table
companies=[];
for(var size=0; size<numComps; size++) {
    companies[size]=[];
    companies[size][0]=priceRange[size][0]; //docs 1-10
    for(var docms=1; docms<5; docms++) {  //docs 11-50
        companies[size][docms]=priceRange[size][1];        
    }
    for(var docms=5; docms<20; docms++) {  //docs 51-200
        companies[size][docms]=priceRange[size][2];        
    }
    companies[size][20]=priceRange[size][3];//docs 200+   
}

function getCost(docs,comps) {
    docs=Math.floor((docs-1)/10);
    if (docs>20) {docs=20};
    comps=Math.floor((comps-1)/100);
    return(companies[comps][docs]);
}

//example

DOC=8;
COMP=43;
console.log(DOC,COMP,getCost(DOC,COMP));

OTHER TIPS

Could you change the company size input to a pick list? That would cut down on the amount of branching you would need to do.

How about this:

//100000001, etc. is the default value scheme for CRM picklists
var optionValues = {
    100000001: "company_0_100", 
    100000002: "company_101_200"
};

var costMap = {
    company_0_100: function (documents) {
        if (documents < 1) return -1;

        if (documents < 11) {
            return 260.39;
        } else if (documents < 51) {
            return 433.99;
        } else if (documents < 201) {
            return 694.38;
        } else {
            return 1041.57;
        }
    },
    company_101_200: function (documents) {
        if (documents < 1) return -1;

        if (documents < 11) {
            return 328.12;
        } else if (documents < 51) {
            return 546.86;
        } else if (documents < 201) {
            return 874.98;
        } else {
            return 1312.47;
        }
    }
};

To retrieve the cost just reach into the costMap object with the picklist and document count values.

var cost = costMap[picklistValue](documentCount);

Here is an example fiddle: http://jsfiddle.net/R9gKZ/

If you cannot use a picklist you could implement a select list web resource and update the textbox field. example

To achieve this, here I use Short-hand :

This is horrible but also other way :

 CV3 =  (DOC < 11?260.39:(DOC < 51?433.99:(DOC < 51?433.99:(DOC < 201?694.38:(DOC < 201?694.38:(DOC > 201?1041.57:''))))))

Full Working if / else if :

CV3 = (EMP < 101)?(DOC < 11?260.39:(DOC < 51?433.99:(DOC < 51?433.99:(DOC < 201?694.38:(DOC < 201?694.38:(DOC > 201?1041.57:'')))))):(EMP < 201? (DOC < 11?328.12:(DOC < 51?546.86:(DOC < 201?874.98:(DOC >  201?1312.47:'')))):'')

FULL WORKING DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top