Ejemplo n.º 1
0
        public static List <INCostAdjustmentInfo> AllocateOverRCTLines(IEnumerable <POReceiptLine> aLines, PXCache aCache, LandedCostCode aLCCode, LandedCostTran aTran, IEnumerable <LandedCostTranSplit> aSplits)
        {
            List <INCostAdjustmentInfo> result = new List <INCostAdjustmentInfo>();
            Decimal toDistribute    = aTran.LCAmount.Value;
            Decimal baseTotal       = decimal.Zero;
            int     distributeCount = 0;

            foreach (POReceiptLine iDet in aLines)
            {
                if (LandedCostUtils.IsApplicable(aTran, aSplits, aLCCode, iDet))
                {
                    baseTotal += LandedCostUtils.GetBaseValue(aLCCode, iDet);
                    distributeCount++;
                }
            }
            Decimal leftToDistribute  = toDistribute;
            INCostAdjustmentInfo last = null;

            foreach (POReceiptLine iDet in aLines)
            {
                if (LandedCostUtils.IsApplicable(aTran, aSplits, aLCCode, iDet))
                {
                    decimal shareAmt = Decimal.Zero;
                    if (distributeCount > 1)
                    {
                        shareAmt = LandedCostUtils.CalcAllocationValue(aLCCode, iDet, baseTotal, toDistribute);
                        shareAmt = PXDBCurrencyAttribute.BaseRound(aCache.Graph, shareAmt);
                    }
                    else
                    {
                        shareAmt = toDistribute;
                    }
                    if (shareAmt != Decimal.Zero)
                    {
                        leftToDistribute -= shareAmt;
                        INCostAdjustmentInfo newLine = new INCostAdjustmentInfo(iDet);
                        //POReceiptLine newLine = (POReceiptLine)aCache.CreateCopy(iDet);
                        //newLine.CuryExtCost = shareAmt;
                        //newLine.ExtCost = shareAmt;
                        newLine.AdjustmentAmount = shareAmt;
                        if (last == null || Math.Abs(last.AdjustmentAmount) < Math.Abs(newLine.AdjustmentAmount))
                        {
                            last = newLine;
                        }
                        result.Add(newLine);
                    }
                }
            }
            if (leftToDistribute != Decimal.Zero)
            {
                if (last == null)
                {
                    last = new INCostAdjustmentInfo();
                    last.AdjustmentAmount = leftToDistribute;
                    result.Add(last);
                }
                else
                {
                    last.AdjustmentAmount += leftToDistribute;
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public virtual void ReleaseLCTrans(IEnumerable <LandedCostTran> aTranSet, DocumentList <INRegister> aINCreated, DocumentList <APInvoice> aAPCreated)
        {
            Dictionary <int, APInvoiceEntry>    apGraphs = new Dictionary <int, APInvoiceEntry>();
            Dictionary <int, INAdjustmentEntry> inGraphs = new Dictionary <int, INAdjustmentEntry>();
            Dictionary <int, int>    combinations        = new Dictionary <int, int>();
            List <APRegister>        forReleaseAP        = new List <APRegister>();
            List <INRegister>        forReleaseIN        = new List <INRegister>();
            DocumentList <APInvoice> apDocuments         = new DocumentList <APInvoice>(this);
            POSetup poSetupR          = this.poSetup.Select();
            bool    autoReleaseIN     = poSetupR.AutoReleaseLCIN.Value;
            bool    autoReleaseAP     = poSetupR.AutoReleaseAP.Value;
            bool    noApplicableItems = false;

            foreach (LandedCostTran iTran in aTranSet)
            {
                LandedCostCode lcCode = PXSelect <LandedCostCode, Where <LandedCostCode.landedCostCodeID, Equal <Required <LandedCostCode.landedCostCodeID> > > > .Select(this, iTran.LandedCostCodeID);

                if ((string.IsNullOrEmpty(iTran.APDocType) || string.IsNullOrEmpty(iTran.APRefNbr)) && iTran.PostponeAP == false)
                {
                    APInvoiceEntry apGraph = null;
                    foreach (KeyValuePair <int, APInvoiceEntry> iGraph in apGraphs)
                    {
                        APInvoice apDoc = iGraph.Value.Document.Current;
                        string    terms = String.IsNullOrEmpty(iTran.TermsID) ? lcCode.TermsID : iTran.TermsID;

                        if (apDoc.VendorID == iTran.VendorID &&
                            apDoc.VendorLocationID == iTran.VendorLocationID &&
                            apDoc.InvoiceNbr == iTran.InvoiceNbr &&
                            apDoc.CuryID == iTran.CuryID &&
                            apDoc.DocDate == iTran.InvoiceDate &&
                            apDoc.TermsID == terms &&
                            (apDoc.DocType == AP.APDocType.Invoice && iTran.CuryLCAmount > Decimal.Zero))
                        {
                            combinations.Add(iTran.LCTranID.Value, iGraph.Key);
                            apGraph = iGraph.Value;
                        }
                    }
                    if (apGraph == null)
                    {
                        apGraph = PXGraph.CreateInstance <APInvoiceEntry>();
                        if (autoReleaseAP)
                        {
                            apGraph.APSetup.Current.RequireControlTotal = false;
                            apGraph.APSetup.Current.HoldEntry           = false;
                        }
                        apGraphs[iTran.LCTranID.Value] = apGraph;
                    }
                    apGraph.InvoiceLandedCost(iTran, null, false);
                    apDocuments.Add(apGraph.Document.Current);
                }

                if (lcCode.AllocationMethod != LandedCostAllocationMethod.None)
                {
                    List <POReceiptLine>       receiptLines = new List <POReceiptLine>();
                    List <LandedCostTranSplit> lcTranSplits = new List <LandedCostTranSplit>();
                    GetReceiptLinesToAllocate(receiptLines, lcTranSplits, iTran);
                    List <LandedCostUtils.INCostAdjustmentInfo> result = LandedCostUtils.AllocateOverRCTLines(receiptLines, this.poLines.Cache, lcCode, iTran, lcTranSplits);
                    if (result.Count > 0)
                    {
                        if (result.Count == 1 && !result[0].InventoryID.HasValue)
                        {
                            noApplicableItems = true;                              //Skip Cost adjustment creation;
                        }
                        else
                        {
                            INAdjustmentEntry inGraph = PXGraph.CreateInstance <INAdjustmentEntry>();
                            if (autoReleaseIN)
                            {
                                inGraph.insetup.Current.RequireControlTotal = false;
                                inGraph.insetup.Current.HoldEntry           = false;
                            }
                            CreateCostAjustment(inGraph, lcCode, iTran, result);
                            inGraphs[iTran.LCTranID.Value] = inGraph;
                        }
                    }
                }
            }

            using (PXConnectionScope cs = new PXConnectionScope())
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    foreach (LandedCostTran iTran in aTranSet)
                    {
                        bool           needUpdate = false;
                        LandedCostTran tran       = this.landedCostTrans.Select(iTran.LCTranID);

                        if (apGraphs.ContainsKey(tran.LCTranID.Value))
                        {
                            APInvoiceEntry apGraph = apGraphs[iTran.LCTranID.Value];
                            apGraph.Save.Press();
                            tran.APDocType = apGraph.Document.Current.DocType;
                            tran.APRefNbr  = apGraph.Document.Current.RefNbr;
                            if (!tran.APCuryInfoID.HasValue)
                            {
                                tran.APCuryInfoID = apGraph.Document.Current.CuryInfoID;
                            }
                            tran.Processed = true;
                            forReleaseAP.Add(apGraph.Document.Current);
                            if (aAPCreated != null)
                            {
                                aAPCreated.Add(apGraph.Document.Current);
                            }
                            needUpdate = true;
                        }
                        else if (combinations.ContainsKey(tran.LCTranID.Value))
                        {
                            //Its already saved at this point
                            APInvoiceEntry apGraph = apGraphs[combinations[tran.LCTranID.Value]];
                            tran.APDocType = apGraph.Document.Current.DocType;
                            tran.APRefNbr  = apGraph.Document.Current.RefNbr;
                            if (!tran.APCuryInfoID.HasValue)
                            {
                                tran.APCuryInfoID = apGraph.Document.Current.CuryInfoID;
                            }
                            tran.Processed = true;
                            needUpdate     = true;
                        }

                        if (inGraphs.ContainsKey(tran.LCTranID.Value))
                        {
                            INAdjustmentEntry inGraph = inGraphs[iTran.LCTranID.Value];
                            inGraph.Save.Press();
                            tran.INDocType = inGraph.adjustment.Current.DocType;
                            tran.INRefNbr  = inGraph.adjustment.Current.RefNbr;
                            tran.Processed = true;
                            forReleaseIN.Add(inGraph.adjustment.Current);
                            if (aINCreated != null)
                            {
                                aINCreated.Add(inGraph.adjustment.Current);
                            }
                            needUpdate = true;
                        }
                        if (needUpdate)
                        {
                            LandedCostTran copy = (LandedCostTran)this.landedCostTrans.Cache.CreateCopy(tran);
                            tran = this.landedCostTrans.Update(copy);
                        }
                    }
                    this.Actions.PressSave();
                    ts.Complete();
                }
            }
            if (noApplicableItems == true)
            {
                throw new NoApplicableSourceException(Messages.LandedCostAmountRemainderCannotBeDistributedMultyLines);
            }

            if (autoReleaseAP)
            {
                if (forReleaseAP.Count > 0)
                {
                    APDocumentRelease.ReleaseDoc(forReleaseAP, true);
                }
            }

            if (autoReleaseIN)
            {
                if (forReleaseIN.Count > 0)
                {
                    INDocumentRelease.ReleaseDoc(forReleaseIN, false);
                }
            }
        }