BLVCKFX
5/9/2019 - 8:37 AM

[BOF] ConsignmentStock

Delete record from ReqItemTable

[
    DataContractAttribute,
    SysOperationContractProcessingAttribute(classstr(SysOperationAutomaticUIBuilder))
]
class A_ConsignmentStockContract
{
    str             packedQuery;
}

/* === Method::Start === */
public Query getQuery()
{
    return
    new Query(SysOperationHelper::base64Decode(packedQuery));
}

/* === Method::Start === */
public void initQuery()
{
    Query                   newQuery;
    QueryBuildDataSource    qbds;
    QuerybuildRange         qbr;

    ;

    newQuery = new Query(queryStr(A_ConsigmentStock));

    qbds = newQuery.dataSourceTable(tableNum(A_SalesQuotationTable));
    qbr = qbds.addRange(fieldnum(A_SalesQuotationTable, ValidToDate));
    qbr.value(SysQuery::value(today()));

    this.setQuery(newQuery);
}

/* === Method::Start === */
[
    DataMemberAttribute,
    AifQueryTypeAttribute('_packedQuery', queryStr(A_ConsigmentStock))
]
public str parmPackedQuery(str _packedQuery = packedQuery)
{
    packedQuery = _packedQuery;

    return packedQuery;
}

/* === Method::Start === */
public void setQuery(Query _query)
{
     packedQuery = SysOperationHelper::base64Encode(_query.pack());
}
class A_ConsignmentStockController extends SysOperationServiceController
{
}

/* === Method::Start === */
public boolean canGoBatch()
{
    return true;
}

/* === Method::Start === */
public void initializeFromArgs(Args _args)
{
    A_ConsignmentStockContract     contract;

    ;

    super(_args);

    contract = this.getDataContractInfoObject().dataContractObject();
    contract.initQuery();
}

/* === Method::Start === */
public static void main (Args _args)
{
    A_ConsignmentStockController    controller;
    SysOperationStartResult         result;

    ;

    controller = A_ConsignmentStockController::newFromArgs(_args);

    result = controller.startOperation();
    if(result != SysOperationStartResult::Started)
        throw Exception::Error;
}

/* === Method::Start === */
public static A_ConsignmentStockController newFromArgs(Args _args)
{
    A_ConsignmentStockController    controller;

    ;

    controller = new A_ConsignmentStockController();

    controller.initializeFromArgs(_args);

    return controller;
}
class A_ConsignmentStockService
{
}

/* === Method::Start === */
private InventLocation defaultLocationId(InventSiteId _siteId, EcoResProductDisplayProductNumber _displayProductNumber)
{
    InventItemSalesSetup    inventItemSalesSetup;
    InventDim               inventDim;
    InventLocationId        inventLocationId;
    InventLocation          inventLocation;

    ;

    select inventItemSalesSetup
            where inventItemSalesSetup.ItemId == _displayProductNumber
        join inventDim
            where inventDim.inventDimId == inventItemSalesSetup.InventDimId
                && inventDim.InventSiteId == _siteId;

    inventLocationId = inventItemSalesSetup.inventDimDefault().InventLocationId;

    inventlocation = Inventlocation::find(inventLocationId);

    if(!inventlocation.RecId)
        throw error(strFmt("@SYS5001", inventLocationId));

    return inventlocation;
}

