public async Task <ActionResult <ExpenseSubClaimDTO> > GetExpenseSubClaim(int id) { var expenseSubClaim = await _context.ExpenseSubClaims.FindAsync(id); if (expenseSubClaim == null) { return(Conflict(new RespStatus { Status = "Failure", Message = "Expense Id is not Valid!" })); } // Expense reimburse request Id int expReimbReqId = _context.ExpenseSubClaims.Find(expenseSubClaim.Id).ExpenseReimburseRequestId; ExpenseReimburseRequest expReimReq = _context.ExpenseReimburseRequests.Find(expReimbReqId); int empId = _context.ExpenseReimburseRequests.Find(expReimbReqId).EmployeeId; string empFullName = _context.Employees.Find(empId).GetFullName(); // ExpenseSubClaimDTO expenseSubClaimsDto = new(); expenseSubClaimsDto.Id = expenseSubClaim.Id; expenseSubClaimsDto.EmployeeId = empId; expenseSubClaimsDto.EmployeeName = empFullName; expenseSubClaimsDto.ExpenseReimbClaimAmount = expenseSubClaim.ExpenseReimbClaimAmount; expenseSubClaimsDto.DocumentIDs = expenseSubClaim.DocumentIDs; expenseSubClaimsDto.ExpReimReqDate = expReimReq.ExpReimReqDate; expenseSubClaimsDto.InvoiceNo = expenseSubClaim.InvoiceNo; expenseSubClaimsDto.InvoiceDate = expenseSubClaim.InvoiceDate; expenseSubClaimsDto.Tax = expenseSubClaim.Tax; expenseSubClaimsDto.TaxAmount = expenseSubClaim.TaxAmount; expenseSubClaimsDto.Vendor = expenseSubClaim.Vendor; expenseSubClaimsDto.Location = expenseSubClaim.Location; expenseSubClaimsDto.Description = expenseSubClaim.Description; expenseSubClaimsDto.CurrencyTypeId = expReimReq.CurrencyTypeId; expenseSubClaimsDto.CurrencyType = _context.CurrencyTypes.Find(expReimReq.CurrencyTypeId).CurrencyCode; expenseSubClaimsDto.ExpenseTypeId = expenseSubClaim.ExpenseTypeId; expenseSubClaimsDto.ExpenseType = _context.ExpenseTypes.Find(expenseSubClaim.ExpenseTypeId).ExpenseTypeName; expenseSubClaimsDto.Department = expReimReq.DepartmentId != null?_context.Departments.Find(expReimReq.DepartmentId).DeptName : null; expenseSubClaimsDto.DepartmentId = expReimReq.DepartmentId; expenseSubClaimsDto.Project = expReimReq.ProjectId != null?_context.Projects.Find(expReimReq.ProjectId).ProjectName : null; expenseSubClaimsDto.ProjectId = expReimReq.ProjectId; expenseSubClaimsDto.SubProject = expReimReq.SubProjectId != null?_context.SubProjects.Find(expReimReq.SubProjectId).SubProjectName : null; expenseSubClaimsDto.SubProjectId = expReimReq.SubProjectId; expenseSubClaimsDto.WorkTask = expReimReq.WorkTaskId != null?_context.WorkTasks.Find(expReimReq.WorkTaskId).TaskName : null; expenseSubClaimsDto.WorkTaskId = expReimReq.WorkTaskId; expenseSubClaimsDto.ApprovalStatusType = _context.ApprovalStatusTypes.Find(expReimReq.ApprovalStatusTypeId).Status; expenseSubClaimsDto.ApprovalStatusTypeId = expReimReq.ApprovalStatusTypeId; expenseSubClaimsDto.ApprovedDate = expReimReq.ApprovedDate; return(Ok(expenseSubClaimsDto)); }
public async Task <ActionResult <IEnumerable <ExpenseSubClaim> > > GetExpenseSubClaims() { List <ExpenseSubClaim> expenseSubClaims = await _context.ExpenseSubClaims.ToListAsync(); List <ExpenseSubClaimDTO> expenseSubClaimDTOs = new(); foreach (ExpenseSubClaim expenseSubClaim in expenseSubClaims) { // Expense reimburse request Id int expReimbReqId = _context.ExpenseSubClaims.Find(expenseSubClaim.Id).ExpenseReimburseRequestId; ExpenseReimburseRequest expReimReq = _context.ExpenseReimburseRequests.Find(expReimbReqId); int empId = _context.ExpenseReimburseRequests.Find(expReimbReqId).EmployeeId; string empFullName = _context.Employees.Find(empId).GetFullName(); // ExpenseSubClaimDTO expenseSubClaimsDto = new(); expenseSubClaimsDto.Id = expenseSubClaim.Id; expenseSubClaimsDto.EmployeeId = empId; expenseSubClaimsDto.EmployeeName = empFullName; expenseSubClaimsDto.ExpenseReimbClaimAmount = expenseSubClaim.ExpenseReimbClaimAmount; expenseSubClaimsDto.DocumentIDs = expenseSubClaim.DocumentIDs; expenseSubClaimsDto.ExpReimReqDate = expReimReq.ExpReimReqDate; expenseSubClaimsDto.InvoiceNo = expenseSubClaim.InvoiceNo; expenseSubClaimsDto.InvoiceDate = expenseSubClaim.InvoiceDate; expenseSubClaimsDto.Tax = expenseSubClaim.Tax; expenseSubClaimsDto.TaxAmount = expenseSubClaim.TaxAmount; expenseSubClaimsDto.Vendor = expenseSubClaim.Vendor; expenseSubClaimsDto.Location = expenseSubClaim.Location; expenseSubClaimsDto.Description = expenseSubClaim.Description; expenseSubClaimsDto.CurrencyTypeId = expReimReq.CurrencyTypeId; expenseSubClaimsDto.CurrencyType = _context.CurrencyTypes.Find(expReimReq.CurrencyTypeId).CurrencyCode; expenseSubClaimsDto.ExpenseTypeId = expenseSubClaim.ExpenseTypeId; expenseSubClaimsDto.ExpenseType = _context.ExpenseTypes.Find(expenseSubClaim.ExpenseTypeId).ExpenseTypeName; expenseSubClaimsDto.Department = _context.Departments.Find(expReimReq.DepartmentId).DeptName; expenseSubClaimsDto.DepartmentId = expReimReq.DepartmentId; expenseSubClaimsDto.Project = _context.Projects.Find(expReimReq.ProjectId).ProjectName; expenseSubClaimsDto.ProjectId = expReimReq.ProjectId; expenseSubClaimsDto.SubProject = _context.SubProjects.Find(expReimReq.SubProjectId).SubProjectName; expenseSubClaimsDto.SubProjectId = expReimReq.SubProjectId; expenseSubClaimsDto.WorkTask = _context.WorkTasks.Find(expReimReq.WorkTaskId).TaskName; expenseSubClaimsDto.WorkTaskId = expReimReq.WorkTaskId; expenseSubClaimsDto.ApprovalStatusType = _context.ApprovalStatusTypes.Find(expReimReq.ApprovalStatusTypeId).Status; expenseSubClaimsDto.ApprovalStatusTypeId = expReimReq.ApprovalStatusTypeId; expenseSubClaimsDto.ApprovedDate = expReimReq.ApprovedDate; expenseSubClaimDTOs.Add(expenseSubClaimsDto); } return(Ok(expenseSubClaimDTOs)); }
public async Task <ActionResult <ExpenseReimburseRequest> > PostExpenseReimburseRequest(List <ExpenseReimburseRequestDTO> listExpenseReimburseRequestDto) { if (listExpenseReimburseRequestDto.Count == 1) { ExpenseReimburseRequest expenseReimburseRequest = null; ExpenseReimburseRequestDTO expenseReimburseRequestDto = listExpenseReimburseRequestDto[0]; StringBuilder StrBuilderUploadedDocuments = new StringBuilder(); if (ModelState.IsValid) { string uniqueFileName = null; uniqueFileName = await SaveFileToFolderAndGetName(expenseReimburseRequestDto); expenseReimburseRequest = new ExpenseReimburseRequest() { EmployeeId = expenseReimburseRequestDto.EmployeeId, ExpenseReimbClaimAmount = expenseReimburseRequestDto.ExpenseReimbClaimAmount, Documents = StrBuilderUploadedDocuments.ToString(), ExpReimReqDate = expenseReimburseRequestDto.ExpReimReqDate //ExpenseTypeId = expenseReimburseRequestDto.ExpenseTypeId }; _context.ExpenseReimburseRequests.Add(expenseReimburseRequest); await _context.SaveChangesAsync(); } //##### 3. Adding a entry in DisbursementsAndClaimsMaster table for records //If it is department based Expense reimbursement claim if (expenseReimburseRequestDto.ProjectId == null) { _context.DisbursementsAndClaimsMasters.Add(new DisbursementsAndClaimsMaster() { EmployeeId = expenseReimburseRequestDto.EmployeeId, PettyCashRequestId = null, ExpenseReimburseReqId = expenseReimburseRequest.Id, AdvanceOrReimburseId = (int)AdvanceOrReimburse.ExpenseReim, ProjectId = null, SubProjectId = null, WorkTaskId = null, RecordDate = DateTime.Now, Amount = expenseReimburseRequest.ExpenseReimbClaimAmount, CostCentreId = _context.Departments.Find(_context.Employees.Find(expenseReimburseRequestDto.EmployeeId).DepartmentId).CostCentreId, ApprovalStatusId = (int)ApprovalStatus.Pending }); await _context.SaveChangesAsync(); } else //If it is Project based Expense reimbursement claim { _context.DisbursementsAndClaimsMasters.Add(new DisbursementsAndClaimsMaster() { EmployeeId = expenseReimburseRequestDto.EmployeeId, PettyCashRequestId = null, ExpenseReimburseReqId = expenseReimburseRequest.Id, AdvanceOrReimburseId = (int)AdvanceOrReimburse.ExpenseReim, ProjectId = expenseReimburseRequest.ProjectId, SubProjectId = expenseReimburseRequest.SubProjectId, WorkTaskId = expenseReimburseRequest.WorkTaskId, RecordDate = DateTime.Now, Amount = expenseReimburseRequest.ExpenseReimbClaimAmount, CostCentreId = _context.Projects.Find(expenseReimburseRequestDto.ProjectId).CostCentreId, ApprovalStatusId = (int)ApprovalStatus.Pending }); await _context.SaveChangesAsync(); } //##### 4. ClaimsApprovalTracker to be updated for all the allowed Approvers if (expenseReimburseRequestDto.ProjectId == null) { await Task.Run(() => ProjectBasedExpReimRequest(expenseReimburseRequestDto, expenseReimburseRequest.Id)); } else { await Task.Run(() => DepartmentBasedExpReimRequest(expenseReimburseRequestDto, expenseReimburseRequest.Id)); } } else { /// for multiple Expenseclaims at the same time /// ///TODO lis } return(Ok()); }
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 // }