public function getSOProductlineHeader() { // Get the current SO Product linked to the Stock Item, if it exists $productlineheader = DataObjectFactory::Factory('SOProductlineHeader'); $cc = new ConstraintChain(); $cc->add(new Constraint('stitem_id', '=', $this->id)); $cc->add(currentDateConstraint()); $productlineheader->loadBy($cc); return $productlineheader; }
public function save_clone() { if (!$this->loadData()) { $this->dataError(); sendBack(); } $flash = Flash::Instance(); $db = DB::Instance(); $db->StartTrans(); $errors = array(); $stitem = $this->_uses[$this->modeltype]; $original_id = $stitem->id; $this->_data[$this->modeltype]['item_code'] = strtoupper($this->_data[$this->modeltype]['item_code']); if ($stitem->item_code == $this->_data[$this->modeltype]['item_code']) { $errors[] = 'The Item Code must be Unique'; } else { $stitem->item_code = $this->_data[$this->modeltype]['item_code']; $stitem->description = $this->_data[$this->modeltype]['description']; $stitem->balance = 0; $stitem->std_cost = 0; $stitem->std_mat = 0; $stitem->std_lab = 0; $stitem->std_ohd = 0; $stitem->std_osc = 0; $stitem->created = $stitem->autoHandle('created'); $stitem->createdby = $stitem->autoHandle('createdby'); $test = $stitem->autoHandle($stitem->idField); if ($test !== false) { $stitem->{$stitem->idField} = $test; $stitem_id = $test; } else { $errors[] = 'Error getting identifier for new item'; } } if (count($errors) == 0 && !$stitem->save()) { $errors[] = 'Error saving cloned item ' . $db->ErrorMsg(); } elseif (count($errors) == 0) { $hasmany = $stitem->getHasMany(); foreach ($this->_data[$this->modeltype] as $key => $value) { if (substr($key, 0, 5) == 'copy_' && isset($hasmany[substr($key, 5)])) { $do_name = $hasmany[substr($key, 5)]['do']; $do = DataObjectFactory::Factory($do_name); $cc = new ConstraintChain(); $cc->add(new Constraint('stitem_id', '=', $original_id)); if ($do->isField('start_date') && $do->isField('end_date')) { $cc->add(currentDateConstraint()); } $children = $do->getAll($cc); if (!empty($children)) { foreach ($children as $child_id => $value) { $child = DataObjectFactory::Factory($do_name); $child->load($child_id); if ($child->isLoaded()) { $test = $child->autoHandle($child->idField); if ($test !== false) { $child->{$stitem->idField} = $test; } else { $errors[] = 'Error getting identifier for new item'; } $child->stitem_id = $stitem_id; $db->StartTrans(); if (!$child->save()) { $errors[] = 'Failed to copy ' . $do_name . ' ' . $db->ErrorMsg(); $db->FailTrans(); $db->CompleteTrans(); break; } $db->CompleteTrans(); } else { $errors[] = 'Failed to load ' . $do_name; break; } } } } } } if (count($errors) > 0) { $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); sendBack(); } // All OK, so now check latest cost $stitem->calcLatestCost(); if (!$stitem->save()) { $flash->addError('Error saving cloned item ' . $db->ErrorMsg()); $db->FailTrans(); $db->CompleteTrans(); sendBack(); } $db->CompleteTrans(); sendTo($this->name, 'view', $this->_modules, array('id' => $stitem_id)); }