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;
            bool    noApplicableTransfers = 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.RequireControlTaxTotal = 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);
                    var lch = new LandedCostHelper(this, false);
                    List <LandedCostHelper.POReceiptLineAdjustment> result = lch.AllocateLCOverRCTLines(receiptLines, lcCode, iTran, lcTranSplits);
                    if (result.Count > 0)
                    {
                        if (result.Count == 1 && !result[0].Item1.InventoryID.HasValue)
                        {
                            noApplicableTransfers = !lch.HasApplicableTransfers;
                            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;
                            if (apGraph.Document.Current.Hold != 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 && tran.PostponeAP == true)
                        {
                            LandedCostCode lcCode = PXSelect <LandedCostCode, Where <LandedCostCode.landedCostCodeID, Equal <Required <LandedCostCode.landedCostCodeID> > > > .Select(this, iTran.LandedCostCodeID);

                            if (lcCode.AllocationMethod == LandedCostAllocationMethod.None)
                            {
                                tran.Processed = true;
                                needUpdate     = true;                             //This combination needs no processing here but must be updated
                            }
                        }
                        if (needUpdate)
                        {
                            LandedCostTran copy = (LandedCostTran)this.landedCostTrans.Cache.CreateCopy(tran);
                            tran = this.landedCostTrans.Update(copy);
                        }
                    }
                    this.Actions.PressSave();
                    ts.Complete();
                }
            }
            if (noApplicableTransfers == true)
            {
                throw new NoApplicableSourceException(Messages.LandedCostCannotBeDistributed);
            }
            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);
                }
            }
        }
Example #2
0
        public static void ReleaseDoc(List <POReceipt> list, bool aIsMassProcess)
        {
            POReceiptEntry            docgraph        = PXGraph.CreateInstance <POReceiptEntry>();
            DocumentList <INRegister> created         = new DocumentList <INRegister>(docgraph);
            DocumentList <APInvoice>  invoicesCreated = new DocumentList <APInvoice>(docgraph);
            INReceiptEntry            iRe             = null;
            INIssueEntry      iIe = null;
            INAdjustmentEntry iAe = null;

            AP.APInvoiceEntry apInvoiceGraph = docgraph.CreateAPInvoiceEntry();
            int  iRow   = 0;
            bool failed = false;

            foreach (POReceipt order in list)
            {
                try
                {
                    switch (order.ReceiptType)
                    {
                    case POReceiptType.POReceipt:
                    case POReceiptType.TransferReceipt:
                        if (iRe == null)
                        {
                            iRe = docgraph.CreateReceiptEntry();
                        }
                        docgraph.ReleaseReceipt(iRe, apInvoiceGraph, order, created, invoicesCreated, aIsMassProcess);
                        break;

                    case POReceiptType.POReturn:
                        if (order.ReturnOrigCost == true)
                        {
                            if (iAe == null)
                            {
                                iAe = docgraph.CreateAdjustmentEntry();
                            }
                            docgraph.ReleaseReturnViaAdjustment(iAe, apInvoiceGraph, order, created, invoicesCreated, aIsMassProcess);
                        }
                        else
                        {
                            if (iIe == null)
                            {
                                iIe = docgraph.CreateIssueEntry();
                            }
                            docgraph.ReleaseReturnViaIssue(iIe, apInvoiceGraph, order, created, invoicesCreated, aIsMassProcess);
                        }
                        break;
                    }
                    PXProcessing <POReceipt> .SetInfo(iRow, ActionsMessages.RecordProcessed);
                }
                catch (Exception e)
                {
                    if (aIsMassProcess)
                    {
                        PXProcessing <POReceipt> .SetError(iRow, e);

                        failed = true;
                    }
                    else
                    {
                        throw;
                    }
                }
                iRow++;
            }
            if (failed)
            {
                throw new PXException(Messages.ReleaseOfOneOrMoreReceiptsHasFailed);
            }
        }
