Bestellstornierung in AX 2012 x++

class AWE_PurchCancel extends RunBase
{
PurchTable purchTable;

#define.CurrentVersion(1)
#LOCALMACRO.CurrentList
purchTable
#ENDMACRO
}



/// 
/// Cancels the markupTrans records
/// 
/// 
/// The ID of the table to which the markupTrans records are related.
/// 
/// 
/// The ID of the record to which the markupTrans records are related.
/// 

private void cancelMarkup(
TableId _transTableId,
RecId _transRecId
)
{
MarkupTrans markupTrans;

while select forupdate * from markupTrans
where markupTrans.TransRecId == _transRecId
&& markupTrans.TransTableId == _transTableId
&& markupTrans.MarkupCategory == MarkupCategory::Fixed
&& !markupTrans.IsDeleted
{
markupTrans.Value = 0;
markupTrans.update();
}
}



protected void new()
{
super();
}



public container pack()
{
return [#CurrentVersion, #CurrentList];
}



public PurchTable parmPurchTable(PurchTable _purchTable = purchTable)
{
purchTable = _purchTable;

return purchTable;
}



/// 
/// Performs the purchase order cancelation.
/// 
public void run()
{
PurchLine purchLine;
PurchPrepayTable purchPrepayTable;
PurchQty purchQty;
boolean hasLines = false;
boolean messageShown = false;
PdsCWInventQty pdsCWQty;
// 
boolean clearFiscalDocumentRef_BR = false;
NoYes isBrazilEnabled = BrazilParameters::isEnabled();
// 
McrOrderEventTable mcrOrderEventTable;

ttsbegin;

//selects a prepayment definition which does not have a paid or settled prepayment invoice
select firstonly forupdate PrepayApplicationRemaining from purchPrepayTable
where purchPrepayTable.PurchTable == purchTable.PurchId
&& purchPrepayTable.IsDeleted == NoYes::No;

if (purchPrepayTable)
{
if (purchPrepayTable.PrepayApplicationRemaining == 0.0)
{
//reverses the prepayment invoices which have not been paid or settled
PurchPrepayTable::reverseUnpaidPostedAdvances(purchTable.PurchId);
purchPrepayTable.Value = 0.0;
purchPrepayTable.PrepayLimit = 0.0;
purchPrepayTable.PrepayAvailable = 0.0;
purchPrepayTable.Description = '';
purchPrepayTable.PrepayCategoryId = 0;
purchPrepayTable.update();
}
else
{
//throws error and returns with message that a paid prepayment invoice exists therefore purchase order cannot be cancelled
error(strFmt("@SYS341265", purchTable.PurchId));
return;
}
}

if (purchTable.ChangeRequestRequired && purchTable.DocumentState >= VersioningDocumentState::Approved)
{
if (!purchTable.selectForUpdate())
{
purchTable = PurchTable::findRecId(purchTable.RecId, true);
}
VersioningPurchaseOrder::newPurchaseOrder(purchTable).createChangeRequest();
}
else if (!VersioningPurchaseOrder::newPurchaseOrder(purchTable).isLastVersionArchived() && purchTable.DocumentState == VersioningDocumentState::Confirmed)
{
// force archiving to avoid it during line cancellation as that would lead to update conflicts.
purchTable.update();
}

// do not cancel invoice matched lines.
while select forupdate purchLine
where purchLine.PurchId == purchTable.PurchId
&& purchLine.IsDeleted == NoYes::No
{
hasLines = true;
if (purchLine.IsInvoiceMatched == NoYes::Yes)
{
if (purchLine.PurchStatus != PurchStatus::Invoiced)
{
if (!messageShown)
{
info("@SYS342633");
messageShown = true;
}
info(strFmt("@SYS342634", purchLine.LineNumber, purchLine.ItemId));
}
}
else
{
if (pdsIsCWItem(purchLine.ItemId))
{
pdsCWQty = purchLine.pdsCWReceivedInTotal() + purchLine.pdsCWArrived() + purchLine.pdsCWRegistered();
}
else
{
pdsCWQty = 0;
}
purchQty = purchLine.receivedInTotal() + purchLine.arrivedInPurchUnit() + purchLine.registeredInPurchUnit();
if (purchQty)
{
// if anything was recieved, arrived or registered then write down to what was recieved, arrived and registered.
purchLine.PurchQty = purchQty;
purchLine.PdsCWQty = pdsCWQty;
purchLine.LineAmount = purchLine.calcLineAmountForced();
}
else
{
purchLine.PurchQty = 0;
purchLine.LineAmount = 0;
purchLine.PdsCWQty = 0;
purchLine.PurchStatus = PurchStatus::Canceled;
// 
if (isBrazilEnabled)
{
clearFiscalDocumentRef_BR = true;
}
// 
if (purchLine.isDropShipment() &&
purchTable.InterCompanyDirectDelivery == NoYes::Yes)
{
purchTable.InterCompanyDirectDelivery = NoYes::No;
purchTable.doUpdate();
}
}

InventMovement::bufferSetTransQtyUnit(purchLine);
InventMovement::bufferSetRemainQty(purchLine);

// now cancel the markup
this.cancelMarkup(purchLine.TableId, purchLine.RecId);

//purchLine.update(false, true, false);

if (purchLine.sourceDocumentLine().AccountingStatus == SourceDocumentLineAccountingStatus::Completed)
{
SourceDocumentProcessorFacade::submitSourceDocumentLineImplementation(purchLine, false, SourceDocumentLineAccountingStatus::FullyDistributed);
}
else
{
SourceDocumentProcessorFacade::submitSourceDocumentLineImplementation(purchLine);
}

purchLine.doupdate();
//purchTable.updateDistribution();

purchTable.type().updateDistribution();
update_recordSet purchTable
setting DocumentState = VersioningDocumentState::Draft,
//DocumentStatus = DocumentStatus::PurchaseOrder,
PurchStatus = PurchStatus::Canceled
where purchTable.PurchId == purchLine.PurchId;

}
}

// cancel header markup
this.cancelMarkup(purchTable.TableId, purchTable.RecId);

if (!hasLines)
{
purchTable.PurchStatus = PurchStatus::Canceled;
purchTable.update();
}

// 
if (isBrazilEnabled && (clearFiscalDocumentRef_BR || !hasLines))
{
purchTable.removeInvoiceRefRecId_BR();
}
// 

if ( purchTable.MCRDropShipment )
{
mcrOrderEventTable.insertFromDropShipment(purchTable.referenceSalesTable().SalesId, purchTable.PurchId, MCROrderEventType::CancelDropShipPO);
}

ttscommit;

PurchPrepayTable::checkApplicationRemaining(purchTable.PurchId, purchTable.InvoiceAccount);
}



public boolean unpack(container packedClass)
{
boolean ret;
Integer version = conPeek(packedClass,1);

switch (version)
{
case #CurrentVersion:
[version, #CurrentList] = packedClass;
ret = true;
break;
default:
ret = false;
}

return ret;
}



public static AWE_PurchCancel construct()
{
return new AWE_PurchCancel();
}



client server static public ClassDescription description()
{
return "@SYS305256";
}



static client void main(Args _args)
{
NoYes skipPrompt;

FormDataSource formDataSource;
Object formDataSourceObject;

if (!_args || !_args.record() || _args.dataset() != tableNum(PurchTable))
{
throw error("@SYS25407");
}

// Option to skip the dialog

if (_args.parmEnumType() == enumNum(NoYes))
{
skipPrompt = _args.parmEnum();
}

// Main method are bound to client due to calls on formDataSource. The update itself are called from MainOnServer which is bound to server.

if (skipPrompt || Box::yesNo("@SYS307326",DialogButton::No, PurchCancel::description()) == DialogButton::Yes)
{
AWE_PurchCancel::mainOnServer(_args.record());

formDataSource = _args.record().dataSource();
if (formDataSource)
{
// The Document state and Status are changed on PurchTable

formDataSource.reread();
formDataSource.refresh();

// This is necessary to update the enable status of the fields on the header
formDataSource.active();

// The quantities on the lines are changed
formDataSourceObject = formDataSource;
if (formDataSourceHasMethod(formDataSourceObject, identifierStr(reReadLines)))
{
formDataSourceObject.rereadLines();
}
}
}
}



/// 
/// Cancels a purchase order.
/// 
/// 
/// A PurchTable record that stores the header of the order to be canceled.
/// 
protected static server void mainOnServer(PurchTable _purchTable)
{
AWE_PurchCancel AWEPurchCancel;

AWEPurchCancel = AWE_PurchCancel::construct();
AWEPurchCancel.parmPurchTable(_purchTable);
AWEPurchCancel.run();
}



------------------------------------- part 2



class AWE_POCancelCleanupOpenOrders extends RunBase
{
DialogField PurchaseOrder;

Dialog dialog;
}



protected Object Dialog()
{
FormStringControl control;
dialog = super();
// Set a title for dialog
dialog.caption("AWE Purchase Order Cancelation");
// Add a new field to Dialog
PurchaseOrder = dialog.addField(extendedTypeStr(PurchId),'Purchase Order No:');
control = PurchaseOrder.control();
control.registerOverrideMethod(methodstr(FormStringControl, lookUp),methodstr(AWE_POCancelCleanupOpenOrders, exchRate_Lookup),this);
return dialog;
}



public void exchRate_Lookup(FormStringControl _control)
{
SysTableLookup sysTableLookUp;
QueryBuildDataSource qbds;
Query query = new Query();

qbds = query.addDataSource(tableNum(PurchTable));

sysTableLookUp = SysTableLookup::newParameters(tableNum(PurchTable), _control, true);

qbds.addRange(fieldNum(PurchTable, PurchStatus)).value(enum2str(PurchStatus::Backorder));
sysTableLookUp.addLookupfield(fieldNum(PurchTable, PurchId));
sysTableLookUp.addLookupfield(fieldNum(PurchTable, PurchName));
sysTableLookUp.addLookupfield(fieldNum(PurchTable, PurchStatus));

sysTableLookUp.parmQuery(query);
sysTableLookUp.performFormLookup();
}



public boolean getFromDialog()
{
return super();
}



public void run()
{
PurchTable PurchTable;
AWE_PurchCancel purchCancel ;
PurchTable = PurchTable::find(PurchaseOrder.value());

purchCancel = AWE_PurchCancel::construct();
purchCancel.parmPurchTable(PurchTable);
purchCancel.run();
info("The Purchase Order status has been updated to cancelled");
}



boolean showQueryValues()
{
;
return true;
}



public static void main(Args _args)
{
AWE_POCancelCleanupOpenOrders cancelPO = new AWE_POCancelCleanupOpenOrders();

// Prompt the dialog, if user clicks OK it returns true
if (cancelPO.prompt())
{
cancelPO.run();
}
}

Author: admin

Leave a Reply

Your email address will not be published.