Exemplo n.º 1
0
        public async Task <StaffApprovalRegRespObj> Handle(SupplierStaffApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var apiResponse = new StaffApprovalRegRespObj {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage()
                    }
                };

                if (request.ApprovalStatus == (int)ApprovalStatus.Revert && request.ReferredStaffId < 1)
                {
                    apiResponse.Status.Message.FriendlyMessage = "Please select staff to revert to";
                    return(apiResponse);
                }

                var currentUserId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                var user          = await _serverRequest.UserDataAsync();

                var supplier = await _repo.GetSupplierAsync(request.TargetId);

                var detail = BuildApprovalDetailObject(request, supplier, user.StaffId);

                var req = new IndentityServerApprovalCommand
                {
                    ApprovalComment = request.ApprovalComment,
                    ApprovalStatus  = request.ApprovalStatus,
                    TargetId        = request.TargetId,
                    WorkflowToken   = supplier.WorkflowToken,
                    ReferredStaffId = request.ReferredStaffId
                };

                var result = await _serverRequest.StaffApprovalRequestAsync(req);

                if (!result.IsSuccessStatusCode)
                {
                    apiResponse.Status.Message.FriendlyMessage = result.ReasonPhrase;
                    return(apiResponse);
                }

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

                response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData);

                if (!response.Status.IsSuccessful)
                {
                    apiResponse.Status = response.Status;
                    return(apiResponse);
                }

                if (response.ResponseId == (int)ApprovalStatus.Processing)
                {
                    await _detailService.AddUpdateApprovalDetailsAsync(detail);

                    supplier.ApprovalStatusId = (int)ApprovalStatus.Processing;
                    await _repo.UpdateSupplierAsync(supplier);

                    apiResponse.ResponseId          = (int)ApprovalStatus.Processing;
                    apiResponse.Status.IsSuccessful = true;
                    apiResponse.Status.Message      = response.Status.Message;
                    return(apiResponse);
                }

                if (response.ResponseId == (int)ApprovalStatus.Revert)
                {
                    await _detailService.AddUpdateApprovalDetailsAsync(detail);

                    supplier.ApprovalStatusId = (int)ApprovalStatus.Revert;
                    await _repo.UpdateSupplierAsync(supplier);

                    apiResponse.ResponseId          = (int)ApprovalStatus.Revert;
                    apiResponse.Status.IsSuccessful = true;
                    apiResponse.Status.Message      = response.Status.Message;
                    return(apiResponse);
                }

                if (response.ResponseId == (int)ApprovalStatus.Approved)
                {
                    await _detailService.AddUpdateApprovalDetailsAsync(detail);

                    supplier.ApprovalStatusId = (int)ApprovalStatus.Approved;
                    supplier.SupplierNumber   = SupplierNumber.Generate(10);
                    await _repo.UpdateSupplierAsync(supplier);


                    apiResponse.ResponseId          = (int)ApprovalStatus.Approved;
                    apiResponse.Status.IsSuccessful = true;
                    apiResponse.Status.Message      = response.Status.Message;
                    return(apiResponse);
                }

                if (response.ResponseId == (int)ApprovalStatus.Disapproved)
                {
                    await _detailService.AddUpdateApprovalDetailsAsync(detail);

                    supplier.ApprovalStatusId = (int)ApprovalStatus.Disapproved;
                    await _repo.UpdateSupplierAsync(supplier);

                    apiResponse.ResponseId          = (int)ApprovalStatus.Disapproved;
                    apiResponse.Status.IsSuccessful = true;
                    apiResponse.Status.Message      = response.Status.Message;
                    return(apiResponse);
                }

                apiResponse.ResponseId = detail.ApprovalDetailId;
                apiResponse.Status     = response.Status;
                return(apiResponse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <StaffApprovalRegRespObj> Handle(LPOStaffApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var apiResponse = new StaffApprovalRegRespObj {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage()
                    }
                };
                if (request.ApprovalStatus == (int)ApprovalStatus.Revert && request.ReferredStaffId < 1)
                {
                    return(new StaffApprovalRegRespObj
                    {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage {
                                FriendlyMessage = "Please select staff to revert to"
                            }
                        }
                    });
                }

                var currentUserId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                var user          = await _serverRequest.UserDataAsync();

                var currentLPO = await _repo.GetLPOsAsync(request.TargetId);

                List <cor_paymentterms> paymentTerms = await _repo.GetPaymenttermsAsync();

                var detail = BuildApprovalDetailObject(request, currentLPO, user.StaffId);

                var req = new IndentityServerApprovalCommand
                {
                    ApprovalComment = request.ApprovalComment,
                    ApprovalStatus  = request.ApprovalStatus,
                    TargetId        = request.TargetId,
                    WorkflowToken   = currentLPO.WorkflowToken,
                    ReferredStaffId = request.ReferredStaffId
                };

                using (var _trans = await _dataContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        var result = await _serverRequest.StaffApprovalRequestAsync(req);

                        if (!result.IsSuccessStatusCode)
                        {
                            response.Status.Message.FriendlyMessage = result.ReasonPhrase.ToString();
                            return(apiResponse);
                        }

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

                        response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData);

                        if (!response.Status.IsSuccessful)
                        {
                            apiResponse.Status = response.Status;
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Processing)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentLPO.ApprovalStatusId = (int)ApprovalStatus.Processing;
                            await _repo.AddUpdateLPOAsync(currentLPO);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            apiResponse.Status.IsSuccessful = true;
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Revert)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentLPO.ApprovalStatusId = (int)ApprovalStatus.Revert;
                            await _repo.AddUpdateLPOAsync(currentLPO);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Approved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentLPO.ApprovalStatusId = (int)ApprovalStatus.Approved;
                            await _repo.AddUpdateLPOAsync(currentLPO);

                            await _repo.ShareTaxToPhasesIthereIsAsync(currentLPO);

                            await _repo.RemoveLostBidsAndProposals(currentLPO);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            apiResponse.Status.Message.FriendlyMessage = $"Final Approval </br> for this LPO {currentLPO.LPONumber}";
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Disapproved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentLPO.ApprovalStatusId = (int)ApprovalStatus.Disapproved;
                            await _repo.AddUpdateLPOAsync(currentLPO);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            return(apiResponse);
                        }
                    }
                    catch (Exception ex)
                    {
                        await _trans.RollbackAsync();

                        throw ex;
                    }
                    finally { await _trans.DisposeAsync(); }
                }
                apiResponse.ResponseId = detail.ApprovalDetailId;
                apiResponse.Status     = response.Status;
                return(apiResponse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 3
0
        public async Task <StaffApprovalRegRespObj> Handle(BidandTenderStaffApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.ApprovalStatus == (int)ApprovalStatus.Revert && request.ReferredStaffId < 1)
                {
                    return(new StaffApprovalRegRespObj
                    {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage {
                                FriendlyMessage = "Please select staff to revert to"
                            }
                        }
                    });
                }

                var currentUserId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                var user          = await _serverRequest.UserDataAsync();

                var currentBid = await _repo.GetBidAndTender(request.TargetId);

                if (!Validation(currentBid).Status.IsSuccessful)
                {
                    return(Validation(currentBid));
                }


                var _ThisBidLPO = await _repo.GetLPOByNumberAsync(currentBid.LPOnumber);

                if (_ThisBidLPO.WinnerSupplierId > 0 && request.ApprovalStatus != (int)ApprovalStatus.Approved)
                {
                    return(new StaffApprovalRegRespObj
                    {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage {
                                FriendlyMessage = $"Supplier already selected for this LPO {currentBid.LPOnumber}"
                            }
                        }
                    });
                }

                IEnumerable <cor_paymentterms> paymentTerms = await _repo.GetPaymenttermsAsync();

                var detail = BuildApprovalDetailObject(request, currentBid, user.StaffId);

                var req = new IndentityServerApprovalCommand
                {
                    ApprovalComment = request.ApprovalComment,
                    ApprovalStatus  = request.ApprovalStatus,
                    TargetId        = request.TargetId,
                    WorkflowToken   = currentBid.WorkflowToken,
                    ReferredStaffId = request.ReferredStaffId
                };

                using (var _trans = await _dataContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        var result = await _serverRequest.StaffApprovalRequestAsync(req);

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

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

                        response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData);

                        if (!response.Status.IsSuccessful)
                        {
                            return(new StaffApprovalRegRespObj
                            {
                                Status = response.Status
                            });
                        }

                        if (response.ResponseId == (int)ApprovalStatus.Processing)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentBid.ApprovalStatusId = (int)ApprovalStatus.Processing;
                            currentBid.DecisionResult   = (int)DecisionResult.Non_Applicable;

                            var thisBidPaymentTerms = paymentTerms.Where(d => d.BidAndTenderId == currentBid.BidAndTenderId).ToList();
                            currentBid.Paymentterms = thisBidPaymentTerms;

                            await _repo.AddUpdateBidAndTender(currentBid);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Processing,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = response.Status.Message
                                }
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Revert)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentBid.ApprovalStatusId = (int)ApprovalStatus.Revert;
                            currentBid.DecisionResult   = (int)DecisionResult.Non_Applicable;



                            await _repo.AddUpdateBidAndTender(currentBid);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Revert,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = response.Status.Message
                                }
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Approved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentBid.ApprovalStatusId = (int)ApprovalStatus.Approved;
                            currentBid.DecisionResult   = (int)DecisionResult.Win;
                            currentBid.PLPOId           = _ThisBidLPO.PLPOId;
                            var thisBidPaymentTerms = paymentTerms.Where(d => d.BidAndTenderId == currentBid.BidAndTenderId).ToList();
                            currentBid.Paymentterms = thisBidPaymentTerms;
                            await _repo.SendEmailToSuppliersSelectedAsync(currentBid.SupplierId, currentBid.DescriptionOfRequest, _ThisBidLPO.PLPOId);

                            await _repo.AddUpdateBidAndTender(currentBid);

                            var terms = paymentTerms.Where(q => q.BidAndTenderId == currentBid.BidAndTenderId).ToList();
                            foreach (var term in terms)
                            {
                                term.LPOId     = currentBid.PLPOId;
                                term.CompanyId = currentBid.CompanyId;
                                await _repo.AddUpdatePaymentTermsAsync(term);
                            }
                            var otherBids = _dataContext.cor_bid_and_tender.Where(q => q.LPOnumber.Trim().ToLower() == currentBid.LPOnumber.Trim().ToLower() && q.BidAndTenderId != currentBid.BidAndTenderId).ToList();
                            if (otherBids.Count() > 0)
                            {
                                foreach (var otherbid in otherBids)
                                {
                                    otherbid.ApprovalStatusId = (int)ApprovalStatus.Disapproved;
                                    otherbid.DecisionResult   = (int)DecisionResult.Lost;
                                    otherbid.Paymentterms     = _dataContext.cor_paymentterms.Where(q => q.BidAndTenderId == otherbid.BidAndTenderId).ToList();
                                    await _repo.AddUpdateBidAndTender(otherbid);
                                }
                            }
                            var thisBidLpo = _repo.BuildThisBidLPO(_ThisBidLPO, currentBid);
                            await _repo.AddUpdateLPOAsync(thisBidLpo);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Approved,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = new APIResponseMessage {
                                        FriendlyMessage = $"Final Approval </br> Successfully selected Supplier for  {currentBid.LPOnumber}"
                                    }
                                }
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Disapproved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            _ThisBidLPO.JobStatus       = _ThisBidLPO.JobStatus;
                            currentBid.ApprovalStatusId = (int)ApprovalStatus.Disapproved;
                            currentBid.DecisionResult   = (int)DecisionResult.Lost;
                            await _repo.AddUpdateBidAndTender(currentBid);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Disapproved,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = response.Status.Message
                                }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        await _trans.RollbackAsync();

                        throw new Exception("Error Occurerd", new Exception($"{ex.Message}"));
                    }
                    finally { await _trans.DisposeAsync(); }
                }

                return(new StaffApprovalRegRespObj
                {
                    ResponseId = detail.ApprovalDetailId,
                    Status = response.Status
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <StaffApprovalRegRespObj> Handle(PRNStaffApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.ApprovalStatus == (int)ApprovalStatus.Revert && request.ReferredStaffId < 1)
                {
                    return(new StaffApprovalRegRespObj
                    {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage {
                                FriendlyMessage = "Please select staff to revert to"
                            }
                        }
                    });
                }
                var allSuppliers = await _dataContext.cor_supplier.ToListAsync();

                var currentUserId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                var user          = await _serverRequest.UserDataAsync();

                var prn = await _repo.GetPurchaseRequisitionNoteAsync(request.TargetId);

                var detail = BuildApprovalDetailObject(request, prn, user.StaffId);

                var req = new IndentityServerApprovalCommand
                {
                    ApprovalComment = request.ApprovalComment,
                    ApprovalStatus  = request.ApprovalStatus,
                    TargetId        = request.TargetId,
                    WorkflowToken   = prn.WorkflowToken,
                    ReferredStaffId = request.ReferredStaffId
                };

                using (var _trans = await _dataContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        var result = await _serverRequest.StaffApprovalRequestAsync(req);

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

                            response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData2);
                            return(new StaffApprovalRegRespObj
                            {
                                Status = response.Status
                            });
                        }

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

                        response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData);

                        if (!response.Status.IsSuccessful)
                        {
                            return(new StaffApprovalRegRespObj
                            {
                                Status = response.Status
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Processing)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            prn.ApprovalStatusId = (int)ApprovalStatus.Processing;
                            await _repo.AddUpdatePurchaseRequisitionNoteAsync(prn);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Processing,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = response.Status.Message
                                }
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Revert)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            prn.ApprovalStatusId = (int)ApprovalStatus.Revert;
                            await _repo.AddUpdatePurchaseRequisitionNoteAsync(prn);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Revert,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = response.Status.Message
                                }
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Approved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            prn.ApprovalStatusId = (int)ApprovalStatus.Approved;
                            var prnDetails = await _repo.GetPrnDetailsByPrnId(prn.PurchaseReqNoteId);

                            EmailMessageObj email = new EmailMessageObj {
                                ToAddresses = new List <EmailAddressObj>()
                            };

                            foreach (var item in prnDetails)
                            {
                                item.LPONumber = _repo.LpoNubmer(prn.PurchaseReqNoteId + item.PRNDetailsId);

                                if (await _repo.AddUpdatePrnDetailsAsync(item))
                                {
                                    var lpoObject = _repo.BuildLPODomianObject(item, prn.DeliveryLocation, prn.ExpectedDeliveryDate ?? DateTime.Today);
                                    item.PurchaseReqNoteId = prn.PurchaseReqNoteId;
                                    if (await _repo.AddUpdateLPOAsync(lpoObject))
                                    {
                                        var SuggestedSupplierList = lpoObject.SupplierIds.Split(',').Select(int.Parse);
                                        foreach (var supplierId in SuggestedSupplierList)
                                        {
                                            var supplier = await _supRepo.GetSupplierAsync(supplierId);

                                            if (supplier != null)
                                            {
                                                var bidAndTenderObject = _repo.BuildBidAndTenderDomianObject(supplier, lpoObject, prn.DepartmentId, item);
                                                bidAndTenderObject.CompanyId         = prn.CompanyId;
                                                bidAndTenderObject.PurchaseReqNoteId = prn.PurchaseReqNoteId;
                                                if (await _repo.AddUpdateBidAndTender(bidAndTenderObject))
                                                {
                                                    email.ToAddresses.Add(new EmailAddressObj {
                                                        Address = supplier.Email, Name = supplier.Name
                                                    });
                                                }
                                            }
                                        }
                                        var otherSupplierbid = _repo.BuildBidAndTenderDomianObjectForNonSelectedSuppliers(lpoObject, prn.DepartmentId, item);
                                        await _repo.AddUpdateBidAndTender(otherSupplierbid);
                                    }
                                }
                            }
                            email.ToAddresses.Distinct();
                            await _repo.SendEmailToSuppliersAsync(email, prn.Description);

                            await _repo.AddUpdatePurchaseRequisitionNoteAsync(prn);

                            await _repo.SendEmailToInitiatingStaffAsync(prn);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Approved,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = new APIResponseMessage {
                                        FriendlyMessage = "Final Approval </br> Successfully created Supplier Bids"
                                    }
                                }
                            });
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Disapproved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            prn.ApprovalStatusId = (int)ApprovalStatus.Disapproved;
                            await _repo.AddUpdatePurchaseRequisitionNoteAsync(prn);

                            await _trans.CommitAsync();

                            return(new StaffApprovalRegRespObj
                            {
                                ResponseId = (int)ApprovalStatus.Disapproved,
                                Status = new APIResponseStatus {
                                    IsSuccessful = true, Message = response.Status.Message
                                }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        await _trans.RollbackAsync();

                        throw ex;
                    }
                    finally { await _trans.DisposeAsync(); }
                }

                return(new StaffApprovalRegRespObj
                {
                    ResponseId = detail.ApprovalDetailId,
                    Status = response.Status
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <StaffApprovalRegRespObj> Handle(PaymentRequestStaffApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var apiResponse = new StaffApprovalRegRespObj {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage()
                    }
                };
                if (request.ApprovalStatus == (int)ApprovalStatus.Revert && request.ReferredStaffId < 1)
                {
                    return(new StaffApprovalRegRespObj
                    {
                        Status = new APIResponseStatus {
                            IsSuccessful = false, Message = new APIResponseMessage {
                                FriendlyMessage = "Please select staff to revert to"
                            }
                        }
                    });
                }

                var currentUserId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                var user          = await _serverRequest.UserDataAsync();

                var currentInvoice = await _invoice.GetInvoiceAsync(request.TargetId);

                var detail = BuildApprovalDetailObject(request, currentInvoice, user.StaffId);

                var req = new IndentityServerApprovalCommand
                {
                    ApprovalComment = request.ApprovalComment,
                    ApprovalStatus  = request.ApprovalStatus,
                    TargetId        = request.TargetId,
                    WorkflowToken   = currentInvoice.WorkflowToken,
                    ReferredStaffId = request.ReferredStaffId
                };

                using (var _trans = await _dataContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        var result = await _serverRequest.StaffApprovalRequestAsync(req);

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

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

                        response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData);

                        if (!response.Status.IsSuccessful)
                        {
                            apiResponse.Status = response.Status;
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Processing)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentInvoice.ApprovalStatusId = (int)ApprovalStatus.Processing;
                            await _invoice.CreateUpdateInvoiceAsync(currentInvoice);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            apiResponse.Status.IsSuccessful = true;
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Revert)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentInvoice.ApprovalStatusId = (int)ApprovalStatus.Revert;
                            await _invoice.CreateUpdateInvoiceAsync(currentInvoice);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Approved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentInvoice.ApprovalStatusId = (int)ApprovalStatus.Approved;

                            var thisInvoicePhase = await _repo.GetSinglePaymenttermAsync(currentInvoice.PaymentTermId);

                            thisInvoicePhase.PaymentStatus    = (int)PaymentStatus.Paid;
                            thisInvoicePhase.CompletionDate   = DateTime.Now;
                            thisInvoicePhase.ApprovalStatusId = (int)ApprovalStatus.Approved;
                            await _repo.AddUpdatePaymentTermsAsync(thisInvoicePhase);

                            currentInvoice.AmountPaid       = currentInvoice.Amount;
                            currentInvoice.ApprovalStatusId = (int)ApprovalStatus.Approved;
                            await _invoice.CreateUpdateInvoiceAsync(currentInvoice);

                            var paymentResp = await _invoice.TransferPaymentAsync(currentInvoice);

                            if (!paymentResp.Status.IsSuccessful)
                            {
                                await _trans.CommitAsync();

                                apiResponse.Status.Message.FriendlyMessage = $"Final Approval Successful <br/>But payment not made 'Reason (s): {paymentResp.Status.Message.FriendlyMessage}";
                                return(apiResponse);
                            }
                            await _repo.SendEmailToSupplierDetailingPaymentAsync(currentInvoice, thisInvoicePhase.Phase);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            apiResponse.Status.Message.FriendlyMessage = $"Final Approval Successful";
                            return(apiResponse);
                        }
                        if (response.ResponseId == (int)ApprovalStatus.Disapproved)
                        {
                            await _detailService.AddUpdateApprovalDetailsAsync(detail);

                            currentInvoice.ApprovalStatusId = (int)ApprovalStatus.Disapproved;

                            await _invoice.CreateUpdateInvoiceAsync(currentInvoice);

                            await _trans.CommitAsync();

                            apiResponse.Status = response.Status;
                            return(apiResponse);
                        }
                    }
                    catch (Exception ex)
                    {
                        await _trans.RollbackAsync();

                        throw ex;
                    }
                    finally { await _trans.DisposeAsync(); }
                }
                apiResponse.ResponseId = detail.ApprovalDetailId;
                apiResponse.Status     = response.Status;
                return(apiResponse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }