BLVCKFX
3/11/2020 - 7:03 PM

Delete ledger voucher

Delete ledger voucher.#SADA => RENDeleteLedgerVoucherRENDeleteLedgerVoucher.PNG

// This is a framework class. Customizing this class may cause problems with future upgrades to the software.
class RENDeleteLedgerVoucher extends RunBaseBatch
{
    // Packed variables
    TransDate               transDate;
    Voucher                 voucher;
    DocumentNum             documentNum;
    NoYesId                 recalcAccount;
    AccountNum              accountNum;
    boolean                 isInvoice;

    NumberSequenceGroupID   NumberSequenceGroup;

    // Dialog fields
    DialogField     dlgVoucher;
    DialogField     dlgTransDate;
    DialogField     dlgRecalcAccount;

    TaxBookRecId            taxBookRecId;
    TaxBookSectionRecId     taxBookSectionRecId;




    #define.CurrentVersion(3)
    #define.Version1(3)
    #localmacro.CurrentList
        transDate,
        voucher,
        documentNum,
        recalcAccount,
        AccountNum,
        isInvoice,
        NumberSequenceGroup
    #endmacro
}


public boolean allowSaveLast()
{
    boolean ret;

    ret = false;

    return ret;
}


/// <summary>
///    Indicates whether the class is shown in the list of <c>Journal</c> types.
/// </summary>
/// <returns>
///    true if the class is shown in the list of <c>Journal</c> types; otherwise, false.
/// </returns>
/// <remarks>
///    A class that can be used in a batch journal is a class where the same parameters can be used
///    repeatedly. The dialog box can be shown and the parameters can be changed, but parameters of some
///    classes might build on data that is only valid for a short time. Running a class two times with the
///    same parameters is not always possible. If the <c>RunBaseBatch.canGoBatch</c> method is false, this
///    method will not have any effect.
/// </remarks>
public boolean canGoBatchJournal()
{
    return false;
}


private void Delete()
{
    unchecked(Uncheck::TableSecurityPermission)
    {
       this.DeleteLedger();
    }
}


private boolean DeleteLedger()
{
    TaxTrans                            taxTrans;
    TaxTransGeneralJournalAccountEntry  taxTransGeneralJournalAccountEntry;
    CosCostTrans                        cosCostTrans;
    AssetTrans                          assetTrans;
    BankAccountTrans                    bankAccountTrans;
    LedgerJournalTrans                  ledgerJournalTrans;
    //SN_ID_ImportPurch
    Set                   ledgerJournalTableList = new Set(Types::String);
    SetEnumerator          setEnum;
    LedgerJournalTable      ledgerJournalTable;
    //EN_ID_ImportPurch
    boolean                             ret = true;

    setprefix("TaxTrans");

    while select forupdate taxTrans
    where taxTrans.Voucher  ==voucher
        && taxTrans.TransDate==transDate
    {
        taxTrans.delete();

        delete_from taxTransGeneralJournalAccountEntry where taxTransGeneralJournalAccountEntry.TaxTrans == taxTrans.RecId;

        INFOLOG.add(EXCEPTION::Info,"deleted voucher " + voucher);
    }
    this.DeleteUpdateLedger(voucher, transDate);

    while select forupdate CosCostTrans
    where CosCostTrans.Voucher  ==voucher
        && CosCostTrans.TransDate==transDate
    {
        CosCostTrans.delete();

        INFOLOG.add(eXCEPTION::Info,"Deleted voucher " + voucher);
    }

    while select forupdate AssetTrans
    where AssetTrans.Voucher  ==voucher
        && AssetTrans.TransDate==transDate
    {
        AssetTrans.delete();

        INFOLOG.add(eXCEPTION::Info,"Deleted voucher " + voucher);
    }



    setprefix("BankAccountTrans");

    while select forupdate bankAccountTrans
    where bankAccountTrans.Voucher  ==voucher
        && bankAccountTrans.TransDate==transDate
    {
        bankAccountTrans.delete();

        INFOLOG.add(eXCEPTION::Info,"Deleted voucher " + voucher);
    }

    setprefix("LedgerJournalTrans");

    while select forupdate ledgerJournalTrans
    where ledgerJournalTrans.Voucher  ==voucher
        && ledgerJournalTrans.TransDate==transDate
    {
        if(!ledgerJournalTableList.in(ledgerJournalTrans.JournalNum))
            ledgerJournalTableList.add(ledgerJournalTrans.JournalNum);
        ledgerJournalTrans.delete();

        INFOLOG.add(eXCEPTION::Info,"deleted voucher " + voucher );
    }
    setEnum = ledgerJournalTableList.getEnumerator();
    while(setEnum.moveNext())
    {
        ledgerJournalTable = ledgerJournalTable::find(setEnum.current(), true);
        if(ledgerJournalTable.A_EinvoiceFilename)
        {
            A_EinvoiceDeletionHandler::updateFileLocation(A_EInvoicePurchImportMode::InvoiceJournal, ledgerJournalTable.A_EinvoiceFilename);
            ledgerJournalTable.A_EinvoiceFilename='';
            ledgerJournalTable.doUpdate();
        }
    }
    return ret;
}


