public function save()
 {
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     if (parent::save('MFOutsideOperation')) {
         $stitem = new STItem();
         if ($stitem->load($this->saved_model->stitem_id)) {
             $old_cost = $stitem->latest_osc;
             $stitem->calcLatestCost();
             $new_cost = $stitem->latest_osc;
             if (bccomp($old_cost, $new_cost, $stitem->cost_decimals) != 0) {
                 if ($stitem->saveCosts() && STCost::saveItemCost($stitem)) {
                     if (!$stitem->rollUp(STItem::ROLL_UP_MAX_LEVEL)) {
                         $errors[] = 'Could not roll-up latest costs';
                         $db->FailTrans();
                     }
                 } else {
                     $errors[] = 'Could not save latest costs';
                     $db->FailTrans();
                 }
             }
         } else {
             $errors[] = 'Could not save latest costs';
             $db->FailTrans();
         }
     } else {
         $errors[] = 'Could not save outside operation';
         $db->FailTrans();
     }
     $db->CompleteTrans();
     if (count($errors) == 0) {
         sendTo('STItems', 'viewoutside_operations', $this->_modules, array('id' => $this->_data['MFOutsideOperation']['stitem_id']));
     } else {
         $flash->addErrors($errors);
         $this->_data['stitem_id'] = $this->_data['MFOutsideOperation']['stitem_id'];
         $this->refresh();
     }
 }
 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();
     }
 }
 public function save()
 {
     $flash = Flash::Instance();
     if (!$this->checkParams('MFOperation')) {
         sendBack();
     }
     $data = $this->_data['MFOperation'];
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     if (!($data['volume_target'] > 0)) {
         $errors[] = 'Volume target must be a number greater than zero';
     }
     if (!($data['uptime_target'] > 0)) {
         $errors[] = 'Uptime target must be a number greater than zero';
     }
     if (!($data['quality_target'] > 0)) {
         $errors[] = 'Quality target must be a number greater than zero';
     }
     if (!($data['resource_qty'] > 0)) {
         $errors[] = 'Resource quantity must be a number greater than zero';
     }
     if (count($errors) == 0 && parent::save_model('MFOperation')) {
         $stitem = new STItem();
         if ($stitem->load($this->saved_model->stitem_id)) {
             $old_costs = array($stitem->latest_lab, $stitem->latest_ohd);
             $stitem->calcLatestCost();
             $new_costs = array($stitem->latest_lab, $stitem->latest_ohd);
             $equal_costs = true;
             $total_costs = count($old_costs);
             for ($i = 0; $i < $total_costs; $i++) {
                 if (bccomp($old_costs[$i], $new_costs[$i], $stitem->cost_decimals) != 0) {
                     $equal_costs = false;
                     break;
                 }
             }
             if (!$equal_costs) {
                 if ($stitem->saveCosts() && STCost::saveItemCost($stitem)) {
                     if (!$stitem->rollUp(STItem::ROLL_UP_MAX_LEVEL)) {
                         $errors[] = 'Could not roll-up latest costs';
                     }
                 } else {
                     $errors[] = 'Could not save latest costs';
                 }
             }
         } else {
             $errors[] = 'Could not save latest costs';
         }
     } else {
         $errors[] = 'Could not save operation';
     }
     if (count($errors) > 0) {
         $db->FailTrans();
     }
     $db->CompleteTrans();
     if (count($errors) == 0) {
         sendTo($this->name, 'index', $this->_modules, array('stitem_id' => $data['stitem_id']));
     } else {
         $flash->addErrors($errors);
         $this->_data['stitem_id'] = $data['stitem_id'];
         $this->refresh();
     }
 }