示例#1
0
        public async Task <IEnumerable <ExpensesViewModel> > GetExpenses(UserProfile user, int?shopId, DateTime from, DateTime to)
        {
            var list = new List <ExpensesViewModel>();
            IEnumerable <Expense> expenses = null;

            var avl = _shopsChecker.CheckAvailability(user, shopId);

            if (!avl.isCorrectShop)
            {
                return(new List <ExpensesViewModel>());
            }
            ;
            if (!avl.hasShop && avl.isAdmin)
            {
                expenses = await _expRepo.GetExpensesAsync(user.business_id.Value, null, from, to);
            }
            else if (!avl.hasShop && !avl.isAdmin)
            {
                return(new List <ExpensesViewModel>());
            }
            else if (avl.hasShop)
            {
                expenses = await _expRepo.GetExpensesAsync(user.business_id.Value, shopId.Value, from, to);
            }

            if (expenses == null || !expenses.Any())
            {
                return(new List <ExpensesViewModel>());
            }
            ;



            foreach (var group in expenses)
            {
                var expensesViewModel = new ExpensesViewModel {
                    id = group.id, reportDate = group.report_date, totalSum = group.sum, shopId = group.shop_id
                };

                var dict = new List <ExpensesValueViewModel>();
                foreach (var ed in group.ExpensesDetails)
                {
                    dict.Add(new ExpensesValueViewModel
                    {
                        id    = ed.expenses_type_id,
                        key   = ed.ExpensesType.type,
                        value = ed.sum
                    });
                }

                expensesViewModel.expenses = dict;
                list.Add(expensesViewModel);
            }

            return(list);
        }
示例#2
0
        public async Task <IEnumerable <ProductViewModel> > GetStocks(UserProfile user, int?shopId)
        {
            var list = new List <ProductViewModel>();
            IEnumerable <Stock> stocks = new List <Stock>();

            var avl = _shopsChecker.CheckAvailability(user, shopId);

            if (!avl.isCorrectShop)
            {
                return(new List <ProductViewModel>());
            }
            if (!avl.hasShop && avl.isAdmin)
            {
                stocks = stockRepo.GetStocksWithProductsByBusiness(user.business_id.Value).ToList();
            }
            else if (!avl.hasShop && !avl.isAdmin)
            {
                return(new List <ProductViewModel>());
            }
            else if (avl.hasShop)
            {
                stocks = await stockRepo.GetStocksWithProducts(shopId.Value);
            }

            if (stocks != null && stocks.Any())
            {
                foreach (var stock in stocks)
                {
                    var product = stock.Product;
                    var cost    = costRepo.GetByProdId(product.id).FirstOrDefault();
                    var price   = priceRepo.GetPriceByProdId(product.id);
                    if (stock.count > 0)
                    {
                        list.Add(new ProductViewModel
                        {
                            Id         = product.id,
                            ProdName   = product.name,
                            Stock      = Convert.ToDecimal(stock.count),
                            Cost       = cost != null && cost.value.HasValue ? cost.value.Value : 0,
                            Price      = price != null && price.price.HasValue ? price.price.Value : 0,
                            VendorCode = product.attr1,
                            ImgUrl     = product.Image?.img_url_temp,
                            Color      = product.attr10,
                            Size       = product.attr9,
                            UnitId     = product.unit_id.HasValue ? product.unit_id.Value : 0
                        });
                    }
                }
            }

            return(list);
        }
