Example #1
0
        public async Task <ActionResult <ExpenseReimburseRequestDTO> > GetExpenseReimburseRequest(int id)
        {
            ExpenseReimburseRequestDTO expenseReimburseRequestDTO = new ExpenseReimburseRequestDTO();

            var expenseReimburseRequest = await _context.ExpenseReimburseRequests.FindAsync(id);

            if (expenseReimburseRequest == null)
            {
                return(NotFound());
            }

            expenseReimburseRequestDTO.Id         = expenseReimburseRequest.Id;
            expenseReimburseRequestDTO.EmployeeId = expenseReimburseRequest.EmployeeId;
            expenseReimburseRequestDTO.ExpenseReimbClaimAmount = expenseReimburseRequest.ExpenseReimbClaimAmount;

            //collect the saved images and conver to IformFile and send to User.

            //Pending
            //expenseReimburseRequestDTO.Documents = expenseReimburseRequest.Documents;



            expenseReimburseRequestDTO.ExpReimReqDate = expenseReimburseRequest.ExpReimReqDate;
            expenseReimburseRequestDTO.ExpenseTypeId  = expenseReimburseRequest.ExpenseTypeId;
            expenseReimburseRequestDTO.ProjectId      = expenseReimburseRequest.ProjectId;
            expenseReimburseRequestDTO.SubProjectId   = expenseReimburseRequest.SubProjectId;
            expenseReimburseRequestDTO.WorkTaskId     = expenseReimburseRequest.WorkTaskId;

            return(expenseReimburseRequestDTO);
        }
Example #2
0
        public async Task <ActionResult <IEnumerable <ExpenseReimburseRequestDTO> > > GetExpenseReimburseRequests()
        {
            List <ExpenseReimburseRequestDTO> ListExpenseReimburseRequestDTO = new List <ExpenseReimburseRequestDTO>();

            var expenseReimburseRequests = await _context.ExpenseReimburseRequests.ToListAsync();

            foreach (ExpenseReimburseRequest expenseReimburseRequest in expenseReimburseRequests)
            {
                ExpenseReimburseRequestDTO expenseReimburseRequestDTO = new ExpenseReimburseRequestDTO();

                expenseReimburseRequestDTO.Id         = expenseReimburseRequest.Id;
                expenseReimburseRequestDTO.EmployeeId = expenseReimburseRequest.EmployeeId;
                expenseReimburseRequestDTO.ExpenseReimbClaimAmount = expenseReimburseRequest.ExpenseReimbClaimAmount;

                //collect the saved images and conver to IformFile and send to User.
                ///


                //expenseReimburseRequestDTO.Documents = expenseReimburseRequest.Documents;


                ////
                expenseReimburseRequestDTO.ExpReimReqDate = expenseReimburseRequest.ExpReimReqDate;
                expenseReimburseRequestDTO.ExpenseTypeId  = expenseReimburseRequest.ExpenseTypeId;
                expenseReimburseRequestDTO.ProjectId      = expenseReimburseRequest.ProjectId;
                expenseReimburseRequestDTO.SubProjectId   = expenseReimburseRequest.SubProjectId;
                expenseReimburseRequestDTO.WorkTaskId     = expenseReimburseRequest.WorkTaskId;


                ListExpenseReimburseRequestDTO.Add(expenseReimburseRequestDTO);
            }

            return(ListExpenseReimburseRequestDTO);
        }
