private static string GetVendor(PgPurchaseOrderDTO purchaseDto, PgProductionDetailsForm productionDetails)
        {
            var code = productionDetails?.DirectPaymentVendor != null
                ? productionDetails.DirectPaymentVendor.SapVendorCode
                : purchaseDto.AgencyLabels.GetSapVendorCode();

            return(code);
        }
        protected void InitData(
            string budgetRegion   = Constants.BudgetRegion.AsiaPacific,
            string contentType    = Constants.ContentType.Photography,
            string productionType = Constants.ProductionType.FullProduction,
            CostType costType     = CostType.Production
            )
        {
            CreateTemplateIfNotCreated(User).Wait();

            var costTemplateId = costType == CostType.Production
                ? CostTemplate.Id
                : costType == CostType.Buyout
                    ? UsageCostTemplate.Id
                    : TrafficCostTemplate.Id;

            var costModel = new CreateCostModelBuilder()
                            .WithBudgetRegion(budgetRegion)
                            .WithContentType(contentType)
                            .WithProductionType(productionType)
                            .WithTemplateId(costTemplateId)
                            .Build();

            var createCostResponse = CreateCost(User, costModel).Result;

            _cost     = Deserialize <Cost>(createCostResponse.Body);
            _revision = EFContext.CostStageRevision.First(csr => csr.Id == _cost.LatestCostStageRevisionId.Value);
            var costStageRevisionId = _revision.Id;

            _contentType    = contentType;
            _productionType = productionType;
            _stageDetails   = new PgStageDetailsForm
            {
                ContentType = new DictionaryValue {
                    Key = _contentType
                },
                ProductionType = new DictionaryValue {
                    Key = _productionType
                },
                BudgetRegion = new AbstractTypeValue {
                    Key = budgetRegion
                }
            };
            _productionDetails = new PgProductionDetailsForm();

            _costStageRevisionServiceMock.Setup(csr => csr.GetRevisionById(costStageRevisionId)).ReturnsAsync(_revision);
            _costStageRevisionServiceMock.Setup(csr => csr.GetStageDetails <PgStageDetailsForm>(costStageRevisionId)).ReturnsAsync(_stageDetails);
            _costStageRevisionServiceMock.Setup(csr => csr.GetProductionDetails <PgProductionDetailsForm>(costStageRevisionId)).ReturnsAsync(_productionDetails);
        }
        public async Task <Currency> GetCurrency(string agencyCurrency, PgProductionDetailsForm productionDetails)
        {
            Currency currency;

            if (productionDetails?.DirectPaymentVendor != null)
            {
                var dpv = productionDetails.DirectPaymentVendor;
                if (dpv.CurrencyId.HasValue)
                {
                    currency = await _efContext.Currency.FirstOrDefaultAsync(x => x.Id == productionDetails.DirectPaymentVendor.CurrencyId.Value);

                    if (currency == null)
                    {
                        throw new EntityNotFoundException <Entity>(dpv.CurrencyId.Value);
                    }
                }
                else
                {
                    var vendor = await _efContext.Vendor
                                 .Include(v => v.Categories)
                                 .FirstOrDefaultAsync(x => x.Id == dpv.Id);

                    if (vendor == null)
                    {
                        throw new EntityNotFoundException <Vendor>(dpv.Id);
                    }

                    var vendorCategory = vendor.Categories.FirstOrDefault(c => c.Name == dpv.ProductionCategory);
                    if (vendorCategory == null)
                    {
                        throw new Exception($"Category {dpv.ProductionCategory} is not enabled for vendor {vendor.Name} id {vendor.Id}");
                    }

                    currency = await _efContext.Currency.FirstOrDefaultAsync(x => x.Id == vendorCategory.DefaultCurrencyId);
                }
            }
            else if (!string.IsNullOrEmpty(agencyCurrency))
            {
                currency = await _efContext.Currency.FirstOrDefaultAsync(x => x.Code == agencyCurrency);
            }
            else
            {
                currency = await _efContext.Currency.FirstOrDefaultAsync(x => x.DefaultCurrency);
            }

            return(currency);
        }
        private PgProductionDetailsForm GetProductionDetails(Guid?dpvCurrency, Guid?dpvId, string vendorCategory = null)
        {
            var productionDetails = new PgProductionDetailsForm();

            if (dpvCurrency.HasValue)
            {
                productionDetails.DirectPaymentVendor = new PgProductionDetailsForm.Vendor {
                    CurrencyId = dpvCurrency
                };
            }

            if (dpvId.HasValue)
            {
                if (productionDetails.DirectPaymentVendor == null)
                {
                    productionDetails.DirectPaymentVendor = new PgProductionDetailsForm.Vendor();
                }

                productionDetails.DirectPaymentVendor.Id = dpvId.Value;
                productionDetails.DirectPaymentVendor.ProductionCategory = vendorCategory;
            }

            return(productionDetails);
        }
        public async Task <string> GetCurrencyCode(string agencyCurrency, PgProductionDetailsForm productionDetails)
        {
            var currency = await GetCurrency(agencyCurrency, productionDetails);

            return(currency.Code);
        }
        public Task <Currency> GetCurrencyIfChanged(PgStageDetailsForm oldStageDetails, PgProductionDetailsForm oldProductionDetails, PgStageDetailsForm newStageDetails, PgProductionDetailsForm newProductionDetails)
        {
            if ((newProductionDetails?.DirectPaymentVendor?.CurrencyId != oldProductionDetails?.DirectPaymentVendor?.CurrencyId) ||
                (newStageDetails.AgencyCurrency != oldStageDetails.AgencyCurrency))
            {
                return(GetCurrency(newStageDetails.AgencyCurrency, newProductionDetails));
            }

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

            var previousCostStageRevisionId = Guid.NewGuid();

            _costStageRevisionId = Guid.NewGuid();
            var _costPreviousStageRevisionId = 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,
                Status    = CostStageRevisionStatus.Approved,
                Approvals = new List <Approval>()
            };

            _previousRevision = new CostStageRevision
            {
                Id           = _costPreviousStageRevisionId,
                StageDetails = new CustomFormData {
                    Data = JsonConvert.SerializeObject(_stageDetails)
                },
                ProductDetails = new CustomFormData {
                    Data = JsonConvert.SerializeObject(_productionDetails)
                },
                CostStage = _stage,
                Status    = CostStageRevisionStatus.Approved,
                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;
            _stage.Name = "Final Actual";

            var previousRevision = new CostStageRevision {
                Id = previousCostStageRevisionId
            };

            //add previous stage revision to the stage
            _stage.CostStageRevisions.Add(_previousRevision);

            _costApprovedEvent = new CostStageRevisionStatusChanged(_cost.Id, _previousRevision.Id, CostStageRevisionStatus.Approved, BuType.Pg);
            _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 _previousCostLineItems = new List <CostLineItemView>();

            if (items != null)
            {
                _previousCostLineItems.AddRange(previousFAItems);
            }

            var paymentsList = new List <CostStageRevisionPaymentTotal>();

            if (payments != null)
            {
                foreach (var payment in payments)
                {
                    payment.CostStageRevision = _revision;
                }
                paymentsList.AddRange(payments);
            }
            var previousPaymentsList = new List <CostStageRevisionPaymentTotal>();

            if (previousFAPayments != null)
            {
                foreach (var payment in previousFAPayments)
                {
                    payment.CostStageRevision = _previousRevision;
                }
                previousPaymentsList.AddRange(previousFAPayments);
            }
            //set upp last stage revision data
            _costStageRevisionServiceMock.Setup(csr => csr.GetRevisionById(_costPreviousStageRevisionId)).ReturnsAsync(_previousRevision);
            _costStageRevisionServiceMock.Setup(csr => csr.GetPreviousRevision(costStageId)).ReturnsAsync(previousRevision);
            _costStageRevisionServiceMock.Setup(csr =>
                                                csr.GetStageDetails <PgStageDetailsForm>(It.Is <CostStageRevision>(r => r.Id == _costPreviousStageRevisionId)))
            .Returns(_stageDetails);
            _costStageRevisionServiceMock.Setup(csr =>
                                                csr.GetProductionDetails <PgProductionDetailsForm>(It.Is <CostStageRevision>(r => r.Id == _costPreviousStageRevisionId)))
            .Returns(_productionDetails);
            _costStageRevisionServiceMock.Setup(csr => csr.GetCostStageRevisionPaymentTotals(_costPreviousStageRevisionId, It.IsAny <bool>())).ReturnsAsync((List <CostStageRevisionPaymentTotal>)null);
            _costStageRevisionServiceMock.Setup(csr => csr.GetCostStageRevisionPaymentTotals(previousCostStageRevisionId, It.IsAny <bool>())).ReturnsAsync(previousPaymentsList);
            _costStageRevisionServiceMock.Setup(csr => csr.GetAllCostPaymentTotals(costId, costStageId)).ReturnsAsync(previousPaymentsList);
            _costStageRevisionServiceMock.Setup(csr => csr.GetAllCostPaymentTotalsFinalActual(costId, costStageId)).ReturnsAsync(previousPaymentsList);
            _customDataServiceMock.Setup(cd => cd.GetCustomData <PgPaymentDetails>(_costPreviousStageRevisionId, CustomObjectDataKeys.PgPaymentDetails))
            .ReturnsAsync(_paymentDetailsData);
            _costStageRevisionServiceMock.Setup(csr => csr.GetCostLineItems(_costPreviousStageRevisionId)).ReturnsAsync(_previousCostLineItems);

            //set up latest stage revision data
            _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();
        }
