private async Task <DeliveryAssuranceOverviewViewModel> PopulateOverviewModel(
            DeliveryAssuranceListPage currentPage,
            Core.DataModels.DeliveryAssuranceDtos.DeliveryAssurance deliveryAssurance, bool isGeneratePdf = false)
        {
            var urlHelper            = ServiceLocator.Current.GetInstance <UrlHelper>();
            var deliveryAssuranceUrl = urlHelper.ContentUrl(currentPage.ContentLink);

            var lorryType =
                _deliveryNoteRepository.GetLorryTypes()
                .FirstOrDefault(x => x.Keyvalue.Equals(deliveryAssurance.Levsatt));

            var deliveryAddress =
                _deliveryNoteRepository.GetDeliveryAdresses(deliveryAssurance.SupplierNumber)
                .FirstOrDefault(x => x.AddressNumber.Equals(deliveryAssurance.DeliveryAddress));

            var mergedItem =
                _deliveryNoteRepository.GetMergedItems(deliveryAssurance.SupplierNumber, isGeneratePdf ? DateTime.Now : deliveryAssurance.Leveransdatum, !SettingPage.IsInternal)?
                .FirstOrDefault(i => DeliveryAssuranceHelper.GetItemSort(deliveryAssurance.Item, deliveryAssurance.Sort) == i.Keyvalue);

            var warehouse =
                (await _deliveryNoteRepository.GetWarehouseListAsync(deliveryAssurance.Item, deliveryAssurance.Sort, deliveryAssurance.Leveransdatum))?
                .FirstOrDefault(w => w.Keyvalue == deliveryAssurance.Warehouse);

            var depaAvtals = await _deliveryNoteRepository.GetDepaAvtalAsync(deliveryAssurance.SupplierNumber, deliveryAssurance.Item, deliveryAssurance.Sort);

            var depaAvtal = depaAvtals.FirstOrDefault(d => d.Keyvalue == deliveryAssurance.Depaavtal);

            var model = new DeliveryAssuranceOverviewViewModel(currentPage)
            {
                DeliveryAssurance = new DeliveryAssuranceOverview()
                {
                    CustomerName   = GetCustomerName(deliveryAssurance.SupplierNumber),
                    CustomerNumber = deliveryAssurance.SupplierNumber,
                    IONumber       = deliveryAssurance.IONumber,

                    KundorderNr  = deliveryAssurance.KundorderNr,
                    GHGvarde     = deliveryAssurance.GHGvarde,
                    KundsOrderNr = deliveryAssurance.KundsOrderNr,

                    TermAndCondition = deliveryAssurance.Leveransvillkor,
                    Gardshamtning    = deliveryAssurance.Gardshamtning,
                    TransportType    = GetTransportType(deliveryAssurance.Gardshamtning, deliveryAssurance.Leveransvillkor),

                    LorryTypeDesc   = lorryType?.Description,
                    Address         = deliveryAddress ?? new DeliveryAddress(),
                    DeliveryAddress = deliveryAddress != null ? $"{deliveryAddress.Street}, {deliveryAddress.City}" : string.Empty,

                    ItemName = mergedItem?.Description ?? deliveryAssurance.ItemName,
                    Quantity = deliveryAssurance.Quantity.ConvertToTon(),

                    SlamDesc          = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Slam, deliveryAssurance.SLAM),
                    StraforkortatDesc = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Straforkortat, deliveryAssurance.Straforkortat),
                    TorkatDesc        = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Torkat, deliveryAssurance.Torkat),
                    RedDesc           = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Red, deliveryAssurance.RED),

                    OtherInfo     = deliveryAssurance.Ovrigt,
                    HarvestYear   = deliveryAssurance.Skordear,
                    WarehouseDesc = warehouse?.Description,
                    DepaAvtal     = !string.IsNullOrWhiteSpace(depaAvtal?.Keyvalue) ? depaAvtal.Description : string.Empty,

                    DeliveryDate     = DeliveryAssuranceHelper.GetValidDate(deliveryAssurance.Leveransdatum),
                    DeliveryTypeDesc = depaAvtals.Length > 1 ? DeliveryTypes.Depa : DeliveryTypes.Spon,
                    Status           = deliveryAssurance.Status
                },
                ChangeUrl = DeliveryAssuranceHelper.BuildQueryUrl($"{deliveryAssuranceUrl}Change", new Dictionary <string, string>()
                {
                    { "a", deliveryAssurance.IONumber },
                    { "l", deliveryAssurance.LineNumber.ToString() }
                })
            };

            return(model);
        }
        private async Task <DeliveryAssuranceOverviewViewModel> PopulateOverviewModelFromDeliveryDetail(DeliveryAssuranceListPage currentPage,
                                                                                                        DeliveryAssuranceDetail deliveryAssurance)
        {
            var lorryType = GetLorryTypes()?.FirstOrDefault(x => x.Key.Equals(deliveryAssurance.LorryType));

            var deliveryAddress = GetDeliveryAddresses(deliveryAssurance.CustomerNumber)?.FirstOrDefault(x => x.Key.Equals(deliveryAssurance.DeliveryAddress));

            if (deliveryAssurance.Action.Equals(DeliveryAssuranceAction.Create))
            {
                deliveryAssurance.Item = DeliveryAssuranceHelper.ParseItemValue(deliveryAssurance.Article);
                deliveryAssurance.Sort = DeliveryAssuranceHelper.ParseSortValue(deliveryAssurance.Article);
                var article = GetMergedItems(deliveryAssurance.CustomerNumber, DateTime.Now)?.FirstOrDefault(d => d.Key == deliveryAssurance.Article);
                deliveryAssurance.ItemName = article?.Value;
            }

            var warehouse =
                (await GetWarehouseList(new Core.DataModels.DeliveryAssuranceDtos.DeliveryAssurance
            {
                Item = deliveryAssurance.Item,
                Sort = deliveryAssurance.Sort,
                Leveransdatum = Convert.ToDateTime(deliveryAssurance.DeliveryDate)
            }))?.FirstOrDefault(w => w.Key == GetWarehouseFromDeliveryInformation(deliveryAssurance));

            //var depaAvtals =
            //    (await GetDepaAvtalDelAssAsync(new Core.DataModels.DeliveryAssuranceDtos.DeliveryAssurance
            //    {
            //        Item = deliveryAssurance.Item,
            //        Sort = deliveryAssurance.Sort,
            //        Leveransdatum = Convert.ToDateTime(deliveryAssurance.DeliveryDate),
            //        SupplierNumber = deliveryAssurance.CustomerNumber
            //    }));

            var depaAvtals =
                await
                _deliveryNoteRepository.GetDepaAvtalAsync(deliveryAssurance.CustomerNumber, deliveryAssurance.Item,
                                                          deliveryAssurance.Sort);

            var depaAvtal = depaAvtals?.FirstOrDefault(d => d.Keyvalue == deliveryAssurance.DepaAvtal);

            var deliveryAssuranceLists = Session[nameof(MultiApprove)] as List <DeliveryAssuranceList>;
            var overviewModel          = new DeliveryAssuranceOverviewViewModel(currentPage)
            {
                DeliveryAssurance = new DeliveryAssuranceOverview()
                {
                    CustomerName   = deliveryAssurance.CustomerName,
                    CustomerNumber = deliveryAssurance.CustomerNumber,
                    IONumber       = deliveryAssurance.IONumber,

                    TermAndCondition = deliveryAssurance.TermAndCondition,
                    Gardshamtning    = deliveryAssurance.Gardshamtning == "1",
                    TransportType    = GetTransportType(deliveryAssurance.Gardshamtning == "1", deliveryAssurance.TermAndCondition),

                    LorryTypeDesc   = lorryType?.Value,
                    DeliveryAddress = deliveryAddress?.Value,

                    ItemName = deliveryAssurance.ItemName,
                    Quantity = deliveryAssurance.Quantity,

                    SlamDesc          = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Slam, deliveryAssurance.Slam),
                    StraforkortatDesc = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Straforkortat, deliveryAssurance.Straforkortat),
                    TorkatDesc        = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Torkat, deliveryAssurance.Torkat),
                    RedDesc           = await _deliveryNoteRepository.GetM3DescriptionAsync(DeliveryAssuranceRadioButtons.Red, deliveryAssurance.Red),

                    OtherInfo     = deliveryAssurance.OtherInfo.ReplaceReturnToBr(),
                    HarvestYear   = deliveryAssurance.HarvestYear,
                    WarehouseDesc = warehouse?.Value,
                    DepaAvtal     = depaAvtal?.Description,

                    DeliveryDate     = deliveryAssurance.DeliveryDate,
                    DeliveryTypeDesc = depaAvtals?.Length > 1 ? DeliveryTypes.Depa : DeliveryTypes.Spon,

                    Action = deliveryAssurance.Action
                },
                ChangeUrl      = deliveryAssurance.CurrentUrl,
                IsMultiApprove = deliveryAssurance.Action.Equals(DeliveryAssuranceAction.Approve) && deliveryAssuranceLists != null && deliveryAssuranceLists.Count > 1
            };

            var sessionKey = $"{nameof(DeliveryAssuranceDetail)}-{deliveryAssurance.Action}-{deliveryAssurance.IONumber}";

            Session[sessionKey] = deliveryAssurance;

            return(overviewModel);
        }