protected void InitData(
            bool isAipe                   = false,
            string stageKey               = "Aipe",
            string budgetRegion           = "AAK (Asia)",
            List <CostLineItemView> items = null,
            string targetBudget           = "0",
            List <CostStageRevisionPaymentTotal> payments = null,
            string contentType    = Constants.ContentType.Photography,
            string productionType = Constants.ProductionType.FullProduction,
            CostType costType     = CostType.Production,
            string agencyCurrency = "USD",
            Guid?dpvCurrency      = null,
            Guid?dpvId            = null,
            string vendorCategory = null
            )
        {
            SetupCurrencies();

            _costStageRevisionId = Guid.NewGuid();
            var previousCostStageRevisionId = Guid.NewGuid();
            var costId            = Guid.NewGuid();
            var costStageId       = Guid.NewGuid();
            var paymentCurrencyId = dpvCurrency ?? _efContext.Currency.FirstOrDefault(c => c.Code == agencyCurrency)?.Id;

            _stage = new CostStage {
                Key = stageKey, Id = costStageId
            };
            _contentType       = contentType;
            _productionType    = productionType;
            _stageDetails      = GetStageDetails(isAipe, budgetRegion, targetBudget, costType, agencyCurrency);
            _productionDetails = GetProductionDetails(dpvCurrency, dpvId, vendorCategory);
            _revision          = new CostStageRevision
            {
                Id           = _costStageRevisionId,
                StageDetails = new CustomFormData {
                    Data = JsonConvert.SerializeObject(_stageDetails)
                },
                ProductDetails = new CustomFormData {
                    Data = JsonConvert.SerializeObject(_productionDetails)
                },
                CostStage = _stage,
                Approvals = new List <Approval>()
            };

            _cost = new Cost
            {
                Id       = costId,
                CostType = costType,
                LatestCostStageRevisionId = _costStageRevisionId,
                LatestCostStageRevision   = _revision,
                Project = new Project(),
                Parent  = new AbstractType
                {
                    Agency = new Agency()
                },
                PaymentCurrencyId = paymentCurrencyId,
                ExchangeRate      = _efContext.ExchangeRate.FirstOrDefault(er => er.FromCurrency == paymentCurrencyId)?.Rate
            };
            _stage.Cost = _cost;

            var previousRevision = new CostStageRevision {
                Id = previousCostStageRevisionId
            };

            _costApprovedEvent = new CostStageRevisionStatusChanged(_cost.Id, _revision.Id, CostStageRevisionStatus.Approved, BuType.Pg);

            _paymentDetailsData = new PgPaymentDetails();
            _costLineItems      = new List <CostLineItemView>();
            if (items != null)
            {
                _costLineItems.AddRange(items);
            }

            var paymentsList = new List <CostStageRevisionPaymentTotal>();

            if (payments != null)
            {
                paymentsList.AddRange(payments);
            }

            _costStageRevisionServiceMock.Setup(csr => csr.GetRevisionById(_costStageRevisionId)).ReturnsAsync(_revision);
            _costStageRevisionServiceMock.Setup(csr => csr.GetPreviousRevision(costStageId)).ReturnsAsync(previousRevision);

            _costStageRevisionServiceMock.Setup(csr =>
                                                csr.GetStageDetails <PgStageDetailsForm>(It.Is <CostStageRevision>(r => r.Id == _costStageRevisionId)))
            .Returns(_stageDetails);

            _costStageRevisionServiceMock.Setup(csr =>
                                                csr.GetProductionDetails <PgProductionDetailsForm>(It.Is <CostStageRevision>(r => r.Id == _costStageRevisionId)))
            .Returns(_productionDetails);

            _costStageRevisionServiceMock.Setup(csr => csr.GetCostStageRevisionPaymentTotals(_costStageRevisionId, It.IsAny <bool>())).ReturnsAsync((List <CostStageRevisionPaymentTotal>)null);
            _costStageRevisionServiceMock.Setup(csr => csr.GetCostStageRevisionPaymentTotals(previousCostStageRevisionId, It.IsAny <bool>())).ReturnsAsync(paymentsList);

            _costStageRevisionServiceMock.Setup(csr => csr.GetAllCostPaymentTotals(costId, costStageId)).ReturnsAsync(paymentsList);

            _customDataServiceMock.Setup(cd => cd.GetCustomData <PgPaymentDetails>(_costStageRevisionId, CustomObjectDataKeys.PgPaymentDetails))
            .ReturnsAsync(_paymentDetailsData);

            _costStageRevisionServiceMock.Setup(csr => csr.GetCostLineItems(_costStageRevisionId)).ReturnsAsync(_costLineItems);
            _efContext.Cost.Add(_cost);
            _efContext.SaveChanges();
        }
        public async Task <PgPurchaseOrder> GetPurchaseOrder(CostStageRevisionStatusChanged stageRevisionStatusChanged)
        {
            var costData = await _efContext.Cost
                           .Include(c => c.LatestCostStageRevision)
                           .ThenInclude(csr => csr.CostStage)
                           .Where(c => c.Id == stageRevisionStatusChanged.AggregateId)
                           .Include(c => c.Parent)
                           .ThenInclude(p => p.Agency)
                           .Select(c => new
            {
                BrandName                 = c.Project.Brand != null ? c.Project.Brand.Name : string.Empty,
                CostNumber                = c.CostNumber,
                CostId                    = c.Id,
                StageDetailsData          = c.LatestCostStageRevision.StageDetails.Data,
                ProductionDetailsData     = c.LatestCostStageRevision.ProductDetails.Data,
                LatestCostStageRevisionId = c.LatestCostStageRevision.Id,
                AgencyLabels              = c.Parent.Agency.Labels,
                CostStageRevisionKey      = c.LatestCostStageRevision.CostStage.Key,
                CostStageRevisionName     = c.LatestCostStageRevision.CostStage.Name,
                Requisitioner             = c.LatestCostStageRevision.Approvals
                                            .Where(a => a.Type == ApprovalType.Brand && a.Requisitioners.Any())
                                            .SelectMany(a => a.Requisitioners)
                                            .Select(r => r.CostUser)
                                            .FirstOrDefault(),
                CostType     = c.CostType,
                ExchangeRate = c.ExchangeRate
            })
                           .FirstOrDefaultAsync();

            if (costData == null)
            {
                _logger.Error($"Couldn't find cost with id {stageRevisionStatusChanged.AggregateId}");
                return(null);
            }

            var purchaseOrderDto = new PgPurchaseOrderDTO
            {
                BrandName                 = costData.BrandName,
                CostNumber                = costData.CostNumber,
                StageDetailsData          = costData.StageDetailsData,
                ProductionDetailsData     = costData.ProductionDetailsData,
                LatestCostStageRevisionId = costData.LatestCostStageRevisionId,
                AgencyLabels              = costData.AgencyLabels,
                CostStageRevisionKey      = costData.CostStageRevisionKey,
                CostStageRevisionName     = costData.CostStageRevisionName,
                RequisitionerEmail        = costData.Requisitioner?.Email,
                TNumber = costData.Requisitioner?.FederationId
            };

            var stageDetailsForm      = JsonConvert.DeserializeObject <PgStageDetailsForm>(purchaseOrderDto.StageDetailsData);
            var productionDetailsForm = purchaseOrderDto.ProductionDetailsData != null?JsonConvert.DeserializeObject <PgProductionDetailsForm>(purchaseOrderDto.ProductionDetailsData) : null;

            var paymentAmount = await GetTotals(stageRevisionStatusChanged.CostStageRevisionId, costData.ExchangeRate);

            var applicableCurrencyCode = await _currencyService.GetCurrencyCode(stageDetailsForm.AgencyCurrency, productionDetailsForm);

            var ledgerMaterialCode = await _ledgerMaterialCode.GetLedgerMaterialCodes(purchaseOrderDto.LatestCostStageRevisionId);

            var purchaseOrderResponse = await _customDataService.GetCustomData <PgPurchaseOrderResponse>(purchaseOrderDto.LatestCostStageRevisionId, CustomObjectDataKeys.PgPurchaseOrderResponse);

            var paymentDetails = await _customDataService.GetCustomData <PgPaymentDetails>(purchaseOrderDto.LatestCostStageRevisionId, CustomObjectDataKeys.PgPaymentDetails);

            var purchaseOrder = new PgPurchaseOrder
            {
                BasketName         = GetBasketName(purchaseOrderDto, stageDetailsForm),
                Description        = GetDescription(purchaseOrderDto, stageDetailsForm),
                TotalAmount        = paymentAmount.TotalAmount,
                PaymentAmount      = paymentAmount.PaymentAmount,
                StartDate          = stageRevisionStatusChanged.TimeStamp,
                CostNumber         = purchaseOrderDto.CostNumber ?? "",
                Currency           = applicableCurrencyCode,
                CategoryId         = ledgerMaterialCode?.MgCode ?? "",
                GL                 = ledgerMaterialCode?.GlCode ?? "",
                DeliveryDate       = GetDeliveryDate(stageRevisionStatusChanged.TimeStamp),
                IONumber           = !string.IsNullOrEmpty(paymentDetails?.IoNumber) ? $"00{paymentDetails.IoNumber}" : "",
                LongText           = GetLongTextField(stageRevisionStatusChanged, purchaseOrderDto, stageDetailsForm, purchaseOrderResponse, paymentAmount.PaymentAmount, applicableCurrencyCode, costData.CostType),
                TNumber            = purchaseOrderDto.TNumber ?? "",
                RequisitionerEmail = purchaseOrderDto.RequisitionerEmail ?? "",
                Vendor             = GetVendor(purchaseOrderDto, productionDetailsForm) ?? "",
                PoNumber           = paymentDetails?.PoNumber ?? "",
                AccountCode        = purchaseOrderResponse?.AccountCode ?? "",
                ItemIdCode         = purchaseOrderResponse?.ItemIdCode ?? "",
                GrNumbers          = await GetGrNumbers(stageRevisionStatusChanged),
                Commodity          = GetCommodity(costData.CostType, stageDetailsForm)
            };

            return(purchaseOrder);
        }