Exemplo n.º 1
0
 public static function useDefault($search_data = null, &$errors = array(), $defaults = null)
 {
     $search = new stcostsSearch($defaults);
     // Search by Stock Item
     $search->addSearchField('stitem_id', 'Stock Item', 'select', '', 'basic');
     // Search by Date
     //		$search->addSearchField(
     //			'start_date/end_date',
     //			'Date',
     //			'betweenfields',
     //			date(DATE_FORMAT),
     //			'basic'
     //		);
     // Search by Type
     $search->addSearchField('type', 'Type', 'select', '', 'basic');
     $stitem = new STItem();
     //		$chain = new ConstraintChain();
     //		$chain->add(new Constraint('comp_class','=','M'));
     //		if (isset($search_data['stitem_id'])) {
     //			$chain->add(new Constraint('id', '!=', $search_data['stitem_id']));
     //		}
     $stitems = $stitem->getAll();
     $options = array('' => 'All');
     $options += $stitems;
     $search->setOptions('stitem_id', $options);
     $stcost = new STCost();
     $options = array('' => 'All');
     $types = $stcost->getEnumOptions('type');
     $options += $types;
     $search->setOptions('type', $options);
     $search->setSearchData($search_data, $errors);
     return $search;
 }
Exemplo n.º 2
0
 public static function getMostRecent($stitem_id, $type)
 {
     $cc = new ConstraintChain();
     $cc->add(new Constraint('stitem_id', '=', $stitem_id));
     $cc->add(new Constraint('type', '=', $type));
     $sh = new SearchHandler(new STCostCollection(), false);
     $sh->addConstraintChain($cc);
     $sh->setOrderBy(array('lastupdated', 'id'), array('DESC', 'DESC'));
     $stcost = new STCost();
     return $stcost->loadBy($sh);
 }
Exemplo n.º 3
0
 public static function useDefault($search_data = null, &$errors = array(), $defaults = null)
 {
     $search = new costSheetSearch($defaults);
     // Search by Stock Item
     $search->addSearchField('stitem_id', 'Stock Item', 'hidden', '', 'hidden');
     // Search by Date
     $search->addSearchField('start_date/end_date', 'Date', 'betweenfields', date(DATE_FORMAT), 'basic');
     // Search by Type
     $search->addSearchField('type', 'Type', 'select', 'Latest', 'basic', false);
     $stcost = new STCost();
     $options = $stcost->getEnumOptions('type');
     $search->setOptions('type', $options);
     $search->setSearchData($search_data, $errors);
     return $search;
 }
 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();
     }
 }
Exemplo n.º 5
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();
     }
 }
Exemplo n.º 6
0
 public function recalcLatestCosts()
 {
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $stitems_done = array();
     $stitem_ids = array_keys(STItem::nonObsoleteItems());
     $max_depth = 5;
     $max_parents = 5;
     $progressBar = new Progressbar('recalclatestcosts');
     $callback = function ($stitem_id, $id) use(&$stitems_done, &$errors) {
         if (in_array($stitem_id, $stitems_done)) {
             return;
         }
         $stitem = DataObjectFactory::Factory('STItem');
         if (!$stitem->load($stitem_id)) {
             return FALSE;
         }
         $parent = null;
         $num_parents = 0;
         do {
             if ($parent) {
                 $stitem = $parent;
             }
             $parent = null;
             $parents = $stitem->getParents();
             if (count($parents) > 0) {
                 list($parent) = $parents;
             }
             $num_parents++;
         } while ($parent && $num_parents <= $max_parents);
         $tree_array = $stitem->getTreeArray($max_depth);
         // Gets child nodes first
         $array_iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($tree_array), 2);
         foreach ($array_iterator as $id => $children) {
             if (in_array($id, $stitems_done)) {
                 return;
             }
             $stitem = DataObjectFactory::Factory('STItem');
             if (!$stitem->load($id)) {
                 return FALSE;
             }
             $stitems_done[] = $id;
             $old_costs = array($stitem->latest_cost, $stitem->latest_mat, $stitem->latest_lab, $stitem->latest_osc, $stitem->latest_ohd);
             $stitem->calcLatestCost();
             $new_costs = array($stitem->latest_cost, $stitem->latest_mat, $stitem->latest_lab, $stitem->latest_osc, $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) {
                 return;
             }
             if (!$stitem->saveCosts() || !STCost::saveItemCost($stitem)) {
                 return FALSE;
             }
         }
     };
     if ($progressBar->process($stitem_ids, $callback) === FALSE) {
         $errors[] = 'Could not re-calculate stock item costs';
         $db->FailTrans();
     }
     $db->CompleteTrans();
     if (count($errors) == 0) {
         $flash->addMessage('Stock item costs re-calculated');
         sendTo('Index', 'index', $this->_modules);
     } else {
         $flash->addErrors($errors);
         sendBack();
     }
 }
