internal static void UpdateHistoryUnitAverageAndProfitMargin(DB.SYS_DOC_Line line, byte typeId, DataContext dataContext) { //Get current History DB.ITM_History itm_history = BL.ITM.ITM_History.GetItemCurrentHistory(dataContext.EntityInventoryContext.ITM_Inventory.FirstOrDefault(n => n.EntityId == line.ItemId), dataContext); //Force Reload history from DB dataContext.EntityInventoryContext.Entry(itm_history).Reload(); //Calculate new Average Cost decimal?newAverageCost = GetNewAverageCost(ref itm_history, line, typeId, dataContext); //Update Unit Selling form profit margin if (newAverageCost > itm_history.UnitAverage && line.LineItem.ProfitMargin != null) { //Added for the Log entry for UnitPrice itm_history.UnitPrice = Convert.ToDecimal(Math.Round(newAverageCost.Value / ((100.00M - (decimal)line.LineItem.ProfitMargin.Value) / 100.00M), 2)); BL.ITM.ITM_History.UpdateHistoryUnitPrice(line.ItemId, itm_history.UnitPrice, dataContext); } //Change Price to new Average Cost (so that we cannot set stock at a loss if (newAverageCost > itm_history.UnitPrice) { itm_history.UnitPrice = newAverageCost.Value; BL.ITM.ITM_History.UpdateHistoryUnitPrice(line.ItemId, itm_history.UnitPrice, dataContext); } //If OnHand + OnReserve != 0 if (newAverageCost != null && newAverageCost != itm_history.UnitAverage) { //Added for the Log entry for UnitAverage itm_history.UnitAverage = newAverageCost.Value; BL.ITM.ITM_History.UpdateHistoryUnitAverage(line.ItemId, newAverageCost.Value, dataContext); } }
public static DB.ITM_History GetItemCurrentHistory(DB.ITM_Inventory entry, DataContext dataContext) { DB.SYS_Period currentPeriod = SYS.SYS_Period.GetCurrentPeriod(dataContext); DB.ITM_History history = dataContext.EntityInventoryContext.ITM_History.FirstOrDefault(n => n.InventoryId == entry.InventoryId && n.PeriodId == currentPeriod.Id && n.SiteId == ApplicationDataContext.Instance.LoggedInUser.DefaultSiteId); return(history); }
internal static String Save(DB.ITM_History entry, DataContext dataContext) { try { if (dataContext.EntityInventoryContext.GetEntityState(entry) == EntityState.Detached) { dataContext.EntityInventoryContext.ITM_History.Add(entry); } Validation.ValidateEntity(dataContext.EntityInventoryContext, entry); } catch (Validation.EntityValidationException ex) { return(dataContext.PackageValidationException()); } return("Success"); }
private static decimal?GetNewAverageCost(ref DB.ITM_History itm_history, DB.SYS_DOC_Line line, byte documentType, DataContext dataContext) { long stockModifier = dataContext.EntitySystemContext.SYS_DOC_Type.Where(n => n.Id == documentType).Select(n => n.StockModifier).FirstOrDefault(); decimal?newAverageCost = null; if (((itm_history.OnHand + itm_history.OnReserve) + (stockModifier * line.Quantity)) != 0) { switch (documentType) { case (byte)BL.SYS.SYS_DOC_Type.Quote: break; case (byte)BL.SYS.SYS_DOC_Type.SalesOrder: break; case (byte)BL.SYS.SYS_DOC_Type.TAXInvoice: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.UnitAverage)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.CreditNote: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.UnitAverage)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.PickingSlip: break; case (byte)BL.SYS.SYS_DOC_Type.PurchaseOrder: break; case (byte)BL.SYS.SYS_DOC_Type.GoodsReceived: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.Amount)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.GoodsReturned: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.Amount)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.Job: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.UnitAverage)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.TransferRequest: break; case (byte)BL.SYS.SYS_DOC_Type.TransferShipment: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.UnitAverage)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.TransferReceived: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.Amount)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.InventoryAdjustment: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.Amount)) / ((itm_history.OnHand + itm_history.OnReserve) + stockModifier * line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.BackOrder: break; case (byte)BL.SYS.SYS_DOC_Type.BOMCanceled: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.UnitAverage)) / ((itm_history.OnHand + itm_history.OnReserve) + line.Quantity); break; case (byte)BL.SYS.SYS_DOC_Type.BOMComplete: newAverageCost = (((itm_history.OnHand + itm_history.OnReserve) * itm_history.UnitAverage) + ((stockModifier * line.Quantity) * line.UnitAverage)) / ((itm_history.OnHand + itm_history.OnReserve) + line.Quantity); break; } } return(newAverageCost); }