private void DeleteUpdateLedger(Voucher _voucher,TransDate   _transDate)
{
    generalJournalAccountEntry              generalJournalAccountEntry;
    generalJournalEntry                     generalJournalEntry;
    subledgerVoucherGeneralJournalEntry     subledgerVoucherGeneralJournalEntry;
    ledgerEntry                             ledgerEntry;
    ledgerEntryJournal                      ledgerEntryJournal;
    ledgerEntryJournalizing                 ledgerEntryJournalizing;
    LedgerJournalTrans                      ledgerJournalTrans;


    ;

    while select RecId from generalJournalAccountEntry
        join RecId from generalJournalEntry
            where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry
                join RecId from subledgerVoucherGeneralJournalEntry
                    where subledgerVoucherGeneralJournalEntry.GeneralJournalEntry == generalJournalEntry.RecId
                        && subledgerVoucherGeneralJournalEntry.Voucher == _voucher
                        && subledgerVoucherGeneralJournalEntry.VoucherDataAreaId == curext()
                        && subledgerVoucherGeneralJournalEntry.AccountingDate == _transDate



    {

            delete_from ledgerEntry
                where ledgerEntry.GeneralJournalAccountEntry == generalJournalAccountEntry.RecId;

            delete_from ledgerEntryJournal
            where ledgerEntryJournal.RecId == generalJournalEntry.LedgerEntryJournal;

            delete_from ledgerEntryJournalizing
            where ledgerEntryJournalizing.GeneralJournalAccountEntry == generalJournalAccountEntry.RecId;


    }


    delete_from ledgerEntry
        exists join generalJournalAccountEntry
            where generalJournalAccountEntry.RecId == ledgerEntry.GeneralJournalAccountEntry
        exists join generalJournalEntry
            where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry
        exists join subledgerVoucherGeneralJournalEntry
            where subledgerVoucherGeneralJournalEntry.GeneralJournalEntry == generalJournalEntry.RecId
                && subledgerVoucherGeneralJournalEntry.Voucher == _voucher
                && subledgerVoucherGeneralJournalEntry.VoucherDataAreaId == curext()
                && subledgerVoucherGeneralJournalEntry.AccountingDate == _transDate;

    delete_from generalJournalAccountEntry
        exists join generalJournalEntry
            where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry
                && generalJournalEntry.Ledger == Ledger::current()
        exists join subledgerVoucherGeneralJournalEntry
            where subledgerVoucherGeneralJournalEntry.GeneralJournalEntry == generalJournalEntry.RecId
                && subledgerVoucherGeneralJournalEntry.Voucher == _voucher
                && subledgerVoucherGeneralJournalEntry.VoucherDataAreaId == curext()
                && subledgerVoucherGeneralJournalEntry.AccountingDate == _transDate;

    GeneralJournalEntry::deleteOrphans();

    delete_from subledgerVoucherGeneralJournalEntry
        where subledgerVoucherGeneralJournalEntry.VoucherDataAreaId == curext()
                && subledgerVoucherGeneralJournalEntry.AccountingDate == _transDate
                && subledgerVoucherGeneralJournalEntry.Voucher == _voucher

                notexists join generalJournalEntry
                    where generalJournalEntry.RecId == subledgerVoucherGeneralJournalEntry.GeneralJournalEntry
                        && generalJournalEntry.Ledger == Ledger::current();


}


/// <summary>
///    Returns a class that contains the methods that are described by the <c>RunBaseDialogable</c>
///    interface.
/// </summary>
/// <returns>
///    A class that contains the methods that are described by the <c>RunBaseDialogable</c> interface.
/// </returns>
/// <remarks>
///    A dialog box can be either built by using the <c>Dialog</c> class or by using a class that is
///    created in the Application Object Tree (AOT).
/// </remarks>
public Object dialog()
{
    DialogRunbase       dialog = super();
;
    dlgVoucher         = dialog.addFieldValue(extendedTypeStr(voucher   ),voucher       ,"@ANR773"  );
    dlgTransDate       = dialog.addFieldValue(extendedTypeStr(transDate ),transDate     ,"@ANR774"  );
    dlgRecalcAccount   = dialog.addFieldValue(extendedTypeStr(NoYesId   ),recalcAccount ,"@SYS15182");


    dlgRecalcAccount.visible(false);
    return dialog;
}