Exemplo n.º 7
0
 public function rollOver()
 {
     $std_costs = array($this->std_cost, $this->std_mat, $this->std_lab, $this->std_osc, $this->std_ohd);
     $latest_costs = array($this->latest_cost, $this->latest_mat, $this->latest_lab, $this->latest_osc, $this->latest_ohd);
     $equal_costs = true;
     $total_costs = count($std_costs);
     for ($i = 0; $i < $total_costs; $i++) {
         if (bccomp($std_costs[$i], $latest_costs[$i], $this->cost_decimals) != 0) {
             $equal_costs = false;
             break;
         }
     }
     if ($equal_costs) {
         return true;
     }
     $this->std_cost = $this->latest_cost;
     $this->std_mat = $this->latest_mat;
     $this->std_lab = $this->latest_lab;
     $this->std_osc = $this->latest_osc;
     $this->std_ohd = $this->latest_ohd;
     $db = DB::Instance();
     $db->StartTrans();
     $success = true;
     if (!$this->save() || !STCost::saveItemCost($this, 'std')) {
         $db->FailTrans();
         $success = false;
     }
     $db->CompleteTrans();
     return $success;
 }
Exemplo n.º 8
0
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $data = $this->_data[$this->modeltype];
     $data['item_code'] = strtoupper($data['item_code']);
     $update_cost = FALSE;
     $stitem = $this->_uses[$this->modeltype];
     if (!empty($data['id'])) {
         $stitem->load($data['id']);
     }
     if ($data['comp_class'] == 'B') {
         $data['latest_cost'] = $data['latest_mat'];
         $data['latest_lab'] = 0;
         $data['latest_osc'] = 0;
         $data['latest_ohd'] = 0;
         if ($stitem->isLoaded()) {
             $old_costs = array($stitem->latest_cost, $stitem->latest_mat, $stitem->latest_lab, $stitem->latest_osc, $stitem->latest_ohd);
             $new_costs = array($data['latest_cost'], $data['latest_mat'], $data['latest_lab'], $data['latest_osc'], $data['latest_ohd']);
             $total_costs = count($old_costs);
             for ($i = 0; $i < $total_costs; $i++) {
                 if (bccomp($old_costs[$i], $new_costs[$i], $stitem->cost_decimals) != 0) {
                     $update_cost = true;
                     break;
                 }
             }
         } elseif ($data['latest_cost'] > 0) {
             $update_cost = true;
         }
     } else {
         unset($data['latest_mat']);
     }
     $product_data = array();
     if ($stitem->isLoaded()) {
         if (is_null($stitem->obsolete_date) && !empty($data['obsolete_date'])) {
             $product_data['end_date'] = 'obsolete_date';
         }
         if ($data['prod_group_id'] != $stitem->prod_group_id) {
             $product_data['prod_group_id'] = 'prod_group_id';
         }
     }
     if (!$update_cost && $stitem->isLoaded()) {
         $update_cost = $data['uom_id'] != $stitem->uom_id || $data['cost_decimals'] != $stitem->cost_decimals || $data['comp_class'] != $stitem->comp_class || strlen($data['obsolete_date']) > 0 && fix_date($data['obsolete_date']) != $stitem->obsolete_date;
     }
     if (parent::save($this->modeltype, $data, $errors)) {
         if ($update_cost) {
             $this->saved_model->calcLatestCost();
             if ($this->saved_model->saveCosts() && STCost::saveItemCost($this->saved_model)) {
                 if (!$this->saved_model->rollUp(STItem::ROLL_UP_MAX_LEVEL)) {
                     $errors[] = 'Could not roll-up latest costs';
                     $db->FailTrans();
                 }
             } else {
                 $errors[] = 'Could not save latest costs';
                 $db->FailTrans();
             }
         }
         if (!empty($product_data)) {
             // Need to cascade data changes to linked current products
             // if they exist
             $products['PO'] = $this->saved_model->getPOProductlineHeader();
             $products['SO'] = $this->saved_model->getSOProductlineHeader();
             foreach ($products as $type => $product) {
                 if ($product->isLoaded()) {
                     foreach ($product_data as $field => $value) {
                         $product->{$field} = $this->saved_model->{$value};
                     }
                     if (!$product->save()) {
                         $errors[] = 'Error updating ' . $type . ' Product : ' . $db->ErrorMsg();
                         $db->FailTrans();
                     }
                 }
             }
         }
     } else {
         $errors[] = 'Could not save stock item';
         $db->FailTrans();
     }
     $db->CompleteTrans();
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $this->refresh();
     } elseif (isset($this->_data['saveform'])) {
         sendTo($this->name, 'view', $this->_modules, array('id' => $this->saved_model->id));
     } else {
         sendTo($this->name, 'new', $this->_modules);
     }
 }
Exemplo n.º 9
0
 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();
     }
 }