This commit is contained in:
2023-01-25 22:24:22 +00:00
parent b9450de83a
commit 1198493bc7
3 changed files with 1278 additions and 22 deletions

View File

@@ -109,10 +109,31 @@ namespace Sockeye.Biz
//this is not the expected format data, stop processing and alert:
throw new System.FormatException($"Vendor data unexpected format:{vn.VendorData}");
}
//parse purchases collection needed up front as it potentially contains Customer record relevant data
var jaPurchaseList = (JArray)jData["order_notification"]["purchase"]["purchase_item"];
string RavenDBId = string.Empty;
foreach (JObject jPurchase in jaPurchaseList)
{
/////DATABASE ID if available used to matchup
//Capture raven database id if present
if (jPurchase["additional_information"] != null)
{
var jaAdditionalItems = (JArray)jPurchase["additional_information"];
foreach (JObject jAdditionalItem in jaAdditionalItems)
{
if (jAdditionalItem["additional_id"] != null && jAdditionalItem["additional_id"].Value<string>() == "DATABASEID")
{
RavenDBId = jAdditionalItem["additional_value"].Value<string>();
break;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
#region CUSTOMER MAKE OR LOCATED
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
//Note: always use reg name not customer name as it may vary between the two and regname is the one we care about
var jCustomerName = jData["order_notification"]["purchase"]["customer_data"]["reg_name"].Value<string>() ?? throw new System.FormatException($"Vendor data empty reg_name:{vn.VendorData}");
if (jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["email"] == null)//we can't process orders with no email at all hard no
throw new System.FormatException($"Vendor data empty email:{vn.VendorData}");
@@ -121,11 +142,22 @@ namespace Sockeye.Biz
var customerBiz = CustomerBiz.GetBiz(ct);
//attempt to match to existing customer
//account number is most ideal match, name second but could be multiple in sockeye from rockfish sites so name will start the same, finally email if nothing else
Customer customer = await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.AccountNumber == jCustomerAccountNumber)
?? await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.Name.StartsWith(jCustomerName))
?? await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.EmailAddress == jCustomerEmail);
//databaseID for raven is best match,account number is next most ideal match, , then name but could be multiple in sockeye from rockfish sites so name will start the same, finally email if nothing else
Customer customer = null;
//First best match is RavenDBID
if (!string.IsNullOrWhiteSpace(RavenDBId))
customer = await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.DbId == RavenDBId);
//if not found then try match in order of best matching
if (customer == null)
customer = await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.AccountNumber == jCustomerAccountNumber)
?? await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.Name.StartsWith(jCustomerName))
?? await ct.Customer.AsNoTracking().FirstOrDefaultAsync(z => z.EmailAddress == jCustomerEmail);
//still no match, consider it a new customer
if (customer == null)
{
//New customer
@@ -180,9 +212,7 @@ namespace Sockeye.Biz
if (await ct.Purchase.AnyAsync(z => z.SalesOrderNumber == salesOrderNumber))
throw new System.ApplicationException($"Sales order already exists: {salesOrderNumber} will not be processed");
//iterate purchase items array
var jaPurchaseList = (JArray)jData["order_notification"]["purchase"]["purchase_item"];
//iterate purchase items array
foreach (JObject jPurchase in jaPurchaseList)
{
Purchase p = new Purchase();
@@ -203,19 +233,8 @@ namespace Sockeye.Biz
p.Revenue = jPurchase["accounting"]["your_revenue"].Value<decimal>();
}
//Capture raven database id if present
if (jPurchase["additional_information"] != null)
{
var jaAdditionalItems = (JArray)jPurchase["additional_information"];
foreach (JObject jAdditionalItem in jaAdditionalItems)
{
if (jAdditionalItem["additional_id"] != null && jAdditionalItem["additional_id"].Value<string>() == "DATABASEID")
{
p.DbId = jAdditionalItem["additional_value"].Value<string>();
break;
}
}
}
if (!string.IsNullOrWhiteSpace(RavenDBId))
p.DbId = RavenDBId;
if (jPurchase["promotion_coupon"] != null)
p.CouponCode = jPurchase["promotion_coupon"].Value<string>();