public async Task <IActionResult> Create(BoatVM model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                var roles = await _userManager.GetRolesAsync(user);

                int PID = 1;
                if (roles.Contains("partner"))
                {
                    PID = 2;
                }
                model.BoatImage = "default.png";
                if (model.File != null)
                {
                    string uploads  = Path.Combine(_hosting.WebRootPath, "img");
                    string fullPath = Path.Combine(uploads, model.File.FileName);
                    model.File.CopyTo(new FileStream(fullPath, FileMode.Create));
                    model.BoatImage = model.File.FileName;
                }
                Boat boat = new Boat()
                {
                    BoatName          = model.BoatName,
                    TypeID            = model.TypeID,
                    BoatImage         = model.BoatImage,
                    BoatLeader        = model.BoatLeader,
                    BoatLicenseNumber = model.BoatLicenseNumber,
                    DebtsOfHalek      = model.DebtsOfHalek,
                    // DebtsOfMulfunction = model.DebtsOfMulfunction,
                    BoatNumber          = model.BoatNumber,
                    DebtsOfStartingWork = model.DebtsOfStartingWork
                    ,
                    IsActive = true
                };
                db.Boats.Add(boat);
                if (model.chkBoatStatus == true)
                {
                    Person p = db.People.Find(PID);
                    p.credit -= model.DebtsOfStartingWork;
                }

                await db.SaveChangesAsync();

                //-----------------------------------------------------------------------------------------------------------------------------------------
                //اضافة مبدئيه لمع حدوث اكسبشن مع احمدفتح الله ويجب على المالك تعديل بيانات السرحه قبل حساب فاتورة المركب
                //وسيتم تعديل التاريخ بعد عمل فاتوره المركب ليصبح بنفس تاريخ عمل الفاتوره
                Sarha s = new Sarha()
                {
                    BoatID = boat.BoatID, IsFinished = false, NumberOfBoxes = 0, NumberOfFishermen = 6, DateOfSarha = TimeNow()
                };
                db.Sarhas.Add(s);
                await db.SaveChangesAsync();

                //------------------------------------------------------------------------------------------------------------------------------------------
                return(RedirectToAction("ActiveBoats"));
            }
            ViewBag.Types = new SelectList(await db.BoatTypes.ToListAsync(), "TypeID", "TypeName", model.TypeID);
            return(View(model));
        }
