public ZCRMInventoryLineItem GetZCRMInventoryLineItem(JObject lineItemJSON) { JObject productDetails = (JObject)lineItemJSON["product"]; long lineItemId = Convert.ToInt64(lineItemJSON["id"]); ZCRMInventoryLineItem lineItem = ZCRMInventoryLineItem.GetInstance(lineItemId); ZCRMRecord product = ZCRMRecord.GetInstance("Products", Convert.ToInt64(productDetails["id"])); product.LookupLabel = (string)productDetails["name"]; lineItem.Product = product; lineItem.Description = (string)lineItemJSON["product_description"]; lineItem.Quantity = Convert.ToDouble(lineItemJSON["quantity"]); lineItem.ListPrice = Convert.ToDouble(lineItemJSON["list_price"]); lineItem.UnitPrice = Convert.ToDouble(lineItemJSON["unit_price"]); lineItem.Total = Convert.ToDouble(lineItemJSON["total"]); lineItem.Discount = Convert.ToDouble(lineItemJSON["Discount"]); lineItem.TotalAfterDiscount = Convert.ToDouble(lineItemJSON["tota_after_discount"]); lineItem.TaxAmount = Convert.ToDouble(lineItemJSON["Tax"]); JArray lineTaxes = (JArray)lineItemJSON["line_tax"]; foreach (JObject lineTax in lineTaxes) { ZCRMTax tax = ZCRMTax.GetInstance((string)lineTax["name"]); tax.Percentage = Convert.ToDouble(lineTax["percentage"]); tax.Value = Convert.ToDouble(lineTax["value"]); lineItem.AddLineTax(tax); } lineItem.NetTotal = Convert.ToDouble(lineItemJSON["net_total"]); return(lineItem); }
public ZCRMInventoryLineItem GetZCRMInventoryLineItem(JObject lineItemJSON) { JObject productDetails = (JObject)lineItemJSON["product"]; long lineItemId = Convert.ToInt64(lineItemJSON["id"]); ZCRMInventoryLineItem lineItem = ZCRMInventoryLineItem.GetInstance(lineItemId); ZCRMRecord product = ZCRMRecord.GetInstance("Products", Convert.ToInt64(productDetails["id"])); product.LookupLabel = (string)productDetails["name"]; if (productDetails.ContainsKey("Product_Code") && productDetails["Product_Code"].Type != JTokenType.Null) { product.SetFieldValue("Product_Code", (string)productDetails["name"]); } lineItem.Product = product; lineItem.Quantity = Convert.ToDouble(lineItemJSON["quantity"]); lineItem.Discount = Convert.ToDouble(lineItemJSON["Discount"]); lineItem.TotalAfterDiscount = Convert.ToDouble(lineItemJSON["total_after_discount"]); lineItem.NetTotal = Convert.ToDouble(lineItemJSON["net_total"]); lineItem.TaxAmount = Convert.ToDouble(lineItemJSON["Tax"]); lineItem.ListPrice = Convert.ToDouble(lineItemJSON["list_price"]); if (lineItemJSON.ContainsKey("unit_price") && lineItemJSON["unit_price"].Type != JTokenType.Null) { lineItem.UnitPrice = Convert.ToDouble(lineItemJSON["unit_price"]); } lineItem.QuantityInStock = Convert.ToInt32(lineItemJSON["quantity_in_stock"]); lineItem.Total = Convert.ToDouble(lineItemJSON["total"]); lineItem.Description = (string)lineItemJSON["product_description"]; JArray lineTaxes = (JArray)lineItemJSON["line_tax"]; foreach (JObject lineTax in lineTaxes) { ZCRMTax tax = ZCRMTax.GetInstance((string)lineTax["name"]); tax.Percentage = Convert.ToDouble(lineTax["percentage"]); tax.Value = Convert.ToDouble(lineTax["value"]); lineItem.AddLineTax(tax); } return(lineItem); }
public void SetRecordProperties(JObject recordJSON, ZCRMRecord record) { foreach (KeyValuePair <string, JToken> token in recordJSON) { string fieldAPIName = token.Key; if (fieldAPIName.Equals("id")) { record.EntityId = Convert.ToInt64(token.Value); } else if (fieldAPIName.Equals("Product_Details")) { SetInventoryLineItems(token.Value); } else if (fieldAPIName.Equals("Participants")) { SetParticipants(token.Value); } else if (fieldAPIName.Equals("Pricing_Details")) { SetPriceDetails((JArray)token.Value); } else if (fieldAPIName.Equals("Created_By") && token.Value.Type != JTokenType.Null) { JObject createdObject = (JObject)token.Value; ZCRMUser createdUser = ZCRMUser.GetInstance(Convert.ToInt64(createdObject["id"]), (string)createdObject["name"]); record.CreatedBy = createdUser; } else if (fieldAPIName.Equals("Modified_By") && token.Value.Type != JTokenType.Null) { JObject modifiedObject = (JObject)token.Value; ZCRMUser modifiedBy = ZCRMUser.GetInstance(Convert.ToInt64(modifiedObject["id"]), (string)modifiedObject["name"]); record.ModifiedBy = modifiedBy; } else if (fieldAPIName.Equals("Created_Time")) { record.CreatedTime = CommonUtil.removeEscaping((string)JsonConvert.SerializeObject(token.Value)); } else if (fieldAPIName.Equals("Modified_Time")) { record.ModifiedTime = CommonUtil.removeEscaping((string)JsonConvert.SerializeObject(token.Value)); } else if (fieldAPIName.Equals("Owner") && token.Value.Type != JTokenType.Null) { JObject ownerObject = (JObject)token.Value; ZCRMUser ownerUser = ZCRMUser.GetInstance(Convert.ToInt64(ownerObject["id"]), (string)ownerObject["name"]); record.Owner = ownerUser; } else if (fieldAPIName.Equals("Layout") && token.Value.Type != JTokenType.Null) { JObject layoutObject = (JObject)token.Value; ZCRMLayout layout = ZCRMLayout.GetInstance(Convert.ToInt64(layoutObject["id"])); layout.Name = (string)layoutObject["name"]; } else if (fieldAPIName.Equals("Handler") && token.Value.Type != JTokenType.Null) { JObject handlerObject = (JObject)token.Value; ZCRMUser handler = ZCRMUser.GetInstance(Convert.ToInt64(handlerObject["id"]), (string)handlerObject["name"]); record.SetFieldValue(fieldAPIName, handler); } else if (fieldAPIName.Equals("Remind_At") && token.Value.Type != JTokenType.Null) { if (token.Value is JObject) { JObject remindObject = (JObject)token.Value; record.SetFieldValue(fieldAPIName, remindObject["ALARM"]); } else { record.SetFieldValue(fieldAPIName, token.Value); } } else if (fieldAPIName.Equals("Recurring_Activity") && token.Value.Type != JTokenType.Null) { JObject recurringActivityObject = (JObject)token.Value; record.SetFieldValue(fieldAPIName, recurringActivityObject["RRULE"]); } else if (fieldAPIName.Equals("$line_tax") && token.Value.Type != JTokenType.Null) { JArray taxDetails = (JArray)token.Value; foreach (JObject taxDetail in taxDetails) { ZCRMTax tax = ZCRMTax.GetInstance((string)taxDetail["name"]); tax.Percentage = Convert.ToDouble(taxDetail["percentage"]); tax.Value = Convert.ToDouble(taxDetail["value"]); record.AddTax(tax); } } else if (fieldAPIName.Equals("Tax") && token.Value.Type != JTokenType.Null) { var taxNames = token.Value; foreach (string data in taxNames) { ZCRMTax tax = ZCRMTax.GetInstance(data); record.AddTax(tax); } } else if (fieldAPIName.Equals("tags") && token.Value.Type != JTokenType.Null) { JArray jsonArray = (JArray)token.Value; List <string> tags = new List <string>(); foreach (string tag in jsonArray) { tags.Add(tag); } record.TagNames = tags; } else if (fieldAPIName.Equals("Tag") && token.Value.Type != JTokenType.Null) { JArray jsonArray = (JArray)token.Value; foreach (JObject tag in jsonArray) { ZCRMTag tagIns = ZCRMTag.GetInstance(Convert.ToInt64(tag.GetValue("id"))); tagIns.Name = tag.GetValue("name").ToString(); record.Tags.Add(tagIns); } } else if (fieldAPIName.StartsWith("$", StringComparison.CurrentCulture)) { fieldAPIName = fieldAPIName.TrimStart('\\', '$'); if (APIConstants.PROPERTIES_AS_FILEDS.Contains(fieldAPIName)) { record.SetFieldValue(fieldAPIName, token.Value); } else { record.SetProperty(fieldAPIName, token.Value); } } else if (token.Value is JObject) { JObject lookupDetails = (JObject)token.Value; ZCRMRecord lookupRecord = ZCRMRecord.GetInstance(fieldAPIName, Convert.ToInt64(lookupDetails["id"])); lookupRecord.LookupLabel = (string)lookupDetails["name"]; record.SetFieldValue(fieldAPIName, lookupRecord); } else if (token.Value is JArray) { JArray jsonArray = (JArray)token.Value; List <object> values = new List <object>(); foreach (Object obj in jsonArray) { if (obj is JObject) { values.Add((JObject)obj); } else { values.Add(obj); } } record.SetFieldValue(fieldAPIName, values); } else { if (token.Value.Type.ToString().Equals("Date")) { record.SetFieldValue(fieldAPIName, CommonUtil.removeEscaping((string)JsonConvert.SerializeObject(token.Value))); } else { record.SetFieldValue(fieldAPIName, token.Value); } } } }
//TODO<IMPORTANT>: Lots of performance tune-ups are needed and needs plenty of testing!!; public void SetRecordProperties(JObject recordJSON, ZCRMRecord record) { //JObject recordDetails = new JObject(recordJSON); //TODO: If Performance is needed implement JSONTextReader or JSONReader and put thought into it later after completing the SDK; foreach (KeyValuePair <string, JToken> token in recordJSON) { string fieldAPIName = token.Key; if (fieldAPIName.Equals("id")) { record.EntityId = Convert.ToInt64(token.Value); } else if (fieldAPIName.Equals("Product_Details")) { SetInventoryLineItems((JArray)token.Value); } else if (fieldAPIName.Equals("Participants")) { SetParticipants((JArray)token.Value); } else if (fieldAPIName.Equals("Pricing_Details")) { SetPriceDetails((JArray)token.Value); } else if (fieldAPIName.Equals("Created_By")) { JObject createdObject = (JObject)token.Value; ZCRMUser createdUser = ZCRMUser.GetInstance(Convert.ToInt64(createdObject.GetValue("id")), Convert.ToString(createdObject.GetValue("name"))); record.CreatedBy = createdUser; } else if (fieldAPIName.Equals("Modified_By")) { JObject modifiedObject = (JObject)token.Value; ZCRMUser modifiedBy = ZCRMUser.GetInstance(Convert.ToInt64(modifiedObject.GetValue("id")), Convert.ToString(modifiedObject.GetValue("name"))); } else if (fieldAPIName.Equals("Created_Time")) { record.CreatedTime = Convert.ToString(token.Value); } else if (fieldAPIName.Equals("Modified_Time")) { record.ModifiedTime = Convert.ToString(token.Value); } else if (fieldAPIName.Equals("Owner")) { JObject ownerObject = (JObject)token.Value; ZCRMUser ownerUser = ZCRMUser.GetInstance(Convert.ToInt64(ownerObject.GetValue("id")), Convert.ToString(ownerObject.GetValue("name"))); record.Owner = ownerUser; } else if (fieldAPIName.Equals("Layout") && token.Value.Type != JTokenType.Null) { JObject layoutObject = (JObject)token.Value; ZCRMLayout layout = ZCRMLayout.GetInstance(Convert.ToInt64(layoutObject.GetValue("id"))); layout.Name = Convert.ToString(layoutObject.GetValue("name")); } else if (fieldAPIName.Equals("Handler") && token.Value.Type != JTokenType.Null) { JObject handlerObject = (JObject)token.Value; ZCRMUser handler = ZCRMUser.GetInstance(Convert.ToInt64(handlerObject.GetValue("id")), Convert.ToString(handlerObject.GetValue("name"))); record.SetFieldValue(fieldAPIName, handler); } else if (fieldAPIName.Equals("Remind_At") && token.Value.Type == JTokenType.Null) { JObject remindObject = (JObject)token.Value; record.SetFieldValue(fieldAPIName, remindObject.GetValue("ALARM")); } else if (fieldAPIName.Equals("Recurring_Activity") && token.Value.Type != JTokenType.Null) { JObject recurringActivityObject = (JObject)token.Value; record.SetFieldValue(fieldAPIName, recurringActivityObject.GetValue("RRULE")); } else if (fieldAPIName.Equals("$line_tax")) { JArray taxDetails = (JArray)token.Value; foreach (JObject taxDetail in taxDetails) { ZCRMTax tax = ZCRMTax.GetInstance(Convert.ToString(taxDetail.GetValue("name"))); tax.Percentage = Convert.ToDouble(taxDetail.GetValue("percentage")); tax.Value = Convert.ToDouble(taxDetail.GetValue("value")); record.AddTax(tax); } } else if (fieldAPIName.Equals("Tax") && token.Value.Type != JTokenType.Null) { JArray taxNames = (JArray)token.Value; int arrayLen = taxNames.Count; for (int i = 0; i < arrayLen; i++) { ZCRMTax tax = ZCRMTax.GetInstance(Convert.ToString(taxNames[i])); record.AddTax(tax); } } else if (fieldAPIName.StartsWith("$", StringComparison.CurrentCulture)) { fieldAPIName = fieldAPIName.TrimStart('\\', '$'); if (APIConstants.PROPERTIES_AS_FILEDS.Contains(fieldAPIName)) { record.SetFieldValue(fieldAPIName, token.Value); } else { record.SetProperty(fieldAPIName, token.Value); } } else if (token.Value is JObject) { JObject lookupDetails = (JObject)token.Value; ZCRMRecord lookupRecord = ZCRMRecord.GetInstance(fieldAPIName, Convert.ToInt64(lookupDetails.GetValue("id"))); lookupRecord.LookupLabel = Convert.ToString(lookupDetails.GetValue("name")); record.SetFieldValue(fieldAPIName, lookupRecord); } else if (token.Value is JArray) { JArray jsonArray = (JArray)token.Value; List <object> values = new List <object>(); foreach (JObject jsonObject in jsonArray) { values.Add(jsonObject); } record.SetFieldValue(fieldAPIName, values); } else { record.SetFieldValue(fieldAPIName, token.Value); } } }