使用QBFC在Quickbooks中查找第一个支票号码

时间:2012-08-29 09:48:25

标签: c# quickbooks qbfc qbxml

我正在使用Quickbooks QBFC,并希望以编程方式检索“First Check Number”字段的值。

可以在Quickbooks的File> Print Forms> Checks

中找到

请建议如何做到这一点或我可以看到的任何参考。

1 个答案:

答案 0 :(得分:1)

您无法直接执行此操作,但您可以查询针对您感兴趣的帐户编写的最新检查。默认的第一个检查号码将大于已写入的最高支票号码

作为一项规则,我不会为他们编写人员代码,我今天只是因为它是星期五而且我心情疯狂。以下代码可用作有用的最后检查编号例程的起点。为了拥有一个现实的,可用的例程,你,而不是我,将不得不处理与人们使用支票簿的方式相关的所有怪癖:非数字数字,数字不按顺序等等。

请注意:此代码使用Zombie open source library和QBFC 11。

class CheckNumbers
{
    private const int DATE_INTERVAL = -7;

    private string _accountName;
    private DateTime _lastToDate;
    private DateTime _lastFromDate;

    public CheckNumbers(string accountName)
    {
        _accountName = accountName;
        _lastToDate = DateTime.Today;
        _lastFromDate = _lastToDate.AddDays(DATE_INTERVAL);
    }

    private void SetCriteria(IORTxnQuery qry)
    {
        qry.TxnFilter.AccountFilter.ORAccountFilter.FullNameList.Add(_accountName);

        var dateFilter = qry.TxnFilter.ORDateRangeFilter.ModifiedDateRangeFilter;

        dateFilter.FromModifiedDate.SetValue(_lastFromDate, true);
        dateFilter.ToModifiedDate.SetValue(_lastToDate, true);
    }

    private void ProcessCheckNumber(string checkNumber, ref int highestNumber)
    {
        if (!string.IsNullOrEmpty(checkNumber))
        {
            int thisCheck;
            if (!int.TryParse(checkNumber, out thisCheck))
            {
                throw new Exception(string.Format("Check number {0} cannot be read as an integer", checkNumber));
            }
            if (thisCheck > highestNumber) highestNumber = thisCheck;
        }
    }

    public int GetLastCheckNumber()
    {
        DateTime failSafe = DateTime.Parse("1/1/2010");

        using (var cn = Zombie.ConnectionMgr.GetConnection())
        {
            int highestCheck = 0;

            while (highestCheck == 0 && _lastFromDate > failSafe)
            {
                var batch = cn.NewBatch();

                var checkQuery = batch.MsgSet.AppendCheckQueryRq();
                checkQuery.IncludeRetElementList.Add("RefNumber");

                SetCriteria(checkQuery.ORTxnQuery);

                batch.SetClosures(checkQuery, b =>
                    {
                        var checks = new Zombie.QBFCIterator<ICheckRetList, ICheckRet>(b);
                        foreach (var check in checks)
                        {
                            ProcessCheckNumber(Zombie.Safe.Value(check.RefNumber), ref highestCheck);
                        }
                    });

                var billCheckQuery = batch.MsgSet.AppendBillPaymentCheckQueryRq();
                billCheckQuery.IncludeRetElementList.Add("RefNumber");

                SetCriteria(billCheckQuery.ORTxnQuery);

                batch.SetClosures(billCheckQuery, b =>
                    {
                        var checks = new Zombie.QBFCIterator<IBillPaymentCheckRetList, IBillPaymentCheckRet>(b);
                        foreach (var check in checks)
                        {
                            ProcessCheckNumber(Zombie.Safe.Value(check.RefNumber), ref highestCheck);
                        }
                    });

                if (!batch.Run()) return 0;

                _lastToDate = _lastFromDate;
                _lastFromDate = _lastToDate.AddDays(DATE_INTERVAL);
            }

            return highestCheck;
        }
    }
}