Example #3
0
        public virtual void CreateAdjustmentTran(INAdjustmentEntry inGraph, List <LandedCostHelper.POReceiptLineAdjustment> pOLinesToProcess, LandedCostCode aLCCode, LandedCostTran aTran, string ReasonCode)
        {
            foreach (LandedCostHelper.POReceiptLineAdjustment poreceiptline in pOLinesToProcess)
            {
                INTran intran   = new INTran();
                bool   isLCTran = aTran != null;

                INTran origtran   = LandedCostHelper.GetOriginalInTran(inGraph, poreceiptline.Item1.ReceiptNbr, poreceiptline.Item1.LineNbr);
                bool   isDropShip = (poreceiptline.Item1.LineType == POLineType.GoodsForDropShip || poreceiptline.Item1.LineType == POLineType.NonStockForDropShip);

                if (!isDropShip && origtran == null)
                {
                    throw new PXException(AP.Messages.CannotFindINReceipt, poreceiptline.Item1.ReceiptNbr);
                }

                //Drop-Ships are considered non-stocks
                if (poreceiptline.Item1.IsStockItem())
                {
                    intran.TranType = INTranType.ReceiptCostAdjustment;
                }
                else
                {
                    //Landed cost and PPV for non-stock items are handled in special way in the inventory.
                    //They should create a GL Batch, but for convinience and unforminty this functionality is placed into IN module
                    //Review this part when the functionality is implemented in IN module
                    intran.IsCostUnmanaged = true;
                    intran.TranType        = INTranType.Adjustment;
                    intran.InvtMult        = 0;
                }
                intran.InventoryID = poreceiptline.Item1.InventoryID;
                intran.SubItemID   = poreceiptline.Item1.SubItemID;
                intran.SiteID      = poreceiptline.Item1.SiteID;
                intran.BAccountID  = isLCTran ? aTran.VendorID : poreceiptline.Item1.VendorID;
                intran.BranchID    = poreceiptline.BranchID;


                if (isDropShip && intran.SiteID != null)
                {
                    INSite invSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <POReceiptLine.siteID> > > > .Select(inGraph, intran.SiteID);

                    if (invSite.DropShipLocationID == null)
                    {
                        throw new PXException(SO.Messages.NoDropShipLocation, invSite.SiteCD);
                    }

                    intran.LocationID = invSite.DropShipLocationID;
                }
                else
                {
                    intran.LocationID = poreceiptline.Item1.LocationID ?? origtran.LocationID;
                }

                intran.LotSerialNbr     = poreceiptline.Item1.LotSerialNbr;
                intran.POReceiptType    = poreceiptline.Item1.ReceiptType;
                intran.POReceiptNbr     = poreceiptline.Item1.ReceiptNbr;
                intran.POReceiptLineNbr = poreceiptline.Item1.LineNbr;

                //tran.Qty = poreceiptline.ReceiptQty;
                intran.TranDesc = isLCTran ? aTran.Descr : poreceiptline.Item1.TranDesc;
                //tran.UnitCost = PXDBPriceCostAttribute.Round(inGraph.transactions.Cache, (decimal)(poreceiptline.ExtCost / poreceiptline.ReceiptQty));
                intran.TranCost   = poreceiptline.Item2;
                intran.ReasonCode = ReasonCode;
                if (origtran != null && origtran.DocType == INDocType.Issue)
                {
                    intran.ARDocType = origtran.ARDocType;
                    intran.ARRefNbr  = origtran.ARRefNbr;
                    intran.ARLineNbr = origtran.ARLineNbr;
                }
                if (!isDropShip)
                {
                    intran.OrigTranType = origtran.DocType;
                    intran.OrigRefNbr   = origtran.RefNbr;
                }

                int?acctID = null;
                int?subID  = null;
                if (isLCTran)
                {
                    intran.AcctID = aLCCode.LCAccrualAcct;
                    intran.SubID  = aLCCode.LCAccrualSub;
                    GetLCVarianceAccountSub(ref acctID, ref subID, inGraph, poreceiptline.Item1);
                }
                else
                {
                    //Set AcctID and SubID = POAccrual Acct/Sub from orig. INTran
                    if (origtran != null)
                    {
                        intran.AcctID = origtran.AcctID;
                        intran.SubID  = origtran.SubID;
                    }
                    else if (isDropShip)
                    {
                        intran.AcctID = poreceiptline.Item1.POAccrualAcctID;
                        intran.SubID  = poreceiptline.Item1.POAccrualSubID;
                    }
                    ReasonCode reasonCode = PXSelect <ReasonCode, Where <ReasonCode.reasonCodeID, Equal <Required <ReasonCode.reasonCodeID> > > > .Select(inGraph, ReasonCode);

                    if (reasonCode == null)
                    {
                        throw new PXException(AP.Messages.ReasonCodeCannotNotFound, ReasonCode);
                    }
                    AP.APReleaseProcess.GetPPVAccountSub(ref acctID, ref subID, inGraph, poreceiptline.Item1, reasonCode);
                }
                intran.COGSAcctID = acctID;
                intran.COGSSubID  = subID;

                intran = inGraph.transactions.Insert(intran);
            }
        }
        protected static void CreateCostAjustment(INAdjustmentEntry inGraph, LandedCostCode aLCCode, LandedCostTran aTran, List <LandedCostUtils.INCostAdjustmentInfo> aAllocatedLines)
        {
            inGraph.Clear();
            inGraph.insetup.Current.RequireControlTotal = false;
            inGraph.insetup.Current.HoldEntry           = false;

            INRegister newdoc = new INRegister();

            newdoc.DocType    = INDocType.Adjustment;
            newdoc.OrigModule = BatchModule.PO;
            newdoc.SiteID     = null;
            newdoc.TranDate   = aTran.InvoiceDate;
            inGraph.adjustment.Insert(newdoc);
            //Find IN Receipt (released??) for the LCTran's  POReceipt
            foreach (LandedCostUtils.INCostAdjustmentInfo it in aAllocatedLines)
            {
                PXResult <INRegister, INTran> res = (PXResult <INRegister, INTran>) PXSelectJoin <INRegister, InnerJoin <INTran, On <INTran.docType, Equal <INRegister.docType>,
                                                                                                                                     And <INTran.refNbr, Equal <INRegister.refNbr> > > >,
                                                                                                  Where <INRegister.docType, NotEqual <INDocType.adjustment>, And <INTran.pOReceiptNbr, Equal <Required <INTran.pOReceiptNbr> > > > > .SelectWindowed(inGraph, 0, 1, it.POReceiptNbr);

                INRegister inReceipt = new INRegister();
                if (res == null || (inReceipt = res).Released != true)
                {
                    throw new PXException(Messages.INReceiptMustBeReleasedBeforeLCProcessing, inReceipt.RefNbr, aTran.POReceiptNbr);
                }

                INTran origtran = res;

                INTran tran = new INTran();
                if (it.IsStockItem)
                {
                    tran.TranType = INTranType.ReceiptCostAdjustment;
                }
                else
                {
                    //Landed cost for non-stock items are handled in special way in the inventory.
                    //They should create a GL Batch, but for convinience and unforminty this functionality is placed into IN module
                    //Review this part when the functionality is implemented in IN module
                    tran.TranType = INTranType.Adjustment;
                    tran.InvtMult = 0;
                }
                tran.InventoryID      = it.InventoryID;
                tran.SubItemID        = it.SubItemID;
                tran.SiteID           = it.SiteID;
                tran.BAccountID       = aTran.VendorID;
                tran.LocationID       = it.LocationID ?? origtran.LocationID;
                tran.LotSerialNbr     = it.LotSerialNbr;
                tran.POReceiptNbr     = it.POReceiptNbr;
                tran.POReceiptLineNbr = it.POReceiptLineNbr;

                tran.ARDocType = (origtran.DocType == INDocType.Issue) ? origtran.ARDocType : null;
                tran.ARRefNbr  = (origtran.DocType == INDocType.Issue) ? origtran.ARRefNbr : null;
                tran.ARLineNbr = (origtran.DocType == INDocType.Issue) ? origtran.ARLineNbr : null;

                //tran.Qty = it.ReceiptQty;
                tran.TranDesc = aTran.Descr;
                //tran.UnitCost = PXDBPriceCostAttribute.Round(inGraph.transactions.Cache, (decimal)(it.ExtCost / it.ReceiptQty));
                tran.TranCost     = it.AdjustmentAmount;
                tran.ReasonCode   = aLCCode.ReasonCode;
                tran.OrigTranType = inReceipt.DocType;
                tran.OrigRefNbr   = inReceipt.RefNbr;
                tran.AcctID       = aLCCode.LCAccrualAcct;
                tran.SubID        = aLCCode.LCAccrualSub;
                int?acctID = null;
                int?subID  = null;
                GetLCVarianceAccountSub(ref acctID, ref subID, inGraph, it);
                tran.COGSAcctID = acctID;
                tran.COGSSubID  = subID;
                tran            = inGraph.transactions.Insert(tran);
            }
        }