public async Task <ActionResult> AllPaymentIndex(int id, DateTime?startDate, DateTime?endDate) { if (Session["LoginUserID"] == null) { return(RedirectToAction("Login", "Account")); } // var materialBuys = db.MaterialBuys.Where( m => m.CompanyID == id ) // .Include(m => m.Company).Include(m => m.Project) // .OrderByDescending(m => m.ID).OrderByDescending(m => m.Date); var company = db.Companys.Include(c => c.Projects.Select(p => p.Company)) .Include(c => c.MaterialBuys.Select(mb => mb.Project)) .Where(c => c.ID == id) .Single(); company.Projects = company.Projects.OrderByDescending(m => m.Date).ToList(); if (startDate != null && endDate != null) { company.MaterialBuys = company.MaterialBuys.Where(m => m.Date <= endDate && m.Date >= startDate) .OrderByDescending(m => m.Date).ToList(); } else { company.MaterialBuys = company.MaterialBuys.OrderByDescending(m => m.Date).ToList(); } AllPaymentIndexData allPayment = new AllPaymentIndexData(); allPayment.Company = company; allPayment.StartDate = startDate; allPayment.EndDate = endDate; return(View(allPayment)); }
public async Task <ActionResult> AllPaymentIndex([Bind(Include = "Company, StartDate, EndDate, Note, PaymentDate")] AllPaymentIndexData allPayment) { if (Session["LoginUserID"] == null) { return(RedirectToAction("Login", "Account")); } allPayment.Company = db.Companys.FirstOrDefault(c => c.ID == allPayment.Company.ID); if (allPayment.Company == null) { return(HttpNotFound()); } IEnumerable <MaterialBuy> MaterialBuys = db.MaterialBuys.Where(m => m.CompanyID == allPayment.Company.ID && m.Date <= allPayment.EndDate && m.Date >= allPayment.StartDate) .OrderByDescending(m => m.Date).ToList(); if (allPayment.PaymentDate == null) { allPayment.PaymentDate = DateTime.Now; } Payment payment = new Payment(); payment.CompanyID = allPayment.Company.ID; payment.Date = allPayment.PaymentDate; payment.EmployeeID = Convert.ToInt32(Session["LoginUserEmployeeID"]); // payment.ProjectID = ; payment.StatementType = Models.StatementType.Payment; payment.Type = PaymentType.Bank; payment.Amount = 0; payment.NoteVn = allPayment.Note; payment.NoteKr = allPayment.Note; payment.MaterialBuys = new List <MaterialBuy>(); foreach (var item in MaterialBuys) { var paymentsum = item.Payments.Sum(p => p.Amount); var price = item.MaterialBuyUnits.Sum(m => m.Quantity * m.MaterialUnitPrice.Price); var amount = price + price * item.VATPer - paymentsum; if (amount > 0) { payment.Amount += (double)amount; payment.MaterialBuys.Add(item); item.Payments.Add(payment); } } if (payment.Amount > 0) { db.Payments.Add(payment); } await db.SaveChangesAsync(); allPayment.Company.MaterialBuys = allPayment.Company.MaterialBuys.Where(m => m.CompanyID == allPayment.Company.ID && m.Date <= allPayment.EndDate && m.Date >= allPayment.StartDate) .OrderByDescending(m => m.Date).ToList(); return(View(allPayment)); }