private static bool InvoicePerEntry(string prevEntryId, AllocationsModel.MyPodData mypod)
 {
     return(BaseDataModel.Instance.CurrentApplicationSettings
            .InvoicePerEntry == true &&
            //prevEntryId != "" &&
            prevEntryId != mypod.EntlnData.EntryDataDetails[0].EntryDataId);
 }
        private void Ex9Bucket(AllocationsModel.MyPodData mypod, string dfp, DocumentCT cdoc)
        {
            // prevent over draw down of pqty == quantity allocated
            try
            {
                var eld          = mypod.EntlnData;
                var previousItem = mypod.EntlnData.PreviousDocumentItem as xcuda_Item;
                if (previousItem == null)
                {
                    return;
                }
                var PdfpAllocated = (dfp == "Duty Free" ? previousItem.DFQtyAllocated : previousItem.DPQtyAllocated);
                var pAllocated    = previousItem.DFQtyAllocated + previousItem.DPQtyAllocated;


                if (previousItem.QtyAllocated == 0)
                {
                    if (dfp == "Duty Free")
                    {
                        previousItem.DFQtyAllocated = 0;
                    }
                    else
                    {
                        previousItem.DPQtyAllocated = 0;
                    }
                }
                if (previousItem.xcuda_PreviousItems.Any() == false)
                {
                    return;
                }

                var pqty  = previousItem.xcuda_PreviousItems.Where(x => x.xcuda_Item.AsycudaDocument.CNumber != null && x.DutyFreePaid == dfp).Sum(xx => xx.Suplementary_Quantity);
                var apqty = previousItem.xcuda_PreviousItems.Where(x => x.xcuda_Item.AsycudaDocument.CNumber != null).Sum(xx => xx.Suplementary_Quantity);

                if (previousItem.QtyAllocated == 0)
                {
                    return;
                }

                if (pqty != 0 && previousItem.xcuda_PreviousItems.Sum(x => x.Suplementary_Quantity) == previousItem.ItemQuantity)
                {
                    pqty = Convert.ToDouble(previousItem.xcuda_PreviousItems.Sum(x => x.Suplementary_Quantity));
                }


                if (pqty == 0 && (eld.Quantity > (previousItem.ItemQuantity - previousItem.xcuda_PreviousItems.Sum(x => x.Suplementary_Quantity))))
                {
                    pqty = Convert.ToDouble(previousItem.xcuda_PreviousItems.Sum(x => x.Suplementary_Quantity));
                }


                if (previousItem.ItemQuantity == apqty)
                {
                    mypod.Allocations.Clear();
                    eld.EntryDataDetails.Clear();
                    eld.Quantity = 0;
                    return;
                }
                if (PdfpAllocated - (apqty + Convert.ToDouble(eld.Quantity)) < 0)
                {
                    var qty = PdfpAllocated - (apqty);

                    var lst  = eld.EntryDataDetails.OrderBy(x => (x as EntryDataDetails).Sales.EntryDataDate).ToList();
                    var aqty = PdfpAllocated;
                    var lqty = lst.Sum(x => x.QtyAllocated);
                    while (qty < lqty)
                    {
                        if (lst.Any() == false)
                        {
                            break;
                        }
                        lqty -= lst.ElementAt(0).QtyAllocated;

                        //if (aqty < qty)
                        //{
                        //}
                        var entlst = mypod.Allocations.Where(x => x.EntryDataDetails.EntryDataDetailsId == lst.ElementAt(0).EntryDataDetailsId).ToList();
                        foreach (var item in entlst)
                        {
                            mypod.Allocations.Remove(item);
                        }


                        eld.EntryDataDetails.Remove(lst.ElementAt(0));
                        lst.RemoveAt(0);
                    }


                    eld.Quantity = qty;
                }
            }
            catch (Exception Ex)
            {
                throw;
            }
        }
        private async Task <int> InitializeDocumentCT(int itmcount, string prevEntryId, AllocationsModel.MyPodData mypod,
                                                      DocumentCT cdoc, AsycudaDocumentSet docSet, string prevIM7, AllocationsModel.AllocationDataBlock monthyear, Document_Type dt,
                                                      string dfp)
        {
            try
            {
                if (MaxLineCount(itmcount) ||
                    InvoicePerEntry(prevEntryId, mypod) ||
                    (cdoc.Document == null || itmcount == 0) ||
                    IsPerIM7(prevIM7, monthyear))
                {
                    if (itmcount != 0)
                    {
                        if (cdoc.Document != null)
                        {
                            await SaveDocumentCT(cdoc).ConfigureAwait(false);

                            //}
                            //else
                            //{
                            cdoc = await BaseDataModel.Instance.CreateDocumentCt(docSet).ConfigureAwait(true);
                        }
                    }
                    Ex9InitializeCdoc(dt, dfp, cdoc, docSet);
                    if (PerIM7 == true)
                    {
                        cdoc.Document.xcuda_Declarant.Number =
                            cdoc.Document.xcuda_Declarant.Number.Replace(
                                docSet.Declarant_Reference_Number,
                                docSet.Declarant_Reference_Number + "-" +
                                monthyear.CNumber);
                    }
                    InsertEntryIdintoRefNum(cdoc, mypod.EntlnData.EntryDataDetails[0].EntryDataId);

                    itmcount = 0;
                }
                return(itmcount);
            }
            catch (Exception)
            {
                throw;
            }
        }