public ActionResult Payment([Bind(Include = "Id,No,Source_BankAccounts_Id,Target_BankAccounts_Id,Amount,ConfirmationNumber,Notes,IdUser,HasPayment")] PaymentViewModels paymentVM) { Common.Master m = new Common.Master(); decimal remaining = db.Payroll.Where(x => x.Id == paymentVM.Id).Select(x => x.Amount).Single() - m.GetTotalPayment(paymentVM.Id); if (paymentVM.Amount > remaining) { ModelState.AddModelError("Over", "The field Amount can't greater than " + remaining.ToString("#,##0")); } if (ModelState.IsValid) { PaymentModels pay = new PaymentModels(); pay.Id = Guid.NewGuid(); pay.No = m.GetLastHexNo("payment").ToString("00000"); pay.Timestamp = DateTime.Now; pay.Source_BankAccounts_Id = paymentVM.Source_BankAccounts_Id; pay.Target_BankAccounts_Id = paymentVM.Target_BankAccounts_Id; pay.Amount = paymentVM.Amount; pay.ConfirmationNumber = paymentVM.ConfirmationNumber; pay.Notes = paymentVM.Notes; db.Payment.Add(pay); PaymentItemModels pi = new PaymentItemModels(); pi.Id = Guid.NewGuid(); pi.Payments_Id = pay.Id; pi.Transaction_RefId = paymentVM.Id; pi.Amount = paymentVM.Amount; pi.Notes = paymentVM.Notes; db.PaymentItem.Add(pi); db.SaveChanges(); if (!paymentVM.hasPayment) { using (var ctx = new HrContext()) { int result = ctx.Database.ExecuteSqlCommand("UPDATE Payrolls SET hasPayment='True' WHERE Id='" + paymentVM.Id + "'"); } } return(RedirectToAction("Payroll")); } ViewBag.listTarget = new SelectList(db.BankAccount.Where(x => x.Active == true && x.Owner_RefId.Value == paymentVM.IdUser).OrderBy(x => x.Name).ToList(), "Id", "Name"); ViewBag.listSource = new SelectList(db.BankAccount.Where(x => x.Active == true && x.Internal == true).OrderBy(x => x.Name).ToList(), "Id", "Name"); return(View(paymentVM)); }
public JsonResult GeneratePayroll(string ids) { if (string.IsNullOrEmpty(ids)) { return(Json(new { status = "404" }, JsonRequestBehavior.AllowGet)); } else { string[] arrID = ids.Split(','); if (arrID.Length > 0) { bool isValid = false; string empID = null; for (int a = 0; a < arrID.Length; a++) { Guid attID = new Guid(arrID[a]); //Approved Checked bool isValidAtt = db.Attendance.Where(x => x.Id == attID).Select(x => x.Approved.HasValue).Single(); //return false jika Approved = NULL if (!isValidAtt) { isValid = false; break; } else { isValid = db.Attendance.Where(x => x.Id == attID).Select(x => x.Approved.Value).Single(); if (!isValid) { break; } } //Same Employee Checked string userID = db.Attendance.Where(x => x.Id == attID).Select(x => x.UserAccounts_Id).Single().ToString(); if (a == 0) { empID = userID; } else { isValid = (empID == userID) ? true : false; } } if (!isValid) { return(Json(new { status = "405" }, JsonRequestBehavior.AllowGet)); } else { Guid _attID = new Guid(arrID[0]); Guid employeeID = db.Attendance.Where(x => x.Id == _attID).Select(x => x.UserAccounts_Id).Single(); PayrollModels pRoll = new PayrollModels(); pRoll.Id = Guid.NewGuid(); pRoll.Timestamp = DateTime.Now; pRoll.Employee_UserAccounts_Id = employeeID; pRoll.Amount = 0; Common.Master m = new Common.Master(); pRoll.No = m.GetLastHexNo("payroll").ToString("00000"); pRoll.hasPayment = false; db.Payroll.Add(pRoll); db.SaveChanges(); for (int i = 0; i < arrID.Length; i++) { Guid attID = new Guid(arrID[i]); PayrollItemModels pi = new PayrollItemModels(); pi.Id = Guid.NewGuid(); pi.Payrolls_Id = pRoll.Id; pi.RefId = attID; pi.Description = string.Empty; int hours = db.Attendance.Where(x => x.Id == attID).Select(x => x.Workshifts_DurationMinutes.Value).Single() / 60; decimal payRate = db.Attendance.Where(x => x.Id == attID).Select(x => x.AttendancePayRates_Amount.Value).FirstOrDefault(); pi.Amount = hours * payRate; pi.Notes = string.Empty; db.PayrollItem.Add(pi); db.SaveChanges(); using (var ctx = new HrContext()) { int result = ctx.Database.ExecuteSqlCommand("UPDATE Attendances SET PayrollItems_Id='" + pi.Id + "' WHERE Id='" + attID + "'"); } } decimal total_amount = db.PayrollItem.Where(x => x.Payrolls_Id == pRoll.Id).Select(x => x.Amount).Sum(); int update_amount = db.Database.ExecuteSqlCommand("UPDATE Payrolls SET Amount=" + total_amount + " WHERE Id='" + pRoll.Id + "'"); return(Json(new { status = "200" }, JsonRequestBehavior.AllowGet)); } } else { return(Json(new { status = "404" }, JsonRequestBehavior.AllowGet)); } } //end ids is not null/empty } //end generate payroll