public async Task <IActionResult> SaveRec(int?id, decimal individualSalary, decimal halek, decimal total, decimal expense, string flag, int NumberOfFisherMen, decimal PaymentLeaderDebts) { var user = await _userManager.GetUserAsync(User); var roles = await _userManager.GetRolesAsync(user); int PID = 1; if (roles.Contains("partner")) { PID = 2; } var rec = db.BoatOwnerReciepts.Find(id); //Person p = db.People.Find(PID); //p.credit += halek; دى بتكون حسابات على ورق لانهم مش بياخدوا فلوس ف ايديهم ..لسه هياخدوه فلوسهم دى لما التجار تسدد decimal leaderPaidDebts = 0.0m; var boat = db.Boats.Find(rec.BoatID); if (PaymentLeaderDebts != 0.0m) { boat.DebtsOfLeader -= PaymentLeaderDebts; LeaderPayback l = new LeaderPayback() { BoatID = boat.BoatID, Date = TimeNow(), Price = PaymentLeaderDebts }; db.LeaderPaybacks.Add(l); //p.credit += PaymentLeaderDebts; leaderPaidDebts = PaymentLeaderDebts; } boat.DebtsOfHalek -= halek; if (flag == "True") // fishermen number is the same { var recs = db.BoatOwnerReciepts.Where(c => c.BoatID == boat.BoatID && c.IsCollected == false && c.IsCalculated == false).ToList(); var sumOfTotalAfterPayment = total - leaderPaidDebts; var finalIncome = 0.0m; if (boat.TypeID == 2) //shared boat { finalIncome = sumOfTotalAfterPayment / 2; var LeaderSalary = 0.0m; if (NumberOfFisherMen != 0) { LeaderSalary = finalIncome / NumberOfFisherMen; } else { LeaderSalary = finalIncome / 6; } boat.IncomeOfSharedBoat += finalIncome - LeaderSalary; IncomesOfSharedBoat inc = new IncomesOfSharedBoat() { BoatID = boat.BoatID, Date = TimeNow(), Income = finalIncome - LeaderSalary }; db.IncomesOfSharedBoats.Add(inc); PaidForBoat paidForBoat = new PaidForBoat() { BoatID = boat.BoatID, Date = TimeNow(), Payment = finalIncome + LeaderSalary, PersonID = PID, HalekDebtsTillNow = boat.DebtsOfHalek }; db.PaidForBoats.Add(paidForBoat); Person pp = db.People.Find(PID); pp.credit -= finalIncome + LeaderSalary; db.SaveChanges(); } else { if (expense != 0.0m) { boat.TotalOfExpenses -= expense; } PaidForBoat paidForBoat = new PaidForBoat() { BoatID = boat.BoatID, Date = TimeNow(), Payment = sumOfTotalAfterPayment, PersonID = PID, HalekDebtsTillNow = boat.DebtsOfHalek }; db.PaidForBoats.Add(paidForBoat); Person pp = db.People.Find(PID); pp.credit -= sumOfTotalAfterPayment; db.SaveChanges(); } foreach (var item in recs) { item.IsCollected = true; item.IsCalculated = true; } } else { rec.PaidFromDebts = halek; rec.TotalAfterPaying = total - leaderPaidDebts; rec.IsCalculated = true; db.SaveChanges(); if (db.BoatOwnerReciepts.Where(c => c.BoatID == boat.BoatID && c.IsCollected == false && c.IsCalculated == false).Count() == 0)//check if last rec { var recs = db.BoatOwnerReciepts.Where(c => c.BoatID == boat.BoatID && c.IsCollected == false && c.IsCalculated == true).ToList(); var sumOfTotalAfterPayment = recs.Sum(c => c.TotalAfterPaying); var finalIncome = 0.0m; if (boat.TypeID == 2) { finalIncome = sumOfTotalAfterPayment / 2; var LeaderSalary = 0.0m; if (NumberOfFisherMen != 0) { LeaderSalary = finalIncome / NumberOfFisherMen; } else { LeaderSalary = finalIncome / 6; } boat.IncomeOfSharedBoat += finalIncome - LeaderSalary; IncomesOfSharedBoat inc = new IncomesOfSharedBoat() { BoatID = boat.BoatID, Date = TimeNow(), Income = finalIncome - LeaderSalary }; db.IncomesOfSharedBoats.Add(inc); PaidForBoat paidForBoat = new PaidForBoat() { BoatID = boat.BoatID, Date = TimeNow(), Payment = finalIncome + LeaderSalary, PersonID = PID, HalekDebtsTillNow = boat.DebtsOfHalek }; db.PaidForBoats.Add(paidForBoat); Person pp = db.People.Find(PID); pp.credit -= finalIncome + LeaderSalary; db.SaveChanges(); } else { if (expense != 0.0m) { boat.TotalOfExpenses -= expense; } PaidForBoat paidForBoat = new PaidForBoat() { BoatID = boat.BoatID, Date = TimeNow(), Payment = total - leaderPaidDebts, PersonID = PID, HalekDebtsTillNow = boat.DebtsOfHalek }; db.PaidForBoats.Add(paidForBoat); Person pp = db.People.Find(PID); pp.credit -= total - leaderPaidDebts; db.SaveChanges(); } foreach (var item in recs) { item.IsCollected = true; } db.SaveChanges(); } else { var finalIncome = 0.0m; if (boat.TypeID == 2) { finalIncome = rec.TotalAfterPaying / 2; var LeaderSalary = 0.0m; if (NumberOfFisherMen != 0) { LeaderSalary = finalIncome / NumberOfFisherMen; } else { LeaderSalary = finalIncome / 6; } PaidForBoat paidForBoat = new PaidForBoat() { BoatID = boat.BoatID, Date = TimeNow(), Payment = finalIncome + LeaderSalary, PersonID = PID, HalekDebtsTillNow = boat.DebtsOfHalek }; db.PaidForBoats.Add(paidForBoat); Person pp = db.People.Find(PID); pp.credit -= finalIncome + LeaderSalary; db.SaveChanges(); } else { if (expense != 0.0m) { boat.TotalOfExpenses -= expense; } PaidForBoat paidForBoat = new PaidForBoat() { BoatID = boat.BoatID, Date = TimeNow(), Payment = total - leaderPaidDebts, PersonID = PID, HalekDebtsTillNow = boat.DebtsOfHalek }; db.PaidForBoats.Add(paidForBoat); Person pp = db.People.Find(PID); pp.credit -= total - leaderPaidDebts; db.SaveChanges(); } } } db.SaveChanges(); return(Json(new { message = "success", current = boat.DebtsOfHalek, income = boat.IncomeOfSharedBoat, cexpense = boat.TotalOfExpenses, leaderdebts = boat.DebtsOfLeader })); }
public async Task <IActionResult> Create([Bind("ExternalReceiptID,BoatID,SarhaID,TotalBeforePaying,Date,Commission,PaidFromDebts,TotalAfterPaying,FinalIncome")] ExternalReceipt externalReceipt) { //if (ModelState.IsValid) //{ // // Subtracting Paid From Halek var user = await _userManager.GetUserAsync(User); var roles = await _userManager.GetRolesAsync(user); int PID = 1; if (roles.Contains("partner")) { PID = 2; } var boat = _context.Boats.Find(externalReceipt.BoatID); boat.DebtsOfHalek -= Convert.ToDecimal(externalReceipt.PaidFromDebts); var p = _context.People.Find(PID); p.credit += Convert.ToDecimal(externalReceipt.PaidFromDebts); var sarhaId = _context.Sarhas.Where(x => x.BoatID == externalReceipt.BoatID).Max(x => x.SarhaID); var TotalAfterPaying = externalReceipt.TotalBeforePaying - externalReceipt.Commission - externalReceipt.PaidFromDebts; // Salary for Each One var sarha = _context.Sarhas.Find(sarhaId); var IndividualSalary = (Convert.ToDecimal(TotalAfterPaying) / 2) / sarha.NumberOfFishermen; // Calculating Final Income // for shared boats decimal FinalIncome; // 5 -> Shared Boat ... We will change it later if (boat.TypeID == 2) { FinalIncome = (Convert.ToDecimal(TotalAfterPaying) / 2) - IndividualSalary; boat.IncomeOfSharedBoat += FinalIncome; IncomesOfSharedBoat i = new IncomesOfSharedBoat() { BoatID = boat.BoatID, Date = TimeNow(), Income = FinalIncome }; _context.IncomesOfSharedBoats.Add(i); p.credit += FinalIncome; } // for ordinary boats else { FinalIncome = Convert.ToDecimal(TotalAfterPaying); } externalReceipt.SarhaID = sarhaId; externalReceipt.TotalAfterPaying = TotalAfterPaying; externalReceipt.FinalIncome = FinalIncome; _context.Add(externalReceipt); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Details), new { id = externalReceipt.ExternalReceiptID })); //} //ViewData["BoatID"] = new SelectList(_context.Boats, "BoatID", "BoatName", externalReceipt.BoatID); //ViewData["SarhaID"] = new SelectList(_context.Sarhas, "SarhaID", "SarhaID", externalReceipt.SarhaID); //return View(externalReceipt); }