Page 1 of 1

calculation on related foundset

PostPosted: Sun Jun 17, 2012 1:20 pm
by Hans Nieuwenhuis
Hi,

I have been breaking my brain on this one :

I have a form with a tabbed form on it. (parent-child(s))

I need a calculation on the parent form to show the total sum of a column in the childs.

When I use the same relation as on the tabbed form, the calculation is
not correct when the users does a find/search in the child form.
Because if the users finds a subset of the related child records, then the calulation
is only done on this subset. Matematically correct, but not what I want.
I always want to show the total of all child record regardless if they are shown or not.

When I use a different relation ( same definition, other name), the calculation stays
correct even when the user does a find/search on the child records.
That makes sense, because the other name results in a different foundset.

But then I have another problem : when I create new child records, they are not part of
the "other" foundset, so the calculation does not get updated.

Has anyone come across this and found a solution ?

My current way around this is to use the use the "different relation name" and do a databaseManager.recalculate on the parent when the childs are saved.
That works, but it does not seem logical...

My calculation on the same relation as the tabbed form is :

Code: Select all
function calc_s() {
   var _tot = 0
   if (par1_to_chi1) {
      for (var i = 1; i <= par1_to_chi1.getSize(); i++) {
         _tot = _tot + par1_to_chi1.getRecord(i).aantal
      }
   }
   return _tot
}


My calculation on a different relation name is :

Code: Select all
function calc1() {
   var _tot = 0
   if (full_par1_to_chi1) {
      for (var i = 1; i <= full_par1_to_chi1.getSize(); i++) {
         _tot = _tot + full_par1_to_chi1.getRecord(i).aantal
      }
   }
   return _tot
}

Re: calculation on related foundset

PostPosted: Thu Jun 21, 2012 1:02 pm
by jcompagner
if you want that the calculation is always up to date even with new records or updated records not yet saved to the database
Then your setup is not possible or at least not that easy
You could do a very tricky solution where you have both relations (so kind of the same relation but under different names like "my_normal_relation" and "my_normal_relation_always_all")
and then in the calculation you walk over both of them, and adding everything from the my_normal_relation + the once you then miss (looking at the pk or something from the all)

Re: calculation on related foundset

PostPosted: Thu Jun 21, 2012 2:29 pm
by Hans Nieuwenhuis
Thanks, Johan
I also thought of this way to do it.
When validating records, i also use 2 relations.
Why do you describe it as very tricky ?

Regards,

Re: calculation on related foundset

PostPosted: Thu Jun 21, 2012 2:41 pm
by jcompagner
you just have to compare exactly which record to get from what relation.
i guess you want to first start summing up the normal relation and then go over the all (if the all has a size() > normal) and you then have to extract the records that you missed from the normal..