This topic is locked
[SOLVED]

 Multiple Calc Fields Based On Phpr Example

2/3/2013 5:04:00 PM
PHPRunner General questions
N
nti author

Based on phpr example below.... would like to place multiple calcs on same edit view... what is the code to add additional calc field?



var ctrlPrice = Runner.getControl(pageid, 'Price');

var ctrlQuantity = Runner.getControl(pageid, 'Quantity');

var ctrlTotal = Runner.getControl(pageid, 'Total');



function func() {

ctrlTotal.setValue(Number(ctrlPrice.getValue()) * Number(ctrlQuantity.getValue()));

};



ctrlPrice.on('keyup', func);

ctrlQuantity.on('keyup', func);
// NEW CALC FIELD NEEDED BELOW: I must be missing ending or separator between the calculations.
var ctrlPrice2 = Runner.getControl(pageid, 'Price2');

var ctrlQuantity2 = Runner.getControl(pageid, 'Quantity2');

var ctrlTotal2 = Runner.getControl(pageid, 'Total2');



function func() {

ctrlTotal2.setValue(Number(ctrlPrice2.getValue()) * Number(ctrlQuantity2.getValue()));

};
// EXTRA QUESTION: the above multiplies the price * the quantity; how to add price1 + price2 * tax rate based on variable of "county field" in table.
ctrlPrice2.on('keyup', func);

ctrlQuantity2.on('keyup', func);
N
nti author 2/3/2013

I thought } would function as script separator. Not true in this case.
script below works for 2nd part only: "calc TWO"... if I comment out "calc TWO" script, 1st script works. Running both scripts produces 2nd result only. Have tried various bracket combinations accept the right one.



var ctrlSerChg = Runner.getControl(pageid, 'SerChg');

var ctrlPartsLabor = Runner.getControl(pageid, 'PartsLabor');

var ctrlTot_Amt_Calc = Runner.getControl(pageid, 'Tot_Amt_Calc');

function func() {

ctrlTot_Amt_Calc.setValue(Number(ctrlSerChg.getValue()) + Number(ctrlPartsLabor.getValue()));

};

ctrlSerChg.on('keyup', func);

ctrlPartsLabor.on('keyup', func);
// calc TWO begins below

//var ctrlCounty = Runner.getControl(pageid, 'TaxCounty');

var ctrlSerChg2 = Runner.getControl(pageid, 'SerChg');

var ctrlPartsLabor2 = Runner.getControl(pageid, 'PartsLabor');

var ctrlTax7_Calc = Runner.getControl(pageid, 'Tax7_Calc');

function func() {

ctrlTax7_Calc.setValue((Number(ctrlSerChg2.getValue()) + Number(ctrlPartsLabor2.getValue())) * (.07));

};

ctrlSerChg2.on('keyup', func);

ctrlPartsLabor2.on('keyup', func);


What would really rock is the following calc based on additional table fields...

calc "CASE" works OK in sql, but not able to convert to javascript calc.



// calc TWO begins below

var ctrlCounty = Runner.getControl(pageid, 'TaxCounty');

var ctrlSerChg2 = Runner.getControl(pageid, 'SerChg');

var ctrlPartsLabor2 = Runner.getControl(pageid, 'PartsLabor');

var ctrlTax_Calc = Runner.getControl(pageid, 'Tax_Calc');

function func() {
// Calc below based on other table fields.
ctrlTax_Calc.setValue(((CASE

WHEN ((tbl_Invoices.PartsCost > 0.01) AND (Lu_Zip.St = 'Fl') AND

(Lu_Zip.County <> 'St Johns')) THEN (((IfNull(tbl_Invoices.SerChg,

0) + tbl_Invoices.PartsLabor) * 0.07) - tbl_Invoices.Tax)

WHEN ((tbl_Invoices.PartsCost > 0.01) AND (Lu_Zip.St = 'Fl') AND

(Lu_Zip.County = 'St Johns')) THEN (((IfNull(tbl_Invoices.SerChg, 0) +

IfNull(tbl_Invoices.PartsLabor, 0)) * 0.06) - tbl_Invoices.Tax)

END);
};

ctrlSerChg2.on('keyup', func);

ctrlPartsLabor2.on('keyup', func);


Thanks for all feedback.

Sergey Kornilov admin 2/4/2013

In regards to your first question - if you want to perform different actions based on different field changes - make sure you create several functions that are named different i.e. func(), func2() etc.
In regards to second question - you will have to use Javascript syntax to perform the same type of calculation. Create a Javascript variable for each field involved in calculation and use the same logic. Instead of WHEN ... THEN construction use if ... else.
Here is an example:

var visitor = "principal";
if(visitor == "teacher"){

document.write("My dog ate my homework...");

}else if(visitor == "principal"){

document.write("What stink bombs?");

} else {

document.write("How do you do?");

}
N
nti author 2/5/2013

Fixed it, based on your suggestions.
Thank you, Admin