public function save() { $flash = Flash::Instance(); $db = DB::Instance(); $db->StartTrans(); $errors = array(); $update_cost = false; if (isset($this->_data[$this->modeltype]['id']) && $this->_data[$this->modeltype]['id']) { $mfresource = new MFResource(); $mfresource->load($this->_data[$this->modeltype]['id']); $old_rate = $mfresource->resource_rate; $new_rate = $this->_data[$this->modeltype]['resource_rate']; $update_cost = $old_rate != $new_rate; } if (parent::save_model($this->modeltype, null, $errors)) { if ($update_cost) { $cc = new ConstraintChain(); $cc->add(new Constraint('mfresource_id', '=', $this->_data[$this->modeltype]['id'])); $db = DB::Instance(); $date = Constraint::TODAY; $between = $date . ' BETWEEN ' . $db->IfNull('start_date', $date) . ' AND ' . $db->IfNull('end_date', $date); $cc->add(new Constraint('', '', '(' . $between . ')')); $mfoperation = new MFOperation(); //$db->Debug(); $mfoperation_ids = array_keys($mfoperation->getAll($cc)); $stitem_ids = array(); foreach ($mfoperation_ids as $mfoperation_id) { if (!$mfoperation->load($mfoperation_id)) { $errors[] = 'Could not save latest costs'; $db->FailTrans(); break; } if (in_array($mfoperation->stitem_id, $stitem_ids)) { continue; } $stitem_ids[] = $mfoperation->stitem_id; } if (count($stitem_ids) > 0) { $stitem = new STItem(); } foreach ($stitem_ids as $stitem_id) { if (!$stitem->load($stitem_id)) { $errors[] = 'Could not save latest costs'; $db->FailTrans(); break; } $old_cost = $stitem->latest_lab; $stitem->calcLatestCost(); $new_cost = $stitem->latest_lab; if (bccomp($old_cost, $new_cost, $stitem->cost_decimals) == 0) { continue; } if (!$stitem->saveCosts() || !STCost::saveItemCost($stitem)) { $errors[] = 'Could not save latest costs'; $db->FailTrans(); break; } if (!$stitem->rollUp(STItem::ROLL_UP_MAX_LEVEL)) { $errors[] = 'Could not roll-up latest costs'; $db->FailTrans(); break; } } } } else { $errors[] = 'Could not save resource'; $db->FailTrans(); } $db->CompleteTrans(); if (count($errors) == 0) { sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null); } else { $flash->addErrors($errors); $this->refresh(); } }