Пример #1
0
        protected virtual void AccumulateAgeBalancesIntoStatements(IEnumerable <PXResult <ARRegister, ARInvoice> > familyDocuments, IDictionary <StatementKey, ARStatement> statements, bool ageCredits)
        {
            foreach (var document in familyDocuments)
            {
                ARRegister   doc = (ARRegister)document;
                ARInvoice    inv = (ARInvoice)document;
                StatementKey key = new StatementKey(doc.BranchID.Value, doc.CuryID);

                if (statements.ContainsKey(key) && IsMultipleInstallmentMaster(inv) == false &&
                    doc.DocType != AR.ARDocType.CashSale && doc.DocType != AR.ARDocType.CashReturn)
                {
                    AccumulateAgeBalances(statements[key], (doc.Payable == true ? inv : doc), ageCredits);
                }
            }
        }
Пример #2
0
        protected virtual void GenerateCustomerStatement(ARStatementCycle aCycle, Customer customer, DateTime aStatementDate,
                                                         Dictionary <Tuple <int, string, int, DateTime>, ARStatement> statementsTrace)
        {
            PXSelectBase <Customer>   selectCustomer = new PXSelect <Customer, Where <Customer.statementCycleId, Equal <Required <Customer.statementCycleId> > > >(this);
            PXSelectBase <ARRegister> selectOpenDocs = new PXSelectJoin <ARRegister, LeftJoin <ARInvoice, On <ARInvoice.docType, Equal <ARRegister.docType>,
                                                                                                              And <ARInvoice.refNbr, Equal <ARRegister.refNbr> > > >,
                                                                         Where <ARRegister.customerID, Equal <Required <ARRegister.customerID> >,
                                                                                And <ARRegister.released, Equal <BQLConstants.BitOn>,
                                                                                     And <ARRegister.docDate, LessEqual <Required <ARRegister.docDate> >,
                                                                                          And <Where <ARRegister.openDoc, Equal <BQLConstants.BitOn>,
                                                                                                      Or <ARRegister.statementDate, IsNull> > > > > > >(this);

            PXSelectBase <ARStatementDetail> selectDocsStmDetail = new PXSelect <ARStatementDetail, Where <ARStatementDetail.customerID,
                                                                                                           Equal <Required <ARStatementDetail.customerID> >,
                                                                                                           And <ARStatementDetail.docType, Equal <Required <ARStatementDetail.docType> >,
                                                                                                                And <ARStatementDetail.refNbr, Equal <Required <ARStatementDetail.refNbr> > > > >, OrderBy <Asc <ARStatementDetail.statementDate> > >(this);

            PXSelectBase <ARStatement> selectAllPreviousByCustomer = new PXSelect <ARStatement,
                                                                                   Where <ARStatement.customerID, Equal <Required <ARStatement.customerID> > >,
                                                                                   OrderBy <Asc <ARStatement.curyID, Desc <ARStatement.statementDate> > > >(this);

            PXSelectBase <ARStatement> selectPreviousStatement = new PXSelect <ARStatement,
                                                                               Where <ARStatement.branchID, Equal <Required <ARStatement.branchID> >,
                                                                                      And <ARStatement.customerID, Equal <Required <ARStatement.customerID> >,
                                                                                           And <ARStatement.curyID, Equal <Required <ARStatement.curyID> > > > >,
                                                                               OrderBy <Desc <ARStatement.statementDate> > >(this);

            bool ageCredits = (this.ARSetup.Current.AgeCredits ?? false);

            Dictionary <StatementKey, ARStatement> statements = new Dictionary <StatementKey, ARStatement>();
            List <ARStatementDetail> details = new List <ARStatementDetail>();

            foreach (PXResult <ARRegister, ARInvoice> iDoc in selectOpenDocs.Select(customer.BAccountID, aStatementDate))
            {
                ARStatement       header          = null;
                ARRegister        doc             = (ARRegister)iDoc;
                ARInvoice         inv             = (ARInvoice)iDoc;
                ARStatementDetail statementDetail = (ARStatementDetail)selectDocsStmDetail.Select(doc.CustomerID, doc.DocType, doc.RefNbr);
                bool         isNewDoc             = (statementDetail == null);
                StatementKey key = new StatementKey(doc.BranchID.Value, doc.CuryID);

                if (IsMultipleInstallmentMaster(inv))
                {
                    continue; //Skip invoice, which is the first in multiple installments sequence (master)
                }
                if (!statements.ContainsKey(key))
                {
                    header = new ARStatement();
                    Clear(header);
                    Copy(header, customer);
                    Copy(header, aCycle);
                    Copy(header, doc);
                    header.StatementDate = aStatementDate;

                    ARStatement trace    = null;
                    bool        gotTrace = statementsTrace.TryGetValue(new Tuple <int, string, int, DateTime>(header.BranchID.Value, header.CuryID, header.CustomerID.Value, header.StatementDate.Value), out trace);
                    if (gotTrace)
                    {
                        header.PrevPrintedCnt = trace.PrevPrintedCnt;
                        header.PrevEmailedCnt = trace.PrevEmailedCnt;
                    }
                    statements[key] = header;
                }
                else
                {
                    header = statements[key];
                }

                ARStatementDetail det = new ARStatementDetail();
                Copy(det, header);
                Copy(det, doc);

                if (doc.DocType != AR.ARDocType.CashSale && doc.DocType != AR.ARDocType.CashReturn)
                {
                    Accumulate(header, (doc.Payable == true ? inv : doc), aCycle, isNewDoc, ageCredits);
                }

                details.Add(det);
            }

            //Merge with previous statements - is needed for Balance Brought Forward
            Dictionary <StatementKey, DateTime> lastCuryStatement = new Dictionary <StatementKey, DateTime>();

            foreach (ARStatement iPrev in selectAllPreviousByCustomer.Select(customer.BAccountID))
            {
                ARStatement  header = null;
                StatementKey key    = new StatementKey(iPrev.BranchID.Value, iPrev.CuryID);
                if (lastCuryStatement.ContainsKey(key) && lastCuryStatement[key] > iPrev.StatementDate)
                {
                    continue;
                }

                if (!statements.ContainsKey(key))
                {
                    header = new ARStatement();
                    Clear(header);
                    header.BranchID      = iPrev.BranchID;
                    header.CuryID        = iPrev.CuryID;
                    header.CustomerID    = iPrev.CustomerID;
                    header.StatementDate = aStatementDate;
                    Copy(header, customer);
                    Copy(header, aCycle);
                    statements[key] = header;
                }
                else
                {
                    header = statements[key];
                }

                header.BegBalance     = iPrev.EndBalance;
                header.CuryBegBalance = iPrev.CuryEndBalance;
                Recalculate(header);
                lastCuryStatement[key] = iPrev.StatementDate.Value;
            }

            foreach (ARStatement iH in statements.Values)
            {
                ARStatement prev = selectPreviousStatement.Select(iH.CustomerID, iH.BranchID, iH.CuryID);
                if (prev != null)
                {
                    iH.BegBalance     = prev.EndBalance;
                    iH.CuryBegBalance = prev.CuryEndBalance;
                }

                ApplyFIFORule(iH, ageCredits);
            }

            PersistStatement(customer.BAccountID, aStatementDate, statements.Values, details);
        }