/* === Method::Start === */
[SysEntryPointAttribute]
public void endConsignmentStock(A_ConsignmentStockContract _contract)
{
    QueryRun                        qr;
    A_SalesQuotationLineOption      salesQuotationLineOption;
    A_SalesQuotationLine            salesQuotationLine;

    InventLocation                  invLocTo;
    InventLocation                  invLocFrom;
    InventTable                     inventTable;
    InventDim                       inventDim;

    reqItemTable                    reqItemTable;

    ;

    try
    {
        ttsBegin;

        qr = new QueryRun(_contract.getQuery());
        while(qr.next())
        {
            salesQuotationLineOption = qr.get(tableNum(A_SalesQuotationLineOption));
            salesQuotationLine = A_SalesQuotationLine::findByRecId(salesQuotationLineOption.SalesQuotationLine);

            invLocTo    = this.findInvLocationTo(salesQuotationLineOption.SalesQuotationLine);

            invLocFrom  = this.defaultLocationId(salesQuotationLineOption.SiteId, salesQuotationLineOption.DisplayProductNumber);

            inventTable = this.findItem(salesQuotationLineOption.DisplayProductNumber
                                                    , salesQuotationLine.Configuration
                                                    , salesQuotationLine.SizeId
                                                    , salesQuotationLine.ColorId
                                                    , salesQuotationLine.StyleId
                                                );

            inventDim = this.findInventDim(inventTable, salesQuotationLine, invLocTo);

            reqItemTable = ReqItemTable::find(inventTable.ItemId, inventDim.inventDimId, true);
            reqItemTable.delete();

        }

        ttsCommit;
    }
    catch
    {
        ttsAbort;
    }
}

/* === Method::Start === */
private InventDim findInventDim(InventTable _inventTable, A_salesQuotationLine _salesQuotationLine, InventLocation _invLocTo)
{
    NoYes               isinventStatusActive;
    inventDim           inventDim;

    ;

    isinventStatusActive = InventDimGroupSetup::newinventTable(_inventTable).isDimensionActive(fieldNum(inventDim, InventStatusId));

    inventDim.clear();
    inventDim.InventSiteId        = _invLocTo.InventSiteId;
    inventDim.InventLocationId    = _invLocTo.InventLocationId;
    inventDim.InventStatusId      = isinventStatusActive ? _invLocTo.DefaultStatusId : "";
    inventDim.configId            = _salesQuotationLine.Configuration;
    inventDim.InventSizeId        = _salesQuotationLine.SizeId;
    inventDim.InventColorId       = _salesQuotationLine.ColorId;
    inventDim.InventStyleId       = _salesQuotationLine.StyleId;
    inventDim.clearNotCovPrDim(InventDimGroupSetup::newinventTable(_inventTable));

    inventDim = InventDim::findOrCreate(inventDim);

    return InventDim;
}

/* === Method::Start === */
private Inventlocation findInvLocationTo(A_SalesQuotationLineRecId  _salesQuotationLine)
{
    dirPartyTable       party;
    CustAccount         accountNum;
    Inventlocation      inventlocation;

    ;

    party = dirPartyTable::findRec(A_SalesQuotationLine::findByRecId(_salesQuotationLine).salesQuotationTable().Party);
    accountNum = CustTable::findByPartyRecId(party.RecId).AccountNum;

    if (!accountNum)
        throw error(strFmt("@SYS4730", party.Name));

    inventlocation = Inventlocation::find(strFmt("%1", accountNum));

    if(!inventlocation.RecId)
        throw error(strFmt("@SYS5001", accountNum));

    return inventlocation;
}

/* === Method::Start === */
private InventTable findItem(EcoResProductDisplayProductNumber _displayProductNumber, EcoResConfigurationName _configId, EcoResSizeName _sizeId, EcoResColorName _colorId, EcoResStyleName _styleId)
{
    EcoResProduct                       product;
    container                           dimensions;
    EcoResDistinctProductVariant        distinctProductVariant;

    CompanyInfo                         companyInfo;

    InventDimCombination                inventDimCombination;
    InventTable                         inventTable;
    InventDim                           inventDim;

    EcoResProductReleaseSessionManager  productReleaseManager;

    ;

    product = EcoResProduct::findByDisplayProductNumber(_displayProductNumber);
    if(!product)
        throw error(strFmt("@SYS5307", _displayProductNumber));

    companyInfo = CompanyInfo::find();

    inventDim.clear();
    inventDim.configId = _configId;
    inventDim.InventSizeId = _sizeId;
    inventDim.InventColorId = _colorId;
    inventDim.InventStyleId = _styleId;
    inventDim = InventDim::findOrCreate(inventDim);

    inventDimCombination = InventDimCombination::findByInventDim(_displayProductNumber, inventDim);
    inventTable = inventDimCombination.inventTable();

    if (!inventTable)
        throw error(strFmt("@SYS5307", _displayProductNumber));


    return inventTable;
}