Exemple #1
0
        public static DaySales GenerateDataForSingleDay(DateTime date, int userID = -1)
        {
            var totalDaySaleInfo = new DaySales();

            totalDaySaleInfo.Date           = date;
            totalDaySaleInfo.TotalIncome    = 0;
            totalDaySaleInfo.TotalProfit    = 0;
            totalDaySaleInfo.ItemsSold      = new List <ReportItemSold>();
            totalDaySaleInfo.TotalItemsSold = 0;
            var currencies = Currency.LoadCurrencies();

            foreach (Currency currency in currencies)
            {
                if (currency.IsDefaultCurrency)
                {
                    totalDaySaleInfo.Currency = currency;
                    break;
                }
            }
            var data = ItemSoldInfo.LoadInfoForDate(date, userID);

            var itemIDToReportSold = new Dictionary <int, ReportItemSold>();

            foreach (ItemSoldInfo singleItemInfo in data)
            {
                if (!itemIDToReportSold.ContainsKey(singleItemInfo.InventoryItemID))
                {
                    ReportItemSold itemSold = new ReportItemSold();
                    itemSold.InventoryItemID   = singleItemInfo.InventoryItemID;
                    itemSold.ItemType          = singleItemInfo.ItemType;
                    itemSold.Name              = singleItemInfo.ItemName;
                    itemSold.Description       = singleItemInfo.ItemDescription;
                    itemSold.QuantityPurchased = 0;
                    itemSold.CostPerItem       = singleItemInfo.Cost; // TODO: should be handled as an average!
                    itemSold.CostCurrency      = singleItemInfo.CostCurrency;
                    itemSold.TotalCost         = 0;
                    itemSold.ProfitPerItem     = singleItemInfo.ProfitPerItem; // TODO: should be handled as an average!
                    itemSold.ProfitCurrency    = singleItemInfo.ProfitPerItemCurrency;
                    itemSold.TotalProfit       = 0;
                    itemIDToReportSold[singleItemInfo.InventoryItemID] = itemSold;
                    totalDaySaleInfo.ItemsSold.Add(itemSold);
                }
                ReportItemSold itemSoldData = itemIDToReportSold[singleItemInfo.InventoryItemID];
                itemSoldData.QuantityPurchased  += singleItemInfo.QuantitySold;
                totalDaySaleInfo.TotalItemsSold += singleItemInfo.QuantitySold;
                if (itemSoldData.CostCurrency.ID == singleItemInfo.CostCurrency.ID)
                {
                    itemSoldData.TotalCost += singleItemInfo.QuantitySold * singleItemInfo.Cost;
                }
                else
                {
                    itemSoldData.TotalCost += singleItemInfo.QuantitySold *
                                              Utilities.ConvertAmount(singleItemInfo.Cost, singleItemInfo.CostCurrency, itemSoldData.CostCurrency);
                }
                if (itemSoldData.ProfitCurrency.ID == singleItemInfo.ProfitPerItemCurrency.ID)
                {
                    itemSoldData.TotalProfit += singleItemInfo.QuantitySold * singleItemInfo.ProfitPerItem;
                }
                else
                {
                    itemSoldData.TotalProfit += singleItemInfo.QuantitySold *
                                                Utilities.ConvertAmount(singleItemInfo.ProfitPerItem, singleItemInfo.ProfitPerItemCurrency, itemSoldData.ProfitCurrency);
                }
                // now add to total income/profit after finding item type money info
                ItemTypeMoneyInfo moneyInfo = null;
                if (singleItemInfo.ItemType != null)
                {
                    if (!totalDaySaleInfo.ItemTypeIDToMoneyInfo.ContainsKey(singleItemInfo.ItemType.ID))
                    {
                        var itemTypeMoneyInfo = new ItemTypeMoneyInfo(singleItemInfo.ItemType);
                        itemTypeMoneyInfo.Currency = totalDaySaleInfo.Currency;
                        totalDaySaleInfo.ItemTypeIDToMoneyInfo[singleItemInfo.ItemType.ID] = itemTypeMoneyInfo;
                        totalDaySaleInfo.ItemTypeMoneyBreakdown.Add(itemTypeMoneyInfo);
                    }
                    moneyInfo = totalDaySaleInfo.ItemTypeIDToMoneyInfo[singleItemInfo.ItemType.ID];
                }
                moneyInfo.TotalItemsSold += singleItemInfo.QuantitySold;
                if (totalDaySaleInfo.Currency.ID == singleItemInfo.CostCurrency.ID)
                {
                    var amountIncrease = singleItemInfo.QuantitySold * singleItemInfo.Cost;
                    totalDaySaleInfo.TotalIncome += amountIncrease;
                    if (moneyInfo != null)
                    {
                        moneyInfo.TotalIncome += amountIncrease;
                    }
                }
                else
                {
                    var amountIncrease = singleItemInfo.QuantitySold *
                                         Utilities.ConvertAmount(singleItemInfo.Cost, singleItemInfo.CostCurrency, totalDaySaleInfo.Currency);
                    totalDaySaleInfo.TotalIncome += amountIncrease;
                    if (moneyInfo != null)
                    {
                        moneyInfo.TotalIncome += amountIncrease;
                    }
                }
                if (totalDaySaleInfo.Currency.ID == singleItemInfo.ProfitPerItemCurrency.ID)
                {
                    var amountIncrease = singleItemInfo.QuantitySold * singleItemInfo.ProfitPerItem;
                    totalDaySaleInfo.TotalProfit += amountIncrease;
                    if (moneyInfo != null)
                    {
                        moneyInfo.TotalProfit += amountIncrease;
                    }
                }
                else
                {
                    var amountIncrease = singleItemInfo.QuantitySold *
                                         Utilities.ConvertAmount(singleItemInfo.ProfitPerItem, singleItemInfo.ProfitPerItemCurrency, totalDaySaleInfo.Currency);
                    totalDaySaleInfo.TotalProfit += amountIncrease;
                    if (moneyInfo != null)
                    {
                        moneyInfo.TotalProfit += amountIncrease;
                    }
                }
            }
            totalDaySaleInfo.ItemsSold.Sort((left, right) => left.Name.CompareTo(right.Name));
            totalDaySaleInfo.ItemTypeMoneyBreakdown.Sort((left, right) => left.Type.Name.CompareTo(right.Type.Name));
            return(totalDaySaleInfo);
        }
