问题
I am using Quickbooks QBFC and want to retrive the value of "First Check Number" field programatically.
It can be found in Quickbooks at File>Print Forms>Checks
Please suggest how this can be done or any reference i can look at.
回答1:
You can't do this directly, but you can query for the most recent checks that have been written against the account that you are interested in. The default first check number will be one greater than the highest check number that's been written.
As a rule, I don't write people's code for them, I'm making an exception today only because it's Friday and I'm in a crazy mood. The following code can be used as a starting point for a useful last check number routine. In order to have a realistic, usable routine you, not I, will have to deal with all of the eccentricities that attach to the way people use their checkbooks: non-numeric numbers, numbers out of sequence, etc.
Please note: this code uses the Zombie open source library and 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;
}
}
}
来源:https://stackoverflow.com/questions/12175127/find-first-check-number-in-quickbooks-using-qbfc