Ejemplo n.º 1
0
        public virtual IEnumerable pOreceiptslist()
        {
            APInvoice doc = Base.Document.Current;

            if (doc?.VendorID == null ||
                doc.VendorLocationID == null ||
                doc.DocType != APDocType.Invoice &&
                doc.DocType != APDocType.DebitAdj)
            {
                yield break;
            }

            string poReceiptType = doc.DocType == APDocType.Invoice
                                ? POReceiptType.POReceipt
                                : POReceiptType.POReturn;

            Dictionary <APTran, int> usedReceipt = new Dictionary <APTran, int>(new POReceiptComparer());

            int count;

            foreach (APTran aPTran in Base.Transactions.Cache.Inserted)
            {
                if (aPTran.ReceiptNbr != null)
                {
                    usedReceipt.TryGetValue(aPTran, out count);
                    usedReceipt[aPTran] = count + 1;
                }
            }

            foreach (APTran aPTran in Base.Transactions.Cache.Deleted)
            {
                if (aPTran.ReceiptNbr != null && Base.Transactions.Cache.GetStatus(aPTran) != PXEntryStatus.InsertedDeleted)
                {
                    usedReceipt.TryGetValue(aPTran, out count);
                    usedReceipt[aPTran] = count - 1;
                }
            }

            foreach (APTran aPTran in Base.Transactions.Cache.Updated)
            {
                string originalValue = (string)Base.Transactions.Cache.GetValueOriginal <APTran.receiptNbr>(aPTran);
                if (aPTran.ReceiptNbr != originalValue)
                {
                    if (originalValue != null)
                    {
                        APTran originTran = new APTran {
                            ReceiptNbr = originalValue
                        };
                        usedReceipt.TryGetValue(originTran, out count);
                        usedReceipt[originTran] = count - 1;
                    }
                    if (aPTran.ReceiptNbr != null)
                    {
                        usedReceipt.TryGetValue(aPTran, out count);
                        usedReceipt[aPTran] = count + 1;
                    }
                }
            }

            PXSelectBase <POReceipt> cmd = new PXSelectJoinGroupBy <
                POReceipt,
                InnerJoin <POReceiptLineS, On <POReceiptLineS.receiptNbr, Equal <POReceipt.receiptNbr> >,
                           LeftJoin <APTran, On <APTran.released, Equal <False>,
                                                 And <Where <POReceiptLineS.pOAccrualType, Equal <POAccrualType.receipt>,
                                                             And <APTran.receiptNbr, Equal <POReceiptLineS.receiptNbr>,
                                                                  And <APTran.receiptType, Equal <POReceiptLineS.receiptType>,
                                                                       And <APTran.receiptLineNbr, Equal <POReceiptLineS.lineNbr>,
                                                                            Or <POReceiptLineS.pOAccrualType, Equal <POAccrualType.order>,
                                                                                And <APTran.pOOrderType, Equal <POReceiptLineS.pOType>,
                                                                                     And <APTran.pONbr, Equal <POReceiptLineS.pONbr>,
                                                                                          And <APTran.pOLineNbr, Equal <POReceiptLineS.pOLineNbr> > > > > > > > > > > > >,
                Where <POReceipt.hold, Equal <False>,
                       And <POReceipt.released, Equal <True>,
                            And <POReceipt.receiptType, Equal <Required <POReceipt.receiptType> >,
                                 And <APTran.refNbr, IsNull,
                                      And <POReceiptLineS.unbilledQty, Greater <decimal0>,
                                           And <Where <POReceiptLineS.pONbr, Equal <Current <POReceiptFilter.orderNbr> >,
                                                       Or <Current <POReceiptFilter.orderNbr>, IsNull> > > > > > > >,
                Aggregate <
                    GroupBy <POReceipt.receiptType,
                             GroupBy <POReceipt.receiptNbr,
                                      Sum <POReceiptLineS.receiptQty,
                                           Sum <POReceiptLineS.unbilledQty,
                                                Count <POReceiptLineS.lineNbr> > > > > > >(Base);

            if (Base.APSetup.Current.RequireSingleProjectPerDocument == true)
            {
                cmd.WhereAnd <Where <POReceipt.projectID, Equal <Current <APInvoice.projectID> > > >();
            }

            if (PXAccess.FeatureInstalled <FeaturesSet.vendorRelations>())
            {
                cmd.Join <LeftJoin <POOrder, On <POOrder.orderType, Equal <POReceiptLineS.pOType>, And <POOrder.orderNbr, Equal <POReceiptLineS.pONbr> > > > >();

                cmd.WhereAnd <Where <POReceipt.vendorID, Equal <Current <APInvoice.suppliedByVendorID> >,
                                     And <POReceipt.vendorLocationID, Equal <Current <APInvoice.suppliedByVendorLocationID> >,
                                          And <Where <POOrder.payToVendorID, IsNull, Or <POOrder.payToVendorID, Equal <Current <APInvoice.vendorID> > > > > > > >();
            }
            else
            {
                cmd.WhereAnd <Where <POReceipt.vendorID, Equal <Current <APInvoice.vendorID> >,
                                     And <POReceipt.vendorLocationID, Equal <Current <APInvoice.vendorLocationID> > > > >();
            }

            foreach (PXResult <POReceipt, POReceiptLineS, APTran> result in cmd.View.SelectMultiBound(new object[] { doc }, poReceiptType))
            {
                POReceipt receipt = result;
                APTran    aPTran  = new APTran {
                    ReceiptNbr = receipt.ReceiptNbr
                };

                if (usedReceipt.TryGetValue(aPTran, out count))
                {
                    usedReceipt.Remove(aPTran);

                    if (count < result.RowCount)
                    {
                        yield return(receipt);
                    }
                }
                else
                {
                    yield return(receipt);
                }
            }

            foreach (APTran deletedTran in usedReceipt.Where(_ => _.Value < 0).Select(_ => _.Key))
            {
                yield return(PXSelect <POReceipt, Where <POReceipt.receiptNbr, Equal <Required <APTran.receiptNbr> > > >
                             .SelectSingleBound(Base, new object[] { }, deletedTran.ReceiptNbr)
                             .RowCast <POReceipt>()
                             .First());
            }
        }