public DataView MovingAverageTable(int itemID, int storeId, int supplierID, int manufacturerID, int?unitId) { this.ItemID = itemID; this.StoreID = storeId; this.ManufacturerID = manufacturerID; this.UnitID = unitId; this.SupplierID = supplierID; DataView dv = ReceiveDoc.LoadForCostSheetDetail(ItemID, StoreID, SupplierID, ManufacturerID, (int)UnitID); dv.Table.Columns.Add("CUnitCost", System.Type.GetType("System.Double")); dv.Table.Columns.Add("CTotalCost", System.Type.GetType("System.Double")); dv.Table.Columns.Add("CQuantity", System.Type.GetType("System.Int32")); dv.Table.Columns.Add("NUnitCost", System.Type.GetType("System.Double")); dv.Table.Columns.Add("NQuantity", System.Type.GetType("System.Int32")); dv.Table.Columns.Add("NTotalCost", System.Type.GetType("System.Double")); dv.Table.Columns.Add("NDifference", System.Type.GetType("System.Double")); dv.Table.Columns.Add("MarginETB", System.Type.GetType("System.Double")); dv.Table.Columns.Add("NUnitPrice", System.Type.GetType("System.Double")); dv.Table.Columns.Add("NTotalPrice", System.Type.GetType("System.Double")); bool Pending = true; foreach (DataRowView drv in dv) { if (drv["Status"].ToString() == "Pending") { Margin = Convert.ToDouble(drv["Margin"]); if (drv["ReceiptID"] != DBNull.Value) { ReceiptID = Convert.ToInt32(drv["ReceiptID"]); } UnpricedQuantity += Convert.ToInt32(drv["RemainingQuantity"]); UnPricedTotalCost += Convert.ToDouble(drv["TotalRemainingCost"]); if (drv["Margin"] != DBNull.Value) { Pending = false; } UnpricedUnitCost = Convert.ToDouble(drv["ActualCost"]); } else { if (Pending) { Margin = Convert.ToDouble(drv["Margin"]); } OldQuantity += Convert.ToInt32(drv["RemainingQuantity"]); OldTotalCost += (Convert.ToDouble(drv["AverageCost"]) * Convert.ToInt32(drv["RemainingQuantity"])); OldUnitCost = Convert.ToDouble(drv["AverageCost"]); } } NewQuantity = UnpricedQuantity + OldQuantity; NewTotalCost = Math.Round(OldTotalCost + UnPricedTotalCost, BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero); BeforeProrate = NewUnitCost = Math.Round(NewTotalCost / NewQuantity, BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero); PriceDifference = Math.Round(NewTotalCost - (NewUnitCost * NewQuantity), BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero); //Hub only MarginETB = Margin * NewUnitCost; NewSellingPrice = Math.Round(MarginETB + NewUnitCost, BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero); TotalSellingPrice = NewSellingPrice * NewQuantity; foreach (DataRowView drv in dv) { if (drv["Status"].ToString() == "Pending") { drv["CQuantity"] = UnpricedQuantity; drv["CUnitCost"] = Convert.ToDouble(drv["ActualCost"]); drv["CTotalCost"] = UnPricedTotalCost; } else { drv["CQuantity"] = Convert.ToInt32(drv["AveragedQty"]); drv["CUnitCost"] = Convert.ToDouble(drv["AverageCost"]); drv["CTotalCost"] = Convert.ToDouble(drv["AveragedTotalCost"]); } drv["NUnitCost"] = NewUnitCost; drv["NQuantity"] = NewQuantity; drv["NTotalCost"] = NewTotalCost; drv["NDifference"] = PriceDifference; drv["Margin"] = Margin; drv["MarginETB"] = MarginETB; drv["NUnitPrice"] = NewSellingPrice; drv["NTotalPrice"] = TotalSellingPrice; } return(dv); }