通过代码计算预算余额

Calculate Budget Balance by code

我需要计算任何特定维度的预算余额 (AX 2012 R2)。

我执行以下代码工作正常,但需要很长时间才能完成。

这是因为这计算了所有有 AX 的预算余额,而不仅仅是我想要的。

最后,变量BudgetTmpBalance里面有所有预算条目的记录。

有些事情我没有考虑。有人知道是什么吗?

提前致谢。

代码:

BudgetTransactionLine              recBudgetTransactionLine;
DimensionAttributeValueCombination recDimensionAttributeValueCombination;        
date                               FiscalYearStart, FiscalYearEnd;
AccountingDistribution             recAccountingDistribution;    
Query                              query = new Query();
QueryBuildDataSource               qbds;
QueryBuildDataSource               qbdsJoin;
QueryBuildRange                    qbr;
BudgetCalculateBalance             budgetCalculateBalance = new BudgetCalculateBalance();
BudgetTmpBalance                   budgetTmpBalance;
DimensionDisplayValue              pDimensionDisplayValue;

;

pDimensionDisplayValue = "Correct Value"; 

select * from recDimensionAttributeValueCombination where recDimensionAttributeValueCombination.LedgerDimensionType == LedgerDimensionType::Budget &&
                                                          recDimensionAttributeValueCombination.DisplayValue        == pDimensionDisplayValue;

FiscalYearStart = str2Date("01/04/" + int2str(year(today())), 123);
FiscalYearEnd   = str2Date("31/03/" + int2str(year(today()) + 1), 123);

//Find recId of header that I Want (Works fine)
select BudgetTransactionHeader from recBudgetTransactionLine where recBudgetTransactionLine.LedgerDimension == recDimensionAttributeValueCombination.RecId &&
                                                                      (recBudgetTransactionLine.Date >= FiscalYearStart &&  recBudgetTransactionLine.Date <= FiscalYearEnd);

qbds = query.addDataSource(tableNum(BudgetTransactionHeader));
qbr  = qbds.addRange(FieldNum(BudgetTransactionHeader, TransactionStatus));
qbr.value(queryValue(1));
qbr  = qbds.addRange(FieldNum(BudgetTransactionHeader, RecId));
qbr.value(queryValue(recBudgetTransactionLine.BudgetTransactionHeader));//Header RecId (correct value)
qbdsJoin= qbds.addDataSource(tableNum(BudgetTransactionLine));
qbdsJoin.relations(false);
qbdsJoin.fields().dynamic(NoYes::Yes);
qbdsJoin.addLink(fieldNum(BudgetTransactionHeader, RecId), fieldNum(BudgetTransactionLine, BudgetTransactionHeader));
qbdsJoin.joinMode(JoinMode::InnerJoin);

budgetCalculateBalance.parmDimensionFocus("CTA+UN+CC");    
budgetCalculateBalance.parmCalculateLedgerAmounts(NoYes::Yes);
budgetCalculateBalance.parmFiscalCalendarRecId(Ledger::fiscalCalendar());
budgetCalculateBalance.parmStartDate(FiscalYearStart);
budgetCalculateBalance.parmEndDate(FiscalYearEnd);
budgetCalculateBalance.parmIncludeSubModels(NoYes::No);
budgetCalculateBalance.parmIncludeAllBudgetModelsIfNotSpecified(NoYes::No);
budgetCalculateBalance.parmBudgetModelId("TOTAL");
budgetCalculateBalance.parmBudgetType();
budgetCalculateBalance.parmAccumulateAmounts(NoYes::No);
budgetCalculateBalance.parmLedgerCategory(CurrentOperationsTax::Current);
budgetCalculateBalance.parmQuery(query);
budgetTmpBalance = budgetCalculateBalance.calculatePeriodBalances();

已解决 现在该过程在 5 秒内完成!

这里是正确的代码:

BudgetTransactionLine              recBudgetTransactionLine;
DimensionAttributeValueCombination recDimensionAttributeValueCombination;        
date                               FiscalYearStart, FiscalYearEnd;
AccountingDistribution             recAccountingDistribution;    
Query                              query;
QueryBuildDataSource               qbds;
QueryBuildDataSource               qbdsJoin;
QueryBuildRange                    qbr;
BudgetCalculateBalance             budgetCalculateBalance = new BudgetCalculateBalance();
BudgetTmpBalance                   budgetTmpBalance;
DimensionDisplayValue              pDimensionDisplayValue;
#define.LedgerDimensionDataSourceName('BudgetTransactionLine_1')

;

pDimensionDisplayValue = "Correct Value"; 

select * from recDimensionAttributeValueCombination where recDimensionAttributeValueCombination.LedgerDimensionType == LedgerDimensionType::Budget &&
                                                      recDimensionAttributeValueCombination.DisplayValue        == pDimensionDisplayValue;

FiscalYearStart = str2Date("01/04/" + int2str(year(today())), 123);
FiscalYearEnd   = str2Date("31/03/" + int2str(year(today()) + 1), 123);

//Find recId of header that I Want (Works fine)
select BudgetTransactionHeader from recBudgetTransactionLine where recBudgetTransactionLine.LedgerDimension == recDimensionAttributeValueCombination.RecId &&
                                                                  (recBudgetTransactionLine.Date >= FiscalYearStart &&  recBudgetTransactionLine.Date <= FiscalYearEnd);

budgetCalculateBalance.parmDimensionFocus("CTA+UN+CC");    
budgetCalculateBalance.parmFiscalCalendarRecId(Ledger::fiscalCalendar());
budgetCalculateBalance.parmCalculateLedgerAmounts(NoYes::Yes);
budgetCalculateBalance.parmFilterByBudgetType(NoYes::Yes);
budgetCalculateBalance.parmIncludeSubModels(NoYes::Yes);
budgetCalculateBalance.parmStartDate(FiscalYearStart);
budgetCalculateBalance.parmEndDate(FiscalYearEnd);
if(TieneCC)
    budgetCalculateBalance.parmDimensionFocus("CTA+UN+CC");
else
    budgetCalculateBalance.parmDimensionFocus("CTA+UN");

query = new Query(querystr(BudgetCalculateBalance));
BudgetTransactionManager::addDimensionCriteriaForLedgerDimension(query, recDimensionAttributeValueCombination.RecId, #LedgerDimensionDataSourceName);
budgetCalculateBalance.parmQuery(query);        
budgetTmpBalance = budgetCalculateBalance.calculatePeriodBalances();