public void dialogPostRun(DialogRunbase dialog)
{
;
    super(dialog);
}


public boolean getFromDialog()
{
;
    transDate       = dlgTransDate.value();
    voucher         = dlgVoucher.value();
    recalcAccount   = dlgRecalcAccount.value();

    return super();
}


public boolean init()
{
    return true;
}


protected void new()
{
    super();
}


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


public NoYesId parmRecalcAccount(NoYesId _recalcAccount = recalcAccount)
{
    recalcAccount = _recalcAccount;

    return recalcAccount;
}


public TransDate parmTransDate(TransDate _transDate = transDate)
{
    transDate = _transDate;

    return transDate;
}


public Voucher parmVoucher(Voucher _voucher = voucher)
{
    voucher = _voucher;

    return voucher;
}


/// <summary>
///    Contains the code that does the actual job of the class.
/// </summary>
public void run()
{
    #OCCRetryCount
    if (! this.validate())
        throw error("");

    try
    {
        ttsbegin;

        this.Delete();
        this.updatelog();

        ttscommit;
    }
    catch (Exception::Deadlock)
    {
        retry;
    }
    catch (Exception::UpdateConflict)
    {
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::UpdateConflictNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::UpdateConflict;
        }
    }

}


public boolean runsImpersonated()
{
    return true;
}


public void saveLast()
{
    //super();
}


public boolean unpack(container packedClass)
{
    Version version = RunBase::getVersion(packedClass);
;
    switch (version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            break;
        default:
            return false;
    }

    return true;
}


void updatelog()
{
    RENLogDeleteVoucher RENLogDeleteVoucher;
    ;

    RENLogDeleteVoucher.clear();
    RENLogDeleteVoucher = RENLogDeleteVoucher::find(Voucher,
                                                    RENModuleCustVendLedger::Ledger,
                                                    DocumentStatus::None,
                                                    true);

    RENLogDeleteVoucher.selectForUpdate(true);
    RENLogDeleteVoucher.Voucher                 =   Voucher;
    RENLogDeleteVoucher.DocumentNum             =   documentNum;
    RENLogDeleteVoucher.DocumentDate            =   transDate;
    RENLogDeleteVoucher.DocumentStatus          =   DocumentStatus::None;
    RENLogDeleteVoucher.AccountNum              =   accountnum;
    RENLogDeleteVoucher.ModuleCustVendLedger    =   RENModuleCustVendLedger::Ledger;
    RENLogDeleteVoucher.numberSequenceGroup     =   numberSequenceGroup;
    RENLogDeleteVoucher.recalcAccount           =   recalcAccount;
    RENLogDeleteVoucher.TaxBook                 =   taxBookRecId;
    RENLogDeleteVoucher.TaxBookSection          =   taxBookSectionRecId;



    RENLogDeleteVoucher.Recovered               =   !isInvoice;

    RENLogDeleteVoucher.RecoveredDate           =   Datenull();
    RENLogDeleteVoucher.InfologData             =   INFOLOG.infologData();

    if(RENLogDeleteVoucher.RecId)
    {
        RENLogDeleteVoucher.update();
    }
    else
    {
        RENLogDeleteVoucher.insert();
    }
}


public boolean validate(Object _calledFrom = null)
{
    vendTrans                   vendTrans;
    CustTrans                   CustTrans;
    boolean                     ret = true;
    ;

    CustTrans   =   CustTrans::findVoucherDate(voucher,transDate);

    if (CustTrans)
    {
        return checkFailed("@ANR777");
    }

    vendTrans = VendTrans::findVoucherDate(voucher,transDate);

    if (vendTrans)
    {
        return checkFailed("@ANR776");
    }

    return ret;
}


server static RENDeleteLedgerVoucher construct()
{
    return new RENDeleteLedgerVoucher();
}


// Here goes a description of the class
static ClassDescription description()
{
    return "@ANR783";
}

static void main(Args args)
{
    RENDeleteLedgerVoucher    RENDeleteLedgerVoucher;

;
    RENDeleteLedgerVoucher = RENDeleteLedgerVoucher::construct();

    /*if(args && args.record() && args.dataset() == tableNum(GeneralJournalVoucherView))
    {
        GeneralJournalVoucherView.data(args.record());
        RENDeleteLedgerVoucher.parmTransDate(GeneralJournalVoucherView.AccountingDate);
        RENDeleteLedgerVoucher.parmVoucher(GeneralJournalVoucherView.Voucher);
    }*/
    if (RENDeleteLedgerVoucher.prompt())
        RENDeleteLedgerVoucher.run();
}