Calc fields based on other calc fields - not Ajax'ing

bggann

Active Member
I have a calc field (charged cost) in a repeated group which is the total of 4 other calc fields and 3 direct entry fields in the repeated group.

I'm having very inconsistent results on getting the "charged_cost" field to calculate when one of the 4 input calc fields changes. It works fine when one of the 3 direct input fields changes.

It seems to not trigger calculation sometimes, and sometimes it does. Sometimes it triggers but returns the wrong result.

It seems like the ajax calls may be out of sequence, perhaps my "total" field is calculated 'before' one of it's inputs.

Another possibility is formating. I've added %$01.2f formats to some of my calculated fields so they show up nicely in the list. But - it seems that maybe this is causing them to be treated differently in the calculation - though that does not seem to mess up the calculations based upon direct entry fields. I added (float) to cast these elements but it did not help.

Code from the calc field. Note - I started with this all in one line - broken it up and assigned variables to try to debug.

---
$hobbs_cost = (float) '{daily_log_7_repeat___hobbs_flight_cost}';
dump($hobbs_cost,'hobbs');

$availability_charged = (float) '{daily_log_7_repeat___availability_cost}';
dump($availability_charged,'aval');

$pilot_per_diem_total = (float) '{daily_log_7_repeat___pilot_per_diem_total}';
dump($pilot_per_diem_total,'pilot');

$mechanic_per_diem_total = (float) '{daily_log_7_repeat___mechanic_per_diem_total}';
dump($mechanic_per_diem_total,'mechanic');

$other_charges = (float) '{daily_log_7_repeat___other_charges}';
dump($other_charges,'other');

$aircraft_fuel = (float) '{daily_log_7_repeat___aircraft_fuel_cost}';
dump($aircraft_fuel,'aircraft fuel');

$relief_crew_travel = (float) '{daily_log_7_repeat___relief_crew_travel_charge}';
dump($relief_crew_travel,'releif');

$charged_cost = $hobbs_cost + $availability_charged + $pilot_per_diem_total + $mechanic_per_diem_total + $other_charges + $aircraft_fuel + $relief_crew_travel;
dump($charged_cost,'charged_cost');

return $charged_cost;
----
Of these fields - the 1st 4 are calculated and changing them does not update "charged_cost"
The last 3 are direct entry fields, and the calculation works fine.
It -seems- as if charged_cost is written to the db correctly - I think.
 
So are you using placeholders for other calc elements, in a calc element?

If so, that almost certainly won't work, as it's a chicken and egg situation. The order in which calcs get run is "undefined" (there is logic to it, but it isn't "published" logic, we make no guarantees about the order), so the calcs you rely in in another calc may not have been run yet.

(Actually, when doing AJAX calc, there isn't even any logic, due to the asynchronous nature of JavaScript ... so if multiple calcs kicks off, the order they return values in is completely random)

Typically what you have to do is re-do the actual code for the other calcs, in the calc that needs those values.

So if you had two calcs like this:

calc1:
return (int){'mytable___foo'} * 10;

calc 2:
return (int)'{mytable___calc1}' + 3;

... what you would actually have to do in calc2 is ...

$calc1 = (int)'{mytable___foo}' * 10;
return $calc1 + 3;

-- hugh
 
I'm not surprised by this Hugh. In line with what I was seeing. Yes, I am using placeholders of calculated fields as inputs to other calc fields.

If I understand correctly, it is not just Ajax calculations that are indeterminate, but even calculations done during submissions. So the only way to do this reliably is to make my "total" fields independent of any other calc fields by repeating the full calc. Nothing dramatic about that, just work.

There isn't a better way to achieve this, maybe a php on the form at submission? It is not necessarily required that the totals show on the form, but they need to be reliable in the database.

Bob
 
If you don't need them displayed on the form, then absolutely, do it with a form submission script, onBeforeProcess, working on the data in the $formModel->formData array, and setting the values of the total elements in the same array.

Sent from my HTC6545LVW using Tapatalk
 
I'd like them to be in the email I'm generating with the plugin. If I do the plugins in the right order will that work?
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top