Please add to contracts calculated field with database column, like this:
array(
'name' => _M('Total Amount'),
'type' => 'calculated',
'param'=>Utils_RecordBrowserCommon::actual_db_type('currency'),
'visible' => true,
'required' => false,
'extra' => false,
),
In common file you shoud have method like this (function name is invalid, because it should be something like "calculate_total_payment", this function is just some basic starting code):
public static function show_total_payment($values, $mode) {
switch($mode) {
case 'edited':
case 'added':
case 'delete':
case 'restore':
$payments = Utils_RecordBrowserCommon::get_records('contracts_payment_schedule', array('contract'=>$values['contract']));
$sum = new Utils_CurrencyField_Sum();
foreach($payments as $payment) {
$sum->add(Utils_CurrencyField_Value::from_string($payment['payment_amount']));
}
$totals = $sum->get_total();
$total = array_shift($totals);
Utils_RecordBrowserCommon::update_record('contracts',$values['contract'],array('total_amount'=>$total->to_string()));
location(array());
}
return $values;
}
Function above will calculate only one currency - if your use multiple currencies you should use premium/exchangerate module to save currencies exchanges and calculate total amount in default currency.
Alternative version - you can create display callback to calculated field and calculate sum there without saving it in database (this method won't allow you filtring with total amount field).
Regards
Pawel