private async Task <PurchasingAgreementViewModel> CreateViewModel(PurchasingAgreementPage currentPage, string customerNo, string productionItemId = "", string grainTypeId = "", string agreementId = "") { var viewModel = new PurchasingAgreementViewModel(currentPage); Dictionary <string, string> periods = new Dictionary <string, string>(); var selectedPriceArea = await _agreementRepository.GetSelectedPriceArea(customerNo); var priceAreaId = selectedPriceArea != null ? selectedPriceArea.PriceAreaId : string.Empty; if (currentPage.AgreementType == AgreementType.PrissakringDepaavtal) { var storageAgreements = await _agreementRepository.GetStorageAgreementsForPriceProtectionAsync(customerNo); storageAgreements = storageAgreements ?? new StorageAgreement[0]; var selectedStorageAgreement = storageAgreements.Any() && !string.IsNullOrEmpty(agreementId) ? storageAgreements.FirstOrDefault(x => x.AgreementId == agreementId) : null; periods = ConvertPeriodToMonthRange(await _agreementRepository.GetPeriodsPriceProtectingStorageAgreementAsync(priceAreaId, productionItemId, grainTypeId)); viewModel.StorageAgreements = storageAgreements.ToList(); viewModel.SelectedAgreement = selectedStorageAgreement; } else if (currentPage.AgreementType == AgreementType.SportAndForwardAvtal) { var products = _agreementRepository.GetProductsSpotAndForwardAgreement(priceAreaId); var grainTypes = !string.IsNullOrEmpty(productionItemId) ? await _agreementRepository.GetGrainTypesSpotAndForwardAgreementAsync(productionItemId, priceAreaId) : new Product[0]; periods = ConvertPeriodToMonthRange(await _agreementRepository.GetPeriodsSpotAndForwardAgreementAsync(priceAreaId)); viewModel.Products = products; viewModel.GrainTypes = grainTypes; } else if (currentPage.AgreementType == AgreementType.Poolavtal || currentPage.AgreementType == AgreementType.Depaavtal) { if (currentPage.AgreementType == AgreementType.Poolavtal) { viewModel.Header = "Teckna Poolavtal"; viewModel.Products = await _agreementRepository.GetProductsPoolAgreementAsync(priceAreaId); viewModel.ModesOfDelivery = await _agreementRepository.GetModesOfDeliveryPoolAgreementAsync(); periods = ConvertPeriodToMonthRange(await _agreementRepository.GetPeriodsPoolAgreementAsync(priceAreaId)); } else if (currentPage.AgreementType == AgreementType.Depaavtal) { viewModel.Header = "Teckna depåavtal"; viewModel.Products = await _agreementRepository.GetProductsStorageAgreementAsync(priceAreaId); viewModel.ModesOfDelivery = await _agreementRepository.GetModesOfDeliveryStorageAgreementAsync(); periods = ConvertPeriodToMonthRange(await _agreementRepository.GetPeriodsStorageAgreementAsync(priceAreaId)); viewModel.DepaPeriod = GetDepaPeriod(periods.FirstOrDefault()); } } viewModel.SelectedPriceArea = selectedPriceArea; viewModel.Periods = periods; viewModel.FormModel = new PurchasingAgreementFormModel(); viewModel.ReferencePrice = GetReferencePrice(); viewModel.MinPrice = SettingPage.PurchasePriceLow; viewModel.CommitQuantityMin = 12; return(viewModel); }