Example #3
0
        private async Task ProjectBasedExpReimRequest(ExpenseReimburseRequestDTO expenseReimburseRequestDto, int ExpReimReqId)
        {
            int projManagerid = _context.ProjectManagements.Find(expenseReimburseRequestDto.ProjectId).EmployeeId;
            var approver      = _context.Employees.Find(projManagerid);

            _context.ClaimApprovalStatusTrackers.Add(new ClaimApprovalStatusTracker
            {
                EmployeeId                = expenseReimburseRequestDto.EmployeeId, //Employee Requester Id
                PettyCashRequestId        = null,
                ExpenseReimburseRequestId = ExpReimReqId,
                DepartmentId              = null,                                  //Department of approver
                ProjectId            = expenseReimburseRequestDto.ProjectId.Value, //Approver Project Id
                RoleId               = approver.RoleId,                            // Approver Role Id
                ReqDate              = DateTime.Now,
                FinalApprovedDate    = null,
                ApprovalStatusTypeId = (int)ApprovalStatus.Pending //1-Pending, 2-Approved, 3-Rejected
            });

            //##### 5. Send email to the Approver
            //Single instance for Project
            //####################################

            var      approverMailAddress = approver.Email;
            string   subject             = "Expense Claim Approval Request " + expenseReimburseRequestDto.Id.ToString();
            Employee emp = _context.Employees.Find(expenseReimburseRequestDto.EmployeeId);
            var      expenseReimClaimReq = _context.ExpenseReimburseRequests.Find(expenseReimburseRequestDto.Id);
            string   content             = "Expense Reimbursement request Approval sought by " + emp.FirstName + "/nfor the amount of " + expenseReimClaimReq.ExpenseReimbClaimAmount + "/ntowards " + expenseReimClaimReq.ExpenseType;
            var      messagemail         = new Message(new string[] { approverMailAddress }, subject, content);

            await _emailSender.SendEmailAsync(messagemail);
        }
Example #4
0
        private async Task <string> SaveFileToFolderAndGetName(ExpenseReimburseRequestDTO expenseReimburseRequestDto)
        {
            string        uniqueFileName = string.Empty;
            StringBuilder StrBuilderUploadedDocuments = new StringBuilder();

            if (expenseReimburseRequestDto.Documents != null && expenseReimburseRequestDto.Documents.Count > 0)
            {
                foreach (IFormFile document in expenseReimburseRequestDto.Documents)
                {
                    string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "Images");

                    uniqueFileName = Guid.NewGuid().ToString() + "_" + document.FileName;

                    StrBuilderUploadedDocuments.Append(uniqueFileName + "^");

                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);


                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await document.CopyToAsync(stream);

                        stream.Flush();
                    }
                }
            }

            return(uniqueFileName);
        }
Example #5
0
        //[Authorize(Roles = "AtominosAdmin, Admin, Manager, Finmgr")]
        public async Task <IActionResult> PutExpenseReimburseRequest(int id, ExpenseReimburseRequestDTO expenseReimbRequestDTO)
        {
            if (id != expenseReimbRequestDTO.Id)
            {
                return(Conflict(new RespStatus {
                    Status = "Failure", Message = "Id is invalid"
                }));
            }



            var expenseReimbRequest = await _context.ExpenseReimburseRequests.FindAsync(expenseReimbRequestDTO.Id);

            expenseReimbRequest.Id                 = expenseReimbRequestDTO.Id;
            expenseReimbRequest.EmployeeId         = expenseReimbRequestDTO.EmployeeId;
            expenseReimbRequest.ExpenseReportTitle = expenseReimbRequestDTO.ExpenseReportTitle;
            expenseReimbRequest.CurrencyTypeId     = expenseReimbRequestDTO.CurrencyTypeId;
            expenseReimbRequest.TotalClaimAmount   = expenseReimbRequestDTO.TotalClaimAmount;

            expenseReimbRequest.DepartmentId = expenseReimbRequestDTO.DepartmentId;
            expenseReimbRequest.ProjectId    = expenseReimbRequestDTO.ProjectId;

            expenseReimbRequest.SubProjectId = expenseReimbRequestDTO.SubProjectId;

            expenseReimbRequest.WorkTaskId = expenseReimbRequestDTO.WorkTaskId;

            expenseReimbRequest.ExpReimReqDate       = expenseReimbRequestDTO.ExpReimReqDate;
            expenseReimbRequest.ApprovedDate         = expenseReimbRequestDTO.ApprovedDate;
            expenseReimbRequest.ApprovalStatusTypeId = expenseReimbRequestDTO.ApprovalStatusTypeId;

            await Task.Run(() => _context.ExpenseReimburseRequests.Update(expenseReimbRequest));


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(Ok(new RespStatus {
                Status = "Success", Message = "Expense Reimburse Data Updated!"
            }));
        }
