private void SetWeights(AllocationsModel.AlloEntryLineData pod, global::DocumentItemDS.Business.Entities.xcuda_PreviousItem pitm, string dfp)
        {
            try
            {
                var previousItem = pod.PreviousDocumentItem as xcuda_Item;
                if (previousItem == null)
                {
                    return;
                }
                var pw = Convert.ToDouble(previousItem.xcuda_Valuation_item.xcuda_Weight_itm.Net_weight_itm);
                //Double iw = System.Convert.ToDouble(Math.Round((pod.PreviousDocumentItem.xcuda_Valuation_item.xcuda_Weight_itm.Net_weight_itm
                //                    / pod.PreviousDocumentItem.ItemQuantity) * Convert.ToDouble(pod.Quantity), 2));
                var iw = Convert.ToDouble((previousItem.xcuda_Valuation_item.xcuda_Weight_itm.Net_weight_itm
                                           / previousItem.ItemQuantity) * Convert.ToDouble(pod.Quantity));

                var ppdfpqty =
                    previousItem.xcuda_PreviousItems.Where(x => x.DutyFreePaid != dfp)
                    .Sum(x => x.Suplementary_Quantity);
                var pi = previousItem.ItemQuantity -
                         previousItem.xcuda_PreviousItems.Where(x => x.DutyFreePaid == dfp)
                         .Sum(x => x.Suplementary_Quantity);
                var pdfpqty = (dfp == "Duty Free"
                    ? previousItem.DPQtyAllocated
                    : previousItem.DFQtyAllocated);
                var rw = previousItem.xcuda_PreviousItems.ToList().Sum(x => x.Net_weight);
                if ((pdfpqty == ppdfpqty) &&
                    (pi == pod.Quantity) &&
                    (previousItem.ItemQuantity - previousItem.QtyAllocated == 0))
                //                //(pod.PreviousDocumentItem.AsycudaSalesAllocations.Max(x => x.EntryDataDetailsId) == pod.EntryDataDetails.Max(x => x.EntryDataDetailsId) && (pod.PreviousDocumentItem.ItemQuantity - pod.PreviousDocumentItem.QtyAllocated == 0))
                {
                    pitm.Net_weight = Convert.ToDouble(pw - rw);
                }
                else
                {
                    pitm.Net_weight = Convert.ToDouble(Math.Round(iw, 2));
                }
                //pitm.Prev_net_weight = pw;
                pitm.Prev_net_weight = Convert.ToDouble(pw - rw);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task CreateIncompOPS(string filterExpression, AsycudaDocumentSet docSet)
        {
            if (docSet == null)
            {
                throw new ApplicationException("Please Select a Asycuda Document Set before proceding");
            }

            var cdoc = new DocumentCT();

            cdoc.Document = BaseDataModel.Instance.CreateNewAsycudaDocument(docSet);

            StatusModel.Timer("Getting Data ...");

            var itmcount = cdoc.DocumentItems.Count();
            var slst     = await AllocationsModel.Instance.GetAsycudaSalesAllocations(filterExpression).ConfigureAwait(false);


            var dt = BaseDataModel.Instance.Document_Types.AsEnumerable().FirstOrDefault(x => x.DisplayName == "IM7");

            if (dt == null)
            {
                throw new ApplicationException(string.Format("Null Document Type for '{0}' Contact your Network Administrator", "IM7"));
                return;
            }

            dt.DefaultCustoms_Procedure = BaseDataModel.Instance.Customs_Procedures.AsEnumerable().FirstOrDefault(x => x.DisplayName.Contains("OPS") && x.Document_TypeId == dt.Document_TypeId);

            if (dt.DefaultCustoms_Procedure == null)
            {
                throw new ApplicationException(string.Format("Null Customs Procedure for '{0}' Contact your Network Administrator", "OPS"));
                return;
            }

            IncompOpsIntializeCdoc(cdoc, dt, docSet);

            var cslst = slst.Where(x => x.EntryDataDetails.Quantity > 0)
                        .Select(x => x.EntryDataDetails).Distinct();

            StatusModel.StartStatusUpdate("Creating Incomplete Allocation OPS entries", cslst.Count());
            foreach (var pod in cslst)
            {
                StatusModel.StatusUpdate();
                var el = new AllocationsModel.AlloEntryLineData()
                {
                    ItemNumber       = pod.ItemNumber,
                    ItemDescription  = pod.ItemDescription,
                    Cost             = pod.Cost,
                    TariffCode       = pod.EntryDataDetailsEx.TariffCode,
                    Quantity         = pod.Quantity - pod.QtyAllocated,
                    EntryDataDetails = new List <IEntryDataDetail>()
                    {
                        pod
                    }
                };

                var itm = BaseDataModel.Instance.CreateItemFromEntryDataDetail(el, cdoc);
                //  SwitchToMyDB(mydb, itm);
                cdoc.DocumentItems.Add(itm);

                // pod.PreviousDocumentItem = itm;


                itmcount += 1;
                if (itmcount % BaseDataModel.Instance.CurrentApplicationSettings.MaxEntryLines == 0)
                {
                    //dup new file
                    cdoc          = new DocumentCT();
                    cdoc.Document = BaseDataModel.Instance.CreateNewAsycudaDocument(docSet);

                    IncompOpsIntializeCdoc(cdoc, dt, docSet);
                }
            }
            if (cdoc.DocumentItems.Count == 0)
            {
                cdoc = null;
            }

            await BaseDataModel.Instance.SaveDocumentCT(cdoc).ConfigureAwait(false);

            StatusModel.StopStatusUpdate();
        }
        private global::DocumentItemDS.Business.Entities.xcuda_PreviousItem CreatePreviousItem(AllocationsModel.AlloEntryLineData pod, int itmcount, string dfp)
        {
            try
            {
                var previousItem = (pod.PreviousDocumentItem as xcuda_Item);

                var pitm = new global::DocumentItemDS.Business.Entities.xcuda_PreviousItem()
                {
                    TrackingState = TrackingState.Added
                };
                if (previousItem == null)
                {
                    return(pitm);
                }

                pitm.Hs_code              = previousItem.xcuda_Tarification.xcuda_HScode.Commodity_code;
                pitm.Commodity_code       = "00";
                pitm.Current_item_number  = (itmcount + 1).ToString(); // piggy back the previous item count
                pitm.Previous_item_number = previousItem.LineNumber.ToString();


                SetWeights(pod, pitm, dfp);


                pitm.Previous_Packages_number = "0";


                pitm.Suplementary_Quantity           = Convert.ToDouble(pod.Quantity);
                pitm.Preveious_suplementary_quantity = Convert.ToDouble(previousItem.ItemQuantity);


                pitm.Goods_origin = previousItem.xcuda_Goods_description.Country_of_origin_code;
                double pval = previousItem.xcuda_Valuation_item.xcuda_Item_Invoice.Amount_national_currency;
                pitm.Previous_value = Convert.ToDouble((pval / previousItem.ItemQuantity));
                pitm.Current_value  = Convert.ToDouble((pval) / Convert.ToDouble(previousItem.ItemQuantity));
                pitm.Prev_reg_ser   = "C";
                pitm.Prev_reg_nbr   = previousItem.AsycudaDocument.CNumber;
                pitm.Prev_reg_dat   = previousItem.AsycudaDocument.RegistrationDate.GetValueOrDefault().Year.ToString();
                pitm.Prev_reg_cuo   = previousItem.AsycudaDocument.Customs_clearance_office_code;

                return(pitm);
            }
            catch (Exception)
            {
                throw;
            }
        }