Exemplo n.º 2
0
        // [ValidateAntiForgeryToken]
        public IActionResult Create([FromBody()] SarhaVM sVM)
        {
            if (ModelState.IsValid)
            {
                Sarha s = new Sarha()
                {
                    BoatID            = sVM.BoatID,
                    NumberOfFishermen = sVM.NoFisherMen,
                    NumberOfBoxes     = sVM.NoBoxes,

                    DateOfSarha = TimeNow()
                };
                _context.Sarhas.Add(s);
                _context.SaveChanges();
                var latestSarha  = _context.Sarhas.Max(x => x.SarhaID);
                var pricesCookie = Request.Cookies["MyItems"];
                if (pricesCookie != null)
                {
                    decimal[] result = pricesCookie.Split(",".ToCharArray()).Select(c => Convert.ToDecimal(c)).ToArray();
                    for (int i = 0; i < _context.Debts.ToList().Count(); i++)
                    {
                        if (result[i] != 0)
                        {
                            Debts_Sarha d_s = new Debts_Sarha()
                            {
                                SarhaID  = s.SarhaID,
                                DebtID   = _context.Debts.ToList().ElementAt(i).DebtID,
                                Price    = result[i],
                                PersonID = 1,
                                Date     = TimeNow()
                            };



                            _context.Debts_Sarhas.Add(d_s);
                            Person pp = _context.People.Find(1);
                            pp.credit -= result[i];
                            _context.SaveChanges();
                        }
                    }
                    var boat = _context.Boats.Find(sVM.BoatID);
                    boat.DebtsOfHalek += result.Sum();
                    _context.SaveChanges();
                    Response.Cookies.Delete("MyItems");
                }
                return(Json(new { message = "success", id = sVM.BoatID }));
            }
            ViewBag.Boats = new SelectList(_context.Boats, "BoatID", "BoatName", sVM.BoatID);
            SarhaViewModel sarhaViewModel = new SarhaViewModel();

            sarhaViewModel.Sarha = new Sarha()
            {
                BoatID            = sVM.BoatID,
                NumberOfFishermen = sVM.NoFisherMen,
                NumberOfBoxes     = sVM.NoBoxes,
                DateOfSarha       = sVM.DateOfSarha
            };
            return(View(sarhaViewModel));
        }
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Create(BoatOwnerReciept boatOwnerReciept)
        {
            var user = await _userManager.GetUserAsync(User);

            var roles = await _userManager.GetRolesAsync(user);

            int PID = 1;

            if (roles.Contains("partner"))
            {
                PID           = 2;
                ViewBag.title = "المكتب الفرعي / يومية علاء";
            }
            else
            {
                ViewBag.title = "المكتب";
            }

            var TotalBeforePaymentCookie   = boatOwnerReciept.TotalBeforePaying;
            var commisionCookie            = boatOwnerReciept.Commission;
            var PercentageCommissionCookie = boatOwnerReciept.PercentageCommission;
            var PaidFromDebtsCookie        = boatOwnerReciept.PaidFromDebts;
            var TotalProductionCookie      = boatOwnerReciept.TotalAfterPaying;
            //find latest sarha related to selected boat
            var sarhaId = _context.Sarhas.Where(x => x.BoatID == boatOwnerReciept.BoatID && x.IsFinished == false).Max(x => x.SarhaID);

            boatOwnerReciept.SarhaID              = sarhaId;
            boatOwnerReciept.TotalBeforePaying    = Convert.ToDecimal(TotalBeforePaymentCookie);
            boatOwnerReciept.Commission           = Convert.ToDecimal(commisionCookie);
            boatOwnerReciept.PercentageCommission = Convert.ToInt32(PercentageCommissionCookie);
            boatOwnerReciept.TotalAfterPaying     = Convert.ToDecimal(TotalProductionCookie);
            boatOwnerReciept.IsCalculated         = false;
            boatOwnerReciept.IsCollected          = false;
            boatOwnerReciept.PaidFromDebts        = Convert.ToDecimal(PaidFromDebtsCookie);
            boatOwnerReciept.PersonID             = PID;


            // Subtracting Paid From Halek
            var boat = _context.Boats.Find(boatOwnerReciept.BoatID);

            boat.DebtsOfHalek -= Convert.ToDecimal(PaidFromDebtsCookie);
            // Salary for Each One
            var sarha            = _context.Sarhas.Find(sarhaId);
            var IndividualSalary = (Convert.ToDecimal(TotalProductionCookie) / 2) / sarha.NumberOfFishermen;
            // Calculating Final Income
            // for shared boats
            decimal FinalIncome = Convert.ToDecimal(TotalProductionCookie);

            boatOwnerReciept.FinalIncome = FinalIncome;
            _context.Add(boatOwnerReciept);
            await _context.SaveChangesAsync();

            // Cookies Of Receipt Items
            var FishesCookie          = Request.Cookies["FishNames"];
            var ProductionTypesCookie = Request.Cookies["ProductionTypes"];
            var qtysCookie            = Request.Cookies["qtys"];
            var unitpricesCookie      = Request.Cookies["unitprices"];
            var pricesCookie          = Request.Cookies["prices"];


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



            var latestReceipt = _context.BoatOwnerReciepts.Max(x => x.BoatOwnerRecieptID);

            for (int i = 0; i < Fishes.Length; i++)
            {
                string[] splitItem = Fishes[i].Split("/").Select(c => Convert.ToString(c)).ToArray();
                if (splitItem.Length > 1)
                {
                    Guid     amountID     = Guid.NewGuid();
                    double[] splitItemQty = qtys[i].Split("/").Select(c => Convert.ToDouble(c)).ToArray();
                    for (int j = 0; j < splitItem.Length; j++)
                    {
                        var fish   = _context.Fishes.Single(x => x.FishName == splitItem[j]);
                        var Produc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);

                        BoatOwnerItem boatOwnerItem = new BoatOwnerItem()
                        {
                            BoatOwnerRecieptID = latestReceipt,
                            FishID             = fish.FishID,
                            ProductionTypeID   = Produc.ProductionTypeID,
                            Qty       = splitItemQty[j],
                            UnitPrice = unitPrices[i],
                            AmountId  = amountID
                        };
                        _context.BoatOwnerItems.Add(boatOwnerItem);
                        _context.SaveChanges();
                    }
                }
                else
                {
                    var fish   = _context.Fishes.Single(x => x.FishName == Fishes[i]);
                    var Produc = _context.ProductionTypes.Single(x => x.ProductionName == Productions[i]);

                    BoatOwnerItem boatOwnerItem = new BoatOwnerItem()
                    {
                        BoatOwnerRecieptID = latestReceipt,
                        FishID             = fish.FishID,
                        ProductionTypeID   = Produc.ProductionTypeID,
                        Qty       = double.Parse(qtys[i]),
                        UnitPrice = unitPrices[i]
                    };
                    _context.BoatOwnerItems.Add(boatOwnerItem);
                    _context.SaveChanges();
                }
            }
            var sarhaa = _context.Sarhas.Find(sarhaId);

            sarhaa.IsFinished = true;
            sarha.DateOfSarha = boatOwnerReciept.Date;

            //-----------------------------------------------------------------------------------------------------------------------------------------
            //اضافة مبدئيه لمع حدوث اكسبشن مع احمدفتح الله ويجب على المالك تعديل بيانات السرحه قبل حساب فاتورة المركب
            //وسيتم تعديل التاريخ بعد عمل فاتوره المركب ليصبح بنفس تاريخ عمل الفاتوره
            Sarha s = new Sarha()
            {
                BoatID = boat.BoatID, IsFinished = false, NumberOfBoxes = 0, NumberOfFishermen = 6, DateOfSarha = TimeNow()
            };

            _context.Sarhas.Add(s);
            //-------------------------------------------------------------------------------------------------------------------------------------------
            _context.SaveChanges();
            //return RedirectToAction(nameof(Index));
            //return RedirectToAction("Details",new { id= latestReceipt });
            return(Json(new { message = "success", id = boatOwnerReciept.BoatOwnerRecieptID, reciept = boatOwnerReciept.BoatOwnerRecieptID }));
        }