internal async Task MapInventoryToAsycuda()
        {
            try
            {
                var ilst = new List <InventoryItem>();
                using (var ctx = new InventoryItemService()
                {
                    StartTracking = true
                })
                {
                    ilst.AddRange(await ctx.GetInventoryItems(new List <string>()
                    {
                        "InventoryAsycudaMappings"
                    }).ConfigureAwait(false));
                }

                var alst = new List <xcuda_Item>();
                using (var ctx = new xcuda_ItemService())
                {
                    alst.AddRange(ctx.Getxcuda_ItemByExpressionLst(
                                      new List <string>()
                    {
                        "AsycudaDocument.DocumentType == \"IM7\" && AsycudaDocument.CNumber != null"
                    },
                                      new List <string>()
                    {
                        "SubItems",
                        "AsycudaDocument",
                        "xcuda_Tarification.xcuda_HScode",
                        "xcuda_Tarification.xcuda_Supplementary_unit"
                    }).Result.Distinct());         //, "EX"
                }

                if (BaseDataModel.Instance.CurrentApplicationSettings.ItemDescriptionContainsAsycudaAttribute == true)
                {
                    // get inventory
                    foreach (var itm in ilst)
                    {
                        var invDescrip = itm.Description;

                        string attrib = invDescrip.Split('|').Length > 2
                                ? invDescrip.Split('|')[2].ToUpper().Replace(" ", "")
                                : null;
                        var res = AllocationsBaseModel.Instance.GetAsycudaEntriesWithItemNumber(alst, attrib, invDescrip,
                                                                                                new List <string>()
                        {
                            itm.ItemNumber
                        }).ToList();
                        foreach (var ae in res)
                        {
                            if (!itm.InventoryAsycudaMappings.Any(x => x.Item_Id == ae.Item_Id))
                            {
                                itm.InventoryAsycudaMappings.Add(new InventoryAsycudaMapping(true)
                                {
                                    ItemNumber    = itm.ItemNumber,
                                    Item_Id       = ae.Item_Id,
                                    TrackingState = TrackingState.Added
                                });
                            }
                        }
                    }


                    ilst.AsParallel(new ParallelLinqOptions()
                    {
                        MaxDegreeOfParallelism = Environment.ProcessorCount
                    }).ForAll(x =>
                    {
                        using (var ctx = new InventoryDSContext())
                        {
                            ctx.ApplyChanges(x);
                            ctx.SaveChanges();
                        }
                    });

                    //await MapWhereItemDescriptionContainsAsycudaAttribute().ConfigureAwait(false);
                }
                else
                {
                    //await MapByMatchingSalestoAsycudaEntriesOnItemNumber().ConfigureAwait(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private async Task SaveToDataBase(IEnumerable <SalesReceiptRet> trackableCollection, AsycudaDocumentSet currentAsycudaDocumentSet)
        {
            var salesReceiptRets = trackableCollection as IList <SalesReceiptRet> ?? trackableCollection.ToList();

            StatusModel.StartStatusUpdate("Importing Sales Data", salesReceiptRets.Count());
            using (var ctx = new EntryDataDSContext())
            {
                foreach (var saleReceipt in salesReceiptRets)
                {
                    StatusModel.StatusUpdate();
                    SalesReceiptRet receipt = saleReceipt;
                    var             s       = ctx.EntryData.OfType <Sales>().FirstOrDefault(x => x.EntryDataId == receipt.SalesReceiptNumber);
                    if (s == null)
                    {
                        s = new Sales(true)
                        {
                            TrackingState = TrackingState.Added
                        };
                    }

                    // RemoveExistingDbReceipts(db, saleReceipt);
                    s.INVNumber     = saleReceipt.SalesReceiptNumber;
                    s.EntryDataId   = saleReceipt.SalesReceiptNumber;
                    s.EntryDataDate = saleReceipt.TxnDate;
                    s.TaxAmount     = Convert.ToDouble(saleReceipt.TaxAmount);
                    s.AsycudaDocumentSets.Add(new AsycudaDocumentSetEntryData(true)
                    {
                        AsycudaDocumentSetId = currentAsycudaDocumentSet.AsycudaDocumentSetId,
                        EntryDataId          = s.EntryDataId,
                        TrackingState        = TrackingState.Added
                    });


                    // do details
                    if (saleReceipt.SalesReceiptItems == null)
                    {
                        continue;
                    }

                    //foreach (var ed in s.EntryDataDetails.ToList())
                    //{
                    //    s.EntryDataDetails.Remove(ed);
                    //    db.DeleteObject(ed);
                    //}

                    for (var i = 0; i < saleReceipt.SalesReceiptItems.Count(); i++)
                    {
                        var saleReceiptDetail = saleReceipt.SalesReceiptItems[i];
                        var itm = s.EntryDataDetails.FirstOrDefault(x => x.LineNumber == i);
                        if (itm == null)
                        {
                            itm = new EntryDataDetails(true)
                            {
                                EntryDataId     = s.EntryDataId,
                                ItemNumber      = saleReceiptDetail.ItemNumber,
                                ItemDescription = saleReceiptDetail.Desc1 + "|" + saleReceiptDetail.Desc2 + "|" + saleReceiptDetail.Attribute,
                                Cost            = Convert.ToSingle(saleReceiptDetail.Cost),
                                LineNumber      = i,
                                UnitWeight      = Convert.ToSingle(saleReceiptDetail.Weight),
                                Units           = saleReceiptDetail.UnitOfMeasure,
                                TrackingState   = TrackingState.Added
                            };
                        }
                        else
                        {
                            itm.ItemNumber      = saleReceiptDetail.ItemNumber;
                            itm.ItemDescription = saleReceiptDetail.Desc1 + "|" + saleReceiptDetail.Desc2 + "|" + saleReceiptDetail.Attribute;
                            itm.Cost            = Convert.ToSingle(saleReceiptDetail.Cost);
                            itm.LineNumber      = i;
                            itm.UnitWeight      = Convert.ToSingle(saleReceiptDetail.Weight);
                            itm.Units           = saleReceiptDetail.UnitOfMeasure;
                        }

                        InventoryItem inv = null;
                        using (var ictx = new InventoryDSContext())
                        {
                            inv = await ictx.InventoryItems.FindAsync(saleReceiptDetail.ItemNumber).ConfigureAwait(false);

                            if (inv == null)
                            {
                                inv = new InventoryItem(true)
                                {
                                    ItemNumber    = saleReceiptDetail.ItemNumber,
                                    Description   = GetQBSaleItemDescription(saleReceiptDetail),
                                    TrackingState = TrackingState.Added
                                };
                                ictx.ApplyChanges(inv);
                                await ictx.SaveChangesAsync().ConfigureAwait(false);
                            }
                        }
                        itm.ItemDescription = inv.Description;

                        switch (saleReceipt.SalesReceiptType)
                        {
                        case "srtReturn":
                            itm.Quantity = Convert.ToSingle(saleReceiptDetail.Qty) * -1;
                            break;

                        case "srtSales":
                            itm.Quantity = Convert.ToSingle(saleReceiptDetail.Qty);
                            break;

                        default:
                            throw new Exception("Unknown SalesType");
                            break;
                        }

                        s.EntryDataDetails.Add(itm);
                    }
                    ctx.ApplyChanges(s);
                    await ctx.SaveChangesAsync().ConfigureAwait(false);
                }
            }


            StatusModel.StopStatusUpdate();
            MessageBox.Show(@"Sale Receipt Import Complete");
        }