示例#3
0
        public async Task <IEnumerable <OrderViewModel> > GetCancellations(UserProfile user, DateTime from, DateTime to, int shopId)
        {
            IEnumerable <Shop> shops = new List <Shop>();
            var orders    = new List <OrderViewModel>();
            var ordersDal = new List <Order>();

            var avl = shopsChecker.CheckAvailability(user, shopId);

            if (!avl.isCorrectShop)
            {
                return(new List <OrderViewModel>());
            }
            if (!avl.hasShop && avl.isAdmin)
            {
                shops = shopRepo.GetShopsByBusiness(user.business_id.Value);
            }
            else if (!avl.hasShop && !avl.isAdmin)
            {
                return(new List <OrderViewModel>());
            }
            else if (avl.hasShop)
            {
                shops = new List <Shop> {
                    shopRepo.GetById(shopId)
                };
            }

            if (shops == null || !shops.Any())
            {
                return(new List <OrderViewModel>());
            }

            foreach (var shop in shops)
            {
                ordersDal.AddRange(await ordersRepo.GetCancellationsByShopIdInDateRange(shop.id, from, to));
            }

            var orderGroups = ordersDal.OrderByDescending(p => p.report_date);

            foreach (var group in orderGroups)
            {
                var orderVm = new OrderViewModel
                {
                    id         = group.id,
                    reportDate = group.report_date
                };
                foreach (var item in group.OrderDetails)
                {
                    var prodDal = await productRepo.GetByIdAsync(item.prod_id);

                    var cost = costRepo.GetByProdAndShopIds(item.prod_id, shopId);
                    var prod = new OrderRowViewModel
                    {
                        image      = (await imgRepo.GetByIdAsync(item.prod_id))?.img_url_temp,
                        name       = prodDal.name,
                        price      = cost.value,
                        count      = item.count,
                        totalPrice = item.count * cost.value,
                        vendorCode = prodDal.attr1
                    };
                    prod.totalPrice = prod.price * prod.count;
                    orderVm.products.Add(prod);
                }
                orders.Add(orderVm);
            }
            return(orders);
        }
示例#4
0
        public async Task <IEnumerable <SalesViewModel> > GetSales(int userId, int shopId, DateTime from, DateTime to)
        {
            IEnumerable <Shop> shops = new List <Shop>();
            var salesVm = new List <SalesViewModel>();

            var user = _userRepo.GetById(userId);

            var avl = _shopsChecker.CheckAvailability(user, shopId);

            if (!avl.isCorrectShop)
            {
                return(new List <SalesViewModel>());
            }
            if (!avl.hasShop && avl.isAdmin)
            {
                shops = _shopRepo.GetShopsByBusiness(user.business_id.Value);
            }
            else if (!avl.hasShop && !avl.isAdmin)
            {
                return(new List <SalesViewModel>());
            }
            else if (avl.hasShop)
            {
                shops = new List <Shop> {
                    _shopRepo.GetById(shopId)
                };
            }

            if (shops == null || !shops.Any())
            {
                return(new List <SalesViewModel>());
            }


            foreach (var shop in shops)
            {
                shop.Bills = (await _billsRepo.GetBillsWithSales(shop.id, from, to)).ToList();
            }

            foreach (var shop in shops)
            {
                var bills = shop.Bills;

                foreach (var bill in bills)
                {
                    decimal totalSum = 0;

                    var productsVm = new List <SalesProductViewModel>();
                    var products   = new List <Product>();

                    foreach (var sale in bill.Sales)
                    {
                        productsVm.Add(new SalesProductViewModel
                        {
                            imageUrl   = (await _imgRepo.GetByIdAsync(sale.prod_id))?.img_url_temp,
                            ProdName   = sale.Product.name,
                            VendorCode = "",
                            Summ       = sale.sum,
                            Count      = sale.count,
                            Price      = sale.price
                        });
                        if (sale.Product.Price != null && sale.Product.Price.price.HasValue)
                        {
                            totalSum += sale.price * sale.count;
                        }
                    }

                    var discount = totalSum != 0 ? Math.Ceiling((1 - bill.sum / totalSum) * 100) : 0;
                    var saleVm   = new SalesViewModel
                    {
                        id         = bill.id,
                        reportDate = bill.report_date,
                        products   = productsVm,
                        totalSum   = bill.sum,
                        discount   = discount < 0 ? 0 : discount
                    };
                    salesVm.Add(saleVm);
                }
            }
            return(salesVm);
        }