Exemplo n.º 1
0
        public IActionResult GiveFathAllah(decimal price, int personID)
        {
            Person        p = _context.People.Find(4);
            FathAllahGift g = new FathAllahGift()
            {
                PersonID = personID, Date = TimeNow(), CreditBefore = p.credit, charge = price, CreditAfter = price + p.credit
            };

            p.credit += price;
            Person pp = _context.People.Find(personID);

            pp.credit -= price;
            _context.FathAllahGifts.Add(g);
            _context.SaveChanges();
            return(Json(new { message = "success", newCredit = g.CreditAfter }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> FinalCalc(decimal fathallah, decimal Cash, string MerchantName, string Price, string ToMerchantName, string ToPrice, string BoatName, string HalekPrice,
                                                    string HalekName, string Adding, string AddingPrice, string BoatNameExpenses, string ExpensePricee, string Cause)
        {
            Collecting c = new Collecting()
            {
                Date             = TimeNow(),
                TotalForFahAllah = fathallah,
                TotalOfCashes    = Cash
            };



            if (MerchantName != null && Price != null)
            {
                var       MerchantNameCookie = MerchantName.TrimEnd(MerchantName[MerchantName.Length - 1]);
                var       PriceCookie        = Price.TrimEnd(Price[Price.Length - 1]);
                string[]  MerchantNames      = MerchantNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] prices             = PriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();

                for (int i = 0; i < MerchantNames.Length; i++)
                {
                    var             merchant = _context.Merchants.FirstOrDefault(c => c.MerchantName == MerchantNames[i]);
                    PaidForMerchant m;
                    if (merchant.IsOwner == true)
                    {
                        m = new PaidForMerchant()
                        {
                            IsPaidForUs = true,
                            MerchantID  = merchant.MerchantID,
                            Payment     = prices[i],
                            PreviousDebtsForMerchant = merchant.PreviousDebts - prices[i],
                            Date     = TimeNow(),
                            IsCash   = true,
                            PersonID = 3
                        };
                    }
                    else
                    {
                        m = new PaidForMerchant()
                        {
                            IsPaidForUs = true,
                            MerchantID  = merchant.MerchantID,
                            Payment     = prices[i],
                            PreviousDebtsForMerchant = merchant.PreviousDebts - prices[i],
                            Date     = TimeNow(),
                            IsCash   = true,
                            PersonID = 3
                        };
                    }
                    Person pppp = _context.People.Find(3);
                    pppp.credit           += prices[i];
                    merchant.PreviousDebts = merchant.PreviousDebts - prices[i];
                    _context.PaidForMerchant.Add(m);
                    _context.SaveChanges();
                    c.TotalPaidFromMerchants = prices.Sum();
                }
            }

            if (ToMerchantName != null && ToPrice != null)
            {
                var       ToMerchantNameCookie = ToMerchantName.TrimEnd(ToMerchantName[ToMerchantName.Length - 1]);
                var       ToPriceCookie        = ToPrice.TrimEnd(ToPrice[ToPrice.Length - 1]);
                string[]  ToMerchantNames      = ToMerchantNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] Toprices             = ToPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();


                for (int i = 0; i < ToMerchantNames.Length; i++)
                {
                    var merchant = _context.Merchants.FirstOrDefault(c => c.MerchantName == ToMerchantNames[i]);

                    PaidForMerchant m = new PaidForMerchant()
                    {
                        IsPaidForUs = false,
                        MerchantID  = merchant.MerchantID,
                        Payment     = Toprices[i],
                        PreviousDebtsForMerchant = merchant.PreviousDebtsForMerchant - Toprices[i],
                        Date     = TimeNow(),
                        IsCash   = true,
                        PersonID = 3
                    };
                    Person person = _context.People.Find(3);
                    person.credit -= Toprices[i];
                    merchant.PreviousDebtsForMerchant = merchant.PreviousDebtsForMerchant - Toprices[i];
                    _context.PaidForMerchant.Add(m);
                    _context.SaveChanges();
                }
                c.TotalPaidForMerchants = Toprices.Sum();
            }

            if (HalekName != null)
            {
                var       BoatNameCookie   = BoatName.TrimEnd(BoatName[BoatName.Length - 1]);
                var       HalekPriceCookie = HalekPrice.TrimEnd(HalekPrice[HalekPrice.Length - 1]);
                var       HalekNameCookie  = HalekName.TrimEnd(HalekName[HalekName.Length - 1]);
                string[]  BoatNames        = BoatNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] HalekPrices      = HalekPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                string[]  HalekNames       = HalekNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();

                for (int i = 0; i < HalekNames.Length; i++)
                {
                    var boat        = _context.Boats.FirstOrDefault(c => c.BoatName == BoatNames[i]);
                    var lastSarhaID = _context.Sarhas.Where(c => c.BoatID == boat.BoatID && c.IsFinished == false).FirstOrDefault().SarhaID;
                    var lastSarha   = _context.Sarhas.Find(lastSarhaID);

                    var debt_sarha = _context.Debts_Sarhas.Include(c => c.Debt).ToList().
                                     Where(c => c.SarhaID == lastSarhaID && c.Debt.DebtName == HalekNames[i] && c.PersonID == 3 && c.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                    boat.DebtsOfHalek += HalekPrices[i];
                    if (debt_sarha != null)
                    {
                        debt_sarha.Price += HalekPrices[i];
                    }
                    else
                    {
                        var         debt = _context.Debts.Where(c => c.DebtName == HalekNames[i]).FirstOrDefault();
                        Debts_Sarha ds   = new Debts_Sarha()
                        {
                            Price = HalekPrices[i], DebtID = debt.DebtID, SarhaID = lastSarhaID, PersonID = 3, Date = TimeNow()
                        };
                        _context.Debts_Sarhas.Add(ds);
                        _context.SaveChanges();
                    }
                    Person person1 = _context.People.Find(3);
                    person1.credit -= HalekPrices[i];

                    _context.SaveChanges();
                }
                c.TotalHalek = HalekPrices.Sum();
            }

            if (BoatNameExpenses != null)
            {
                var       BoatNameExpensesCookie = BoatNameExpenses.TrimEnd(BoatNameExpenses[BoatNameExpenses.Length - 1]);
                var       ExpensePriceCookie     = ExpensePricee.TrimEnd(ExpensePricee[ExpensePricee.Length - 1]);
                var       CauseCookie            = Cause.TrimEnd(Cause[Cause.Length - 1]);
                string[]  BoatNamesExpenses      = BoatNameExpensesCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] ExpensePrice           = ExpensePriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                string[]  Causes = CauseCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                for (int i = 0; i < BoatNamesExpenses.Length; i++)
                {
                    var     boat = _context.Boats.FirstOrDefault(c => c.BoatName == BoatNamesExpenses[i]);
                    Expense ex   = new Expense()
                    {
                        BoatID   = boat.BoatID,
                        Cause    = Causes[i],
                        Date     = TimeNow(),
                        PersonID = 3,
                        Price    = ExpensePrice[i]
                    };
                    _context.Expenses.Add(ex);
                    await _context.SaveChangesAsync();

                    Person person1 = _context.People.Find(3);
                    person1.credit -= ExpensePrice[i];

                    await _context.SaveChangesAsync();
                }
                c.TotalOfExpenses = ExpensePrice.Sum();
            }

            var           p = _context.People.Find(4);
            FathAllahGift g = new FathAllahGift()
            {
                charge = fathallah, CreditBefore = p.credit, CreditAfter = fathallah + p.credit, Date = TimeNow(), PersonID = 3
            };

            p.credit += fathallah;
            Person pp = _context.People.Find(3);

            pp.credit -= fathallah;
            _context.FathAllahGifts.Add(g);
            _context.Collectings.Add(c);
            await _context.SaveChangesAsync();

            if (Adding != null)
            {
                var       AddingCookie      = Adding.TrimEnd(Adding[Adding.Length - 1]);
                var       AddingPriceCookie = AddingPrice.TrimEnd(AddingPrice[AddingPrice.Length - 1]);
                string[]  AddingNames       = AddingCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] AddingPrices      = AddingPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                var       collecting        = _context.Collectings.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                collecting.TotalOfAdditionalPayment = AddingPrices.Sum();

                for (int i = 0; i < AddingNames.Length; i++)
                {
                    AdditionalPayment ad = new AdditionalPayment()
                    {
                        Name  = AddingNames[i],
                        Value = AddingPrices[i],
                        ID    = collecting.ID,
                        Date  = TimeNow()
                    };

                    _context.AdditionalPayments.Add(ad);
                    Person ppp = _context.People.Find(3);
                    ppp.credit -= AddingPrices[i];
                    _context.SaveChanges();
                }
            }

            var FinalCredit = c.TotalPaidFromMerchants - (c.TotalPaidForMerchants + c.TotalHalek + c.TotalOfAdditionalPayment + fathallah + c.TotalOfExpenses);

            //Person halaka = _context.People.Find(1);
            //halaka.credit += FinalCredit;
            pp.credit = 0.0m;
            await _context.SaveChangesAsync();

            return(Json(new { message = "success" }));
        }
        public async Task <IActionResult> FinalCalc(decimal PricePaidFromMagdy, decimal driversTotal, decimal PricePaidForFathallah,
                                                    decimal PaidFathallahSalary, decimal totalOfIncome, decimal totalSarhas, decimal totalOfDailyExpense, decimal FinalTotal,
                                                    string InternalMerchant, string InternalMerchantPrice, string ToLeaderBoatName, string ToLeaderPrice, string BoatNameForHalek,
                                                    string HalekPrice, string HalekName, string Adding, string AddingPrice, string BoatNameExpenses, string ExpensePricee,
                                                    string OperatorName, string OperatorPrice, string Cause)
        {
            var     c = _context.Collectings.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
            decimal collectedToday = 0.0m;

            if (c != null)
            {
                collectedToday = (c.TotalPaidFromMerchants + c.TotalOfCashes) - (c.TotalPaidForMerchants + c.TotalHalek + c.TotalOfAdditionalPayment + c.TotalForFahAllah + c.TotalOfExpenses);
            }
            BranchOffice branch = new BranchOffice()
            {
                Date            = TimeNow(),
                Collecting      = collectedToday,
                DriversSalary   = driversTotal,
                ExpensesTotal   = totalOfDailyExpense,
                FathallahSalary = PaidFathallahSalary,
                OfficeMoney     = PricePaidFromMagdy,
                SarhasTotal     = totalSarhas,
                CurrentCredit   = FinalTotal,
                IncomeTotal     = totalOfIncome
            };

            _context.BranchOffices.Add(branch);
            _context.SaveChanges();
            var user = await _userManager.GetUserAsync(User);

            var roles = await _userManager.GetRolesAsync(user);

            int PID = 1;

            if (roles.Contains("partner"))
            {
                PID = 2;
            }
            if (InternalMerchant != null && InternalMerchantPrice != null)
            {
                var       InternalMerchantCookie      = InternalMerchant.TrimEnd(InternalMerchant[InternalMerchant.Length - 1]);
                var       InternalMerchantPriceCookie = InternalMerchantPrice.TrimEnd(InternalMerchantPrice[InternalMerchantPrice.Length - 1]);
                string[]  InternalMerchantNames       = InternalMerchantCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] InternalMerchantprices      = InternalMerchantPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();

                for (int i = 0; i < InternalMerchantNames.Length; i++)
                {
                    var             merchant = _context.Merchants.FirstOrDefault(c => c.MerchantName == InternalMerchantNames[i]);
                    PaidForMerchant m;
                    m = new PaidForMerchant()
                    {
                        IsPaidForUs = true,
                        MerchantID  = merchant.MerchantID,
                        Payment     = InternalMerchantprices[i],
                        PreviousDebtsForMerchant = merchant.PreviousDebts - InternalMerchantprices[i],
                        Date     = TimeNow(),
                        IsCash   = true,
                        PersonID = PID
                    };

                    //Person pppp = _context.People.Find(PID);
                    //pppp.credit += InternalMerchantprices[i];
                    merchant.PreviousDebts = merchant.PreviousDebts - InternalMerchantprices[i];
                    _context.PaidForMerchant.Add(m);
                    _context.SaveChanges();
                }
            }

            if (ToLeaderBoatName != null && ToLeaderPrice != null)
            {
                var ToLeaderCookie = ToLeaderBoatName.TrimEnd(ToLeaderBoatName[ToLeaderBoatName.Length - 1]);
                var ToPriceCookie  = ToLeaderPrice.TrimEnd(ToLeaderPrice[ToLeaderPrice.Length - 1]);

                string[]  ToLeaderBoatNames = ToLeaderCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] Toprices          = ToPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();

                for (int i = 0; i < ToLeaderBoatNames.Length; i++)
                {
                    var Boat = _context.Boats.Where(c => c.BoatName == ToLeaderBoatNames[i]).FirstOrDefault();
                    _context.LeaderLoans.Add(new LeaderLoan {
                        Price = Toprices[i], BoatID = Boat.BoatID, Date = TimeNow(), PersonID = PID
                    });
                    Boat.DebtsOfLeader += Toprices[i];
                    //pp.credit -= Toprices[i];
                    _context.SaveChanges();
                }
            }

            if (HalekName != null)
            {
                var       BoatNameCookie   = BoatNameForHalek.TrimEnd(BoatNameForHalek[BoatNameForHalek.Length - 1]);
                var       HalekPriceCookie = HalekPrice.TrimEnd(HalekPrice[HalekPrice.Length - 1]);
                var       HalekNameCookie  = HalekName.TrimEnd(HalekName[HalekName.Length - 1]);
                string[]  BoatNames        = BoatNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] HalekPrices      = HalekPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                string[]  HalekNames       = HalekNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();

                for (int i = 0; i < HalekNames.Length; i++)
                {
                    var boat        = _context.Boats.FirstOrDefault(c => c.BoatName == BoatNames[i]);
                    var lastSarhaID = _context.Sarhas.Where(c => c.BoatID == boat.BoatID && c.IsFinished == false).FirstOrDefault().SarhaID;
                    var lastSarha   = _context.Sarhas.Find(lastSarhaID);

                    var debt_sarha = _context.Debts_Sarhas.Include(c => c.Debt).ToList().Where(c => c.SarhaID == lastSarhaID && c.Debt.DebtName == HalekNames[i] && c.PersonID == PID && c.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                    boat.DebtsOfHalek += HalekPrices[i];
                    if (debt_sarha != null)
                    {
                        debt_sarha.Price += HalekPrices[i];
                    }
                    else
                    {
                        var         debt = _context.Debts.Where(c => c.DebtName == HalekNames[i]).FirstOrDefault();
                        Debts_Sarha ds   = new Debts_Sarha()
                        {
                            Price = HalekPrices[i], DebtID = debt.DebtID, SarhaID = lastSarhaID, PersonID = PID, Date = TimeNow()
                        };
                        _context.Debts_Sarhas.Add(ds);
                        _context.SaveChanges();
                    }
                    //Person person1 = _context.People.Find(3);
                    //person1.credit -= HalekPrices[i];

                    _context.SaveChanges();
                }
                //c.TotalHalek = HalekPrices.Sum();
            }

            if (BoatNameExpenses != null)
            {
                var       BoatNameExpensesCookie = BoatNameExpenses.TrimEnd(BoatNameExpenses[BoatNameExpenses.Length - 1]);
                var       ExpensePriceCookie     = ExpensePricee.TrimEnd(ExpensePricee[ExpensePricee.Length - 1]);
                var       CauseCookie            = Cause.TrimEnd(Cause[Cause.Length - 1]);
                string[]  BoatNamesExpenses      = BoatNameExpensesCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] ExpensePrice           = ExpensePriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                string[]  Causes = CauseCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                for (int i = 0; i < BoatNamesExpenses.Length; i++)
                {
                    var     boat = _context.Boats.FirstOrDefault(c => c.BoatName == BoatNamesExpenses[i]);
                    Expense ex   = new Expense()
                    {
                        BoatID   = boat.BoatID,
                        Cause    = Causes[i],
                        Date     = TimeNow(),
                        PersonID = PID,
                        Price    = ExpensePrice[i]
                    };

                    _context.Expenses.Add(ex);
                    boat.TotalOfExpenses += ExpensePrice[i];
                    await _context.SaveChangesAsync();

                    //Person person1 = _context.People.Find(3);
                    //person1.credit -= ExpensePrice[i];

                    await _context.SaveChangesAsync();
                }
            }

            var           p = _context.People.Find(4);
            FathAllahGift g = new FathAllahGift()
            {
                charge = PricePaidForFathallah, CreditBefore = p.credit, CreditAfter = PricePaidForFathallah + p.credit, Date = TimeNow(), PersonID = PID
            };

            p.credit += PricePaidForFathallah;
            //Person pp = _context.People.Find(3);
            //pp.credit -= fathallah;
            _context.FathAllahGifts.Add(g);
            //_context.Collectings.Add(c);
            await _context.SaveChangesAsync();

            if (Adding != null)
            {
                var       AddingCookie      = Adding.TrimEnd(Adding[Adding.Length - 1]);
                var       AddingPriceCookie = AddingPrice.TrimEnd(AddingPrice[AddingPrice.Length - 1]);
                string[]  AddingNames       = AddingCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] AddingPrices      = AddingPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                //var collecting = _context.Collectings.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                //collecting.TotalOfAdditionalPayment = AddingPrices.Sum();

                for (int i = 0; i < AddingNames.Length; i++)
                {
                    AdditionalForOffice ad = new AdditionalForOffice()
                    {
                        Name     = AddingNames[i],
                        Value    = AddingPrices[i],
                        PersonID = PID,
                        Date     = TimeNow()
                    };

                    _context.AdditionalForOffices.Add(ad);
                    //Person ppp = _context.People.Find(3);
                    //ppp.credit -= AddingPrices[i];
                    _context.SaveChanges();
                }
            }
            if (OperatorName != null)
            {
                var       OperatorNameCookie  = OperatorName.TrimEnd(OperatorName[OperatorName.Length - 1]);
                var       OperatorPriceCookie = OperatorPrice.TrimEnd(AddingPrice[AddingPrice.Length - 1]);
                string[]  OperatorNames       = OperatorNameCookie.Split(",").Select(c => Convert.ToString(c)).ToArray();
                decimal[] OperatorPrices      = OperatorPriceCookie.Split(",").Select(c => Convert.ToDecimal(c)).ToArray();
                //var collecting = _context.Collectings.ToList().Where(x => x.Date.ToShortDateString() == TimeNow().ToShortDateString()).FirstOrDefault();
                //collecting.TotalOfAdditionalPayment = AddingPrices.Sum();

                for (int i = 0; i < OperatorNames.Length; i++)
                {
                    var             operatorr = _context.Operators.Where(x => x.OperatorName == OperatorNames[i]).FirstOrDefault();
                    PaidForOperator pp        = new PaidForOperator()
                    {
                        OperatorID        = operatorr.OperatorID,
                        Payment           = OperatorPrices[i],
                        DebtsAfterPayment = operatorr.Credit - OperatorPrices[i],
                        PersonID          = PID,
                        Date = TimeNow()
                    };

                    _context.PaidForOperators.Add(pp);
                    //Person ppp = _context.People.Find(3);
                    //ppp.credit -= AddingPrices[i];
                    _context.SaveChanges();
                }
            }

            Person pppp = _context.People.Find(PID);

            pppp.credit = FinalTotal;
            await _context.SaveChangesAsync();

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