Exemple #2
0
        public static WeekSales GenerateDataForWeek(DateTime date, int userID = -1)
        {
            WeekSales weekSales = new WeekSales();

            weekSales.Date = date;
            var allItemsSoldReports = new List <ReportItemSold>();

            var currencies = Currency.LoadCurrencies();

            foreach (Currency currency in currencies)
            {
                if (currency.IsDefaultCurrency)
                {
                    weekSales.Currency = currency;
                    break;
                }
            }

            for (int i = 0; i < 7; i++) // get all sales for the week
            {
                DaySales sales = DaySales.GenerateDataForSingleDay(date.AddDays(i), userID);
                weekSales.AllDaySales.Add(sales);
                if (weekSales.Currency.ID == sales.Currency.ID)
                {
                    weekSales.TotalIncome += sales.TotalIncome;
                }
                else
                {
                    weekSales.TotalIncome += Utilities.ConvertAmount(sales.TotalIncome, sales.Currency, weekSales.Currency);
                }

                if (weekSales.Currency.ID == sales.Currency.ID)
                {
                    weekSales.TotalProfit += sales.TotalProfit;
                }
                else
                {
                    weekSales.TotalProfit += Utilities.ConvertAmount(sales.TotalProfit, sales.Currency, weekSales.Currency);
                }
                weekSales.TotalItemsSold += sales.TotalItemsSold;
                allItemsSoldReports.AddRange(sales.ItemsSold);
                // must add up item type category incomes & profits now
                foreach (ItemTypeMoneyInfo moneyInfo in sales.ItemTypeMoneyBreakdown)
                {
                    // if we don't have info on that item type already, create it
                    if (!weekSales.ItemTypeIDToMoneyInfo.ContainsKey(moneyInfo.Type.ID))
                    {
                        var createdMoneyInfo = new ItemTypeMoneyInfo(moneyInfo.Type);
                        createdMoneyInfo.Currency = weekSales.Currency;
                        weekSales.ItemTypeIDToMoneyInfo[moneyInfo.Type.ID] = createdMoneyInfo;
                        weekSales.ItemTypeMoneyBreakdown.Add(createdMoneyInfo);
                    }
                    var moneyInfoToAdjust = weekSales.ItemTypeIDToMoneyInfo[moneyInfo.Type.ID];
                    moneyInfoToAdjust.TotalItemsSold += moneyInfo.TotalItemsSold;
                    // need to add in the income and profit
                    if (weekSales.Currency.ID == sales.Currency.ID)
                    {
                        moneyInfoToAdjust.TotalIncome += moneyInfo.TotalIncome;
                    }
                    else
                    {
                        moneyInfoToAdjust.TotalIncome += Utilities.ConvertAmount(moneyInfo.TotalIncome, sales.Currency, weekSales.Currency);
                    }
                    if (weekSales.Currency.ID == sales.Currency.ID)
                    {
                        moneyInfoToAdjust.TotalProfit += moneyInfo.TotalProfit;
                    }
                    else
                    {
                        moneyInfoToAdjust.TotalProfit += Utilities.ConvertAmount(moneyInfo.TotalProfit, sales.Currency, weekSales.Currency);
                    }
                }
            }
            // now we need to set up the AllItemsSold array
            var itemIDToReportSold = new Dictionary <int, ReportItemSold>();

            foreach (ReportItemSold singleItemSoldReport in allItemsSoldReports)
            {
                if (!itemIDToReportSold.ContainsKey(singleItemSoldReport.InventoryItemID))
                {
                    itemIDToReportSold[singleItemSoldReport.InventoryItemID] = singleItemSoldReport;
                    weekSales.AllItemsSold.Add(singleItemSoldReport);
                }
                else
                {
                    ReportItemSold allItemsSoldData = itemIDToReportSold[singleItemSoldReport.InventoryItemID];
                    allItemsSoldData.QuantityPurchased += singleItemSoldReport.QuantityPurchased;
                    if (allItemsSoldData.CostCurrency.ID == singleItemSoldReport.CostCurrency.ID)
                    {
                        allItemsSoldData.TotalCost += singleItemSoldReport.QuantityPurchased * singleItemSoldReport.CostPerItem;
                    }
                    else
                    {
                        allItemsSoldData.TotalCost += singleItemSoldReport.QuantityPurchased *
                                                      Utilities.ConvertAmount(singleItemSoldReport.CostPerItem, singleItemSoldReport.CostCurrency, allItemsSoldData.CostCurrency);
                    }
                    if (allItemsSoldData.ProfitCurrency.ID == singleItemSoldReport.ProfitCurrency.ID)
                    {
                        allItemsSoldData.TotalProfit += singleItemSoldReport.QuantityPurchased * singleItemSoldReport.ProfitPerItem;
                    }
                    else
                    {
                        allItemsSoldData.TotalProfit += singleItemSoldReport.QuantityPurchased *
                                                        Utilities.ConvertAmount(singleItemSoldReport.ProfitPerItem, singleItemSoldReport.ProfitCurrency, allItemsSoldData.ProfitCurrency);
                    }
                }
            }
            // sort final arrays for nice display
            weekSales.AllItemsSold.Sort((left, right) => left.Name.CompareTo(right.Name));
            weekSales.ItemTypeMoneyBreakdown.Sort((left, right) => left.Type.Name.CompareTo(right.Type.Name));
            return(weekSales);
        }