/// <summary> /// Save Genral attribute /// </summary> /// <param name="windowNoParent"></param> /// <param name="mAttributeSetInstanceId"></param> /// <param name="windowNo"></param> /// <param name="values"></param> /// <param name="ctx"></param> /// <returns></returns> public AttributeInstance SaveGenAttribute(int windowNoParent, int mAttributeSetInstanceId, int vadms_AttributeSet_ID, int windowNo, List <KeyNamePair> values, Ctx ctx) { var editors = values; AttributeInstance obj = new AttributeInstance(); bool _changed = false; MGenAttributeSet aset = null; MGenAttribute[] attributes = null; String mandatory = ""; var _masi = new MGenAttributeSetInstance(ctx, mAttributeSetInstanceId, null); _masi.SetC_GenAttributeSet_ID(vadms_AttributeSet_ID); aset = _masi.GetMGenAttributeSet(); if (aset == null) { return(null); } // *** Save Attributes *** // New Instance if (_changed || _masi.GetC_GenAttributeSetInstance_ID() == 0) { _masi.Save(); obj.M_AttributeSetInstance_ID = _masi.GetC_GenAttributeSetInstance_ID(); mAttributeSetInstanceId = _masi.GetC_GenAttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } else { obj.M_AttributeSetInstance_ID = _masi.GetC_GenAttributeSetInstance_ID(); mAttributeSetInstanceId = _masi.GetC_GenAttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } // Save Instance Attributes attributes = aset.GetCGenAttributes(false); Dictionary <MGenAttribute, object> lst = new Dictionary <MGenAttribute, object>(); for (int i = 0; i < attributes.Length; i++) { if (MGenAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType())) { object editor = editors[i]; //MGenAttributeValue value = (MGenAttributeValue)editor; MGenAttributeValue value = null; if (Convert.ToInt32(editors[i].Key) > 0) { value = new MGenAttributeValue(ctx, Convert.ToInt32(editors[i].Key), null); value.SetName(editors[i].Name); } //log.fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory() && value == null) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; } else if (MGenAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType())) { object editor = editors[i].Name; decimal value = Convert.ToDecimal(editor); // log.fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory()) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; } else { object editor = editors[i].Name; String value = Convert.ToString(editor); // log.Fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory() && (value == null || value.Length == 0)) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; } _changed = true; } // Save Model if (_changed) { for (int i = 0; i < attributes.Length; i++) { if (MGenAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType())) { var editor = editors[i]; MGenAttributeValue value = lst[attributes[i]] != null ? lst[attributes[i]] as MGenAttributeValue : null; attributes[i].SetCGenAttributeInstance(mAttributeSetInstanceId, value); } else if (MGenAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType())) { attributes[i].SetCGenAttributeInstance(mAttributeSetInstanceId, (decimal?)lst[attributes[i]]); } else { attributes[i].SetCGenAttributeInstance(mAttributeSetInstanceId, (String)lst[attributes[i]]); } } _masi.SetDescription(); _masi.Save(); obj.M_AttributeSetInstance_ID = _masi.GetC_GenAttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } else { obj.M_AttributeSetInstance_ID = _masi.GetC_GenAttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } return(obj); }
public AttributeInstance SearchValuesAttribute(int windowNoParent, int mAttributeSetInstanceId, int vadms_AttributeSet_ID, int windowNo, List <KeyNamePair> values, Ctx ctx) { AttributeInstance obj = new AttributeInstance(); try { var editors = values; MGenAttributeSet aset = null; MGenAttribute[] attributes = null; String mandatory = ""; var _masi = new MGenAttributeSetInstance(ctx, mAttributeSetInstanceId, null); _masi.SetC_GenAttributeSet_ID(vadms_AttributeSet_ID); aset = _masi.GetMGenAttributeSet(); if (aset == null) { return(obj); } if (_masi.GetC_GenAttributeSetInstance_ID() == 0) { // _masi.Save(); obj.M_AttributeSetInstance_ID = _masi.GetC_GenAttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } else { var _masi1 = new MGenAttributeSetInstance(ctx, 0, null); _masi.CopyTo(_masi1); // _masi1.Save(); _masi = null; _masi = _masi1; obj.M_AttributeSetInstance_ID = _masi.GetC_GenAttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } // Save Instance Attributes attributes = aset.GetCGenAttributes(false); Dictionary <MGenAttribute, object> lst = new Dictionary <MGenAttribute, object>(); StringBuilder sql = new StringBuilder(); StringBuilder description = new StringBuilder(); for (int i = 0; i < attributes.Length; i++) { if (MGenAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType())) { object editor = editors[i]; MGenAttributeValue value = null; if (Convert.ToInt32(editors[i].Key) > 0) { value = new MGenAttributeValue(ctx, Convert.ToInt32(editors[i].Key), null); value.SetName(editors[i].Name); } log.Fine(attributes[i].GetName() + "=" + value); if (value != null) { if (attributes[i].IsMandatory() && value == null) { mandatory += " - " + attributes[i].GetName(); } // attributes[i].SetMAttributeInstance(_M_AttributeSetInstance_ID, value); lst[attributes[i]] = value; description.Append(value + "_"); if (i > 0 && sql.Length > 0) { sql.Append(" intersect "); } sql.Append("SELECT DIstinct C_genAttributeSetInstance_ID FROM c_genattributeinstance WHERE IsActive='Y' "); if (value != null) { sql.Append(" AND ( C_GenattributeValue_ID=").Append(value.GetC_GenAttributeValue_ID()).Append(" AND C_GenAttribute_ID=").Append(attributes[i].GetC_GenAttribute_ID() + ")"); } else { sql.Append(" AND ( C_GenattributeValue_ID is null ").Append(" AND C_GenAttribute_ID=").Append(attributes[i].GetC_GenAttribute_ID() + ")"); } } } else if (MGenAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType())) { var editor = editors[i]; var value = Convert.ToDecimal(editor.Name); if (value > 0) { log.Fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory()) { mandatory += " - " + attributes[i].GetName(); } //attributes[i].SetMAttributeInstance(_M_AttributeSetInstance_ID, value); lst[attributes[i]] = value; if (i > 0 && sql.Length > 0) { sql.Append(" intersect "); } sql.Append("SELECT DIstinct C_genAttributeSetInstance_ID FROM c_genattributeinstance WHERE IsActive='Y' "); sql.Append(" AND (VALUENUMBER=").Append(value).Append(" AND C_GenAttribute_ID=").Append(attributes[i].GetC_GenAttribute_ID() + ")"); description.Append(value + "_"); } } else { var editor = editors[i]; var value = editor.Name; if (value != null && value != "") { log.Fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory() && (value == null || value.ToString().Length == 0)) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; if (i > 0 && sql.Length > 0) { sql.Append(" intersect "); } sql.Append("SELECT DISTINCT C_genAttributeSetInstance_ID FROM c_genattributeinstance WHERE IsActive='Y' "); sql.Append(" AND ( upper(VALUE)=upper('").Append(value).Append("') AND C_GenAttribute_ID=").Append(attributes[i].GetC_GenAttribute_ID() + ")"); description.Append(value + "_"); } } } if (description.Length > 0) { description.Remove(description.Length - 1, 1); DataSet ds = DB.ExecuteDataset(sql.ToString(), null); StringBuilder ids = new StringBuilder(); if (ds != null && ds.Tables[0].Rows.Count > 0) { for (int c = 0; c < ds.Tables[0].Rows.Count; c++) { ids.Append(Util.GetValueOfString(ds.Tables[0].Rows[c][0])).Append(","); } } if (ids.ToString().Contains(",")) { ids.Remove(ids.Length - 1, 1); } obj.Description = description.ToString(); if (ids.Length > 0) { obj.GenSetInstance = ids.ToString(); } else { obj.GenSetInstance = "0"; } } else { obj.Description = description.ToString(); } } catch (Exception ex) { log.Severe(ex.Message); } return(obj); }
public AttributeInstance SaveAttribute(int windowNoParent, string strLotStringC, string strSerNoC, string dtGuaranteeDateC, string strAttrCodeC, bool productWindow, int mAttributeSetInstanceId, int mProductId, int windowNo, List <KeyNamePair> values, Ctx ctx) { var editors = values; AttributeInstance obj = new AttributeInstance(); String strLotString = "", strSerNo = "", strAttrCode = ""; int attributeID = 0, prdAttributes = 0, pAttribute_ID = 0, product_id = 0; StringBuilder sql = new StringBuilder(); string qry = ""; StringBuilder qryAttr = null; DataSet ds = null; DateTime? dtGuaranteeDate = null; bool _changed = false; if (!productWindow && strLotStringC != null) { strLotString = strLotStringC; } // L if (!productWindow && strSerNoC != null) { log.Fine("SerNo=" + strSerNoC); strSerNo = strSerNoC; } if (!productWindow && dtGuaranteeDateC != null) { dtGuaranteeDate = Convert.ToDateTime(dtGuaranteeDateC); } // Gua if (!productWindow && strAttrCodeC != null) { strAttrCode = strAttrCodeC; } if (String.IsNullOrEmpty(strAttrCode)) { ctx.SetContext(windowNoParent, "AttrCode", ""); } else { ctx.SetContext(windowNoParent, "AttrCode", strAttrCode); } MAttributeSet aset = null; MAttribute[] attributes = null; String mandatory = ""; var _masi = MAttributeSetInstance.Get(ctx, 0, mProductId); aset = _masi.GetMAttributeSet(); if (aset == null) { return(null); } if (!productWindow && strAttrCode != "") { qryAttr = new StringBuilder(); qryAttr.Append(@"SELECT count(*) FROM M_Product prd LEFT JOIN M_ProductAttributes patr on (prd.M_Product_ID=patr.M_Product_ID) " + " LEFT JOIN M_Manufacturer muf on (prd.M_Product_ID=muf.M_Product_ID) WHERE (patr.UPC = '" + strAttrCode + "' OR prd.UPC = '" + strAttrCode + "' OR muf.UPC = '" + strAttrCode + "')"); //"AND (patr.M_Product_ID = " + _M_Product_ID + " OR prd.M_Product_ID = " + _M_Product_ID + " OR muf.M_Product_ID = " + _M_Product_ID + ")"; prdAttributes = Util.GetValueOfInt(DB.ExecuteScalar(qryAttr.ToString())); if (prdAttributes != 0) { //qryAttr.Clear(); //qryAttr.Append("SELECT M_AttributeSetInstance_ID FROM M_ProductAttributes WHERE UPC = '" + strAttrCode + "' AND M_Product_ID = " + _M_Product_ID); //attributeID = Util.GetValueOfInt(DB.ExecuteScalar(qryAttr.ToString())); //if (attributeID == 0) //{ qryAttr.Clear(); qryAttr.Append("SELECT M_ProductAttributes_ID FROM M_ProductAttributes WHERE UPC = '" + strAttrCode + "'"); pAttribute_ID = Util.GetValueOfInt(DB.ExecuteScalar(qryAttr.ToString())); if (pAttribute_ID != 0) { MProductAttributes patr = new MProductAttributes(ctx, pAttribute_ID, null); attributeID = patr.GetM_AttributeSetInstance_ID(); product_id = patr.GetM_Product_ID(); } //} } _changed = true; } // Attribute Code if (!productWindow && aset.IsLot()) { log.Fine("Lot=" + strLotString); String text = strLotString; _masi.SetLot(text); sql.Append("UPPER(ats.Lot) = '" + text.ToUpper() + "'"); if (aset.IsLotMandatory() && (text == null || text.Length == 0)) { mandatory += " - " + Msg.Translate(ctx, "Lot"); } _changed = true; } // Lot if (!productWindow && aset.IsSerNo()) { log.Fine("SerNo=" + strSerNo); String text = strSerNo; _masi.SetSerNo(text); _masi.SetSerNo(text); if (sql.Length > 0) { sql.Append(" and UPPER(ats.SerNo) = '" + text.ToUpper() + "'"); } else { sql.Append(" UPPER(ats.SerNo) = '" + text.ToUpper() + "'"); } if (aset.IsSerNoMandatory() && (text == null || text.Length == 0)) { mandatory += " - " + Msg.Translate(ctx, "SerNo"); } _changed = true; } if (!productWindow && aset.IsGuaranteeDate()) { log.Fine("GuaranteeDate=" + dtGuaranteeDate); DateTime?ts = dtGuaranteeDate; _masi.SetGuaranteeDate(ts); if (sql.Length > 0) { sql.Append(" AND ats.GuaranteeDate = " + GlobalVariable.TO_DATE(dtGuaranteeDate, true)); } else { sql.Append(" ats.GuaranteeDate = " + GlobalVariable.TO_DATE(dtGuaranteeDate, true)); } if (aset.IsGuaranteeDateMandatory() && ts == null) { mandatory += " - " + Msg.Translate(ctx, "GuaranteeDate"); } _changed = true; } // GuaranteeDate if (sql.Length > 0) { sql.Insert(0, " where "); } sql.Append(" order by ats.m_attributesetinstance_id"); // *** Save Attributes *** // New Instance if (_changed || _masi.GetM_AttributeSetInstance_ID() == 0) { //_masi.Save(); //obj.M_AttributeSetInstance_ID = _masi.GetM_AttributeSetInstance_ID(); //mAttributeSetInstanceId = _masi.GetM_AttributeSetInstance_ID(); //obj.M_AttributeSetInstanceName = _masi.GetDescription(); } // Save Instance Attributes attributes = aset.GetMAttributes(!productWindow); if (attributes.Length > 0) { qry = @"SELECT ats.M_AttributeSetInstance_ID, av.M_AttributeValue_ID,ats.M_AttributeSet_ID,au.Value,att.AttributeValueType FROM M_AttributeSetInstance ats INNER JOIN M_AttributeInstance au ON ats.M_AttributeSetInstance_ID=au.M_AttributeSetInstance_ID LEFT JOIN M_Attribute att ON au.M_Attribute_ID=att.M_Attribute_ID LEFT JOIN M_AttributeValue av ON au.M_AttributeValue_ID=av.M_AttributeValue_ID"; } else { qry = @"SELECT ats.M_AttributeSetInstance_ID FROM M_AttributeSetInstance ats "; } if (sql.Length > 0) { qry += sql; } if (attributes.Length > 0) { qry += ",au.M_Attribute_ID"; } ds = DB.ExecuteDataset(qry, null, null); Dictionary <MAttribute, object> lst = new Dictionary <MAttribute, object>(); for (int i = 0; i < attributes.Length; i++) { if (MAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType())) { object editor = editors[i]; MAttributeValue value = null; if (Convert.ToInt32(editors[i].Key) > 0) { value = new MAttributeValue(ctx, Convert.ToInt32(editors[i].Key), null); value.SetName(editors[i].Name); } log.Fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory() && value == null) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; //attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value); } else if (MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType())) { object editor = editors[i].Name; decimal value = Convert.ToDecimal(editor); log.Fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory()) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; //attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value); } else { object editor = editors[i].Name; String value = Convert.ToString(editor); log.Fine(attributes[i].GetName() + "=" + value); if (attributes[i].IsMandatory() && (value == null || value.Length == 0)) { mandatory += " - " + attributes[i].GetName(); } lst[attributes[i]] = value; //attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value); } _changed = true; } if (_changed) { if (mandatory.Length > 0) { obj.Error = Msg.GetMsg(ctx, "FillMandatory") + mandatory; return(obj); } if (attributes.Length > 0) { if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { int attCount = 0; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { int attSetID = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSet_ID"]); string valueType = Util.GetValueOfString(ds.Tables[0].Rows[i]["AttributeValueType"]); int attributesetinstance_iD = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSetInstance_ID"]); if (i > 0 && attributesetinstance_iD != Util.GetValueOfInt(ds.Tables[0].Rows[i - 1]["M_AttributeSetInstance_ID"])) { attCount = 0; } for (int j = 0; j < attributes.Length; j++) { if (MAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[j].GetAttributeValueType()) && MAttribute.ATTRIBUTEVALUETYPE_List.Equals(valueType)) { int attID = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeValue_ID"]); MAttributeValue atr = new MAttributeValue(ctx, attID, null); if (Util.GetValueOfString(atr.GetName()) == Util.GetValueOfString(lst[attributes[j]]) && attSetID == aset.GetM_AttributeSet_ID()) { attCount += 1; } else { continue; } } else if (MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[j].GetAttributeValueType()) && MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(valueType)) { decimal?attVal = Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["Value"]); if (attVal == Util.GetValueOfDecimal(lst[attributes[j]]) && attSetID == aset.GetM_AttributeSet_ID()) { attCount += 1; } else { continue; } } else if (MAttribute.ATTRIBUTEVALUETYPE_StringMax40.Equals(attributes[j].GetAttributeValueType()) && MAttribute.ATTRIBUTEVALUETYPE_StringMax40.Equals(valueType)) { string attVal = Util.GetValueOfString(ds.Tables[0].Rows[i]["Value"]); if (attVal == Util.GetValueOfString(lst[attributes[j]]) && attSetID == aset.GetM_AttributeSet_ID()) { attCount += 1; } else { continue; } } } if (attCount == attributes.Length) { mAttributeSetInstanceId = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSetInstance_ID"]); break; } } if (attCount != attributes.Length) { mAttributeSetInstanceId = 0; } } else { mAttributeSetInstanceId = 0; } } else { mAttributeSetInstanceId = 0; } ds.Dispose(); } else { if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { mAttributeSetInstanceId = Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_AttributeSetInstance_ID"]); break; } } else { mAttributeSetInstanceId = 0; } } else { mAttributeSetInstanceId = 0; } ds.Dispose(); } if (mAttributeSetInstanceId == 0) { _masi.Save(); mAttributeSetInstanceId = _masi.GetM_AttributeSetInstance_ID(); obj.M_AttributeSetInstance_ID = _masi.GetM_AttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } else { _masi = new MAttributeSetInstance(ctx, mAttributeSetInstanceId, null); } for (int i = 0; i < attributes.Length; i++) { if (MAttribute.ATTRIBUTEVALUETYPE_List.Equals(attributes[i].GetAttributeValueType())) { MAttributeValue value = lst[attributes[i]] != null ? lst[attributes[i]] as MAttributeValue : null; attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, value); } else if (MAttribute.ATTRIBUTEVALUETYPE_Number.Equals(attributes[i].GetAttributeValueType())) { attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, (decimal?)lst[attributes[i]]); } else { attributes[i].SetMAttributeInstance(mAttributeSetInstanceId, (String)lst[attributes[i]]); } } if (attributeID == 0 && strAttrCode != "") { MProductAttributes pAttr = new MProductAttributes(ctx, 0, null); pAttr.SetUPC(strAttrCode); pAttr.SetM_Product_ID(mProductId); pAttr.SetM_AttributeSetInstance_ID(mAttributeSetInstanceId); pAttr.Save(); } _masi.SetDescription(); _masi.Save(); mAttributeSetInstanceId = _masi.GetM_AttributeSetInstance_ID(); obj.M_AttributeSetInstance_ID = _masi.GetM_AttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); // if (attributeID != 0 && (attributeID != mAttributeSetInstanceId || product_id != mProductId)) { obj.Error = Msg.GetMsg(ctx, "AttributeCodeExists"); } } else { obj.M_AttributeSetInstance_ID = _masi.GetM_AttributeSetInstance_ID(); obj.M_AttributeSetInstanceName = _masi.GetDescription(); } return(obj); }