public async Task <IActionResult> PersonRecieptCreate(PersonReciept model)
        {
            if (ModelState.IsValid)
            {
                PersonReciept PersonReciept = new PersonReciept()
                {
                    Date = model.Date, PersonName = model.PersonName, TotalPrice = model.TotalPrice
                };
                _context.PersonReciepts.Add(PersonReciept);
                await _context.SaveChangesAsync();

                #region Cookies

                var FishesCookie          = Request.Cookies["FishNames"];
                var ProductionTypesCookie = Request.Cookies["ProductionTypes"];
                var qtysCookie            = Request.Cookies["qtys"];
                var unitpricesCookie      = Request.Cookies["unitprices"];
                var boatsCookie           = Request.Cookies["boats"];

                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();
                #endregion

                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]);
                    PersonRecieptItem PersonRecieptItem = new PersonRecieptItem()
                    {
                        PersonRecieptID  = PersonReciept.PersonRecieptID,
                        FishID           = fish.FishID,
                        ProductionTypeID = Produc.ProductionTypeID,
                        Qty       = qtys[i],
                        UnitPrice = unitPrices[i],
                        BoatID    = boat.BoatID
                    };

                    _context.PersonRecieptItems.Add(PersonRecieptItem);
                    await _context.SaveChangesAsync();
                }


                //فى الوقت الحالى يتم دفع الثمن الى المحصل
                Person p = _context.People.Find(3);
                p.credit += PersonReciept.TotalPrice;

                await _context.SaveChangesAsync();


                return(Json(new { message = "success" }));
                //return RedirectToAction(nameof(Index));
            }

            //return View(model);
            return(Json(new { message = "fail" }));
        }
示例#2
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" }));
        }