Пример #1
0
        public async Task <ActionResult <List <CardWaste> > > ResetBalance(int CardId)
        {
            Card card   = _context.Cards.Find(CardId);
            int  cardId = card.CardId;

            foreach (Waste waste in _context.Wastes.ToList())
            {
                CardWaste wb = _context.CardWastes.FirstOrDefault(x => x.CardId == cardId && x.WasteId == waste.WasteId);
                if (wb != null)
                {
                    card.Balance            += (wb.Amount * wb.Waste.RecyclingPrice) / 5;
                    wb.Amount                = waste.StartAmount;
                    _context.Entry(wb).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }
                else if (wb == null)
                {
                    _context.CardWastes.Add(new CardWaste {
                        WasteId = waste.WasteId, CardId = cardId, Amount = waste.StartAmount
                    });
                    await _context.SaveChangesAsync();
                }
            }
            _context.Entry(card).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(_context.CardWastes.Where(x => x.CardId == CardId).ToList());
        }
Пример #2
0
        public async Task <ActionResult <CardWaste> > PostWasteBalance(CardWaste wasteBalance)
        {
            _context.CardWastes.Add(wasteBalance);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetWasteBalance", new { id = wasteBalance.CardWasteId }, wasteBalance));
        }
Пример #3
0
        public async Task <IActionResult> PutWasteBalance(int id, CardWaste wasteBalance)
        {
            if (id != wasteBalance.CardWasteId)
            {
                return(BadRequest());
            }
            var local = _context.CardWastes.Find(id);

            _context.Entry(local).State        = EntityState.Detached;
            _context.Entry(wasteBalance).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WasteBalanceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #4
0
        // GET: Employee/Edit/5
        public ActionResult EditCardWaste(int id)
        {
            var       cookie = HttpContext.Request.Cookies["jwt"];
            CardWaste cw     = CallApi.Get(cookie, "CardWastes", id.ToString()).Content.ReadAsAsync <CardWaste>().Result;

            return(View(cw));
        }
Пример #5
0
        public ActionResult EditCardWaste(int id, CardWaste cardWaste)
        {
            var cookie   = HttpContext.Request.Cookies["jwt"];
            var response = CallApi.Put(cookie, "CardWastes", cardWaste, id.ToString());

            return(RedirectToAction("Edit", new { id = cardWaste.CardId }));
        }
Пример #6
0
        public async Task <ActionResult <BillList> > CreateBill(int id, int cardId)
        {
            BillList billList = new BillList()
            {
                bill         = _context.Bills.Find(id),
                billProducts = _context.BillProducts.Where(x => x.BillId == id).ToList()
            };

            billList.bill.CardId = cardId;

            List <BillProduct> billProducts = billList.billProducts;

            if (billProducts == null || billProducts.Count == 0)
            {
                return(BadRequest());
            }
            using (var transaction = _context.Database.BeginTransaction())
            {
                //foreach (BillProduct billProduct in billProducts)
                //{
                //    billProduct.BillId = billList.bill.BillId;
                //}
                //_context.BillProducts.AddRange(billProducts);
                //await _context.SaveChangesAsync();

                double sum      = 0.0;
                double discount = 0.0;
                foreach (BillProduct billProduct in billProducts)
                {
                    sum += billProduct.Amount * _context.Products.Find(billProduct.ProductId).BasePrice;
                    List <CardWaste>    wasteBalances = _context.CardWastes.Where(x => x.CardId == billList.bill.CardId).ToList();
                    List <ProductWaste> productWastes = _context.ProductWastes.Where(x => x.ProductId == billProduct.ProductId).ToList();

                    foreach (ProductWaste productWaste in productWastes)
                    {
                        double    totalAmount = productWaste.Amount * billProduct.Amount;
                        CardWaste cardWaste   = wasteBalances.FirstOrDefault(x => x.WasteId == productWaste.WasteId);
                        totalAmount -= cardWaste.Amount;
                        if (totalAmount >= 0)
                        {
                            cardWaste.Amount -= productWaste.Amount * billProduct.Amount - totalAmount;
                        }
                        else
                        {
                            cardWaste.Amount = -totalAmount;
                            totalAmount      = 0;
                        }
                        _context.Entry(cardWaste).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        discount += (productWaste.Amount * billProduct.Amount - totalAmount) * productWaste.Waste.RecyclingPrice;
                        _context.BillProductWastes.Add(new BillProductWaste {
                            BillProductId = billProduct.Id, WasteId = productWaste.WasteId, DiscountedAmount = productWaste.Amount * billProduct.Amount - totalAmount
                        });
                        await _context.SaveChangesAsync();
                    }
                }
                double total = sum - discount;
                if (_context.Cards.Find(billList.bill.CardId).Balance < total)
                {
                    transaction.Rollback();
                    return(StatusCode(402));
                }
                billList.bill.Discount              = discount;
                billList.bill.Total                 = total;
                billList.bill.IsCompleted           = true;
                _context.Entry(billList.bill).State = EntityState.Modified;
                _context.Cards.Find(billList.bill.CardId).Balance -= total;
                await _context.SaveChangesAsync();

                transaction.Commit();
            }
            return(Redirect("Details/" + billList.bill.BillId));
        }