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);
            }
        }
        private static DB.SYS_DOC_Header CopyDocument(DB.SYS_DOC_Header entity, DataContext dataContext)
        {
            DB.SYS_DOC_Header docHeader = ApplicationDataContext.DeepClone <DB.SYS_DOC_Header>(entity, SYS.SYS_DOC_Document.New(SYS.SYS_DOC_Type.Quote));
            docHeader.DocumentNumber = null;
            DB.ORG_TRX_Header trxHeader = null;
            if (entity.ORG_TRX_Header != null)
            {
                trxHeader = ApplicationDataContext.DeepClone <DB.ORG_TRX_Header>(entity.ORG_TRX_Header, ORG.ORG_TRX_Header.New);
            }
            DB.JOB_Header jobHeader = null;
            if (entity.JOB_Header != null)
            {
                jobHeader = ApplicationDataContext.DeepClone <DB.JOB_Header>(entity.JOB_Header, JOB.JOB_Header.New);
            }

            foreach (DB.SYS_DOC_Line line in entity.SYS_DOC_Line)
            {
                DB.SYS_DOC_Line newLine = ApplicationDataContext.DeepClone <DB.SYS_DOC_Line>(line, SYS.SYS_DOC_Line.New);

                //Werner: Wanted to do this here but some of the document needs these values to generate another document
                ////Need to clear these when copying a documents lines
                //newLine.QtyReceived = 0;
                //newLine.QtyOutstanding = 0;
                //TODO : Just check but this should always be null as the DeepCopy
                //ignores all DB.* types
                if (line.LineItem != null)
                {
                    newLine.LineItem = line.LineItem;
                }
                else
                {
                    if (!dataContext.ReadonlyContext.VW_LineItem.Any(n => n.Id == newLine.ItemId && n.TypeId == (byte)SYS.SYS_Type.Account))
                    {
                        newLine.LineItem    = dataContext.ReadonlyContext.VW_LineItem.FirstOrDefault(n => n.Id == newLine.ItemId);
                        newLine.UnitAverage = newLine.LineItem.UnitAverage;
                        newLine.UnitCost    = newLine.LineItem.UnitCost;
                    }
                }

                docHeader.SYS_DOC_Line.Add(newLine);
            }

            docHeader.ORG_TRX_Header = trxHeader;
            docHeader.JOB_Header     = jobHeader;

            return(docHeader);
        }
Exemple #3
0
        internal static String Save(DB.SYS_DOC_Line entry, DataContext dataContext)
        {
            try
            {
                if (dataContext.EntitySystemContext.GetEntityState(entry) == EntityState.Detached)
                {
                    dataContext.EntitySystemContext.SYS_DOC_Line.Add(entry);
                }

                Validation.ValidateEntity(dataContext.EntitySystemContext, 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);
        }
Exemple #5
0
        public static void GetSellPrice(DB.SYS_DOC_Line line, DB.VW_Company entity, decimal amount, bool useWarehouseDiscount, out decimal sellPrice, out decimal discountPrice, out decimal discountPercentage, DataContext dataContext)
        {
            if (line == null || line.LineItem == null || (line.LineItem as DB.VW_LineItem).InventoryId == null)
            {
                sellPrice          = line.Amount;
                discountPrice      = line.Amount;
                discountPercentage = 0M;
                return;
            }

            sellPrice          = line.Amount;
            discountPrice      = 0M;
            discountPercentage = 0M;
            if ((entity == null))
            {
                // NO entity SELECTED
                //sellPrice = inventoryItem.UnitPrice;
                return;
            }

            DB.VW_LineItem inventory = line.LineItem as DB.VW_LineItem;
            var            discounts = dataContext.ReadonlyContext.VW_Discount.Where(n => (n.CompanyDiscountCode == entity.DiscountCode || n.EntityId == entity.OrgEntityId) && (n.InventoryDiscountCode == inventory.DiscountCode || n.InventoryId == inventory.InventoryId)).ToList();


            //If any of the discounts are fixed price discounts
            if (discounts.Any(n => n.DiscountAmountTypeId == (byte)BL.ITM.ITM_DIS_AmountType.FixedPriceDiscount))
            {
                if (!useWarehouseDiscount)
                {
                    sellPrice = discounts.Min(n => n.CompanyDiscount).Value;
                }
                else
                {
                    sellPrice = discounts.Min(n => n.WorkshopDiscount).Value;
                }
                discountPrice      = sellPrice;
                discountPercentage = 0;
                return;
            }
            else
            {
                //Henko: I am pretty sure if anything this should be MIN not MAX? But either way this is really badly done...
                if (!useWarehouseDiscount)
                {
                    discountPercentage = discounts.Count() > 0 ? discounts.Max(n => n.CompanyDiscount).Value : 0;
                }
                else
                {
                    discountPercentage = discounts.Count() > 0 ? discounts.Max(n => n.WorkshopDiscount).Value : 0;
                }
            }

            if ((entity.CostCategoryId == (byte)BL.ORG.ORG_CostCategory.CostExcludingSalesTax) || (entity.CostCategoryId == (byte)BL.ORG.ORG_CostCategory.CostIncludingSalesTax))
            {
                // COST INCLUDING or COST EXCLUDING
                if (discountPercentage < 0)
                {
                    discountPrice = inventory.UnitCost - (inventory.UnitCost * discountPercentage / 100);
                    sellPrice     = inventory.UnitCost;
                }
                else
                {
                    discountPrice = (inventory.UnitCost * (100 - discountPercentage)) / 100;
                    sellPrice     = inventory.UnitCost;
                }
                //sellPrice = line.UnitCost;
                return;
            }
            else if (entity.CostCategoryId == (byte)BL.ORG.ORG_CostCategory.AverageCostExcludingSalesTax)
            {
                // AVG COST EXCLUDING
                if (discountPercentage < 0)
                {
                    discountPrice = inventory.UnitAverage - (line.UnitAverage * discountPercentage / 100);
                    sellPrice     = inventory.UnitAverage;
                }
                else
                {
                    discountPrice = (inventory.UnitAverage * (100 - discountPercentage)) / 100;
                    sellPrice     = inventory.UnitAverage;
                }
                //sellPrice = line.UnitAverage;
                return;
            }
            else if ((entity.CostCategoryId == (byte)BL.ORG.ORG_CostCategory.SellingPriceExcludingSalesTax) || (entity.CostCategoryId == (byte)BL.ORG.ORG_CostCategory.SellingPriceIncludingSalesTax))
            {
                // SELLING PRICE INCLUDING or SELLING PRICE EXCLUDING
                if (discountPercentage < 0)
                {
                    discountPrice = inventory.UnitPrice - (inventory.UnitPrice * discountPercentage / 100);
                    sellPrice     = inventory.UnitPrice;
                }
                else
                {
                    discountPrice = (inventory.UnitPrice * (100 - discountPercentage)) / 100;
                    sellPrice     = inventory.UnitPrice;
                }
                //sellPrice = line.UnitAverage;
                return;
            }
        }