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 <ActionResult <ApprovalRegRespObj> > GoForApproval([FromBody] ApprovalObj entity) { try { using (var _trans = _dataContext.Database.BeginTransaction()) { try { var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _serverRequest.UserDataAsync(); var loan = _dataContext.credit_loanreviewapplication.Find(entity.TargetId); var req = new IndentityServerApprovalCommand { ApprovalComment = entity.Comment, ApprovalStatus = entity.ApprovalStatusId, TargetId = entity.TargetId, WorkflowToken = loan.WorkflowToken, ReferredStaffId = entity.ReferredStaffId }; var previousDetails = _dataContext.cor_approvaldetail.Where(x => x.WorkflowToken.Contains(loan.WorkflowToken) && x.TargetId == entity.TargetId).ToList(); var lastDate = loan.CreatedOn; if (previousDetails.Count() > 0) { lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date; } var details = new cor_approvaldetail { Comment = entity.Comment, Date = DateTime.Now, ArrivalDate = previousDetails.Count() > 0 ? lastDate : loan.CreatedOn, StatusId = entity.ApprovalStatusId, TargetId = entity.TargetId, StaffId = user.StaffId, WorkflowToken = loan.WorkflowToken }; var result = await _serverRequest.StaffApprovalRequestAsync(req); if (!result.IsSuccessStatusCode) { return(new ApprovalRegRespObj { Status = new APIResponseStatus { Message = new APIResponseMessage { FriendlyMessage = result.ReasonPhrase } } }); } var stringData = await result.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <ApprovalRegRespObj>(stringData); if (!response.Status.IsSuccessful) { return(new ApprovalRegRespObj { Status = response.Status }); } if (response.ResponseId == (int)ApprovalStatus.Processing) { loan.ApprovalStatusId = (int)ApprovalStatus.Processing; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Processing, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Revert) { loan.ApprovalStatusId = (int)ApprovalStatus.Revert; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Approved) { var appicationResponse = _repo.LoanReviewApplicationApproval(entity, user.StaffId, user.UserName); if (appicationResponse.loanPayment != null && appicationResponse.AnyIdentifier > 0) { await _schedule.AddReviewedLoanSchedule(appicationResponse.AnyIdentifier, appicationResponse.loanPayment); } } if (response.ResponseId == (int)ApprovalStatus.Approved) { loan.ApprovalStatusId = (int)ApprovalStatus.Approved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Approved, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Disapproved) { loan.ApprovalStatusId = (int)ApprovalStatus.Disapproved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Disapproved, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } return(new ApprovalRegRespObj { Status = response.Status }); } catch (SqlException ex) { _trans.Rollback(); throw; } finally { _trans.Dispose(); } } } catch (Exception ex) { var errorCode = ErrorID.Generate(5); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new ApprovalRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode } } }); } }
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; } }
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 <ActionResult <ApprovalRegRespObj> > GoForApproval([FromBody] ApprovalObj entity) { using (var _trans = _dataContext.Database.BeginTransaction()) { try { var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _serverRequest.UserDataAsync(); var loan = _dataContext.credit_loan.Find(entity.TargetId); var req = new IndentityServerApprovalCommand { ApprovalComment = entity.Comment, ApprovalStatus = entity.ApprovalStatusId, TargetId = entity.TargetId, WorkflowToken = loan.WorkflowToken, ReferredStaffId = entity.ReferredStaffId }; var previousDetails = _dataContext.cor_approvaldetail.Where(x => x.WorkflowToken.Contains(loan.WorkflowToken) && x.TargetId == entity.TargetId).ToList(); var lastDate = loan.CreatedOn; if (previousDetails.Count() > 0) { lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date; } if (previousDetails.Count() > 0) { lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date; } var details = new cor_approvaldetail { Comment = entity.Comment, Date = DateTime.Now, ArrivalDate = previousDetails.Count() > 0 ? lastDate : loan.CreatedOn, StatusId = entity.ApprovalStatusId, TargetId = entity.TargetId, StaffId = user.StaffId, WorkflowToken = loan.WorkflowToken }; var result = await _serverRequest.StaffApprovalRequestAsync(req); if (!result.IsSuccessStatusCode) { return(new ApprovalRegRespObj { Status = new APIResponseStatus { Message = new APIResponseMessage { FriendlyMessage = result.ReasonPhrase } } }); } var stringData = await result.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <ApprovalRegRespObj>(stringData); if (!response.Status.IsSuccessful) { return(new ApprovalRegRespObj { Status = response.Status }); } if (response.ResponseId == (int)ApprovalStatus.Processing) { loan.ApprovalStatusId = (int)ApprovalStatus.Processing; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Processing, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Revert) { loan.ApprovalStatusId = (int)ApprovalStatus.Revert; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Approved) { var applicationResponse = _repo.DisburseLoan(entity.TargetId, user.StaffId, user.UserName, entity.Comment); var customer = _dataContext.credit_loancustomer.Find(loan.CustomerId); await _serverRequest.SendMail(new MailObj { fromAddresses = new List <FromAddress> { }, toAddresses = new List <ToAddress> { new ToAddress { address = customer.Email, name = customer.FirstName } }, subject = "Loan Successfully Approved", content = $"Hi {customer.FirstName}, <br> Your loan application has been finally approved. <br/> Loan Amount : {loan.PrincipalAmount}", sendIt = true, }); //Update CustomerTransaction if (applicationResponse.DisbursementEntry != null && applicationResponse.IntegralFeeEntry != null) { _customerTrans.CustomerTransaction(applicationResponse.DisbursementEntry); _customerTrans.CustomerTransaction(applicationResponse.IntegralFeeEntry); } //Generate Schedule if (applicationResponse.loanPayment != null && applicationResponse.AnyIdentifier > 0) { await _schedule.AddLoanSchedule(applicationResponse.AnyIdentifier, applicationResponse.loanPayment); } //Update IFRS _ifrs.UpdateScoreCardHistoryByLoanDisbursement(entity.TargetId, user.UserId); //Pay with Flutterwave if (applicationResponse.FlutterObj != null) { var flutter = _serverRequest.GetFlutterWaveKeys().Result; if (flutter.keys.useFlutterWave) { var res = _flutter.createBulkTransfer(applicationResponse.FlutterObj).Result; loan.ApprovalStatusId = (int)ApprovalStatus.Approved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); if (res.status.ToLower().Trim() != "success") { return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Loan disbursed successfully but transfer creation failed" } } }); } else if (res.status.ToLower().Trim() == "success") { return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Loan disbursed successfully and transfer creation successful" } } }); } } } loan.ApprovalStatusId = (int)ApprovalStatus.Approved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Approved, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Disapproved) { loan.ApprovalStatusId = (int)ApprovalStatus.Disapproved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Disapproved, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } return(new ApprovalRegRespObj { Status = response.Status }); } catch (Exception ex) { _trans.Rollback(); var errorCode = ErrorID.Generate(5); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new ApprovalRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode } } }); } finally { _trans.Dispose(); } } }
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(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; } }