//procurement attribute
// find or create brand value
// brand (attribute) - find brand , if no, create new brand
EcoResAttributeType attribType;
EcoResEnumerationAttributeTypeValue attribTypeVal;
EcoResTextValue attribTextValuetmp, attribTextValue;
EcoResAttribute ecoResAttribute;
while select attribType
where attribType.Name == EcoResProductParameters::find().THK_AttributeName
join attribTypeVal
where attribTypeVal.AttributeType == attribType.RecId
join attribTextValuetmp
where attribTextValuetmp.RecId == attribTypeVal.Value
{
if(attribTextValuetmp.TextValue == brand )
{
// brand exist already
brandReady = true;
attribTextValue = EcoResTextValue::find(attribTextValuetmp.RecId);
}
}
attribType.clear();
attribType = EcoResAttributeType::findByName(EcoResProductParameters::find().THK_AttributeName);
if(!brandReady)
{
// if brand not ready, create new value set in Brand
if(attribType)
{
attribTextValue.clear();
attribTextValue.TextValue = brand;
attribTextValue.insert();
attribTypeVal.clear();
attribTypeVal.initProductConfigurationValues(attribType,attribTextValue);
attribTypeVal.THK_Description = brand_name;
attribTypeVal.insert();
brandReady = true;
}
}
if(brandReady)
{
ecoResAttribute = EcoResAttribute::findByName(EcoResProductParameters::find().THK_AttributeName,attribType.RecId);
// After brand ready, add item.brand relation
EcoResProductInstanceValue ecoResProductInstanceValue;
EcoResAttributeValue ecoResAttributeValue;
ecoResProductInstanceValue = EcoResProductInstanceValue::findByProduct(ecoResProduct.RecId);
if (!ecoResProductInstanceValue && ecoResProduct)
{
ecoResProductInstanceValue.clear();
ecoResProductInstanceValue.product = ecoResProduct.RecId;
ecoResProductInstanceValue.insert();
}
ecoResAttributeValue = EcoResAttributeValue::findByInstanceAttribute(ecoResProductInstanceValue.RecId, ecoResAttribute.RecId);
if (!ecoResAttributeValue)
{
if(attribTextValue.RecId && ecoResProductInstanceValue.RecId && ecoResAttribute.RecId)
{
ecoResAttributeValue.clear();
ecoResAttributeValue.Value = attribTextValue.RecId;
ecoResAttributeValue.InstanceValue = ecoResProductInstanceValue.RecId;
ecoResAttributeValue.Attribute = ecoResAttribute.RecId;
ecoResAttributeValue.insert();
}
}
else
{
ecoResAttributeValue.selectforUpdate(true);
ecoResAttributeValue.Value = attribTextValue.RecId;
ecoResAttributeValue.update();
}
}
// procurement category
//if not find category, find add category under cate
EcoResCategoryHierarchy _categoryHierarchy = EcoResCategoryHierarchy::find(EcoResCategoryHierarchyRole::getHierarchiesByRole(EcoResCategoryNamedHierarchyRole::Procurement).CategoryHierarchy);
resCategory = EcoResCategory::findByCode(cms_category, _categoryHierarchy.RecId);
ttsbegin;
// find category
if(resCategory)
{
resProductCateg = EcoResProductCategory::findByItemIdCategoryHierarchyRole(inventTable.ItemId,EcoResCategoryNamedHierarchyRole::Procurement);
// cat exit
if(resProductCateg)
{
if(resProductCateg.Category == resCategory.RecId)
{
categoryReady = true;
}
else
{
// change category code to new one
resProductCateg.selectForUpdate(true);
resProductCateg.Category = resCategory.RecId;
resProductCateg.update();
}
}
else
{
EcoResProductCategory ecoResProductCategoryToCreate;
ecoResProductCategoryToCreate.initValue();
ecoResProductCategoryToCreate.Category = resCategory.RecId;
ecoResProductCategoryToCreate.CategoryHierarchy = resCategory.CategoryHierarchy;
ecoResProductCategoryToCreate.Product = ecoResProduct.RecId;
ecoResProductCategoryToCreate.insert();
}
}
else
{
EcoResCategory rootCategory , ecoResCategory;
select firstonly rootCategory where rootCategory.CategoryHierarchy == _categoryHierarchy.RecId
&& rootCategory.NestedSetLeft == 1;
// create category & Product Category:
ecoResCategory.initValue();
ecoResCategory.ParentCategory = rootCategory.RecId;
ecoResCategory.Name = cms_category_name;
ecoResCategory.Code = cms_category;
if (ecoResCategory.addToHierarchy() && ecoResCategory.RecId)
{
EcoResCategoryTranslation::createOrUpdateCategoryTranslation(ecoResCategory.RecId, ecoResCategory.Name,"","");
}
EcoResProductCategory ecoResProductCategoryToCreate;
ecoResProductCategoryToCreate.initValue();
ecoResProductCategoryToCreate.Category = ecoResCategory.RecId;
ecoResProductCategoryToCreate.CategoryHierarchy = ecoResCategory.CategoryHierarchy;
ecoResProductCategoryToCreate.Product = ecoResProduct.RecId;
ecoResProductCategoryToCreate.insert();
}