/// <summary>
/// Post open inventory journals
/// </summary>
public void run()
{
TradeLineRefId headerReference;
VendInvoiceInfoTable vendInvoiceInfoTable;
//vendInvoiceInfoTable = VendInvoiceInfoTable::findTableRefId(headerReference);
while (gQueryRun.next())
{
vendInvoiceInfoTable = gQueryRun.get(tableNum(VendInvoiceInfoTable));
try
{
if(this.validationWorkflowSubmit(vendInvoiceInfoTable))
{
this.SubmitVendorInvoice(vendInvoiceInfoTable);
}
}
catch (Exception::CLRError)
{
System.Exception interopException = CLRInterop::getLastException();
error(strFmt("%1", interopException));
}
catch (Exception::Error)
{
error(strFmt("An error occured during the update."));
}
}
}
/// <summary>
/// Validates if the workflow can be started.
/// </summary>
/// <returns>true if workflow can start</returns>
private boolean validationWorkflowSubmit(vendInvoiceInfoTable _vendInvoiceInfoTable)
{
// Don't submit if a posted invoice with the same invoice number already exists.
if (!this.validateDuplicateInvoice(_vendInvoiceInfoTable))
{
throw Exception::error;
}
if (PublicSectorUtils::isBudgetReservationEnabled())
{
VendInvoiceInfoTable::checkBudgetReservationBalance_PSN(_vendInvoiceInfoTable.TableRefId, _vendInvoiceInfoTable.SourceDocumentHeader);
}
if (isConfigurationkeyEnabled(configurationKeyNum(Project)) && ProjFundingLimitTrackingManager::hasLimitErrorSourceDocument(_vendInvoiceInfoTable.SourceDocumentHeader))
{
throw error("@SYS4110061");
}
boolean areAccountingDistributionsValid;
VendParameters vendParameters = VendParameters::find();
if((Dynamics.AX.Application.FeatureStateProvider::isFeatureEnabled( AccountingDistributionWorkflowSubmissionFeature::instance()) && vendParameters.BypassValidationOfAccountingDistributions == NoYes::Yes)
|| SourceDocumentProvider::areSourceDocAccDistAccountValid( SourceDocumentHeader::find( _vendInvoiceInfoTable.SourceDocumentHeader), true, true))
{
areAccountingDistributionsValid = true;
}
if (!areAccountingDistributionsValid)
{
return false;
}
return true;
}
/// <summary>
/// Determines if a posted invoice with the same invoice number already exists.
/// </summary>
/// <returns>
/// True if a posted invoice with the same invoice number does not exist. False if already exists.
/// </returns>
private boolean validateDuplicateInvoice(VendInvoiceInfoTable _vendInvoiceInfoTable)
{
boolean isInvoiceNumberFreeForUse = true;
if (Dynamics.AX.Application.FeatureStateProvider::isFeatureEnabled( VendInvoiceRejectDuplicateNumOnWorkflowSubmitFeature::instance()))
{
VendTable localVendTable = VendTable::find(_vendInvoiceInfoTable.InvoiceAccount);
if (!localVendTable.checkInvoice(_vendInvoiceInfoTable.Num, _vendInvoiceInfoTable.TransDate))
{
isInvoiceNumberFreeForUse = false;
}
}
return isInvoiceNumberFreeForUse;
}
/// <summary>
/// Creates the vendor invoice.
/// </summary>
public void SubmitVendorInvoice(VendInvoiceInfoTable _vendInvoiceInfoTable)
{
workflowComment workflowComment = "Auto submission to workflow.";
//changecompany (custInvoice.ProjIntercompany)
//{
if (_vendInvoiceInfoTable.RecId
&& _vendInvoiceInfoTable.RequestStatus == VendInvoiceRequestStatus::Draft)
// && custTable.interCompanyTradingPartner().interCompanyTradingRelation().Active
//&& this.validationWorkflowSubmit())
{
VendInvoiceHeaderWorkflow::submitToWorkflow( _vendInvoiceInfoTable, workflowComment);
}
//}
}