Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("ISellerRecieptID,Date,MerchantID")] ISellerReciept iSellerReciept)
        {
            if (id != iSellerReciept.ISellerRecieptID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(iSellerReciept);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ISellerRecieptExists(iSellerReciept.ISellerRecieptID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MerchantID"] = new SelectList(_context.Merchants, "MerchantID", "MerchantName", iSellerReciept.MerchantID);
            return(View(iSellerReciept));
        }
Пример #2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Create(int MerchantID, DateTime Date, double CarPrice, string FishNames, string ProductionTypes, string qtyss, string NOfBoxess)
        {
            if (ModelState.IsValid)
            {
                ISellerReciept sellerReciept = new ISellerReciept();
                sellerReciept.MerchantID     = MerchantID;
                sellerReciept.Date           = Date;
                sellerReciept.CarPrice       = CarPrice;
                sellerReciept.CarDistination = _context.Merchants.Find(MerchantID).Address;
                sellerReciept.PersonID       = 1;
                _context.Add(sellerReciept);


                /*
                 *
                 * دى مش هتتحسب دلوقت لان السواق مش شرط يتحاسب وقتها ممكن يتحاسب بعد كذا يوم
                 *
                 * Person p = _context.People.Find(1);
                 * p.credit -= (decimal)CarPrice;
                 *
                 */

                _context.SaveChanges();


                var FishesCookie          = FishNames.TrimEnd(FishNames[FishNames.Length - 1]);
                var ProductionTypesCookie = ProductionTypes.TrimEnd(ProductionTypes[ProductionTypes.Length - 1]);
                var qtysCookie            = qtyss.TrimEnd(qtyss[qtyss.Length - 1]);
                var NOfBoxesCookie        = NOfBoxess.TrimEnd(NOfBoxess[NOfBoxess.Length - 1]);

                string[] Fishes      = FishesCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                string[] Productions = ProductionTypesCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                double[] qtys        = qtysCookie.Split(",").Select(c => Convert.ToDouble(c)).ToArray();
                //decimal[] unitPrices = unitpricesCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                int[] NOfBoxes = NOfBoxesCookie.Split(",").Select(c => Convert.ToInt32(c)).ToArray();

                //var latestReceipt = _context.ISellerReciepts.Max(x => x.ISellerRecieptID);
                for (int i = 0; i < Fishes.Length; i++)
                {
                    var fish   = _context.Fishes.Single(x => x.FishName == Fishes[i]);
                    var Produc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);

                    ISellerRecieptItem ISellerRecieptItem = new ISellerRecieptItem()
                    {
                        ISellerRecieptID = sellerReciept.ISellerRecieptID,
                        FishID           = fish.FishID,
                        ProductionTypeID = Produc.ProductionTypeID,
                        Qty    = qtys[i],
                        BoxQty = NOfBoxes[i],
                    };
                    var stock = _context.Stocks.Where(i => i.FishID == fish.FishID && i.ProductionTypeID == Produc.ProductionTypeID).FirstOrDefault();

                    if (stock.ProductionTypeID == 3)
                    {
                        stock.Qty         -= qtys[i] * Convert.ToDouble(NOfBoxes[i]);
                        stock.TotalWeight -= qtys[i] * Convert.ToDouble(NOfBoxes[i]);
                    }
                    else
                    {
                        stock.Qty -= qtys[i];
                    }

                    _context.SaveChanges();
                    stock = _context.Stocks.Where(i => i.FishID == fish.FishID && i.ProductionTypeID == Produc.ProductionTypeID).FirstOrDefault();
                    if (stock.Qty == 0)
                    {
                        _context.Stocks.Remove(stock);
                    }
                    _context.ISellerRecieptItems.Add(ISellerRecieptItem);
                    _context.SaveChanges();
                }

                return(Json(new { message = "success", id = sellerReciept.ISellerRecieptID }));
            }

            return(Json(new { message = "fail" }));
        }