Example #6
0
        public async Task <IActionResult> PutExpenseReimburseRequest(int id, ExpenseReimburseRequestDTO expenseReimburseRequestDto)
        {
            if (id != expenseReimburseRequestDto.Id)
            {
                return(BadRequest());
            }

            var expenseReimburseRequest = await _context.ExpenseReimburseRequests.FindAsync(id);

            expenseReimburseRequest.Id         = expenseReimburseRequestDto.Id;
            expenseReimburseRequest.EmployeeId = expenseReimburseRequestDto.EmployeeId;
            expenseReimburseRequest.ExpenseReimbClaimAmount = expenseReimburseRequestDto.ExpenseReimbClaimAmount;

            //receiving Iformfile so process it and save to folder =>
            expenseReimburseRequest.Documents = await SaveFileToFolderAndGetName(expenseReimburseRequestDto);

            expenseReimburseRequest.ExpReimReqDate = expenseReimburseRequestDto.ExpReimReqDate;
            expenseReimburseRequest.ExpenseTypeId  = expenseReimburseRequestDto.ExpenseTypeId;
            expenseReimburseRequest.ProjectId      = expenseReimburseRequestDto.ProjectId;
            expenseReimburseRequest.SubProjectId   = expenseReimburseRequestDto.SubProjectId;
            expenseReimburseRequest.WorkTaskId     = expenseReimburseRequestDto.WorkTaskId;

            _context.ExpenseReimburseRequests.Update(expenseReimburseRequest);
            //_context.Entry(expenseReimburseRequestDTO).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExpenseReimburseRequestExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #7
0
        private async Task DepartmentBasedExpReimRequest(ExpenseReimburseRequestDTO expenseReimburseRequestDto, int ExpReimReqId)
        {
            var getEmpClaimApproversAllLevels = _context.ApprovalRoleMaps.Where(a => a.ApprovalGroupId == expenseReimburseRequestDto.EmployeeId).ToList().OrderBy(a => a.ApprovalLevel);

            foreach (ApprovalRoleMap ApprMap in getEmpClaimApproversAllLevels)
            {
                int role_id  = ApprMap.RoleId;
                var approver = _context.Employees.Where(e => e.RoleId == role_id).FirstOrDefault();

                _context.ClaimApprovalStatusTrackers.Add(new ClaimApprovalStatusTracker
                {
                    EmployeeId                = expenseReimburseRequestDto.EmployeeId,
                    PettyCashRequestId        = null,
                    ExpenseReimburseRequestId = ExpReimReqId,
                    DepartmentId              = approver.DepartmentId,
                    ProjectId            = null, //Approver Project Id
                    RoleId               = approver.RoleId,
                    ReqDate              = DateTime.Now,
                    FinalApprovedDate    = null,
                    ApprovalStatusTypeId = (int)ApprovalStatus.Pending //1-Pending, 2-Approved, 3-Rejected
                });

                await _context.SaveChangesAsync();

                //##### 5. Send email to the Approver
                //####################################

                var      approverMailAddress = approver.Email;
                string   subject             = "Expense Claim Approval Request " + expenseReimburseRequestDto.Id.ToString();
                Employee emp = _context.Employees.Find(expenseReimburseRequestDto.EmployeeId);
                var      expenseReimClaimReq = _context.ExpenseReimburseRequests.Find(expenseReimburseRequestDto.Id);
                string   content             = "Expense Reimbursement request Approval sought by " + emp.FirstName + "/nfor the amount of " + expenseReimClaimReq.ExpenseReimbClaimAmount + "/ntowards " + expenseReimClaimReq.ExpenseType;
                var      messagemail         = new Message(new string[] { approverMailAddress }, subject, content);

                await _emailSender.SendEmailAsync(messagemail);

                //repeat for each approver
            }
        }
Example #8
0
        public async Task <ActionResult> PostExpenseReimburseRequest(ExpenseReimburseRequestDTO expenseReimburseRequestDto)
        {
            if (expenseReimburseRequestDto == null)
            {
                return(Conflict(new RespStatus {
                    Status = "Failure", Message = "expenseReimburseRequest Id invalid!"
                }));
            }

            if (expenseReimburseRequestDto.ProjectId != null)
            {
                //Goes to Option 1 (Project)
                await Task.Run(() => ProjectBasedExpReimRequest(expenseReimburseRequestDto));
            }
            else
            {
                //Goes to Option 2 (Department)
                await Task.Run(() => DepartmentBasedExpReimRequest(expenseReimburseRequestDto));
            }

            return(Ok(new RespStatus {
                Status = "Success", Message = "Expense Reimburse Request Created!"
            }));
        }
Example #9
0
        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());
        }
Example #10
0
        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

        //
    }