This commit is contained in:
2021-05-12 18:59:40 +00:00
parent 96ad139e79
commit 647dca217a

View File

@@ -1719,50 +1719,44 @@ namespace AyaNova.Biz
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//BIZ ACTIONS //BIZ ACTIONS
// //
//automatic actions on record change, called AFTER validation
// also called when contract changes
// //
private async Task LaborBizActionsAsync(AyaEvent ayaEvent, WorkOrderItemLabor newObj, WorkOrderItemLabor oldObj, IDbContextTransaction transaction, bool contractChanged=false) private async Task LaborBizActionsAsync(AyaEvent ayaEvent, WorkOrderItemLabor newObj, WorkOrderItemLabor oldObj, IDbContextTransaction transaction)
{ {
//automatic actions on record change, called AFTER validation
//currently no processing required except for created or modified at this time //currently no processing required except for created or modified at this time
if (ayaEvent != AyaEvent.Created && ayaEvent != AyaEvent.Modified) if (ayaEvent != AyaEvent.Created && ayaEvent != AyaEvent.Modified)
return; return;
//SET COST / TAX / PRICE //SET TAXES AND PRICING
var Contract = await GetCurrentWorkOrderContractFromRelatedAsync(AyaType.WorkOrderItem, newObj.WorkOrderItemId);
//by default apply all automatic actions with further restrictions possible below //by default apply all automatic actions with further restrictions possible below
bool ApplyTax = true; bool ApplyTax = true;
bool ApplyListPricing = true; bool ApplyPricingUpdate = true;
bool ApplyContractPrice = true;
//NOTE: If contract itself was changed, that will trigger this from the header change and forceFullUpdate will be in effect
//if modifed, see what has changed and should be re-applied //if modifed, see what has changed and should be re-applied
if (ayaEvent == AyaEvent.Modified) if (ayaEvent == AyaEvent.Modified)
{ {
//If it was a service rate change it's like a new record and list pricing and contract applies //If it wasn't a service rate change there is no need to set pricing
//so only potentially restrict what applies if that hasn't changed
if (newObj.ServiceRateId == oldObj.ServiceRateId) if (newObj.ServiceRateId == oldObj.ServiceRateId)
{ {
if (newObj.Price != oldObj.Price)//user manually overrode the price ApplyPricingUpdate = false;
{
ApplyListPricing = false;
ApplyContractPrice = false;
} }
} //If taxes haven't change then no need to update taxes
//same taxes
if (newObj.TaxCodeSaleId == oldObj.TaxCodeSaleId) if (newObj.TaxCodeSaleId == oldObj.TaxCodeSaleId)
ApplyTax = false; ApplyTax = false;
} }
//Tax code //Tax code
if (ApplyTax && newObj.TaxCodeSaleId != null) if (ApplyTax)
{ {
//Default in case nothing to apply
newObj.TaxAPct = 0;
newObj.TaxBPct = 0;
newObj.TaxOnTax = false;
if (newObj.TaxCodeSaleId != null)
{
var t = await ct.TaxCode.AsNoTracking().FirstOrDefaultAsync(z => z.Id == newObj.TaxCodeSaleId); var t = await ct.TaxCode.AsNoTracking().FirstOrDefaultAsync(z => z.Id == newObj.TaxCodeSaleId);
if (t != null) if (t != null)
{ {
@@ -1770,46 +1764,45 @@ namespace AyaNova.Biz
newObj.TaxBPct = t.TaxBPct; newObj.TaxBPct = t.TaxBPct;
newObj.TaxOnTax = t.TaxOnTax; newObj.TaxOnTax = t.TaxOnTax;
} }
else
{
newObj.TaxAPct = 0;
newObj.TaxBPct = 0;
newObj.TaxOnTax = false;
} }
} }
//Pricing //Pricing
if (ApplyListPricing && newObj.ServiceRateId != null) if (ApplyPricingUpdate)
{
//default in case nothing to apply
newObj.Cost = 0;
newObj.ListPrice = 0;
newObj.Price = 0;
//in v7 it was ok to have no service rate selected
//not sure why but carried forward to v8 so..
if (newObj.ServiceRateId != null)
{ {
var s = await ct.ServiceRate.AsNoTracking().FirstOrDefaultAsync(z => z.Id == newObj.ServiceRateId); var s = await ct.ServiceRate.AsNoTracking().FirstOrDefaultAsync(z => z.Id == newObj.ServiceRateId);
if (s != null) if (s != null)
{ {
newObj.Cost = s.Cost; newObj.Cost = s.Cost;
newObj.ListPrice = s.Charge; newObj.ListPrice = s.Charge;
var Contract = await GetCurrentWorkOrderContractFromRelatedAsync(AyaType.WorkOrderItem, newObj.WorkOrderItemId);
LaborApplyContract(newObj, Contract);
} }
else }
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
// APPLY CONTRACT PER UNIT PRICING
//
//(called by woitemlabor save and also by header save on change of contract)
private static void LaborApplyContract(WorkOrderItemLabor o, Contract c)
{ {
newObj.Cost = 0; if (c == null || c.ServiceRatesOverridePct == 0)
newObj.ListPrice = 0; return;
} if (c.ServiceRatesOverrideType == ContractOverrideType.CostMarkup)
} o.Price = o.Cost + (o.Cost * c.ServiceRatesOverridePct);
else if (c.ServiceRatesOverrideType == ContractOverrideType.PriceDiscount)
//Contract o.Price = o.ListPrice - (o.ListPrice * c.ServiceRatesOverridePct);
if (ApplyContractPrice && Contract != null)
{
//update discount pricing
if (Contract.ServiceRatesOverridePct != 0)
{
if (Contract.ServiceRatesOverrideType == ContractOverrideType.CostMarkup)
newObj.Price = newObj.Cost + (newObj.Cost * Contract.ServiceRatesOverridePct);
else if (Contract.ServiceRatesOverrideType == ContractOverrideType.PriceDiscount)
newObj.Price = newObj.ListPrice - (newObj.ListPrice * Contract.ServiceRatesOverridePct);
}
}
} }