protected static bool PersistStatement(int?aCustomerID, DateTime aStmtDate, IEnumerable <ARStatement> aStatements, IEnumerable <ARStatementDetail> aDetails)
        {
            StatementCreateBO statement = PXGraph.CreateInstance <StatementCreateBO>();

            foreach (Customer cst in statement.Customer.Select(aCustomerID))
            {
                if (cst != null)
                {
                    cst.StatementLastDate = aStmtDate;
                    statement.Customer.Update(cst);
                }
                foreach (ARStatement it in aStatements)
                {
                    statement.Statement.Insert(it);
                }

                foreach (ARStatementDetail it in aDetails)
                {
                    statement.StatementDetail.Insert(it);
                    foreach (ARRegister iDoc in statement.Docs.Select(it.DocType, it.RefNbr))
                    {
                        if (iDoc.StatementDate == null)
                        {
                            iDoc.StatementDate = aStmtDate;
                            statement.Docs.Update(iDoc);
                        }
                    }
                }
                statement.Actions.PressSave();
            }
            return(true);
        }
Esempio n. 2
0
        protected static bool PersistStatement(StatementCreateBO statement, int?aCustomerID, DateTime aStmtDate, IEnumerable <ARStatement> aStatements, IEnumerable <ARStatementDetail> aDetails)
        {
            statement.Clear();

            PXDatabase.Update <Customer>(
                new PXDataFieldAssign <Customer.statementLastDate>(aStmtDate),
                new PXDataFieldRestrict <Customer.bAccountID>(aCustomerID));

            foreach (ARStatement it in aStatements)
            {
                statement.Statement.Insert(it);
            }

            foreach (ARStatementDetail it in aDetails)
            {
                statement.StatementDetail.Insert(it);

                PXDatabase.Update <ARRegister>(
                    new PXDataFieldAssign <ARRegister.statementDate>(aStmtDate),
                    new PXDataFieldRestrict <ARRegister.docType>(it.DocType),
                    new PXDataFieldRestrict <ARRegister.refNbr>(it.RefNbr),
                    new PXDataFieldRestrict <ARRegister.statementDate>(PXDbType.SmallDateTime, 4, null, PXComp.ISNULL));
            }
            statement.Actions.PressSave();

            return(true);
        }
        protected static void DeleteStatement(ARStatementCycle aStatementCycle, DateTime aStmtDate)
        {
            StatementCreateBO statementBO = PXGraph.CreateInstance <StatementCreateBO>();

            foreach (ARStatement iStm in statementBO.Statement.Select(aStatementCycle.StatementCycleId, aStmtDate))
            {
                statementBO.Statement.Delete(iStm);
            }
            statementBO.Actions.PressSave();
            if (aStatementCycle.LastStmtDate == aStmtDate)
            {
                aStatementCycle.LastStmtDate = statementBO.FindLastCycleStatementDate(aStatementCycle.StatementCycleId, aStmtDate);
            }
        }
        protected static IEnumerable <ARStatement> DeleteStatements(ARStatementCycle aStatementCycle, IEnumerable <Customer> customers, DateTime aStmtDate)
        {
            var trace = new List <ARStatement>();

            StatementCreateBO statementBO = PXGraph.CreateInstance <StatementCreateBO>();

            foreach (var customer in customers)
            {
                foreach (ARStatement iStm in statementBO.CustomerStatement.Select(aStatementCycle.StatementCycleId, customer.BAccountID, aStmtDate))
                {
                    trace.Add(StatementTrace(iStm));
                    statementBO.CustomerStatement.Delete(iStm);
                }
            }
            statementBO.Actions.PressSave();

            return(trace);
        }
Esempio n. 5
0
        protected virtual IEnumerable <ARStatement> DeleteCustomerStatement(ARStatementCycle aCycle, Customer customer, DateTime aStatementDate)
        {
            StatementCreateBO statementBO = PXGraph.CreateInstance <StatementCreateBO>();
            var trace        = new List <ARStatement>();
            int deletedCount = 0;

            foreach (ARStatement iStm in statementBO.CustomerStatement.Select(customer.BAccountID, aStatementDate))
            {
                trace.Add(StatementTrace(iStm));
                statementBO.CustomerStatement.Delete(iStm);
                deletedCount++;
            }
            if (deletedCount == 0)
            {
                customer.StatementLastDate = statementBO.FindLastCstmStatementDate(customer.BAccountID, aStatementDate);
                statementBO.Customer.Update(customer);
            }
            statementBO.Actions.PressSave();
            return(trace);
        }
Esempio n. 6
0
        protected virtual void GenerateStatementForCustomerFamily(StatementCreateBO statementGraph, ARStatementCycle cycle, IEnumerable <Customer> customers, DateTime statementDate, bool clearExisting)
        {
            var documentsList = new List <PXResult <ARRegister, ARInvoice> > {
            };

            foreach (var customer in customers.OrderByDescending(c => c.ParentBAccountID))
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    Dictionary <Tuple <int, string, int, DateTime>, ARStatement> trace = new Dictionary <Tuple <int, string, int, DateTime>, ARStatement>();
                    if (clearExisting || customer.StatementLastDate == statementDate)
                    {
                        //Need to pass Prints and E-mail counts to the regenerated statement - for this reason we need to copy
                        IEnumerable <ARStatement> deleted = this.DeleteCustomerStatement(cycle, customer, statementDate);
                        trace = TraceToDict(deleted);
                    }

                    GenerateCustomerStatement(statementGraph, cycle, customer, statementDate, trace, documentsList);
                    ts.Complete();
                }
            }
        }
Esempio n. 7
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);
        }