public async Task <IActionResult> DeletePettyCashRequest(int id) { var pettyCashRequest = await _context.PettyCashRequests.FindAsync(id); if (pettyCashRequest == null) { return(Conflict(new RespStatus { Status = "Failure", Message = "Cash Advance Request Id Invalid!" })); } var ClmApprvStatusTrackers = _context.ClaimApprovalStatusTrackers.Where(c => c.PettyCashRequestId == pettyCashRequest.Id && c.ApprovalStatusTypeId == (int)EApprovalStatus.Approved); int ApprovedCount = ClmApprvStatusTrackers.Count(); if (ApprovedCount > 0) { return(Conflict(new RespStatus { Status = "Failure", Message = "Cash Advance Request cant be Deleted after Approval!" })); } //update the EmpPettyCashBalance to credit back the deducted amount EmpCurrentPettyCashBalance empPettyCashBal = _context.EmpCurrentPettyCashBalances.Where(e => e.EmployeeId == pettyCashRequest.EmployeeId).FirstOrDefault(); empPettyCashBal.CurBalance = empPettyCashBal.CurBalance + pettyCashRequest.PettyClaimAmount; empPettyCashBal.UpdatedOn = DateTime.Now; _context.EmpCurrentPettyCashBalances.Update(empPettyCashBal); _context.PettyCashRequests.Remove(pettyCashRequest); var ClaimApprStatusTrackers = _context.ClaimApprovalStatusTrackers.Where(c => c.PettyCashRequestId == pettyCashRequest.Id).ToList(); foreach (var claim in ClaimApprStatusTrackers) { _context.ClaimApprovalStatusTrackers.Remove(claim); } var disburseAndClaims = _context.DisbursementsAndClaimsMasters.Where(d => d.PettyCashRequestId == pettyCashRequest.Id).ToList(); foreach (var disburse in disburseAndClaims) { _context.DisbursementsAndClaimsMasters.Remove(disburse); } await _context.SaveChangesAsync(); return(Ok(new RespStatus { Status = "Success", Message = "Cash Advance Request Deleted!" })); }
public async Task <ActionResult <EmpCurrentPettyCashBalance> > PostEmpCurrentPettyCashBalance(EmpCurrentPettyCashBalanceDTO empCurrentPettyCashBalanceDto) { EmpCurrentPettyCashBalance empCurrentPettyCashBalance = new EmpCurrentPettyCashBalance(); empCurrentPettyCashBalance.Id = empCurrentPettyCashBalanceDto.Id; empCurrentPettyCashBalance.EmployeeId = empCurrentPettyCashBalanceDto.EmployeeId; empCurrentPettyCashBalance.CurBalance = empCurrentPettyCashBalanceDto.CurBalance; empCurrentPettyCashBalance.UpdatedOn = empCurrentPettyCashBalanceDto.UpdatedOn; _context.EmpCurrentPettyCashBalances.Add(empCurrentPettyCashBalance); await _context.SaveChangesAsync(); return(CreatedAtAction("GetEmpCurrentPettyCashBalance", new { id = empCurrentPettyCashBalance.Id }, empCurrentPettyCashBalance)); }
public async Task <IActionResult> PutExpenseReimburseStatusTracker(List <ExpenseReimburseStatusTrackerDTO> ListExpenseReimburseStatusTrackerDto) { if (ListExpenseReimburseStatusTrackerDto.Count == 0) { return(Conflict(new RespStatus { Status = "Failure", Message = "No Request to Approve!" })); } bool isNextApproverAvailable = true; bool bRejectMessage = false; foreach (ExpenseReimburseStatusTrackerDTO expenseReimburseStatusTrackerDto in ListExpenseReimburseStatusTrackerDto) { var expenseReimburseStatusTracker = await _context.ExpenseReimburseStatusTrackers.FindAsync(expenseReimburseStatusTrackerDto.Id); //if same status continue to next loop, otherwise process if (expenseReimburseStatusTracker.ApprovalStatusTypeId == expenseReimburseStatusTrackerDto.ApprovalStatusTypeId) { continue; } if (expenseReimburseStatusTrackerDto.ApprovalStatusTypeId == (int)EApprovalStatus.Rejected) { bRejectMessage = true; } expenseReimburseStatusTracker.Id = expenseReimburseStatusTrackerDto.Id; expenseReimburseStatusTracker.EmployeeId = expenseReimburseStatusTrackerDto.EmployeeId; expenseReimburseStatusTracker.ExpenseReimburseRequestId = expenseReimburseStatusTrackerDto.ExpenseReimburseRequestId; expenseReimburseStatusTracker.DepartmentId = expenseReimburseStatusTrackerDto.DepartmentId; expenseReimburseStatusTracker.ProjectId = expenseReimburseStatusTrackerDto.ProjectId; expenseReimburseStatusTracker.JobRoleId = expenseReimburseStatusTrackerDto.JobRoleId; expenseReimburseStatusTracker.ApprovalLevelId = expenseReimburseStatusTrackerDto.ApprovalLevelId; expenseReimburseStatusTracker.ExpReimReqDate = expenseReimburseStatusTrackerDto.ExpReimReqDate; expenseReimburseStatusTracker.ApprovedDate = expenseReimburseStatusTrackerDto.ApprovedDate; expenseReimburseStatusTracker.ApprovalStatusTypeId = expenseReimburseStatusTrackerDto.ApprovalStatusTypeId; expenseReimburseStatusTracker.Comments = bRejectMessage ? expenseReimburseStatusTrackerDto.Comments : "Approved"; ExpenseReimburseStatusTracker claimitem; //Department based Expense Reimburse approval/rejection if (expenseReimburseStatusTrackerDto.DepartmentId != null) { int empApprGroupId = _context.Employees.Find(expenseReimburseStatusTracker.EmployeeId).ApprovalGroupId; //Check if the record is already approved //if it is not approved then trigger next approver level email & Change the status to approved if (expenseReimburseStatusTrackerDto.ApprovalStatusTypeId == (int)EApprovalStatus.Approved) { //Get the next approval level (get its ID) //int qExpReimRequestId = expenseReimburseStatusTrackerDto.ExpenseReimburseRequestId ?? 0; int qExpReimRequestId = expenseReimburseStatusTrackerDto.ExpenseReimburseRequestId; isNextApproverAvailable = true; int CurClaimApprovalLevel = _context.ApprovalLevels.Find(expenseReimburseStatusTrackerDto.ApprovalLevelId).Level; int nextClaimApprovalLevel = CurClaimApprovalLevel + 1; int qApprovalLevelId; int apprGroupId = _context.ExpenseReimburseStatusTrackers.Find(expenseReimburseStatusTrackerDto.Id).ApprovalGroupId; if (_context.ApprovalRoleMaps.Where(a => a.ApprovalGroupId == apprGroupId && a.ApprovalLevelId == nextClaimApprovalLevel).FirstOrDefault() != null) { qApprovalLevelId = _context.ApprovalLevels.Where(x => x.Level == nextClaimApprovalLevel).FirstOrDefault().Id; } else { qApprovalLevelId = _context.ApprovalLevels.Where(x => x.Level == CurClaimApprovalLevel).FirstOrDefault().Id; isNextApproverAvailable = false; } int qApprovalStatusTypeId = isNextApproverAvailable ? (int)EApprovalStatus.Initiating : (int)EApprovalStatus.Pending; //update the next level approver Track request to PENDING (from Initiating) //if claimitem is not null change the status if (isNextApproverAvailable) { claimitem = _context.ExpenseReimburseStatusTrackers.Where(c => c.ExpenseReimburseRequestId == qExpReimRequestId && c.ApprovalStatusTypeId == qApprovalStatusTypeId && c.ApprovalGroupId == empApprGroupId && c.ApprovalLevelId == qApprovalLevelId).FirstOrDefault(); if (claimitem != null) { claimitem.ApprovalStatusTypeId = (int)EApprovalStatus.Pending; } } else { //final approver hence update PettyCashRequest claimitem = _context.ExpenseReimburseStatusTrackers.Where(c => c.ExpenseReimburseRequestId == qExpReimRequestId && c.ApprovalStatusTypeId == qApprovalStatusTypeId && c.ApprovalGroupId == empApprGroupId && c.ApprovalLevelId == qApprovalLevelId).FirstOrDefault(); //claimitem.ApprovalStatusTypeId = (int)EApprovalStatus.Approved; claimitem.ApprovedDate = DateTime.Now; //final Approver hence updating ExpenseReimburseRequest table var expenseReimburseRequest = _context.ExpenseReimburseRequests.Find(qExpReimRequestId); expenseReimburseRequest.ApprovalStatusTypeId = (int)EApprovalStatus.Approved; expenseReimburseRequest.ApprovedDate = DateTime.Now; expenseReimburseRequest.Comments = bRejectMessage ? expenseReimburseStatusTrackerDto.Comments : "Approved"; _context.Update(expenseReimburseRequest); //DisbursementAndClaimsMaster update the record to Approved (ApprovalStatusId int disbAndClaimItemId = _context.DisbursementsAndClaimsMasters.Where(d => d.ExpenseReimburseReqId == claimitem.ExpenseReimburseRequestId).FirstOrDefault().Id; var disbAndClaimItem = await _context.DisbursementsAndClaimsMasters.FindAsync(disbAndClaimItemId); /// ############################# // Crediting back to the wallet /// ############################# double expenseReimAmt = expenseReimburseRequest.TotalClaimAmount; double RoleLimitAmt = _context.JobRoles.Find(_context.Employees.Find(expenseReimburseRequest.EmployeeId).RoleId).MaxPettyCashAllowed; EmpCurrentPettyCashBalance empCurrentPettyCashBalance = _context.EmpCurrentPettyCashBalances.Where(e => e.EmployeeId == expenseReimburseRequest.EmployeeId).FirstOrDefault(); double empCurPettyBal = empCurrentPettyCashBalance.CurBalance; //logic goes here if (expenseReimAmt + empCurPettyBal >= RoleLimitAmt) // claiming amount is greater than replishable amount { disbAndClaimItem.AmountToWallet = RoleLimitAmt - empCurPettyBal; disbAndClaimItem.AmountToCredit = expenseReimAmt - (RoleLimitAmt - empCurPettyBal); } else { //fully credit to Wallet - Zero amount to bank amount disbAndClaimItem.AmountToWallet = expenseReimAmt; disbAndClaimItem.AmountToCredit = 0; } disbAndClaimItem.ApprovalStatusId = (int)EApprovalStatus.Approved; _context.Update(disbAndClaimItem); //Final Approveer hence update the EmpCurrentPettyCashBalance table for the employee to reflect the credit empCurrentPettyCashBalance.CurBalance = empCurPettyBal + disbAndClaimItem.AmountToWallet ?? 0; empCurrentPettyCashBalance.UpdatedOn = DateTime.Now; _context.EmpCurrentPettyCashBalances.Update(empCurrentPettyCashBalance); /// } //Save to database if (claimitem != null) { _context.Update(claimitem); } ; await _context.SaveChangesAsync(); int reqApprGroupId = _context.Employees.Find(expenseReimburseStatusTrackerDto.EmployeeId).ApprovalGroupId; var getEmpClaimApproversAllLevels = _context.ApprovalRoleMaps.Include(a => a.ApprovalLevel).Where(a => a.ApprovalGroupId == reqApprGroupId).OrderBy(o => o.ApprovalLevel.Level).ToList(); foreach (var ApprMap in getEmpClaimApproversAllLevels) { //only next level (level + 1) approver is considered here if (ApprMap.ApprovalLevelId == expenseReimburseStatusTracker.ApprovalLevelId + 1) { int role_id = ApprMap.RoleId; var approver = _context.Employees.Where(e => e.RoleId == role_id && e.ApprovalGroupId == reqApprGroupId).FirstOrDefault(); //##### 4. Send email to the Approver //#################################### var approverMailAddress = approver.Email; var expReimReqt = _context.ExpenseReimburseRequests.Find(expenseReimburseStatusTracker.ExpenseReimburseRequestId); string subject = expReimReqt.ExpenseReportTitle + " - #" + expenseReimburseStatusTracker.ExpenseReimburseRequest.Id.ToString(); Employee emp = _context.Employees.Find(expenseReimburseStatusTracker.EmployeeId); string content = "Expense Reimbursement request Approval sought by " + emp.FirstName + "<br/>for the amount of " + expReimReqt.TotalClaimAmount + "<br/>towards " + expReimReqt.ExpenseReportTitle; var messagemail = new Message(new string[] { approverMailAddress }, subject, content); await _emailSender.SendEmailAsync(messagemail); break; } } } //if nothing else then just update the approval status expenseReimburseStatusTracker.ApprovalStatusTypeId = expenseReimburseStatusTrackerDto.ApprovalStatusTypeId; //If no expenseReimburseStatusTrackers are in pending for the Expense request then update the ExpenseReimburse request table int pendingApprovals = _context.ExpenseReimburseStatusTrackers .Where(t => t.ExpenseReimburseRequestId == expenseReimburseStatusTrackerDto.ExpenseReimburseRequestId && t.ApprovalStatusTypeId == (int)EApprovalStatus.Pending).Count(); if (pendingApprovals == 0) { var expReimbReq = _context.ExpenseReimburseRequests.Where(p => p.Id == expenseReimburseStatusTrackerDto.ExpenseReimburseRequestId).FirstOrDefault(); expReimbReq.ApprovalStatusTypeId = expenseReimburseStatusTrackerDto.ApprovalStatusTypeId; expReimbReq.ApprovedDate = DateTime.Now; expReimbReq.Comments = bRejectMessage ? expenseReimburseStatusTrackerDto.Comments : "Approved"; _context.ExpenseReimburseRequests.Update(expReimbReq); await _context.SaveChangesAsync(); } //update the Expense Reimburse request table to reflect the rejection if (bRejectMessage) { var expReimbReq = _context.ExpenseReimburseRequests.Where(p => p.Id == expenseReimburseStatusTrackerDto.ExpenseReimburseRequestId).FirstOrDefault(); expReimbReq.ApprovalStatusTypeId = expenseReimburseStatusTrackerDto.ApprovalStatusTypeId; expReimbReq.ApprovedDate = DateTime.Now; expReimbReq.Comments = expenseReimburseStatusTrackerDto.Comments; _context.ExpenseReimburseRequests.Update(expReimbReq); await _context.SaveChangesAsync(); } } //project based Expense Reimburse approval/rejection //only one approver (Project manager) else { //final approver hence update Expense Reimburse request claim claimitem = _context.ExpenseReimburseStatusTrackers.Where(c => c.ExpenseReimburseRequestId == expenseReimburseStatusTracker.ExpenseReimburseRequestId && c.ApprovalStatusTypeId == (int)EApprovalStatus.Pending).FirstOrDefault(); expenseReimburseStatusTracker.ApprovalStatusTypeId = expenseReimburseStatusTrackerDto.ApprovalStatusTypeId; //DisbursementAndClaimsMaster update the record to Approved (ApprovalStatusId int disbAndClaimItemId = _context.DisbursementsAndClaimsMasters.Where(d => d.ExpenseReimburseReqId == claimitem.ExpenseReimburseRequestId).FirstOrDefault().Id; var disbAndClaimItem = await _context.DisbursementsAndClaimsMasters.FindAsync(disbAndClaimItemId); /// ############################# // Crediting back to the wallet /// ############################# double expenseReimAmt = claimitem.TotalClaimAmount; double RoleLimitAmt = _context.JobRoles.Find(_context.Employees.Find(claimitem.EmployeeId).RoleId).MaxPettyCashAllowed; EmpCurrentPettyCashBalance empCurrentPettyCashBalance = _context.EmpCurrentPettyCashBalances.Where(e => e.EmployeeId == claimitem.EmployeeId).FirstOrDefault(); double empCurPettyBal = empCurrentPettyCashBalance.CurBalance; //logic goes here if (expenseReimAmt + empCurPettyBal >= RoleLimitAmt) // claiming amount is greater than replishable amount { disbAndClaimItem.AmountToWallet = RoleLimitAmt - empCurPettyBal; disbAndClaimItem.AmountToCredit = expenseReimAmt - (RoleLimitAmt - empCurPettyBal); } else { //fully credit to Wallet - Zero amount to bank amount disbAndClaimItem.AmountToWallet = expenseReimAmt; disbAndClaimItem.AmountToCredit = 0; } disbAndClaimItem.ApprovalStatusId = bRejectMessage ? (int)EApprovalStatus.Rejected : (int)EApprovalStatus.Approved; _context.Update(disbAndClaimItem); //Final Approveer hence update the EmpCurrentPettyCashBalance table for the employee to reflect the credit empCurrentPettyCashBalance.CurBalance = empCurPettyBal + disbAndClaimItem.AmountToWallet ?? 0; _context.EmpCurrentPettyCashBalances.Update(empCurrentPettyCashBalance); ///// /// //Update ExpenseReimburseRequests table to update the record to Approved as the final approver has approved it. int expenseReimReqId = _context.ExpenseReimburseRequests.Where(d => d.Id == claimitem.ExpenseReimburseRequestId).FirstOrDefault().Id; var expenseReimReq = await _context.ExpenseReimburseRequests.FindAsync(expenseReimReqId); expenseReimReq.ApprovalStatusTypeId = bRejectMessage ? (int)EApprovalStatus.Rejected : (int)EApprovalStatus.Approved; expenseReimReq.Comments = bRejectMessage? expenseReimburseStatusTrackerDto.Comments: "Approved"; expenseReimReq.ApprovedDate = DateTime.Now; _context.Update(expenseReimReq); } _context.ExpenseReimburseStatusTrackers.Update(expenseReimburseStatusTracker); } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } RespStatus respStatus = new(); if (bRejectMessage) { respStatus.Status = "Success"; respStatus.Message = "Expense-Reimburse Request(s) Rejected!"; } else { respStatus.Status = "Success"; respStatus.Message = "Expense-Reimburse Request(s) Approved!"; } return(Ok(respStatus)); }
public async Task <IActionResult> PutPettyCashRequest(int id, PettyCashRequestDTO pettyCashRequestDto) { if (id != pettyCashRequestDto.Id) { return(Conflict(new RespStatus { Status = "Failure", Message = "Id is invalid" })); } var pettyCashRequest = await _context.PettyCashRequests.FindAsync(id); pettyCashRequestDto.EmployeeId = pettyCashRequest.EmployeeId; Double empCurAvailBal = GetEmpCurrentAvailablePettyCashBalance(pettyCashRequestDto); if (!(pettyCashRequestDto.PettyClaimAmount <= empCurAvailBal && pettyCashRequestDto.PettyClaimAmount > 0)) { return(Conflict(new RespStatus() { Status = "Failure", Message = "Invalid Cash Request Amount Or Limit Exceeded" })); } int ApprovedCount = _context.ExpenseReimburseStatusTrackers.Where(e => e.ExpenseReimburseRequestId == pettyCashRequest.Id && e.ApprovalStatusTypeId == (int)EApprovalStatus.Approved).Count(); if (ApprovedCount != 0) { return(Conflict(new RespStatus { Status = "Failure", Message = "PettyCash Requests cant be Edited after Approval!" })); } //if Pettycash request is modified then trigger changes to other tables if (pettyCashRequest.PettyClaimAmount != pettyCashRequestDto.PettyClaimAmount) { //update the EmpPettyCashBalance to credit back the deducted amount EmpCurrentPettyCashBalance empPettyCashBal = _context.EmpCurrentPettyCashBalances.Where(e => e.EmployeeId == pettyCashRequest.EmployeeId).FirstOrDefault(); double oldBal = empPettyCashBal.CurBalance; double prevAmt = pettyCashRequest.PettyClaimAmount; double NewAmt = pettyCashRequestDto.PettyClaimAmount; pettyCashRequest.PettyClaimAmount = pettyCashRequestDto.PettyClaimAmount; pettyCashRequest.PettyClaimRequestDesc = pettyCashRequestDto.PettyClaimRequestDesc; //check employee allowed limit to Cash Advance, if limit exceeded return with an conflict message. double maxAllowed = _context.JobRoles.Find(_context.Employees.Find(pettyCashRequest.EmployeeId).RoleId).MaxPettyCashAllowed; if (maxAllowed >= oldBal + prevAmt - NewAmt && oldBal + prevAmt - NewAmt > 0) { empPettyCashBal.CurBalance = oldBal + prevAmt - NewAmt; empPettyCashBal.UpdatedOn = DateTime.Now; _context.EmpCurrentPettyCashBalances.Update(empPettyCashBal); } else { return(Conflict(new RespStatus() { Status = "Failure", Message = "Invalid Cash Request Amount Or Limit Exceeded" })); } } //// /// pettyCashRequest.PettyClaimAmount = pettyCashRequestDto.PettyClaimAmount; pettyCashRequest.PettyClaimRequestDesc = pettyCashRequestDto.PettyClaimRequestDesc; pettyCashRequest.CashReqDate = DateTime.Now; _context.PettyCashRequests.Update(pettyCashRequest); //Step -2 change the claim approval status tracker records var claims = await _context.ClaimApprovalStatusTrackers.Where(c => c.PettyCashRequestId == pettyCashRequestDto.Id).ToListAsync(); bool IsFirstEmail = true; int? newDeptId = pettyCashRequest.DepartmentId; int? newProjId = pettyCashRequestDto.ProjectId; int? newSubProjId = pettyCashRequestDto.SubProjectId; int? newWorkTaskId = pettyCashRequestDto.WorkTaskId; foreach (ClaimApprovalStatusTracker claim in claims) { claim.DepartmentId = newDeptId; claim.ProjectId = newProjId; claim.SubProjectId = newSubProjId; claim.WorkTaskId = newWorkTaskId; claim.ReqDate = pettyCashRequest.CashReqDate; claim.FinalApprovedDate = null; //claim.ApprovalStatusTypeId = claim.ApprovalLevelId == 1 ? (int)EApprovalStatus.Pending : (int)EApprovalStatus.Initiating; claim.Comments = "Modified Request"; _context.ClaimApprovalStatusTrackers.Update(claim); if (IsFirstEmail) { //#################################### var approver = _context.Employees.Where(e => e.RoleId == claim.RoleId && e.ApprovalGroupId == claim.ApprovalGroupId).FirstOrDefault(); var approverMailAddress = approver.Email; string subject = "(Modified) Pettycash Request Approval " + pettyCashRequestDto.Id.ToString(); Employee emp = await _context.Employees.FindAsync(pettyCashRequest.EmployeeId); var pettycashreq = _context.PettyCashRequests.Find(pettyCashRequestDto.Id); string content = "(Modified) Petty Cash Approval sought by " + emp.FirstName + "@<br/>Cash Request for the amount of " + pettycashreq.PettyClaimAmount + "@<br/>towards " + pettycashreq.PettyClaimRequestDesc; var messagemail = new Message(new string[] { approverMailAddress }, subject, content); await _emailSender.SendEmailAsync(messagemail); IsFirstEmail = false; } } //_context.Entry(pettyCashRequest).State = EntityState.Modified; //Step-3 change the Disbursements and Claims Master record var disburseMasterRecord = _context.DisbursementsAndClaimsMasters.Where(d => d.PettyCashRequestId == pettyCashRequestDto.Id).FirstOrDefault(); disburseMasterRecord.DepartmentId = newDeptId; disburseMasterRecord.ProjectId = newProjId; disburseMasterRecord.SubProjectId = newSubProjId; disburseMasterRecord.WorkTaskId = newWorkTaskId; disburseMasterRecord.RecordDate = DateTime.Now; disburseMasterRecord.ClaimAmount = pettyCashRequestDto.PettyClaimAmount; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(Ok(new RespStatus { Status = "Success", Message = "Request Updated!" })); }
private async Task <IActionResult> ProjectBasedExpReimRequest(ExpenseReimburseRequestDTO expenseReimburseRequestDto) { //### 1. If Employee Eligible for Cash Claim enter a record and reduce the available amount for next claim #region int costCenterId = _context.Projects.Find(expenseReimburseRequestDto.ProjectId).CostCenterId; int projManagerid = _context.Projects.Find(expenseReimburseRequestDto.ProjectId).ProjectManagerId; var approver = _context.Employees.Find(projManagerid); int reqEmpid = expenseReimburseRequestDto.EmployeeId; Employee reqEmp = _context.Employees.Find(reqEmpid); int reqApprGroupId = reqEmp.ApprovalGroupId; int reqRoleId = reqEmp.RoleId; int maxApprLevel = _context.ApprovalRoleMaps.Include("ApprovalLevel").Where(a => a.ApprovalGroupId == reqApprGroupId).ToList().Select(x => x.ApprovalLevel).Max(a => a.Level); int reqApprLevel = _context.ApprovalRoleMaps.Include("ApprovalLevel").Where(a => a.ApprovalGroupId == reqApprGroupId && a.RoleId == reqRoleId).Select(x => x.ApprovalLevel).FirstOrDefault().Level; bool isSelfApprovedRequest = false; //// /// ExpenseReimburseRequest expenseReimburseRequest = new(); double dblTotalClaimAmount = 0; expenseReimburseRequest.ExpenseReportTitle = expenseReimburseRequestDto.ExpenseReportTitle; expenseReimburseRequest.EmployeeId = expenseReimburseRequestDto.EmployeeId; expenseReimburseRequest.CurrencyTypeId = expenseReimburseRequestDto.CurrencyTypeId; expenseReimburseRequest.TotalClaimAmount = dblTotalClaimAmount; //Currently Zero but added as per the request expenseReimburseRequest.ExpReimReqDate = DateTime.Now; expenseReimburseRequest.DepartmentId = null; expenseReimburseRequest.ProjectId = expenseReimburseRequestDto.ProjectId; expenseReimburseRequest.SubProjectId = expenseReimburseRequestDto.SubProjectId; expenseReimburseRequest.WorkTaskId = expenseReimburseRequestDto.WorkTaskId; expenseReimburseRequest.ApprovalStatusTypeId = (int)EApprovalStatus.Pending; //expenseReimburseRequest.ApprovedDate = expenseReimburseRequestDto.ApprovedDate; expenseReimburseRequest.Comments = "Expense Reimburse Request in Process!"; _context.ExpenseReimburseRequests.Add(expenseReimburseRequest); // <= this generated the Id await _context.SaveChangesAsync(); /// foreach (ExpenseSubClaimDTO expenseSubClaimDto in expenseReimburseRequestDto.ExpenseSubClaims) { ExpenseSubClaim expenseSubClaim = new(); //get expensereimburserequestId from the saved record and then use here for sub-claims expenseSubClaim.ExpenseReimburseRequestId = expenseReimburseRequest.Id; expenseSubClaim.ExpenseTypeId = expenseSubClaimDto.ExpenseTypeId; expenseSubClaim.ExpenseReimbClaimAmount = expenseSubClaimDto.ExpenseReimbClaimAmount; expenseSubClaim.DocumentIDs = expenseSubClaimDto.DocumentIDs; expenseSubClaim.InvoiceNo = expenseSubClaimDto.InvoiceNo; expenseSubClaim.InvoiceDate = expenseSubClaimDto.InvoiceDate; expenseSubClaim.Tax = expenseSubClaimDto.Tax; expenseSubClaim.TaxAmount = expenseSubClaimDto.TaxAmount; expenseSubClaim.Vendor = expenseSubClaimDto.Vendor; expenseSubClaim.Location = expenseSubClaimDto.Location; expenseSubClaim.Description = expenseSubClaimDto.Description; _context.ExpenseSubClaims.Add(expenseSubClaim); await _context.SaveChangesAsync(); dblTotalClaimAmount = dblTotalClaimAmount + expenseSubClaimDto.TaxAmount + expenseSubClaimDto.ExpenseReimbClaimAmount; } ExpenseReimburseRequest exp = _context.ExpenseReimburseRequests.Find(expenseReimburseRequest.Id); exp.TotalClaimAmount = dblTotalClaimAmount; _context.ExpenseReimburseRequests.Update(exp); await _context.SaveChangesAsync(); ///////////////////////////// Check if self Approved Request ///////////////////////////// //if highest approver is requesting Petty cash request himself if (maxApprLevel == reqApprLevel || projManagerid == reqEmpid) { isSelfApprovedRequest = true; } ////////////////////////////////////////////////////////////////////////////////////////// //var test = _context.ApprovalRoleMaps.Include(a => a.ApprovalLevel).ToList().OrderBy(o => o.ApprovalLevel.Level); if (isSelfApprovedRequest) { ExpenseReimburseStatusTracker expenseReimburseStatusTracker = new() { EmployeeId = expenseReimburseRequestDto.EmployeeId, ExpenseReimburseRequestId = expenseReimburseRequest.Id, CurrencyTypeId = expenseReimburseRequestDto.CurrencyTypeId, TotalClaimAmount = dblTotalClaimAmount, ExpReimReqDate = DateTime.Now, DepartmentId = null, ProjManagerId = projManagerid, ProjectId = expenseReimburseRequestDto.ProjectId, //Approver Project Id SubProjectId = expenseReimburseRequestDto.SubProjectId, WorkTaskId = expenseReimburseRequestDto.WorkTaskId, JobRoleId = _context.Employees.Find(expenseReimburseRequestDto.EmployeeId).RoleId, ApprovalGroupId = reqApprGroupId, ApprovalLevelId = 2, //(reqApprLevel) or 2 default approval level is 2 for Project based request ApprovedDate = null, ApprovalStatusTypeId = (int)EApprovalStatus.Approved, //1-Pending, 2-Approved, 3-Rejected Comments = "Self Approved Request!" }; _context.ExpenseReimburseStatusTrackers.Add(expenseReimburseStatusTracker); expenseReimburseRequest.ApprovalStatusTypeId = (int)EApprovalStatus.Approved; _context.ExpenseReimburseRequests.Update(expenseReimburseRequest); await _context.SaveChangesAsync(); } else { ExpenseReimburseStatusTracker expenseReimburseStatusTracker = new() { EmployeeId = expenseReimburseRequestDto.EmployeeId, ExpenseReimburseRequestId = expenseReimburseRequest.Id, CurrencyTypeId = expenseReimburseRequestDto.CurrencyTypeId, TotalClaimAmount = dblTotalClaimAmount, ExpReimReqDate = DateTime.Now, DepartmentId = null, ProjManagerId = projManagerid, ProjectId = expenseReimburseRequestDto.ProjectId, //Approver Project Id SubProjectId = expenseReimburseRequestDto.SubProjectId, WorkTaskId = expenseReimburseRequestDto.WorkTaskId, JobRoleId = approver.RoleId, ApprovalGroupId = reqApprGroupId, ApprovalLevelId = 2, // default approval level is 2 for Project based request ApprovedDate = null, ApprovalStatusTypeId = (int)EApprovalStatus.Pending, Comments = "Expense Reimburse is in Process!" }; _context.ExpenseReimburseStatusTrackers.Add(expenseReimburseStatusTracker); await _context.SaveChangesAsync(); //##### 5. Send email to the Approver //#################################### if (isSelfApprovedRequest) { return(null); } var approverMailAddress = approver.Email; string subject = expenseReimburseRequest.ExpenseReportTitle + " - #" + expenseReimburseRequest.Id.ToString(); Employee emp = _context.Employees.Find(expenseReimburseRequestDto.EmployeeId); string content = "Expense Reimbursement request Approval sought by " + emp.FirstName + "<br/>for the amount of " + expenseReimburseRequest.TotalClaimAmount + "<br/>towards " + expenseReimburseRequest.ExpenseReportTitle; var messagemail = new Message(new string[] { approverMailAddress }, subject, content); await _emailSender.SendEmailAsync(messagemail); //repeat for each approver } #endregion //##### 5. Adding a entry in DisbursementsAndClaimsMaster table for records #region DisbursementsAndClaimsMaster disbursementsAndClaimsMaster = new(); disbursementsAndClaimsMaster.EmployeeId = expenseReimburseRequestDto.EmployeeId; disbursementsAndClaimsMaster.ExpenseReimburseReqId = expenseReimburseRequest.Id; disbursementsAndClaimsMaster.RequestTypeId = (int)ERequestType.ExpenseReim; disbursementsAndClaimsMaster.DepartmentId = null; disbursementsAndClaimsMaster.ProjectId = expenseReimburseRequestDto.ProjectId; disbursementsAndClaimsMaster.SubProjectId = expenseReimburseRequestDto.SubProjectId; disbursementsAndClaimsMaster.WorkTaskId = expenseReimburseRequestDto.WorkTaskId; disbursementsAndClaimsMaster.RecordDate = DateTime.Now; disbursementsAndClaimsMaster.CurrencyTypeId = expenseReimburseRequestDto.CurrencyTypeId; disbursementsAndClaimsMaster.ClaimAmount = dblTotalClaimAmount; disbursementsAndClaimsMaster.CostCenterId = costCenterId; disbursementsAndClaimsMaster.ApprovalStatusId = (int)EApprovalStatus.Pending; //1-Initiating; 2-Pending; 3-InReview; 4-Approved; 5-Rejected disbursementsAndClaimsMaster.IsSettledAmountCredited = false; //save at the end of the code. not here! #endregion /// ############################# // Crediting back to the wallet (for self approvedRequest Only) /// ############################# /// if (isSelfApprovedRequest) { double expenseReimAmt = expenseReimburseRequest.TotalClaimAmount; double RoleLimitAmt = _context.JobRoles.Find(_context.Employees.Find(expenseReimburseRequest.EmployeeId).RoleId).MaxPettyCashAllowed; EmpCurrentPettyCashBalance empCurrentPettyCashBalance = _context.EmpCurrentPettyCashBalances.Where(e => e.EmployeeId == expenseReimburseRequest.EmployeeId).FirstOrDefault(); double empCurPettyBal = empCurrentPettyCashBalance.CurBalance; //logic goes here if (expenseReimAmt + empCurPettyBal >= RoleLimitAmt) // claiming amount is greater than replishable amount { disbursementsAndClaimsMaster.AmountToWallet = RoleLimitAmt - empCurPettyBal; disbursementsAndClaimsMaster.AmountToCredit = expenseReimAmt - (RoleLimitAmt - empCurPettyBal); } else { //fully credit to Wallet - Zero amount to bank amount disbursementsAndClaimsMaster.AmountToWallet = expenseReimAmt; disbursementsAndClaimsMaster.AmountToCredit = 0; } disbursementsAndClaimsMaster.ApprovalStatusId = (int)EApprovalStatus.Approved; _context.Update(disbursementsAndClaimsMaster); //Final Approveer hence update the EmpCurrentPettyCashBalance table for the employee to reflect the credit empCurrentPettyCashBalance.CurBalance = empCurPettyBal + disbursementsAndClaimsMaster.AmountToWallet ?? 0; empCurrentPettyCashBalance.UpdatedOn = DateTime.Now; _context.EmpCurrentPettyCashBalances.Update(empCurrentPettyCashBalance); await _context.DisbursementsAndClaimsMasters.AddAsync(disbursementsAndClaimsMaster); await _context.SaveChangesAsync(); return(Ok(new RespStatus { Status = "Success", Message = "Self approved Expense Claim Submitted Successfully!" })); } /// await _context.DisbursementsAndClaimsMasters.AddAsync(disbursementsAndClaimsMaster); await _context.SaveChangesAsync(); return(Ok(new RespStatus { Status = "Success", Message = "Expense Claim Submitted Successfully!" })); } #endregion // }
public async Task <IActionResult> PutEmployee(int id, EmployeeDTO employeeDto) { if (id != employeeDto.Id) { return(Conflict(new RespStatus { Status = "Failure", Message = "Id is invalid" })); } var emp = _context.Employees.Find(employeeDto.Id); int _testempId = _context.Employees.Where(e => e.MobileNumber == employeeDto.MobileNumber || e.EmpCode == employeeDto.EmpCode || e.Email == employeeDto.Email).Select(x => x.Id).FirstOrDefault(); if (employeeDto.Id != _testempId) { return(Conflict(new RespStatus { Status = "Failure", Message = "Unique EmpCode/Mobile/Email required" })); } //var emplye = _context.Employees.Where(e => e.FirstName == employeeDto.FirstName && e.MiddleName == employeeDto.MiddleName && e.LastName == employeeDto.LastName).FirstOrDefault(); //if (emplye != null) //{ // return Conflict(new RespStatus { Status = "Failure", Message = "Employee Already Exists" }); //} var employee = await _context.Employees.FindAsync(id); employee.Id = employeeDto.Id; employee.FirstName = employeeDto.FirstName; employee.MiddleName = employeeDto.MiddleName; employee.LastName = employeeDto.LastName; employee.EmpCode = employeeDto.EmpCode; employee.BankAccount = employeeDto.BankAccount; employee.BankCardNo = employeeDto.BankCardNo; employee.NationalID = employeeDto.NationalID; employee.PassportNo = employeeDto.PassportNo; employee.TaxNumber = employeeDto.TaxNumber; employee.Nationality = employeeDto.Nationality; employee.DOB = employeeDto.DOB; employee.DOJ = employeeDto.DOJ; employee.Gender = employeeDto.Gender; MailAddress mailAdd = new MailAddress(employeeDto.Email); if ((mailAdd.Host.ToUpper() != "MAILINATOR.COM") && mailAdd.Host.ToUpper() != "GMAIL.COM") { return(Conflict(new RespStatus { Status = "Failure", Message = "Use company mail address!" })); } else { employee.Email = employeeDto.Email; } employee.Email = employeeDto.Email; employee.MobileNumber = employeeDto.MobileNumber; employee.EmploymentTypeId = employeeDto.EmploymentTypeId; employee.DepartmentId = employeeDto.DepartmentId; employee.CurrencyTypeId = employeeDto.CurrencyTypeId; employee.ApprovalGroupId = employeeDto.ApprovalGroupId; employee.StatusTypeId = employeeDto.StatusTypeId; if (employee.RoleId != employeeDto.RoleId) { double oldAmt = _context.JobRoles.Find(employee.RoleId).MaxPettyCashAllowed; double newAmt = _context.JobRoles.Find(employeeDto.RoleId).MaxPettyCashAllowed; EmpCurrentPettyCashBalance empCurrentPettyCashBalance = _context.EmpCurrentPettyCashBalances.Where(e => e.EmployeeId == employee.Id).FirstOrDefault(); double empCurBal = empCurrentPettyCashBalance.CurBalance; //update the roleId to new RoleId employee.RoleId = employeeDto.RoleId; double usedAmount = oldAmt - empCurrentPettyCashBalance.CurBalance; double NewUpdatedLimit = newAmt - usedAmount; empCurrentPettyCashBalance.CurBalance = NewUpdatedLimit; _context.EmpCurrentPettyCashBalances.Update(empCurrentPettyCashBalance); } _context.Employees.Update(employee); //_context.Entry(employeeDto).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(Conflict(new RespStatus { Status = "Failure", Message = "Employee Doesn't Exist!" })); } else { throw; } } return(Ok(new RespStatus { Status = "Success", Message = "Employee Records Updated!" })); }