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();
     }
 }
Example #2
0
 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()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     if (isset($this->_data[$this->modeltype]['documentation'])) {
         $this->_data[$this->modeltype]['documentation'] = serialize($this->_data[$this->modeltype]['documentation']);
     } else {
         // If no documents set, set as a blank array.
         $this->_data[$this->modeltype]['documentation'] = serialize(array('0' => ''));
     }
     if (isset($this->_data['saveAnother'])) {
         unset($this->_data['saveAnother']);
         $saveanother = true;
     } else {
         $saveanother = false;
     }
     if ($this->_data[$this->modeltype]['order_qty'] <= 0) {
         $errors['order_qty'] = 'Order Quantity must be greater than zero';
     } elseif (parent::save_model($this->modeltype)) {
         $data = $this->saved_model;
         if (!MFWOStructure::exists($data->id)) {
             $models = MFWOStructure::copyStructure($data, $errors);
             foreach ($models as $model) {
                 $result = $model->save($errors);
                 if ($result === false) {
                     break;
                 }
             }
             if ($result === false || count($errors) > 0) {
                 $errors[] = 'Error creating Works Order Product Structure';
             } else {
                 $flash->addMessage('Creation of Works Order Product Structure completed successfully');
             }
         }
     } else {
         $errors[] = 'Failed to save Works Order';
     }
     if (count($errors) == 0 && $db->CompleteTrans()) {
         if ($saveanother) {
             $this->refresh();
             return;
         } else {
             sendTo('STItems', 'viewWorkorders', $this->_modules, array('id' => $data->stitem_id));
         }
     }
     $db->FailTrans();
     $db->CompleteTrans();
     $this->_data['stitem_id'] = $this->_data[$this->modeltype]['stitem_id'];
     $this->view->set('selected_docs', InjectorClass::unserialize($this->_data[$this->modeltype]['documentation']));
     $flash->addErrors($errors);
     $this->refresh();
 }