示例#1
0
        public async Task <IActionResult> MCreate(MerRecCreateVm model)
        {
            if (model.RecID != 0)
            {
                var FishesCookie          = model.FishNames.TrimEnd(model.FishNames[model.FishNames.Length - 1]);
                var MerchantsCookie       = model.MerchantNames.TrimEnd(model.MerchantNames[model.MerchantNames.Length - 1]);
                var ProductionTypesCookie = model.ProductionTypes.TrimEnd(model.ProductionTypes[model.ProductionTypes.Length - 1]);
                var qtysCookie            = model.qtys.TrimEnd(model.qtys[model.qtys.Length - 1]);
                var unitpricesCookie      = model.unitprices.TrimEnd(model.unitprices[model.unitprices.Length - 1]);

                string[]  Fishes      = FishesCookie.Split(",");          //.Select(c => Convert.ToString(c)).ToArray();
                string[]  Productions = ProductionTypesCookie.Split(","); //.Select(c => Convert.ToString(c)).ToArray();
                string[]  Merchants   = MerchantsCookie.Split(",");       //.Select(c => Convert.ToString(c)).ToArray();
                string[]  qtys        = qtysCookie.Split(",");            //.Select(c => Convert.ToString(c)).ToArray();
                decimal[] unitPrices  = unitpricesCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();

                for (int i = 0; i < Merchants.Length; i++)
                {
                    var merchantt = _context.Merchants.Where(c => c.MerchantName == Merchants.ElementAt(i)).FirstOrDefault();
                    if (merchantt != null)
                    {
                        if (merchantt.IsOwner == false)
                        {
                            MerchantReciept merchantReciept;
                            Merchant        m;

                            var      ee         = Fishes[i].TrimEnd(Fishes[i][Fishes[i].Length - 1]);
                            string[] splitItemm = ee.Split("/");
                            //string[] splitItemm = Fishes[i].Split("/").Select(c => Convert.ToString(c)).ToArray();
                            decimal AddTo_TotalOfReciept;
                            decimal AddTo_CurrentDebt;
                            decimal AddTo_PreviousDebts;
                            if (splitItemm.Length > 1)
                            {
                                AddTo_TotalOfReciept = unitPrices[i];
                                AddTo_CurrentDebt    = unitPrices[i];
                                AddTo_PreviousDebts  = unitPrices[i];
                            }
                            else
                            {
                                AddTo_TotalOfReciept = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                                AddTo_CurrentDebt    = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                                AddTo_PreviousDebts  = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                            }
                            if (GetMerchant(merchantt.MerchantID, TimeNow()) == 0)
                            {
                                merchantReciept = new MerchantReciept()
                                {
                                    Date = TimeNow(), payment = 0, TotalOfReciept = AddTo_TotalOfReciept, MerchantID = merchantt.MerchantID, CurrentDebt = merchantt.PreviousDebts + AddTo_PreviousDebts
                                };
                                _context.Add(merchantReciept);
                                await _context.SaveChangesAsync();
                            }
                            else
                            {
                                merchantReciept = _context.MerchantReciepts.Find(GetMerchant(merchantt.MerchantID, TimeNow()));
                                m = _context.Merchants.Find(merchantt.MerchantID);

                                merchantReciept.TotalOfReciept += AddTo_TotalOfReciept;
                                merchantReciept.CurrentDebt    += AddTo_CurrentDebt;
                            }
                            merchantt.PreviousDebts += AddTo_PreviousDebts;
                            var      boat      = _context.Boats.Where(c => c.BoatName == "المخزن" && c.BoatLicenseNumber == "0").FirstOrDefault();
                            var      eeee      = Fishes[i].TrimEnd(Fishes[i][Fishes[i].Length - 1]);
                            string[] splitItem = eeee.Split("/");
                            if (splitItem.Length > 1)
                            {
                                var      cc           = qtys[i].TrimEnd(qtys[i][qtys[i].Length - 1]);
                                double[] splitItemQty = cc.Split("/").Select(c => Convert.ToDouble(c)).ToArray();
                                var      amountId     = Guid.NewGuid();
                                for (int xx = 0; xx < splitItem.Length; xx++)
                                {
                                    var fishh = _context.Fishes.Single(x => x.FishName == splitItem.ElementAt(xx));
                                    //var Producc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[j]);
                                    var Producc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);
                                    MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                                    {
                                        MerchantRecieptID = merchantReciept.MerchantRecieptID,
                                        FishID            = fishh.FishID,
                                        ProductionTypeID  = Producc.ProductionTypeID,
                                        //Qty = Convert.ToInt32(qtys[i]),
                                        Qty       = splitItemQty[xx],
                                        UnitPrice = unitPrices[i],
                                        BoatID    = boat.BoatID,
                                        AmountId  = amountId
                                    };
                                    _context.MerchantRecieptItems.Add(MerchantRecieptItems);
                                    _context.SaveChanges();
                                }
                            }
                            else
                            {
                                var Individualfish   = _context.Fishes.Single(x => x.FishName == Fishes[i]);
                                var IndividualProduc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);

                                var TodaysMerchantRecItems = _context.MerchantRecieptItems.Include(c => c.MerchantReciept).ToList()
                                                             .Where(c => c.MerchantReciept.Date.ToShortDateString() == TimeNow().ToShortDateString() && c.MerchantRecieptID == merchantReciept.MerchantRecieptID).ToList();
                                var existingFish = TodaysMerchantRecItems.Where(c => c.FishID == Individualfish.FishID && c.BoatID == boat.BoatID).FirstOrDefault();
                                if (existingFish != null)
                                {
                                    if (existingFish.ProductionTypeID == IndividualProduc.ProductionTypeID && existingFish.UnitPrice == unitPrices[i])
                                    {
                                        existingFish.Qty += Convert.ToDouble(qtys[i]);
                                    }
                                    else
                                    {
                                        MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                                        {
                                            MerchantRecieptID = merchantReciept.MerchantRecieptID,
                                            FishID            = Individualfish.FishID,
                                            ProductionTypeID  = IndividualProduc.ProductionTypeID,
                                            Qty       = Convert.ToDouble(qtys[i]),
                                            UnitPrice = unitPrices[i],
                                            BoatID    = boat.BoatID
                                        };
                                        _context.MerchantRecieptItems.Add(MerchantRecieptItems);
                                    }
                                }
                                else
                                {
                                    MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                                    {
                                        MerchantRecieptID = merchantReciept.MerchantRecieptID,
                                        FishID            = Individualfish.FishID,
                                        ProductionTypeID  = IndividualProduc.ProductionTypeID,
                                        Qty       = Convert.ToDouble(qtys[i]),
                                        UnitPrice = unitPrices[i],
                                        BoatID    = boat.BoatID
                                    };
                                    _context.MerchantRecieptItems.Add(MerchantRecieptItems);
                                }
                                _context.SaveChanges();
                            }
                            await _context.SaveChangesAsync();
                        }

                        else
                        {
                            //-----------------------------------------------------
                            IMerchantReciept ImerchantReciept;
                            var      ee         = Fishes[i].TrimEnd(Fishes[i][Fishes[i].Length - 1]);
                            string[] splitItemm = ee.Split("/");
                            //string[] splitItemm = Fishes[i].Split("/").Select(c => Convert.ToString(c)).ToArray();
                            decimal AddTo_TotalOfReciept;

                            if (splitItemm.Length > 1)
                            {
                                AddTo_TotalOfReciept = unitPrices[i];
                            }
                            else
                            {
                                AddTo_TotalOfReciept = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                            }

                            if (GetMerchant(merchantt.MerchantID, TimeNow()) == 0)
                            {
                                ImerchantReciept = new IMerchantReciept()
                                {
                                    Date = TimeNow(), MerchantID = merchantt.MerchantID, TotalOfReciept = AddTo_TotalOfReciept
                                };
                                _context.Add(ImerchantReciept);
                                await _context.SaveChangesAsync();
                            }
                            else
                            {
                                ImerchantReciept = _context.IMerchantReciept.Find(GetMerchant(merchantt.MerchantID, TimeNow()));
                                ImerchantReciept.TotalOfReciept += AddTo_TotalOfReciept;
                            }
                            //--------------------------------------------------------

                            if (splitItemm.Length > 1)
                            {
                                var amountId = Guid.NewGuid();
                                var cc       = qtys[i].TrimEnd(qtys[i][qtys[i].Length - 1]);
                                for (int z = 0; z < splitItemm.Length; z++)
                                {
                                    double[] splitItemQty = cc.Split("/").Select(c => Convert.ToDouble(c)).ToArray();
                                    var      fish         = _context.Fishes.SingleOrDefault(x => x.FishName == splitItemm[z]);

                                    var Produc = _context.ProductionTypes.SingleOrDefault(x => x.ProductionName == Productions[i]);



                                    IMerchantRecieptItem NewIMerchantRecieptItems = new IMerchantRecieptItem()
                                    {
                                        IMerchantRecieptID = ImerchantReciept.IMerchantRecieptID,
                                        FishID             = fish.FishID,
                                        ProductionTypeID   = Produc.ProductionTypeID,
                                        Qty       = splitItemQty[z],
                                        UnitPrice = unitPrices[i],
                                        AmountId  = amountId
                                    };
                                    _context.IMerchantRecieptItem.Add(NewIMerchantRecieptItems);


                                    _context.SaveChanges();
                                    var s = _context.Stocks.ToList().Where(c => c.FishID == fish.FishID).FirstOrDefault();
                                    if (s != null)
                                    {
                                        if (s.ProductionTypeID == Produc.ProductionTypeID)
                                        {
                                            s.Qty += splitItemQty[z];
                                        }
                                        else
                                        {
                                            Stock stoc = new Stock()
                                            {
                                                FishID           = fish.FishID,
                                                ProductionTypeID = Produc.ProductionTypeID,
                                                Qty  = splitItemQty[z],
                                                Date = ImerchantReciept.Date
                                            };
                                            _context.Stocks.Add(stoc);
                                        }
                                    }
                                    else
                                    {
                                        Stock stock = new Stock()
                                        {
                                            FishID           = fish.FishID,
                                            ProductionTypeID = Produc.ProductionTypeID,
                                            Qty = splitItemQty[z]
                                        };
                                        _context.Stocks.Add(stock);
                                    }
                                }
                            }
                            else
                            {
                                var fish   = _context.Fishes.Single(x => x.FishName == Fishes[i]);
                                var Produc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);
                                var TodaysIMerchantRecItems = _context.IMerchantRecieptItem.Include(c => c.IMerchantReciept).ToList().Where(c => c.IMerchantReciept.Date.ToShortDateString() == TimeNow().ToShortDateString() && c.IMerchantRecieptID == ImerchantReciept.IMerchantRecieptID).ToList();
                                var IMerchantRecieptItems   = TodaysIMerchantRecItems.Where(c => c.FishID == fish.FishID && c.UnitPrice == unitPrices[i]).FirstOrDefault();
                                // IMerchantRecieptItem IMerchantRecieptItems;
                                if (IMerchantRecieptItems != null)
                                {
                                    if (IMerchantRecieptItems.ProductionTypeID == Produc.ProductionTypeID && IMerchantRecieptItems.UnitPrice == unitPrices[i])
                                    {
                                        IMerchantRecieptItems.Qty += Convert.ToDouble(qtys[i]);
                                    }
                                    else
                                    {
                                        IMerchantRecieptItem NewIMerchantRecieptItems = new IMerchantRecieptItem()
                                        {
                                            IMerchantRecieptID = ImerchantReciept.IMerchantRecieptID,
                                            FishID             = fish.FishID,
                                            ProductionTypeID   = Produc.ProductionTypeID,
                                            Qty       = Convert.ToDouble(qtys[i]),
                                            UnitPrice = unitPrices[i],
                                        }; _context.IMerchantRecieptItem.Add(NewIMerchantRecieptItems);
                                    }
                                }
                                else
                                {
                                    IMerchantRecieptItem NewIMerchantRecieptItems = new IMerchantRecieptItem()
                                    {
                                        IMerchantRecieptID = ImerchantReciept.IMerchantRecieptID,
                                        FishID             = fish.FishID,
                                        ProductionTypeID   = Produc.ProductionTypeID,
                                        Qty       = Convert.ToDouble(qtys[i]),
                                        UnitPrice = unitPrices[i],
                                    }; _context.IMerchantRecieptItem.Add(NewIMerchantRecieptItems);
                                }
                                _context.SaveChanges();


                                var s = _context.Stocks.ToList().Where(c => c.FishID == fish.FishID).FirstOrDefault();
                                if (s != null)
                                {
                                    if (s.ProductionTypeID == Produc.ProductionTypeID)
                                    {
                                        s.Qty += Convert.ToDouble(qtys[i]);
                                    }
                                    else
                                    {
                                        Stock stoc = new Stock()
                                        {
                                            FishID           = fish.FishID,
                                            ProductionTypeID = Produc.ProductionTypeID,
                                            Qty  = Convert.ToDouble(qtys[i]),
                                            Date = ImerchantReciept.Date
                                        };
                                        _context.Stocks.Add(stoc);
                                    }
                                }
                                else
                                {
                                    Stock stock = new Stock()
                                    {
                                        FishID           = fish.FishID,
                                        ProductionTypeID = Produc.ProductionTypeID,
                                        Qty = Convert.ToDouble(qtys[i])
                                    };
                                    _context.Stocks.Add(stock);
                                }
                            }
                            _context.SaveChanges();



                            var stockrows = _context.Stocks.ToList();
                            foreach (var item in stockrows)
                            {
                                if (item.ProductionTypeID == 3)//ميزان
                                {
                                    item.TotalWeight = item.Qty;
                                }
                            }

                            _context.SaveChanges();
                        }
                    }

                    else
                    {
                        var      ee         = Fishes[i].TrimEnd(Fishes[i][Fishes[i].Length - 1]);
                        string[] splitItemm = ee.Split("/");
                        decimal  AddTo_TotalOfReciept;
                        decimal  AddTo_CurrentDebt;
                        decimal  AddTo_PreviousDebts;
                        if (splitItemm.Length > 1)
                        {
                            AddTo_TotalOfReciept = unitPrices[i];
                            AddTo_CurrentDebt    = unitPrices[i];
                            AddTo_PreviousDebts  = unitPrices[i];
                        }
                        else
                        {
                            AddTo_TotalOfReciept = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                            AddTo_CurrentDebt    = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                            AddTo_PreviousDebts  = unitPrices.ElementAt(i) * Convert.ToDecimal(qtys[i]);
                        }
                        var rec = _context.PersonReciepts.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                        if (rec != null)
                        {
                            rec.TotalPrice += AddTo_TotalOfReciept;
                        }
                        else
                        {
                            PersonReciept personReciept = new PersonReciept()
                            {
                                Date = TimeNow(), PersonName = Merchants.ElementAt(i), TotalPrice = AddTo_TotalOfReciept
                            };
                            _context.PersonReciepts.Add(personReciept);
                        }
                        _context.SaveChanges();
                        var boat = _context.Boats.Where(c => c.BoatName == "المخزن" && c.BoatLicenseNumber == "0").FirstOrDefault();
                        if (splitItemm.Length > 1)  // بيعة
                        {
                            var      cc           = qtys[i].TrimEnd(qtys[i][qtys[i].Length - 1]);
                            double[] splitItemQty = cc.Split("/").Select(c => Convert.ToDouble(c)).ToArray();
                            var      amountId     = Guid.NewGuid();
                            var      latestrec    = _context.PersonReciepts.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                            for (int xx = 0; xx < splitItemm.Length; xx++)
                            {
                                var fishh   = _context.Fishes.Single(x => x.FishName == splitItemm.ElementAt(xx));
                                var Producc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);
                                PersonRecieptItem personRecieptItem = new PersonRecieptItem()
                                {
                                    FishID           = fishh.FishID,
                                    ProductionTypeID = Producc.ProductionTypeID,
                                    Qty             = splitItemQty[xx],
                                    UnitPrice       = unitPrices[i],
                                    BoatID          = boat.BoatID,
                                    AmountId        = amountId,
                                    PersonRecieptID = latestrec.PersonRecieptID
                                };
                                _context.PersonRecieptItems.Add(personRecieptItem);
                                _context.SaveChanges();
                            }
                        }
                        else
                        {
                            var Individualfish   = _context.Fishes.Single(x => x.FishName == Fishes[i]);
                            var IndividualProduc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);
                            var latestrec        = _context.PersonReciepts.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                            var fishh            = _context.Fishes.Single(x => x.FishName == Individualfish.FishName);
                            var Producc          = _context.ProductionTypes.Single(x => x.ProductionName == IndividualProduc.ProductionName);
                            PersonRecieptItem personRecieptItem = new PersonRecieptItem()
                            {
                                FishID           = fishh.FishID,
                                ProductionTypeID = Producc.ProductionTypeID,
                                Qty             = Convert.ToDouble(qtys[i]),
                                UnitPrice       = unitPrices[i],
                                BoatID          = boat.BoatID,
                                PersonRecieptID = latestrec.PersonRecieptID
                            };
                            _context.PersonRecieptItems.Add(personRecieptItem);
                            _context.SaveChanges();
                        }
                    }
                }


                return(Json(new { message = "success" }));
            }
            //return View(model);
            return(Json(new { message = "fail" }));
        }
        public IActionResult Create(MerRecCreateVm model)
        {
            if (model.MerchantID != 0)
            {
                var FishesCookie          = model.FishNames.TrimEnd(model.FishNames[model.FishNames.Length - 1]);
                var ProductionTypesCookie = model.ProductionTypes.TrimEnd(model.ProductionTypes[model.ProductionTypes.Length - 1]);
                var qtysCookie            = model.qtys.TrimEnd(model.qtys[model.qtys.Length - 1]);
                var unitpricesCookie      = model.unitprices.TrimEnd(model.unitprices[model.unitprices.Length - 1]);
                var boatsCookie           = model.boats.TrimEnd(model.boats[model.boats.Length - 1]);


                string[]  Fishes      = FishesCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                string[]  Productions = ProductionTypesCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                string[]  boats       = boatsCookie.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();


                Merchant m = _context.Merchants.Find(model.MerchantID);

                _context.SaveChanges();

                if (m.IsOwner == false)
                {
                    MerchantReciept merchantReciept;

                    if (model.RecID == 0)
                    {
                        merchantReciept = new MerchantReciept()
                        {
                            Date = model.Date, payment = model.payment, TotalOfReciept = model.TotalOfReciept, MerchantID = model.MerchantID, CurrentDebt = model.CurrentDebt
                        };
                        _context.Add(merchantReciept);
                        _context.SaveChanges();
                    }
                    else
                    {
                        merchantReciept = _context.MerchantReciepts.Find(model.RecID);
                        m = _context.Merchants.Find(model.MerchantID);
                        merchantReciept.TotalOfReciept += model.TotalOfReciept;
                        merchantReciept.payment        += model.payment;
                    }


                    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]);
                        var boat   = _context.Boats.Single(x => x.BoatName == boats[i]);

                        var TodaysMerchantRecItems = _context.MerchantRecieptItems.Include(c => c.MerchantReciept).ToList().Where(c => c.MerchantReciept.Date.ToShortDateString() == TimeNow().ToShortDateString() && c.MerchantRecieptID == merchantReciept.MerchantRecieptID).ToList();
                        var existingFish           = TodaysMerchantRecItems.Where(c => c.FishID == fish.FishID && c.BoatID == boat.BoatID).FirstOrDefault();
                        if (existingFish != null)
                        {
                            if (existingFish.ProductionTypeID == Produc.ProductionTypeID)
                            {
                                existingFish.Qty += qtys[i];
                            }
                            else
                            {
                                MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                                {
                                    MerchantRecieptID = merchantReciept.MerchantRecieptID,
                                    FishID            = fish.FishID,
                                    ProductionTypeID  = Produc.ProductionTypeID,
                                    Qty       = qtys[i],
                                    UnitPrice = unitPrices[i],
                                    BoatID    = boat.BoatID
                                };
                                _context.MerchantRecieptItems.Add(MerchantRecieptItems);
                            }
                        }
                        else
                        {
                            MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                            {
                                MerchantRecieptID = merchantReciept.MerchantRecieptID,
                                FishID            = fish.FishID,
                                ProductionTypeID  = Produc.ProductionTypeID,
                                Qty       = qtys[i],
                                UnitPrice = unitPrices[i],
                                BoatID    = boat.BoatID
                            };
                            _context.MerchantRecieptItems.Add(MerchantRecieptItems);
                        }
                        #region trying
                        //if (TodaysMerchantRecItems != null && TodaysMerchantRecItems.Count > 0)
                        //{
                        //    for (int j = 0; j < TodaysMerchantRecItems.Count; j++)
                        //    {
                        //        if (TodaysMerchantRecItems.ElementAt(j).FishID == fish.FishID && TodaysMerchantRecItems.ElementAt(j).ProductionTypeID == Produc.ProductionTypeID && TodaysMerchantRecItems.ElementAt(j).BoatID == boat.BoatID)
                        //        {
                        //            TodaysMerchantRecItems.ElementAt(j).Qty += qtys[i];
                        //        }
                        //        else
                        //        {
                        //            MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                        //            {
                        //                MerchantRecieptID = merchantReciept.MerchantRecieptID,
                        //                FishID = fish.FishID,
                        //                ProductionTypeID = Produc.ProductionTypeID,
                        //                Qty = qtys[i],
                        //                UnitPrice = unitPrices[i],
                        //                BoatID = boat.BoatID
                        //            };
                        //            _context.MerchantRecieptItems.Add(MerchantRecieptItems);
                        //        }


                        //    }


                        //}
                        //else
                        //{
                        //    MerchantRecieptItem MerchantRecieptItems = new MerchantRecieptItem()
                        //    {
                        //        MerchantRecieptID = merchantReciept.MerchantRecieptID,
                        //        FishID = fish.FishID,
                        //        ProductionTypeID = Produc.ProductionTypeID,
                        //        Qty = qtys[i],
                        //        UnitPrice = unitPrices[i],
                        //        BoatID = boat.BoatID
                        //    };
                        //    _context.MerchantRecieptItems.Add(MerchantRecieptItems);

                        //}
                        #endregion
                        _context.SaveChanges();
                    }

                    m = _context.Merchants.Find(model.MerchantID);
                    m.PreviousDebts             = model.CurrentDebt + model.TotalOfReciept;
                    merchantReciept.CurrentDebt = model.CurrentDebt;

                    _context.SaveChanges();
                    return(Json(new { message = "success", id = merchantReciept.MerchantRecieptID }));
                }
                else
                {
                    IMerchantReciept ImerchantReciept;
                    if (model.RecID == 0)
                    {
                        ImerchantReciept = new IMerchantReciept()
                        {
                            Date = model.Date, MerchantID = model.MerchantID, TotalOfReciept = model.TotalOfReciept
                        };
                        _context.Add(ImerchantReciept);
                        _context.SaveChanges();
                    }
                    else
                    {
                        ImerchantReciept = _context.IMerchantReciept.Find(model.RecID);
                        ImerchantReciept.TotalOfReciept += model.TotalOfReciept;
                    }
                    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]);


                        var TodaysIMerchantRecItems = _context.IMerchantRecieptItem.Include(c => c.IMerchantReciept).ToList().Where(c => c.IMerchantReciept.Date.ToShortDateString() == TimeNow().ToShortDateString() && c.IMerchantRecieptID == ImerchantReciept.IMerchantRecieptID).ToList();
                        var IMerchantRecieptItems   = TodaysIMerchantRecItems.Where(c => c.FishID == fish.FishID && c.UnitPrice == unitPrices[i]).FirstOrDefault();
                        // IMerchantRecieptItem IMerchantRecieptItems;
                        if (IMerchantRecieptItems != null)
                        {
                            if (IMerchantRecieptItems.ProductionTypeID == Produc.ProductionTypeID)
                            {
                                IMerchantRecieptItems.Qty += qtys[i];
                            }
                            else
                            {
                                IMerchantRecieptItem NewIMerchantRecieptItems = new IMerchantRecieptItem()
                                {
                                    IMerchantRecieptID = ImerchantReciept.IMerchantRecieptID,
                                    FishID             = fish.FishID,
                                    ProductionTypeID   = Produc.ProductionTypeID,
                                    Qty       = qtys[i],
                                    UnitPrice = unitPrices[i],
                                }; _context.IMerchantRecieptItem.Add(NewIMerchantRecieptItems);
                            }
                        }
                        else
                        {
                            IMerchantRecieptItem NewIMerchantRecieptItems = new IMerchantRecieptItem()
                            {
                                IMerchantRecieptID = ImerchantReciept.IMerchantRecieptID,
                                FishID             = fish.FishID,
                                ProductionTypeID   = Produc.ProductionTypeID,
                                Qty       = qtys[i],
                                UnitPrice = unitPrices[i],
                            }; _context.IMerchantRecieptItem.Add(NewIMerchantRecieptItems);
                        }
                        _context.SaveChanges();
                        var s = _context.Stocks.ToList().Where(c => c.FishID == fish.FishID).FirstOrDefault();
                        if (s != null)
                        {
                            if (s.ProductionTypeID == Produc.ProductionTypeID)
                            {
                                s.Qty += qtys[i];
                            }
                            else
                            {
                                Stock stoc = new Stock()
                                {
                                    FishID           = fish.FishID,
                                    ProductionTypeID = Produc.ProductionTypeID,
                                    Qty  = qtys[i],
                                    Date = ImerchantReciept.Date
                                };
                                _context.Stocks.Add(stoc);
                            }
                        }
                        else
                        {
                            Stock stock = new Stock()
                            {
                                FishID           = fish.FishID,
                                ProductionTypeID = Produc.ProductionTypeID,
                                Qty = qtys[i]
                            };
                            _context.Stocks.Add(stock);
                        }



                        _context.SaveChanges();
                    }

                    var stockrows = _context.Stocks.ToList();
                    foreach (var item in stockrows)
                    {
                        if (item.ProductionTypeID == 3)//ميزان
                        {
                            item.TotalWeight = item.Qty;
                        }
                        //else
                        //{
                        //    //الطوايل هتتم عن طريق التصنيف
                        //}
                    }

                    _context.SaveChanges();
                    return(Json(new { message = "success", id = ImerchantReciept.IMerchantRecieptID }));
                }
            }
            ViewData["MerchantID"] = new SelectList(_context.Merchants, "MerchantID", "MerchantName", model.MerchantID);
            //return View(model);
            return(Json(new { message = "fail" }));
        }