Пример #3
0
        protected virtual void GenerateCustomerStatement(StatementCreateBO statementGraph, ARStatementCycle aCycle, Customer customer, DateTime aStatementDate,
                                                         Dictionary <Tuple <int, string, int, DateTime>, ARStatement> statementsTrace, IList <PXResult <ARRegister, ARInvoice> > familyDocuments)
        {
            PXSelectBase <ARRegister> selectOpenDocs = new PXSelectJoin <ARRegister, LeftJoin <ARInvoice, On <ARInvoice.docType, Equal <ARRegister.docType>,
                                                                                                              And <ARInvoice.refNbr, Equal <ARRegister.refNbr> > > >,
                                                                         Where <ARRegister.customerID, Equal <Required <ARRegister.customerID> >,
                                                                                And <ARRegister.released, Equal <BQLConstants.BitOn>,
                                                                                     And <ARRegister.docDate, LessEqual <Required <ARRegister.docDate> >,
                                                                                          And <Where <ARRegister.openDoc, Equal <BQLConstants.BitOn>,
                                                                                                      Or <ARRegister.statementDate, IsNull> > > > > > >(this);

            PXSelectBase <ARStatementDetail> selectDocsStmDetail = new PXSelect <ARStatementDetail, Where <ARStatementDetail.customerID,
                                                                                                           Equal <Required <ARStatementDetail.customerID> >,
                                                                                                           And <ARStatementDetail.docType, Equal <Required <ARStatementDetail.docType> >,
                                                                                                                And <ARStatementDetail.refNbr, Equal <Required <ARStatementDetail.refNbr> > > > >, OrderBy <Asc <ARStatementDetail.statementDate> > >(this);

            PXSelectBase <ARStatement> selectAllPreviousByCustomer = new PXSelect <ARStatement,
                                                                                   Where <ARStatement.customerID, Equal <Required <ARStatement.customerID> > >,
                                                                                   OrderBy <Asc <ARStatement.curyID, Desc <ARStatement.statementDate> > > >(this);

            PXSelectBase <ARStatement> selectPreviousStatement = new PXSelect <ARStatement,
                                                                               Where <ARStatement.branchID, Equal <Required <ARStatement.branchID> >,
                                                                                      And <ARStatement.customerID, Equal <Required <ARStatement.customerID> >,
                                                                                           And <ARStatement.curyID, Equal <Required <ARStatement.curyID> > > > >,
                                                                               OrderBy <Desc <ARStatement.statementDate> > >(this);

            bool ageCredits = (this.ARSetup.Current.AgeCredits ?? false);
            bool isParent   = customer.StatementCustomerID == customer.BAccountID;

            Dictionary <StatementKey, ARStatement> statements = new Dictionary <StatementKey, ARStatement>();

            if (isParent)
            {
                foreach (PXResult <ARRegister, ARInvoice> res in familyDocuments)
                {
                    ARRegister   doc = res;
                    StatementKey key = new StatementKey(doc.BranchID.Value, doc.CuryID);
                    AddARStatement(statements, key, aCycle, customer, aStatementDate);
                }
            }

            List <ARStatementDetail> details = new List <ARStatementDetail>();

            foreach (PXResult <ARRegister, ARInvoice> iDoc in selectOpenDocs.Select(customer.BAccountID, aStatementDate))
            {
                ARRegister        doc             = iDoc;
                ARInvoice         inv             = iDoc;
                ARStatementDetail statementDetail = selectDocsStmDetail.Select(doc.CustomerID, doc.DocType, doc.RefNbr);
                bool         isNewDoc             = statementDetail == null;
                StatementKey key = new StatementKey(doc.BranchID.Value, doc.CuryID);

                familyDocuments.Add(iDoc);

                if (IsMultipleInstallmentMaster(inv))
                {
                    continue;                     //Skip invoice, which is the first in multiple installments sequence (master)
                }
                ARStatement header = AddARStatement(statements, key, aCycle, customer, aStatementDate);
                if (header.Processed != true)
                {
                    ARStatement prevStatement = selectPreviousStatement.Select(header.BranchID, header.CustomerID, header.CuryID);
                    if (prevStatement != null)
                    {
                        header.PrevStatementDate = prevStatement.StatementDate;
                    }

                    ARStatement trace;
                    if (statementsTrace.TryGetValue(new Tuple <int, string, int, DateTime>(header.BranchID.Value,
                                                                                           header.CuryID, header.CustomerID.Value, header.StatementDate.Value), out trace))
                    {
                        header.PrevPrintedCnt = trace.PrevPrintedCnt;
                        header.PrevEmailedCnt = trace.PrevEmailedCnt;
                    }

                    header.Processed = true;
                }

                ARStatementDetail det = new ARStatementDetail();
                Copy(det, header);
                Copy(det, doc);

                if (doc.DocType != AR.ARDocType.CashSale && doc.DocType != AR.ARDocType.CashReturn)
                {
                    var document = (doc.Payable == true ? inv : doc);

                    if (header.StatementType == StatementTypes.CS_BALANCE_BROUGHT_FORWARD && isNewDoc)
                    {
                        header.EndBalance     = (header.EndBalance ?? 0m) + document.SignBalance * document.OrigDocAmt;
                        header.CuryEndBalance = (header.CuryEndBalance ?? 0m) + document.SignBalance * document.CuryOrigDocAmt;
                    }
                    else if (header.StatementType == StatementTypes.CS_OPEN_ITEM)
                    {
                        header.EndBalance     = (header.EndBalance ?? 0m) + document.SignBalance * document.DocBal;
                        header.CuryEndBalance = (header.CuryEndBalance ?? 0m) + document.SignBalance * document.CuryDocBal;
                    }
                }

                details.Add(det);
            }

            if (isParent)
            {
                AccumulateAgeBalancesIntoStatements(familyDocuments, statements, ageCredits);
            }

            //Merge with previous statements - is needed for Balance Brought Forward
            Dictionary <StatementKey, DateTime> lastCuryStatement = new Dictionary <StatementKey, DateTime>();

            foreach (ARStatement iPrev in selectAllPreviousByCustomer.Select(customer.BAccountID))
            {
                StatementKey key = new StatementKey(iPrev.BranchID.Value, iPrev.CuryID);
                if (lastCuryStatement.ContainsKey(key) && lastCuryStatement[key] > iPrev.StatementDate)
                {
                    continue;
                }

                ARStatement header = AddARStatement(statements, key, aCycle, customer, aStatementDate);

                header.BegBalance     = iPrev.EndBalance;
                header.CuryBegBalance = iPrev.CuryEndBalance;
                Recalculate(header);
                lastCuryStatement[key] = iPrev.StatementDate.Value;
            }

            foreach (ARStatement iH in statements.Values)
            {
                ARStatement prev = selectPreviousStatement.Select(iH.CustomerID, iH.BranchID, iH.CuryID);
                if (prev != null)
                {
                    iH.BegBalance     = prev.EndBalance;
                    iH.CuryBegBalance = prev.CuryEndBalance;
                }

                ApplyFIFORule(iH, ageCredits);
            }

            PersistStatement(statementGraph, customer.BAccountID, aStatementDate, statements.Values, details);
        }
Пример #4
0
        protected virtual ARStatement AddARStatement(Dictionary <StatementKey, ARStatement> statements, StatementKey key,
                                                     ARStatementCycle cycle, Customer customer, DateTime statementDate)
        {
            ARStatement statement;

            if (!statements.TryGetValue(key, out statement))
            {
                statement = new ARStatement();
                Clear(statement);
                Copy(statement, customer);
                Copy(statement, cycle);
                Copy(statement, key);
                statement.StatementDate = statementDate;

                statements[key] = statement;
            }

            return(statement);
        }
Пример #5
0
 protected static void Copy(ARStatement aDest, StatementKey key)
 {
     aDest.BranchID = key.first;
     aDest.CuryID   = key.second;
 }