We have REST setups that create new Sales Orders that don’t include customer discounts.
I am trying to create a data directive in OrderHed update that will update OrderHed and OrderDtls for a SalesOrder if the Customer’s discount wasn’t added to an order. So far I can update all OrderDtls and fields in OrderHed, but can’t figure out how to get OrderHed to recalculate the totals with the new discount.
Here is my code:
using(var txScope = IceContext.CreateDefaultTransactionScope())
{
// Current ttOrderHed Row
var tempOrderHed = ttOrderHed.FirstOrDefault();
string tqhCompany = tempOrderHed.Company;
int tqhOrderNum = tempOrderHed.OrderNum;
int tqhCustNum = tempOrderHed.CustNum;
string tqhRowMod = tempOrderHed.RowMod;
// Empty Varibles to Use Later
string companyNum = "";
int orderNum = 0;
int custNum = 0;
decimal custDisc = 0.00m;
decimal orderDisc = 0.00m;
string sysRowID = "";
// Look Up OrderHed in Database - Confirm Order's in Company #####
if( tqhCompany == "#####" )
{
companyNum = tempOrderHed.Company.ToString();
orderNum = Convert.ToInt32(tempOrderHed.OrderNum);
custNum = Convert.ToInt32(tempOrderHed.CustNum);
// Create Db var orderHed and Link to OrderHed in Db
var orderHed = Db.OrderHed.Where(oh => oh.Company == companyNum && oh.OrderNum == orderNum && oh.CustNum == custNum).FirstOrDefault();
// Make sure there is a valid OrderHed in orderHed
if(orderHed != null)
{
// Get OrderHed Discount & SysRowID of OrderHed
orderNum = Convert.ToInt32(orderHed.OrderNum);
orderDisc = Convert.ToDecimal(orderHed.DiscountPercent);
sysRowID = orderHed.SysRowID.ToString();
}
// Create Db var customerDB and Link to Customer in Db
var customerDB = Db.Customer.Where(cdb => cdb.Company == companyNum && cdb.CustNum == custNum).FirstOrDefault();
// Make sure there is a valid Customer in customerDB
if(customerDB != null)
{
// Get Customer Discount for the Customer
custDisc = Convert.ToDecimal(customerDB.DiscountPercent);
}
// If OrderHed Discount Percent doesn't equal Customer Discount Percent run the below code
if(orderDisc != custDisc)
{
var docTotal = Convert.ToDecimal(orderHed.DocTotalCharges);
var discount = Convert.ToDecimal(custDisc / 100);
var discountTotal = Convert.ToDecimal(docTotal * discount);
orderHed.DiscountPercent = Convert.ToDecimal(custDisc);
orderHed.DocTotalDiscount = Convert.ToDecimal(discountTotal);
Db.Validate();
var bo = ServiceRenderer.GetService<Erp.Contracts.SalesOrderSvcContract>(Db);
// Run foreach function to check and update discount percentage for each OrderDtl Line
foreach(var orderDtl in Db.OrderDtl.Where(od => od.Company == orderHed.Company && od.OrderNum == orderHed.OrderNum).OrderBy(ol => ol.OrderLine))
{
// Empty Varibles
string companyNumber = "";
int ordNum = 0;
int orderLine = 0;
int customerNum = 0;
decimal lineDisc = 0.00m;
// Make sure there is a valid OrderDtl Line in orderDtl
if(orderDtl != null)
{
// Set Varibles Values
companyNumber = orderDtl.Company.ToString();
ordNum = Convert.ToInt32(orderDtl.OrderNum);
orderLine = Convert.ToInt32(orderDtl.OrderLine);
customerNum = Convert.ToInt32(orderDtl.CustNum);
lineDisc = Convert.ToDecimal(orderDtl.DiscountPercent);
// If Customer Discount Percent doesn't equal OrderDtl Line Discount Percent update var lineDisc to equal var custDisc
if(lineDisc != custDisc)
{
lineDisc = Convert.ToDecimal(custDisc);
int serialNum = (Convert.ToInt32(orderDtl.OrderLine) - 1);
var ds = bo.GetByID(ordNum);
// Create Before Image
var origRow = ds.OrderDtl.NewRow();
BufferCopy.Copy(orderDtl, origRow);
ds.OrderDtl.Add(origRow);
// Update Record
ds.OrderDtl[serialNum].OrderLine = Convert.ToInt32(orderLine);
ds.OrderDtl[serialNum].DiscountPercent = Convert.ToDecimal(lineDisc);
ds.OrderDtl[serialNum].RowMod = "U";
bo.ChangeDiscountPercent(ref ds);
ds.OrderDtl[serialNum].RowMod = "U";
ds.OrderDtl[serialNum].DiscountPercent = Convert.ToDecimal(lineDisc);
ds.OrderDtl[serialNum].OrderLine = Convert.ToInt32(orderLine);
bo.Update(ref ds);
// Update Discount Percentage for Each OrderDtl
Db.Validate();
}
}
}
}
}
////////// Now To Test :)
txScope.Complete();
}