Example #1
0
        public async Task <bool> GetAwaitingApproval()
        {
            try
            {
                var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                if (!result.IsSuccessStatusCode)
                {
                    return(false);
                }

                var data = await result.Content.ReadAsStringAsync();

                var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                if (res == null)
                {
                    return(false);
                }

                if (res.workflowTasks.Count() > 0)
                {
                    var targetIds = res.workflowTasks.Select(x => x.TargetId).ToList();
                    var tokens    = res.workflowTasks.Select(d => d.WorkflowToken).ToList();

                    disburseCount = await GetLoansAwaitingApprovalAsync(targetIds, tokens);

                    creditAppraisalCount = await GetLoanApplicationAwaitingApprovalAsync(targetIds, tokens);

                    investmentCount1 = await GetInvestmentAwaitingApprovalAsync(targetIds, tokens);

                    liquidationCount1 = await GetLiquidationAwaitingApprovalAsync(targetIds, tokens);

                    collectionCount1 = await GetCollectionAwaitingApprovalAsync(targetIds, tokens);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
            public async Task <DashboardCountResp> Handle(DashboardCountQuery request, CancellationToken cancellationToken)
            {
                var response = new DashboardCountResp {
                    DashboardCount = new DashboardCount(), Status = new APIResponseStatus {
                        IsSuccessful = true, Message = new APIResponseMessage()
                    }
                };

                try
                {
                    var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                    var data = await result.Content.ReadAsStringAsync();

                    var res       = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);
                    var tokens    = res.workflowTasks.Select(s => s.WorkflowToken).ToList();
                    var targetIds = res.workflowTasks.Select(x => x.TargetId).ToList();

                    response.DashboardCount.SupplierCount = _supplier.GetSupplierDataAwaitingApprovalAsync(targetIds, tokens).Result.Count();

                    response.DashboardCount.PRNCount = _purchase.GetPRNAwaitingApprovalAsync(targetIds, tokens).Result.Count();

                    response.DashboardCount.BIDCount = _purchase.GetBidAndTenderAwaitingApprovalAsync(targetIds, tokens).Result.Count();

                    response.DashboardCount.LPOCount = _purchase.GetLPOAwaitingApprovalAsync(targetIds, tokens).Result.Count();

                    response.DashboardCount.PaymentsCount = _purchase.GetRequestedPaymentsAwaitingApprovalAsync(targetIds, tokens).Result.Count();

                    response.DashboardCount.PayablesCount = 0;//_dataContext.purch_requisitionnote.Count(r => tokens.Contains(r.WorkflowToken) && targetIds.Contains(r.PurchaseReqNoteId));
                    return(response);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public async Task <ProposalPaymentRespObj> Handle(GetRequestedPaymentsAwaitingApprovalQuery request, CancellationToken cancellationToken)
            {
                try
                {
                    var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                    if (!result.IsSuccessStatusCode)
                    {
                        var data1 = await result.Content.ReadAsStringAsync();

                        var res1 = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data1);
                        return(new ProposalPaymentRespObj
                        {
                            Status = new APIResponseStatus
                            {
                                IsSuccessful = false,
                                Message = new APIResponseMessage {
                                    FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}"
                                }
                            }
                        });
                    }

                    var data = await result.Content.ReadAsStringAsync();

                    var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                    if (res == null)
                    {
                        return(new ProposalPaymentRespObj
                        {
                            Status = res.Status
                        });
                    }

                    if (res.workflowTasks.Count() < 1)
                    {
                        return(new ProposalPaymentRespObj
                        {
                            Status = new APIResponseStatus
                            {
                                IsSuccessful = true,
                                Message = new APIResponseMessage
                                {
                                    FriendlyMessage = "No Pending Approval"
                                }
                            }
                        });
                    }
                    CompanyStructureRespObj _Department = new CompanyStructureRespObj();
                    _Department = await _serverRequest.GetAllCompanyStructureAsync();

                    var paymentTerms = await _repo.GetPaymenttermsAsync();

                    var bids = await _repo.GetAllBidAndTender();

                    var staffRequestedPaymentsawaiting = await _repo.GetRequestedPaymentsAwaitingApprovalAsync(res.workflowTasks.Select(x => x.TargetId).ToList(), res.workflowTasks.Select(s => s.WorkflowToken).ToList());

                    //var RequestedPaymentss = await _repo.GetAllPurchaseRequisitionNoteAsync();


                    var payments = staffRequestedPaymentsawaiting.Select(d => new ProposalPayment
                    {
                        PaymentTermId            = d.PaymentTermId,
                        Completion               = d.Completion,
                        Amount                   = d.Amount,
                        NetAmount                = d.NetAmount,
                        Payment                  = d.Payment,
                        PaymentStatus            = d.PaymentStatus,
                        PaymentStatusName        = Convert.ToString(d.PaymentStatus),
                        Phase                    = d.Phase,
                        PhaseName                = Convert.ToString((PaymentTermsPhase)d.Phase),
                        ProjectStatusDescription = d.ProjectStatusDescription,
                        Status                   = d.Status,
                        StatusName               = Convert.ToString((JobProgressStatus)d.Status),
                        Comment                  = d.Comment,
                        AmountPaid               = 0,
                        LPOId                    = d.LPOId,
                        AmountPayable            = d.NetAmount,
                        PaymentOutstanding       = d.NetAmount,
                    }).ToList();

                    if (payments.Count() > 0)
                    {
                        foreach (var item in payments)
                        {
                            var alreadyPaidPhases = _dataContext.cor_paymentterms.Where(a => a.LPOId == item.LPOId && a.ProposedBy == (int)Proposer.STAFF && a.PaymentStatus == (int)PaymentStatus.Paid).ToList();
                            if (alreadyPaidPhases.Count() > 0)
                            {
                                item.AmountPaid         = alreadyPaidPhases.Sum(f => f.NetAmount);
                                item.PaymentOutstanding = (item.AmountPayable - alreadyPaidPhases.Sum(f => f.NetAmount));
                            }
                        }
                    }
                    return(new ProposalPaymentRespObj
                    {
                        ProposalPayment = payments,
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = true,
                            Message = new APIResponseMessage {
                                FriendlyMessage = staffRequestedPaymentsawaiting.Count() > 0 ? "" : "Search Complete : No Payment awaitingt approvals"
                            }
                        }
                    });
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
            }
            public async Task <PaymentApprovalRespObj> Handle(GetPaymentProposalAwaitingApprovalQuery request, CancellationToken cancellationToken)
            {
                try
                {
                    var apiResponse = new PaymentApprovalRespObj {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage()
                        }
                    };

                    var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                    if (!result.IsSuccessStatusCode)
                    {
                        apiResponse.Status.Message.FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}";
                        return(apiResponse);
                    }
                    var data = await result.Content.ReadAsStringAsync();

                    var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                    if (res == null)
                    {
                        apiResponse.Status = res.Status;
                        return(apiResponse);
                    }
                    var banks = await _financeServer.GetAllBanksAsync();

                    if (res.workflowTasks.Count() < 1)
                    {
                        apiResponse.Status.IsSuccessful            = true;
                        apiResponse.Status.Message.FriendlyMessage = "No Pending Approval";
                        return(apiResponse);
                    }

                    var pendingTaskIds    = res.workflowTasks.Select(x => x.TargetId).ToList();
                    var pendingTaskTokens = res.workflowTasks.Select(s => s.WorkflowToken).ToList();
                    var inv = await _repo.GetInvoiceAwaitingApprovalAsync(pendingTaskIds, pendingTaskTokens);

                    if (inv.Count() == 0)
                    {
                        apiResponse.Status.IsSuccessful            = true;
                        apiResponse.Status.Message.FriendlyMessage = "No Payments awaiting approvals";
                        return(apiResponse);
                    }
                    var phases = _dataContext.cor_paymentterms.Where(a => inv.Select(e => e.PaymentTermId).Contains(a.PaymentTermId) && a.ProposedBy == (int)Proposer.STAFF).ToList();

                    var result1 = (from a in phases
                                   join d in inv on a.PaymentTermId equals d.PaymentTermId
                                   select new InvoiceObj
                    {
                        AmountPaid = d.AmountPayable,
                        AmountPayable = d.AmountPayable,
                        InvoiceId = d.InvoiceId,
                        DescriptionOfRequest = d.Description,
                        ExpectedDeliveryDate = d.DeliveryDate,
                        Location = d.Address,
                        LPONumber = d.LPONumber,
                        PaymentOutstanding = 0,
                        InvoiceNumber = d.InvoiceNumber,
                        PaymentTermId = d.PaymentTermId,
                        Amount = d.Amount,
                        RequestDate = d.RequestDate,
                        Workflowtoken = d.WorkflowToken,
                        SupplierId = d.SupplierId,
                        LpoId = a.LPOId,
                        PaymentBankId = d.SupplierBankId
                    }).ToList();


                    if (result1.Count() > 0)
                    {
                        foreach (var item in result1)
                        {
                            var alreadyPaidPhases = _dataContext.cor_paymentterms.Where(a => a.PaymentTermId == item.PaymentTermId && a.ProposedBy == (int)Proposer.STAFF && a.PaymentStatus == (int)PaymentStatus.Paid).ToList();
                            item.BankName           = banks.bank.FirstOrDefault(q => q.BankGlId == item.SupplierBankId)?.BankName;
                            item.Supplier           = _dataContext.cor_supplier.FirstOrDefault(q => q.SupplierId == item.SupplierId)?.Name;
                            item.AmountPaid         = alreadyPaidPhases.Sum(f => f.NetAmount);
                            item.PaymentOutstanding = (item.AmountPayable - alreadyPaidPhases.Sum(f => f.NetAmount));
                        }
                        apiResponse.PaymentApprovals = result1;
                    }


                    //resp.Add(item);

                    apiResponse.Status.IsSuccessful            = true;
                    apiResponse.Status.Message.FriendlyMessage = inv.Count() < 1 ? "No Payments awaiting approvals" : null;
                    return(apiResponse);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
Example #5
0
        public async Task <BidAndTenderRespObj> Handle(GetBidAndTenderAwaitingApprovalQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var response = new BidAndTenderRespObj {
                    Status = new APIResponseStatus {
                        Message = new APIResponseMessage()
                    }
                };

                CompanyStructureRespObj _Department = new CompanyStructureRespObj();
                _Department = await _serverRequest.GetAllCompanyStructureAsync();

                var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                if (!result.IsSuccessStatusCode)
                {
                    var data1 = await result.Content.ReadAsStringAsync();

                    var res1 = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data1);
                    response.Status.IsSuccessful            = false;
                    response.Status.Message.FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}";
                    return(response);
                }

                var data = await result.Content.ReadAsStringAsync();

                var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                if (res == null)
                {
                    response.Status = res.Status;
                    return(response);
                }

                if (res.workflowTasks.Count() < 1)
                {
                    response.Status.IsSuccessful            = true;
                    response.Status.Message.FriendlyMessage = "No Pending Approval";
                    return(response);
                }
                var staffBidawaiting = await _repo.GetBidAndTenderAwaitingApprovalAsync(res.workflowTasks.Select(x => x.TargetId).ToList(), res.workflowTasks.Select(s => s.WorkflowToken).ToList());

                var paymentTerms = await _repo.GetPaymenttermsAsync();

                var requisitions = await _repo.GetAllPurchaseRequisitionNoteAsync();

                response.BidAndTenders = staffBidawaiting?.Where(w => w.ApprovalStatusId != (int)ApprovalStatus.Disapproved).Select(d => new BidAndTenderObj
                {
                    BidAndTenderId               = d.BidAndTenderId,
                    AmountApproved               = d.AmountApproved,
                    DateSubmitted                = d.DateSubmitted,
                    DecisionResult               = d.DecisionResult,
                    DescriptionOfRequest         = d?.DescriptionOfRequest,
                    Location                     = d?.Location,
                    LPOnumber                    = d?.LPOnumber,
                    ProposalTenderUploadType     = d.ProposalTenderUploadType,
                    ProposalTenderUploadPath     = d.ProposalTenderUploadPath,
                    ProposalTenderUploadName     = d.ProposalTenderUploadName,
                    ProposalTenderUploadFullPath = d.ProposalTenderUploadFullPath,
                    ProposedAmount               = d.ProposedAmount.ToString(),
                    RequestDate                  = d.RequestDate,
                    RequestingDepartment         = d.RequestingDepartment,
                    SupplierName                 = d?.SupplierName,
                    Suppliernumber               = d?.Suppliernumber,
                    DecisionReultName            = Convert.ToString((DecisionResult)d.DecisionResult),
                    Quantity                     = d.Quantity,
                    SupplierId                   = d.SupplierId,
                    PLPOId                   = d.PLPOId,
                    Total                    = d.Total,
                    ApprovalStatusId         = d.ApprovalStatusId,
                    WorkflowToken            = d.WorkflowToken,
                    ExpectedDeliveryDate     = d.ExpectedDeliveryDate,
                    SupplierAddress          = d.SupplierAddress,
                    PRNId                    = d.PurchaseReqNoteId,
                    RequestingDepartmentName = _Department.companyStructures.FirstOrDefault(e => e.CompanyStructureId == d.RequestingDepartment)?.Name,
                    RequisitionNotes         = _dataContext.purch_requisitionnote.Where(q => q.PurchaseReqNoteId == d.PurchaseReqNoteId).Select(w => new RequisitionNoteObj {
                        PurchaseReqNoteId    = w.PurchaseReqNoteId,
                        ApprovalStatusId     = w.ApprovalStatusId,
                        Comment              = w.Comment,
                        DeliveryLocation     = w.DeliveryLocation,
                        DepartmentId         = w.DepartmentId,
                        Description          = w.Description,
                        DocumentNumber       = w.DocumentNumber,
                        ExpectedDeliveryDate = w.ExpectedDeliveryDate,
                        IsFundAvailable      = w.IsFundAvailable,
                        PRNNumber            = w.PRNNumber,
                        Total       = w.Total,
                        RequestDate = w.CreatedOn,
                        RequestBy   = w.RequestBy,
                        StatusName  = Convert.ToString((ApprovalStatus)d.ApprovalStatusId),
                    }).ToList(),
                    PaymentTerms = paymentTerms.Where(s => s.BidAndTenderId == d.BidAndTenderId).Select(a => new PaymentTermsObj
                    {
                        BidAndTenderId           = a.BidAndTenderId,
                        Comment                  = a.Comment,
                        Completion               = a.Completion,
                        Amount                   = a.Amount,
                        NetAmount                = a.NetAmount,
                        Payment                  = a.Payment,
                        PaymentStatus            = a.PaymentStatus,
                        PaymentStatusName        = Convert.ToString((PaymentStatus)a.PaymentStatus),
                        PaymentTermId            = a.PaymentTermId,
                        Phase                    = a.Phase,
                        ProjectStatusDescription = a.ProjectStatusDescription,
                        Status                   = a.Status,
                        ProposedBy               = a.ProposedBy,

                        StatusName = Convert.ToString((JobProgressStatus)a.Status),
                    }).ToList(),
                }).ToList() ?? new List <BidAndTenderObj>();

                response.Status = new APIResponseStatus
                {
                    IsSuccessful = true,
                    Message      = new APIResponseMessage
                    {
                        FriendlyMessage = staffBidawaiting.Count() > 0 ? null : "Search Complete! No Record found"
                    }
                };


                return(response);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public async Task <SupplierRespObj> Handle(GetAllSupplierDataAwaitingApprovalQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                if (!result.IsSuccessStatusCode)
                {
                    return(new SupplierRespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = false,
                            Message = new APIResponseMessage {
                                FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}"
                            }
                        }
                    });
                }

                var data = await result.Content.ReadAsStringAsync();

                var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                if (res == null)
                {
                    return(new SupplierRespObj
                    {
                        Status = res.Status
                    });
                }

                if (res.workflowTasks.Count() < 1)
                {
                    return(new SupplierRespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = true,
                            Message = new APIResponseMessage
                            {
                                FriendlyMessage = "No Pending Approval"
                            }
                        }
                    });
                }

                var supplierType = await _repo.GetAllSupplierTypeAsync();

                var supplier = await _repo.GetSupplierDataAwaitingApprovalAsync(res.workflowTasks.Select(x => x.TargetId).ToList(), res.workflowTasks.Select(d => d.WorkflowToken).ToList());


                return(new SupplierRespObj
                {
                    Suppliers = supplier.Select(d => new SupplierObj {
                        Name = d.Name,
                        SupplierTypeId = d.SupplierTypeId,
                        Active = d.Active,
                        Address = d.Address,
                        ApprovalStatusId = d.ApprovalStatusId,
                        CountryId = d.CountryId,
                        Email = d.Email,
                        ExpiryDate = d.ExpiryDate,
                        CreatedBy = d.CreatedBy,
                        CreatedOn = d.CreatedOn,
                        Deleted = d.Deleted,
                        HaveWorkPrintPermit = d.HaveWorkPrintPermit,
                        Passport = d.Passport,
                        PhoneNo = d.PhoneNo,
                        PostalAddress = d.PostalAddress,
                        RegistrationNo = d.RegistrationNo,
                        SupplierId = d.SupplierId,
                        SupplierNumber = d.SupplierNumber,
                        SupplierTypeName = supplierType.FirstOrDefault(s => s.SupplierTypeId == d.SupplierTypeId)?.SupplierTypeName ?? "",
                        TaxIDorVATID = d.TaxIDorVATID,
                        UpdatedBy = d.UpdatedBy,
                        Website = d.Website,
                        UpdatedOn = d.UpdatedOn,
                        WorkflowToken = d.WorkflowToken,
                        StatusName = Convert.ToString((ApprovalStatus)d.ApprovalStatusId),
                        Particulars = d.Particulars,
                    }).ToList(),
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = true,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = supplier.Count() < 1 ? "No supplier detail awaiting approvals" : null
                        }
                    }
                });
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
Example #7
0
        public async Task <ActionResult <LoanApplicationRespObj> > GetLoanApplicationForAppraisalAsync()
        {
            try
            {
                var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                if (!result.IsSuccessStatusCode)
                {
                    return(new LoanApplicationRespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = false,
                            Message = new APIResponseMessage {
                                FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}"
                            }
                        }
                    });
                }

                var data = await result.Content.ReadAsStringAsync();

                var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                if (res == null)
                {
                    return(new LoanApplicationRespObj
                    {
                        Status = res.Status
                    });
                }

                if (res.workflowTasks.Count() < 1)
                {
                    return(new LoanApplicationRespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = true,
                            Message = new APIResponseMessage
                            {
                                FriendlyMessage = "No Pending Approval"
                            }
                        }
                    });
                }
                var loanApplication = await GetLoanApplicationAwaitingApprovalAsync(res.workflowTasks.Select(x => x.TargetId).ToList(), res.workflowTasks.Select(d => d.WorkflowToken).ToList());


                return(new LoanApplicationRespObj
                {
                    LoanApplications = loanApplication.Select(a => new LoanApplicationObj
                    {
                        ApplicationDate = (DateTime)a.CreatedOn,
                        ApprovedAmount = a.ApprovedAmount,
                        ApprovedProductId = a.ApprovedProductId,
                        ApprovedRate = a.ApprovedRate,
                        ApprovedTenor = a.ApprovedTenor,
                        CurrencyId = a.CurrencyId,
                        CustomerId = a.CustomerId,
                        EffectiveDate = a.EffectiveDate,
                        ExchangeRate = a.ExchangeRate,
                        HasDoneChecklist = a.HasDoneChecklist,
                        MaturityDate = a.MaturityDate,
                        ProposedAmount = a.ProposedAmount,
                        ProposedProductId = a.ProposedProductId,
                        ProposedRate = a.ProposedRate,
                        ProposedTenor = a.ProposedTenor,
                        LoanApplicationId = a.LoanApplicationId,
                        //CurrencyName = a.cor_currency.CurrencyName,
                        CustomerName = _dataContext.credit_loancustomer.FirstOrDefault(x => x.CustomerId == a.CustomerId).FirstName + " " + _dataContext.credit_loancustomer.FirstOrDefault(x => x.CustomerId == a.CustomerId).LastName,
                        ApprovedProductName = _dataContext.credit_product.FirstOrDefault(x => x.ProductId == a.ApprovedProductId).ProductName,
                        ProposedProductName = _dataContext.credit_product.FirstOrDefault(x => x.ProductId == a.ProposedProductId).ProductName,
                        LoanApplicationStatusId = a.LoanApplicationStatusId,
                        CompanyId = a.CompanyId,
                        ApplicationRefNumber = a.ApplicationRefNumber,
                        CreditScore = a.Score,
                        ProbabilityOfDefault = a.PD,
                        WorkflowToken = a.WorkflowToken
                    }).ToList(),
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = true,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = loanApplication.Count() < 1 ? "No Loan Application awaiting approvals" : null
                        }
                    }
                });
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public async Task <LPORespObj> Handle(GetLPOAwaitingApprovalQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                if (!result.IsSuccessStatusCode)
                {
                    var data1 = await result.Content.ReadAsStringAsync();

                    var res1 = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data1);
                    return(new LPORespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = false,
                            Message = new APIResponseMessage {
                                FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}"
                            }
                        }
                    });
                }

                var data = await result.Content.ReadAsStringAsync();

                var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                if (res == null)
                {
                    return(new LPORespObj
                    {
                        Status = res.Status
                    });
                }

                if (res.workflowTasks.Count() < 1)
                {
                    return(new LPORespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = true,
                            Message = new APIResponseMessage
                            {
                                FriendlyMessage = "No Pending Approval"
                            }
                        }
                    });
                }

                CompanyStructureRespObj _Department = new CompanyStructureRespObj();
                _Department = await _serverRequest.GetAllCompanyStructureAsync();

                var paymentTerms = await _repo.GetPaymenttermsAsync();

                var bids = await _repo.GetAllBidAndTender();

                var staffLPOawaiting = await _repo.GetLPOAwaitingApprovalAsync(res.workflowTasks.Select(x => x.TargetId).ToList(), res.workflowTasks.Select(s => s.WorkflowToken).ToList());

                var lpos = await _repo.GetAllPurchaseRequisitionNoteAsync();

                return(new LPORespObj
                {
                    LPOs = staffLPOawaiting?.Select(d => new LPOObj
                    {
                        AmountPayable = d.AmountPayable,
                        ApprovalStatusId = d.ApprovalStatusId,
                        BidAndTenderId = d.BidAndTenderId,
                        DeliveryDate = d.DeliveryDate,
                        Description = d.Description,
                        GrossAmount = d.GrossAmount,
                        JobStatus = d.JobStatus,
                        JobStatusName = Convert.ToString((JobProgressStatus)d.JobStatus),
                        LPONumber = d.LPONumber,
                        Name = d.Name,
                        PLPOId = d.PLPOId,
                        RequestDate = d.RequestDate,
                        SupplierAddress = d.SupplierAddress,
                        SupplierId = d.SupplierIds,
                        SupplierNumber = d.SupplierNumber,
                        Tax = d.Tax,
                        Total = d.Total,
                        WorkflowToken = d.WorkflowToken,
                        WinnerSupplierId = d.WinnerSupplierId,
                        BidAndTender = bids.Where(w => w.BidAndTenderId == d.BidAndTenderId).Select(s => new BidAndTenderObj
                        {
                            BidAndTenderId = s.BidAndTenderId,
                            AmountApproved = s.AmountApproved,
                            ApprovalStatusId = s.ApprovalStatusId,
                            DateSubmitted = s.DateSubmitted,
                            DecisionResult = s.DecisionResult,
                            DecisionReultName = Convert.ToString((DecisionResult)s.DecisionResult),
                            DescriptionOfRequest = s.DescriptionOfRequest,
                            ExpectedDeliveryDate = s.ExpectedDeliveryDate,
                            Location = s.Location,
                            LPOnumber = s.LPOnumber,
                            PLPOId = s.PLPOId,
                            PRNId = s.PLPOId,
                            ProposedAmount = s.ProposedAmount.ToString(),
                            Quantity = s.Quantity,
                            RequestDate = s.RequestDate,
                            RequestingDepartmentName = _Department.companyStructures.FirstOrDefault(e => e.CompanyStructureId == s.RequestingDepartment)?.Name,
                            SupplierAddress = s.SupplierAddress,
                            Suppliernumber = s.Suppliernumber,
                            SupplierName = s.SupplierName,
                            Total = s.Total,
                        }).ToList(),
                        RequisitionNotes = lpos.Where(w => w.PurchaseReqNoteId == d.PurchaseReqNoteId).Select(q => new RequisitionNoteObj
                        {
                            PurchaseReqNoteId = q.PurchaseReqNoteId,
                            ApprovalStatusId = q.ApprovalStatusId,
                            Comment = q.Comment,
                            DeliveryLocation = q.DeliveryLocation,
                            DepartmentId = q.DepartmentId,
                            Description = q.Description,
                            DocumentNumber = q.DocumentNumber,
                            ExpectedDeliveryDate = q.ExpectedDeliveryDate,
                            IsFundAvailable = q.IsFundAvailable,
                            PRNNumber = q.PRNNumber,
                            RequestBy = q.RequestBy,
                            Total = q.Total,
                            RequestDate = q.CreatedOn,
                        }).ToList(),
                        PaymentTerms = paymentTerms.Where(e => e.BidAndTenderId == d.BidAndTenderId).Select(p => new PaymentTermsObj
                        {
                            BidAndTenderId = p.BidAndTenderId,
                            Comment = p.Comment,
                            Completion = p.Completion,
                            Amount = p.Amount,
                            NetAmount = p.NetAmount,
                            Payment = p.Payment,
                            PaymentStatus = p.PaymentStatus,
                            PaymentTermId = p.PaymentTermId,
                            Phase = p.Phase,
                            ProjectStatusDescription = p.ProjectStatusDescription,
                            Status = p.Status,
                            ProposedBy = p.ProposedBy,
                            StatusName = Convert.ToString((JobProgressStatus)p.Status),
                            PaymentStatusName = Convert.ToString((PaymentStatus)p.PaymentStatus),
                        }).ToList(),
                    }).ToList() ?? new List <LPOObj>(),
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = true,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = staffLPOawaiting.Count() > 0 ? null : "Search Complete! No Record found"
                        }
                    }
                });
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public async Task <RequisitionNoteRespObj> Handle(GetAllPRNAwaitingApprovalQuery request, CancellationToken cancellationToken)
        {
            try
            {
                var apiResponse = new RequisitionNoteRespObj {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage()
                    }
                };

                var result = await _serverRequest.GetAnApproverItemsFromIdentityServer();

                if (!result.IsSuccessStatusCode)
                {
                    apiResponse.Status.Message.FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}";
                    return(apiResponse);
                }
                var data = await result.Content.ReadAsStringAsync();

                var res = JsonConvert.DeserializeObject <WorkflowTaskRespObj>(data);

                if (res == null)
                {
                    apiResponse.Status = res.Status;
                    return(apiResponse);
                }

                if (res.workflowTasks.Count() < 1)
                {
                    apiResponse.Status.IsSuccessful            = true;
                    apiResponse.Status.Message.FriendlyMessage = "No Pending Approval";
                    return(apiResponse);
                }

                var _Department = await _serverRequest.GetAllCompanyStructureAsync();

                var pendingTaskIds    = res.workflowTasks.Select(x => x.TargetId).ToList();
                var pendingTaskTokens = res.workflowTasks.Select(s => s.WorkflowToken).ToList();
                var prn = await _repo.GetPRNAwaitingApprovalAsync(pendingTaskIds, pendingTaskTokens);

                apiResponse.RequisitionNotes = prn.Select(d => new RequisitionNoteObj {
                    ApprovalStatusId     = d.ApprovalStatusId,
                    Comment              = d.Comment,
                    DeliveryLocation     = d.DeliveryLocation,
                    DepartmentId         = d.DepartmentId,
                    Description          = d.Description,
                    DocumentNumber       = d.DocumentNumber,
                    ExpectedDeliveryDate = d.ExpectedDeliveryDate,
                    IsFundAvailable      = d.IsFundAvailable,
                    PurchaseReqNoteId    = d.PurchaseReqNoteId,
                    RequestBy            = d.RequestBy,
                    StatusName           = Convert.ToString((ApprovalStatus)d.ApprovalStatusId),
                    Total          = d.Total,
                    RequestDate    = d.CreatedOn,
                    WorkflowToken  = d.WorkflowToken,
                    PRNNumber      = d.PRNNumber,
                    DepartmentName = _Department.companyStructures.FirstOrDefault(c => c.CompanyStructureId == d.DepartmentId)?.Name,
                    DetailsCount   = d.purch_prndetails.Count()
                }).ToList();

                apiResponse.Status.IsSuccessful            = true;
                apiResponse.Status.Message.FriendlyMessage = prn.Count() < 1 ? "No PRN awaiting approvals" : null;
                return(apiResponse);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }