public async Task <bool> PayForMembership(PaymentCreateModelView model, ClientsMembership clientsMembership, Client client, long employeeId) { int sum = (int)model.CashSum + (int)model.CardSum; int balance = client.Balance; if (balance < 0) { balance = 0; } Membership membership = client.Membership; int debts = membership.Price - sum - balance; Payment payment = new Payment() { Comment = model.Comment, ClientsMembershipId = clientsMembership.Id, CreatorId = employeeId, CashSum = (int)model.CashSum, CardSum = (int)model.CardSum, Type = model.Type, BranchId = model.BranchId }; CurrentSum currentSum = _db.CurrentSums.FirstOrDefault(p => p.BranchId == model.BranchId); currentSum.CashSum += model.CashSum; currentSum.CreditSum += model.CardSum; _db.Entry(currentSum).State = EntityState.Modified; SetParams(ref payment, ref client, debts, model.Type, sum); _db.Entry(client).State = EntityState.Modified; _db.Entry(payment).State = EntityState.Added; await _db.SaveChangesAsync(); return(true); }
public async Task <IActionResult> CreateBranch(string name, string info, string address, long?adminId) { Branch branch = new Branch() { Name = name, Info = info, Address = address, AdminId = adminId, }; _db.Entry(branch).State = EntityState.Added; await _db.SaveChangesAsync(); long branchId = branch.Id; Employee chief = _db.Employees.FirstOrDefault(p => p.Id == 1); CurrentSum cs = new CurrentSum() { CashSum = 0, CreditSum = 0, BranchId = branchId }; Employee admin = _db.Employees.FirstOrDefault(e => e.Id == adminId); admin.Branch = branch; _db.Entry(admin).State = EntityState.Modified; _db.Entry(cs).State = EntityState.Added; await _db.SaveChangesAsync(); return(RedirectToAction("Index", "Chief")); }
public async Task <IActionResult> Index(long branchId, DateTime start) { if (start == DateTime.MinValue) { start = DateTime.Now; } ViewBag.BranchId = branchId; if (User.IsInRole("admin")) { Employee user = await _userManager.GetUserAsync(User); branchId = _db.Branches.FirstOrDefault(p => p.AdminId == user.Id).Id; } List <Payment> payments = _db.Payments.Where(p => p.BranchId == branchId && p.CateringDate.Date == start.Date).ToList(); List <Withdrawal> withdrawals = _db.Withdrawals.Where(p => p.BranchId == branchId && p.Date.Date == start.Date).ToList(); CurrentSum currentSum = _db.CurrentSums.FirstOrDefault(p => p.BranchId == branchId); TransactionIndexModel model = new TransactionIndexModel() { Payments = payments, Withdrawals = withdrawals, CurrentSum = currentSum, BranchId = branchId }; if (model.CurrentSum == null) { model.CurrentSum = new CurrentSum(); } return(View(model)); }
public async Task <string> CreatePayment(PaymentCreateModelView model, ClientsMembership clientsMembership, Client client, long employeeId) { model.BranchId = client.Group.BranchId; int sum = model.CashSum + model.CardSum; if (sum == 0 || sum < 0) { return("negativeOrZero"); } if (client.Balance < 0 && model.Type == PaymentType.Pay) { return("false"); } if (client.Membership is null && model.Type == PaymentType.Pay) { return("false"); } int balance = client.Balance; if (balance < 0) { balance = 0; } Membership membership = client.Membership; int debts = membership.Price - sum - balance; if (clientsMembership is null) { return("false"); } Payment payment = new Payment() { Comment = model.Comment, ClientsMembershipId = clientsMembership.Id, CreatorId = employeeId, CashSum = (int)model.CashSum, CardSum = (int)model.CardSum, Type = model.Type, BranchId = model.BranchId }; CurrentSum currentSum = _db.CurrentSums.FirstOrDefault(p => p.BranchId == model.BranchId); currentSum.CashSum += model.CashSum; currentSum.CreditSum += model.CardSum; _db.Entry(currentSum).State = EntityState.Modified; SetParams(ref payment, ref client, debts, model.Type, sum); _db.Entry(client).State = EntityState.Modified; _db.Entry(payment).State = EntityState.Added; await _db.SaveChangesAsync(); return("success"); }
public IActionResult Withdraw(long branchId) { CurrentSum cs = _db.CurrentSums.FirstOrDefault(p => p.BranchId == branchId); ViewBag.CurrentSum = cs; return(View(new Withdrawal() { BranchId = branchId })); }
public async Task <IActionResult> Delete(long id, CheckPasswordModelView model) { Employee empl = await _userManager.GetUserAsync(User); Branch branch = _db.Branches.FirstOrDefault(p => p.Id == id); CurrentSum currentSum = _db.CurrentSums.FirstOrDefault(p => p.BranchId == id); var result = await _userManager.CheckPasswordAsync(empl, model.Password);; if (result == true) { _db.Entry(branch).State = EntityState.Deleted; _db.Entry(currentSum).State = EntityState.Deleted; await _db.SaveChangesAsync(); } return(RedirectToAction("Index", "Chief")); }
public async Task <IActionResult> Withdraw(Withdrawal model) { CurrentSum cs = _db.CurrentSums.FirstOrDefault(p => p.BranchId == model.BranchId); ViewBag.CurrentSum = cs; if (ModelState.IsValid) { model.CreatorId = GetUserId.GetCurrentUserId(HttpContext); model.Date = DateTime.Now; if (model.IsCash == true) { if (cs.CashSum >= model.Sum) { cs.CashSum -= model.Sum; } else { ModelState.AddModelError("Sum", "Недостаточно средств для снятия"); return(View(model)); } } else { if (cs.CreditSum >= model.Sum) { cs.CreditSum -= model.Sum; } else { ModelState.AddModelError("Sum", "Недостаточно средств для снятия"); return(View(model)); } } _db.Entry(model).State = EntityState.Added; _db.Entry(cs).State = EntityState.Modified; await _db.SaveChangesAsync(); return(RedirectToAction("Index", new { branchId = model.BranchId })); } return(View(model)); }