This topic is locked
[SOLVED]

 Calculation On The Fly With Subtotals

6/21/2013 6:19:25 AM
PHPRunner General questions
petert455 author

JavaScript being used for a cash drawer/register type form in the JavaScript OnLoad event of add/edit pages.
User input fields are cs1-7, css12, csss14-15
Hope this helps others and works great. Can be modified to fit other needs and I'm sure written better :-) The only problem has been a decimal problem when adding odd numbers. In the subtotal for example looks like 111.000002. Anybody have any ideas?
Thanks in advance for any ideas.
var cs1 = Runner.getControl(pageid, '100s');

var cs2 = Runner.getControl(pageid, '50s');

var cs3 = Runner.getControl(pageid, '20s');

var cs4 = Runner.getControl(pageid, '10s');

var cs5 = Runner.getControl(pageid, '5s');

var cs6 = Runner.getControl(pageid, '1s');

var cs7 = Runner.getControl(pageid, 'coin');

var css11 = Runner.getControl(pageid, 'totalcash');

var css12 = Runner.getControl(pageid, 'lessbank');
var csss13 = Runner.getControl(pageid, 'cashtotal');

var csss14 = Runner.getControl(pageid, 'creditcards');

var csss15 = Runner.getControl(pageid, 'credittips');
var cstotal = Runner.getControl(pageid, 'totalcash');

var csstotal = Runner.getControl(pageid, 'cashtotal');

var cssstotal = Runner.getControl(pageid, 'grandtotal');
function func() {

cstotal.setValue(Number(cs1.getValue()) + Number(cs2.getValue()) + Number(cs3.getValue()) + Number(cs4.getValue()) + Number
(cs5.getValue()) + Number(cs6.getValue()) + Number(cs7.getValue()));
csstotal.setValue(Number(css11.getValue()) - Number(css12.getValue()));
cssstotal.setValue(Number(csss13.getValue()) + Number(csss14.getValue()) + Number(csss15.getValue()));
};
cs1.on('keyup', func);

cs2.on('keyup', func);

cs3.on('keyup', func);

cs4.on('keyup', func);

cs5.on('keyup', func);

cs6.on('keyup', func);

cs7.on('keyup', func);
css11.on('keyup', func);

css12.on('keyup', func);
csss13.on('keyup', func);

csss14.on('keyup', func);

csss15.on('keyup', func);

C
cgphp 6/21/2013

Please, try to explain better your issue with an example.

petert455 author 6/21/2013



Please, try to explain better your issue with an example.


Hi Cristian,
Thanks for the reply... your the man!
The form has txt fields that a user enters cash/credit card/credit tip amounts into that were taken in on a shift. For example, at the end of a shift the user has 150.00 in fifty dollar bills. He/She would type into 50's 150.00. He/She is issued a bank at the start of the shift and is a subtraction (less bank)
txt field names
100's

50's

20's

10's

5's

1's

coin
total cash (this is a subtotal)
less bank
cash total (this is a subtotal)
credit cards
credit tips
grand total (this is a subtotal)
_____
Here is the user typing amounts into the form
100.00 (in the 100's)

150.00 (in the 50's)

40.00 (in the 20's)

30.00 (in the 10's)

15.00 (in the 5's)

11.00 (in the 1's)

1.11 (in the coin)
total cash: 347.11 (subtotal so far ok)
300.00 (in the less bank)
cash total: 47.110000000000014 (decimal problem - would like to see 47.11)
122.12 (in the credit cards)
11.00 (in the credit tips)
grand total: 180.23000000000002 (decimal problem - would like to see 180.23)
Hope this is clearer and thanks again

C
cgphp 6/21/2013
var diff = Number(css11.getValue()) - Number(css12.getValue());

csstotal.setValue(diff.toFixed(2));
petert455 author 6/21/2013

Hi Cristian,
Thank you so much for you timely responses. It was still doing the decimal places wrong. Then I modified your code for the 3rd part of the func as well and now works flawlessly. It was all you though!
I got it with:
var cs1 = Runner.getControl(pageid, '100s');

var cs2 = Runner.getControl(pageid, '50s');

var cs3 = Runner.getControl(pageid, '20s');

var cs4 = Runner.getControl(pageid, '10s');

var cs5 = Runner.getControl(pageid, '5s');

var cs6 = Runner.getControl(pageid, '1s');

var cs7 = Runner.getControl(pageid, 'coin');

var css11 = Runner.getControl(pageid, 'totalcash');

var css12 = Runner.getControl(pageid, 'lessbank');
var csss13 = Runner.getControl(pageid, 'cashtotal');

var csss14 = Runner.getControl(pageid, 'creditcards');

var csss15 = Runner.getControl(pageid, 'credittips');
var cstotal = Runner.getControl(pageid, 'totalcash');

var csstotal = Runner.getControl(pageid, 'cashtotal');

var cssstotal = Runner.getControl(pageid, 'grandtotal');
function func() {

cstotal.setValue(Number(cs1.getValue()) + Number(cs2.getValue()) + Number(cs3.getValue()) + Number(cs4.getValue()) + Number
(cs5.getValue()) + Number(cs6.getValue()) + Number(cs7.getValue()));
var diff = Number(css11.getValue()) - Number(css12.getValue());

csstotal.setValue(diff.toFixed(2));
var diff = Number(csss13.getValue()) + Number(csss14.getValue()) + Number(csss15.getValue());

cssstotal.setValue(diff.toFixed(2));
};
cs1.on('keyup', func);

cs2.on('keyup', func);

cs3.on('keyup', func);

cs4.on('keyup', func);

cs5.on('keyup', func);

cs6.on('keyup', func);

cs7.on('keyup', func);
css11.on('keyup', func);

css12.on('keyup', func);
csss13.on('keyup', func);

csss14.on('keyup', func);

csss15.on('keyup', func);