Beispiel #8
0
        private async Task <CostSearchItem> CostSearchItem(Cost cost, ICostBuilder costBuilder)
        {
            Currency currency             = null;
            var      customObjectFormData =
                await _efContext.CustomObjectData.FirstOrDefaultAsync(
                    cofd => cofd.ObjectId == cost.LatestCostStageRevision.Id && cofd.Name == CustomObjectDataKeys.PgPaymentDetails);

            var stageDetailsForm = JsonConvert.DeserializeObject <PgStageDetailsForm>(cost.LatestCostStageRevision.StageDetails.Data);

            var productionDetailsForm = new PgProductionDetailsForm();

            if (cost.LatestCostStageRevision.ProductDetails != null)
            {
                productionDetailsForm = JsonConvert.DeserializeObject <PgProductionDetailsForm>(cost.LatestCostStageRevision.ProductDetails.Data);
            }

            PgPaymentDetails paymentDetails = null;

            if (customObjectFormData?.Data != null)
            {
                paymentDetails = JsonConvert.DeserializeObject <PgPaymentDetails>(customObjectFormData.Data);
            }

            var approvers =
                cost.LatestCostStageRevision.Approvals
                .SelectMany(a => a.ApprovalMembers)
                .Where(am => !am.IsExternal)
                .Select(am => new Approver
            {
                CostUserId = am.CostUser.Id,
                Name       = $"{am.CostUser.FirstName} {am.CostUser.LastName}",
                Role       = am.CostUser.UserBusinessRoles?.FirstOrDefault()?.BusinessRole?.Value ?? "Sap Approver",
                Status     = am.Status.ToString()
            });

            if (!string.IsNullOrEmpty(stageDetailsForm.AgencyCurrency))
            {
                currency = await _efContext.Currency.FirstOrDefaultAsync(c => c.Code == stageDetailsForm.AgencyCurrency);
            }

            if (productionDetailsForm?.DirectPaymentVendor?.CurrencyId != null)
            {
                currency = await _efContext.Currency.FirstOrDefaultAsync(c => c.Id == productionDetailsForm.DirectPaymentVendor.CurrencyId);
            }

            if (currency == null)
            {
                currency = await _efContext.Currency.FirstOrDefaultAsync(c => c.DefaultCurrency);
            }

            var costSearchItem = _mapper.Map <CostSearchItem>(cost);
            var totals         = await costBuilder.GetRevisionTotals(cost.LatestCostStageRevision.Id);

            costSearchItem.GrandTotal = totals.totalInLocalCurrency;
            costSearchItem.GrandTotalDefaultCurrency = totals.total;
            costSearchItem.ApprovalMembers           = new List <Approver>();
            costSearchItem.ApprovalMembers.AddRange(approvers);
            costSearchItem.LatestRevisionId = cost.LatestCostStageRevision.Id;

            costSearchItem.UserGroups  = (await _permissionService.GetObjectUserGroups(cost.Id, null)).ToList();
            costSearchItem.BrandId     = cost.Project.BrandId.ToString();
            costSearchItem.AgencyId    = cost.ParentId.ToString();
            costSearchItem.IoNumber    = paymentDetails?.IoNumber;
            costSearchItem.Initiatives = cost.LatestCostStageRevision.ExpectedAssets?.Select(a => a.Initiative).Distinct().ToList();
            _mapper.Map(stageDetailsForm, costSearchItem);
            costSearchItem.Stage    = cost.LatestCostStageRevision.CostStage.Name;
            costSearchItem.StageKey = cost.LatestCostStageRevision.CostStage.Key;

            return(_mapper.Map(currency, costSearchItem));
        }