The Solution!!
to give some context to this code, in our configurator we have added some fields as in the image below:
the code below will then only fire if the checkbox is ticked, otherwise it stops there
if it is ticked the code will execute and in a breif summary do the following:
- Look at the quote / order and retreive any misc line charges
- delete any exitsing misc charges that have the code (in our case) “CC”
- then look at all the fields in the configurator and see which ones have a value greater than zero
- write one misc line per charge along with the description
- sets the prices and frequency
if(!Inputs.CCRequired_chk.Value) return;
int iQuoteNum = Context.QuoteNumber;
int iQuoteLine = Context.QuoteLineNumber;
int iOrderNum = Context.OrderNumber;
int iOrderLine = Context.OrderLineNumber;
if (iQuoteNum > 0)
{
using (var svc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.QuoteSvcContract>(Db))
{
// 1) Define charges (amount + description inputs)
var charges = new[]
{
new { amount = Inputs.Aperture600Total_dec.Value, desc = Inputs.Aperture600des_txt.Value },
new { =====ADD MORE INPUTS AS REQUIRED },
}
// 2) Delete all existing lines where code is equal to "cc"
var tsExisting = svc.GetByID(iQuoteNum);
var ccLines = tsExisting.QuoteMsc
.Where(m => m.QuoteNum == iQuoteNum && m.QuoteLine == iQuoteLine && m.MiscCode == "CC")
.ToList();
if (ccLines.Count > 0)
{
foreach (var m in ccLines) m.RowMod = "D"; // set rows to delete
svc.Update(ref tsExisting); // commit deletions
}
// 3) Add back only non-zero charges
foreach (var c in charges)
{
if (c.amount <= 0m) continue; // ignore where there are no charges
var ts = new Erp.Tablesets.QuoteTableset();
svc.GetNewQuoteMsc(ref ts, iQuoteNum, iQuoteLine, 0);
var row = ts.QuoteMsc.First();
row.MiscCode = "CC"; // Sets to the CC misc code
row.Description = string.IsNullOrWhiteSpace(c.desc) ? "CC Charge" : c.desc.Trim(); // if the description field is blank, sets it to CC Charge, otherwise uses the description and removes white space
row.FreqCode = "F"; // sets the frequency to first can also use E for every or L for Last
row.Type = "A"; // tells database its a new row being added
row.CurrencySwitch = false; // does not switch currency
row.DocDspMiscAmt = c.amount; // sets the price
svc.ChangeMiscAmt(ref ts, "QuoteMsc"); // writes the price
svc.Update(ref ts); // saves and commits changes
}
}
}
else if (iOrderNum > 0)
{
using (var svc = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.SalesOrderSvcContract>(Db))
{
// 1) Define charges (amount + description inputs)
var charges = new[]
{
new { amount = Inputs.Aperture600Total_dec.Value, desc = Inputs.Aperture600Desc_txt.Value },
new { amount = Inputs.Aperture601Total_dec.Value, desc = Inputs.Aperture601Desc_txt.Value },
new { amount = Inputs.Aperture2Total_dec.Value, desc = Inputs.Aperture2Dec_txt.Value },
new { amount = Inputs.Doorway1Total_dec.Value, desc = Inputs.Doorway1Des_txt.Value },
new { amount = Inputs.Doorway2Total_dec.Value, desc = Inputs.Doorway2Desc_txt.Value },
new { amount = Inputs.Penetration100Total_dec.Value,desc = Inputs.Penetration100Des_txt.Value },
new { amount = Inputs.MitreWidthTotal_dec.Value, desc = Inputs.MitreWidthDes_txt.Value },
new { amount = Inputs.ApertureWonkyTotal_dec.Value, desc = Inputs.ApertureWonkyDes_txt.Value },
new { amount = Inputs.NotchTotal_dec.Value, desc = Inputs.NotchDesc_txt.Value },
new { amount = Inputs.ProSawTotal_dec.Value, desc = Inputs.ProsawCutDesc_txt.Value }
};
// 2) Hard delete all existing CC lines on this order line
var tsExisting = svc.GetByID(iOrderNum);
var ccLines = tsExisting.OrderMsc
.Where(m => m.OrderNum == iOrderNum && m.OrderLine == iOrderLine && m.MiscCode == "CC")
.ToList();
if(ccLines.Count > 0)
{
foreach(var m in ccLines) m.RowMod = "D"; // set rows to delete
svc.Update(ref tsExisting); //commit delete
}
// 3) Add back only non-zero charges
foreach(var c in charges)
{
if(c.amount <=0m) continue; // ignore where there are no charges
var tsNew = new Erp.Tablesets.SalesOrderTableset();
svc.GetNewOrderMsc(ref tsNew, iOrderNum, iOrderLine);
var row = tsNew.OrderMsc.First();
row.MiscCode = "CC"; // Sets to the CC misc code
svc.ChangeMiscCode(ref tsNew, "OrderMsc"); // sets defaults that are set on the misc code
row.Description = string.IsNullOrWhiteSpace(c.desc) ? "CC Charge" : c.desc.Trim(); // if the description field is blank, sets it to CC Charge, otherwise uses the description and removes white space
row.FreqCode = "F"; // sets the frequency to first can also use E for every or L for Last
row.DocDspMiscAmt = c.amount; // sets the price from the total fields in the configurator
svc.ChangeMiscAmount(ref tsNew, "OrderMsc"); // writes the price
svc.Update(ref tsNew); // saves and commits the changes
}
}
}
final output looks like the following:
you can then go into the configurator, changes quantities etc and the comments if required and it will remove ALL existing lines with the CC code, then repace with new ones.