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); }
private async Task <PurchasingAgreementViewModel> CreatePurchasingAgreementViewModel(PurchasingAgreementPage currentPage, string customerNo, string priceAreaId = "", string productionItemId = "", string grainTypeId = "", string agreementId = "") { PricePeriod[] pricePeriods = null; var result = new PurchasingAgreementViewModel(currentPage); //#if DEBUG // customerNo = "1000000"; //#endif if (currentPage.AgreementType == AgreementType.PrissakringDepaavtal) { var storageAgreements = await _agreementRepository.GetStorageAgreementsForPriceProtectionAsync(customerNo); storageAgreements = storageAgreements ?? new StorageAgreement[0]; StorageAgreement selectedAgreement = null; if (storageAgreements.Any()) { selectedAgreement = string.IsNullOrEmpty(agreementId) ? storageAgreements.FirstOrDefault() : storageAgreements.FirstOrDefault(x => x.AgreementId == agreementId); result.SelectedAgreement = selectedAgreement; } if (!string.IsNullOrEmpty(priceAreaId) || !string.IsNullOrEmpty(productionItemId)) { #if DEBUG priceAreaId = "1"; productionItemId = "100160"; grainTypeId = "BRONS"; #endif pricePeriods = await GetPricePeriods(customerNo, priceAreaId, currentPage.AgreementType, productionItemId, grainTypeId); } else { result.StorageAgreements = storageAgreements.ToList(); if (selectedAgreement != null) { pricePeriods = await GetPricePeriods(customerNo, selectedAgreement.PriceArea.ToString(), currentPage.AgreementType, selectedAgreement.ProductItemId, selectedAgreement.GrainType); } } pricePeriods = pricePeriods ?? await Task.FromResult(new PricePeriod[0]); } else { var priceAreas = await _agreementRepository.GetPriceAreasAsync(customerNo); priceAreas = priceAreas ?? await Task.FromResult(new PriceArea[0]); var selectedPriceArea = priceAreas.FirstOrDefault(x => x.FavoritePriceAreaId) ?? priceAreas.FirstOrDefault(); pricePeriods = await GetPricePeriods(customerNo, selectedPriceArea?.PriceAreaId, currentPage.AgreementType); pricePeriods = pricePeriods ?? await Task.FromResult(new PricePeriod[0]); var productCategories = pricePeriods.Where(x => !string.IsNullOrEmpty(x.ProductItemHierarchy)) .GroupBy(x => x.ProductItemHierarchy) .Select(x => new ProductItemCategory() { ID = Guid.NewGuid().ToString(), Hierarchy = x.Key, ProductItems = x.ToList() }).ToList(); result.SelectedPriceArea = selectedPriceArea; result.PriceAreas = priceAreas.ToList(); result.ProductCategories = productCategories.ToList(); } result.PricePeriods = GetPricePeriodHeader(pricePeriods.FirstOrDefault(), currentPage.AgreementType); return(result); }