Sunday, July 3, 2016

Update Blocking in server for journal (System blocked Code)

// Update Blocking in server for journal (System blocked Code)

public static server JournalTableMap updateBlockServer(
    JournalTableMap     _journalTable,
    JournalBlockLevel   _fromLevel,
    JournalBlockLevel   _toLevel,
    boolean             _updateTotals = true)
{
    xSession                xSession;
    JournalTableMap         journalTableCopy;
    boolean                 dropBlocking;
    JournalTableData        journalTableData;
    ttsbegin;
    journalTableData = JournalTableData::newTable(_journalTable);
    journalTableCopy = journalTableData.journalStatic().findJournalTable(_journalTable.JournalId,true);
    if (!journalTableCopy.RecId)
    {
        // throw an error if the record was deleted since no further processing is valid
        throw error("@SYS77596");
    }
    if (_fromLevel >  _toLevel)     // unlock
    {
        if (journalTableCopy.SystemBlocked && _fromLevel < JournalBlockLevel::System)
        {
            dropBlocking = true;
        }
        else
        {
            if (_fromLevel == JournalBlockLevel::InUse)
            {
                if (journalTableCopy.SessionId)
                {
                    if (!isSessionActive(journalTableCopy.SessionId,journalTableCopy.SessionLoginDateTime))
                    {
                        journalTableData.initTotal();
                        _updateTotal = true;
                    }
                }
                else if (_journalTable.SystemBlocked)
                {
                    _updateTotal = false;
                }
                else
                {
                    journalTableData.initTotal();
                    _updateTotal = true;
                }
            }
            else // _fromLevel == JournalBlockLevel::System
            if (! journalTableCopy.SystemBlocked)
            {
                journalTableData.initTotal();
                _updateTotal = true;
            }
            if (_updateTotal)
            {
                journalTableData.copyTotalTo(journalTableCopy);
            }
        }
    }
    else if (_fromLevel == _toLevel)
    {
        if (_toLevel == JournalBlockLevel::None)
        {
            journalTableData.initTotal();
            journalTableData.copyTotalTo(journalTableCopy);
        }
        else
        {
            throw error(strFmt("@SYS22828",funcName()));
        }
    }
    else  // lock
    {
        if (journalTableCopy.SystemBlocked && _toLevel < JournalBlockLevel::System && _journalTable.dataSource())
        {
            dropBlocking = true;
        }
        else
        {
            if (_fromLevel == JournalBlockLevel::None)
            {
                if (journalTableCopy.SessionId) // allready in use
                {
                    if (isSessionActive(journalTableCopy.SessionId,journalTableCopy.SessionLoginDateTime))
                    {
                        throw error(strFmt("@SYS18418",journalTableCopy.JournalId,new xSession(journalTableCopy.SessionId).userId()));
                    }
                    throw error(strFmt("@SYS69035",journalTableCopy.JournalId));
                }
            }
            else  // _fromLevel = JournalBlockLevel::InUse
            {
                if (! journalTableCopy.SessionId) // is not in use
                {
                    throw error(strFmt("@SYS69036",journalTableCopy.JournalId));
                }
                if (_updateTotal)
                {
                    journalTableData.copyTotalTo(journalTableCopy);
                }
            }
            if (journalTableCopy.SystemBlocked)
            {
                throw error(strFmt("@SYS21618",journalTableCopy.JournalId));
            }
        }
    }
    if (! dropBlocking)
    {
        if (_toLevel == JournalBlockLevel::System)
        {
            journalTableCopy.SystemBlocked          = true;
            journalTableCopy.SessionId              = 0;
            journalTableCopy.SessionLoginDateTime   = utcDateTimeNull();
        }
        else if (_toLevel == JournalBlockLevel::InUse)
        {
            xSession = new xSession(sessionId());

            journalTableCopy.SystemBlocked      = false;
            journalTableCopy.SessionId              = sessionId();
            journalTableCopy.SessionLoginDateTime   = xSession.loginDateTime();
        }
        else if (_toLevel == JournalBlockLevel::None)
        {
            journalTableCopy.SystemBlocked      = false;
            journalTableCopy.SessionId              = 0;
            journalTableCopy.SessionLoginDateTime   = utcDateTimeNull();
        }
        journalTableCopy.doUpdate();
    }
    ttscommit;
    // Remove the journalNum cache that was set on \Classes\LedgerJournalTaxDocument\doTaxGroupExistsForDocument,
    // since a different user may edit the tax information on the document
    // while this user is not editing the document. Since the update() will
    // be called when the InUse flag is cleared on the journal header, this
    // will ensure the journal re-checks for taxes on this session the next
    // time it is used
    LedgerJournalTaxDocument::clearCachedTaxExistenceCheck(_journalTable.JournalId);
    return journalTableCopy;
}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...