Nov 20, 2012

PO creation on Button click

The following is to create the  PO for the journals on the button click event for the selected posted journals on ledgerJournalTable form which has TradeAgreement

Step 1 : Created one field TradeAgreement  (TradeAgreement is associated with items on its master form) on LedgerJournalTable.

Step 2  : Create one edit method on LedgerJournalTable forms datasource Level to select the journals for PO creation.

The following is the method


public edit Noyes createPO(boolean _set, LedgerJournalTable _table, NoYes _value)
{
    LedgerJournalTable    updatePOCreation;
    ; 
    LedgerJournalTable_ds.allowEdit(true);
    if (_set)
    {
        if(_table.Posted  == Noyes::No)
        {
            throw error("Please choose posted record");
        }
        if(_value)
        { 
            setRecordId.add(_table.RecId);
            _value =  NOYes::Yes;
        } 
        else
        {
            setRecordId.remove(_table.RecId);
            _value =    NOYes::Yes;
        }
    }
    return setRecordId.in(_table.RecId);
}


Drag this method on to the Design --> Overview --> Grid

Step 3 :  Create a button and call the Class (which has PO Creation code) on its click() for selected journals
 
void clicked()
{
    SS_POCreate                     creationOfPO;
    boolean                               created;
    LedgerJournalTable              _ledgerJournalTable;
    SetEnumerator                      setRetention, updateRetention;
    SSRetentionReversal             RetentionReversal;
    ;

    super();
    creationOfPO    =   new SS_POCreate();
    RetentionReversal   =   SSRetentionReversal::construct();
    setRetention            =    Set::create(setRecordId.pack()).getEnumerator();
    updateRetention     = Set::create(setRecordId.pack()).getEnumerator();
    if(!setRecordId.empty())
    {

        ttsbegin;
        while(setRetention.moveNext())
        {
            select _ledgerJournalTable where  _ledgerJournalTable.RecId == setRetention.current();
            creationOfPO.createPO(_ledgerJournalTable.JournalNum);
            created  = true;
        }
        ttscommit;
    }
    if(created)
    {
        info("PO's created sucessfully");
    }
    else
    {
        info("PO's creation failed");
    }
}

Step 4 : The PO creation class should be as following

class SS_POCreate
{
    PurchId         purchaseId;
    PurchTable      purchaseTable;
    PurchLine       purchaseLines;
    boolean         created;
}


Void  createPO(LedgerJournalId     _journal)
{
    SS_TradeAgreementId     tradeAgreementId;
    PurchTable              purchTableCur;
    ;
    tradeAgreementId  = LedgerJournalTable::find(_journal).SS_TradeAgreementId;

    select purchaseTable where purchaseTable.SS_JournalNo == _journal;

    if(purchaseTable.RecId)
    {
        throw error (strfmt("PO is already created for the Journal : %1 , so unable to create again for the same",_journal));
    }
    else
    {
       ttsbegin;
        if(tradeAgreementId)
        { 
            purchaseTable.PurchId        = NumberSeq::newGetNumFromCode(NumberSequenceReference::find(extendedTypeNum(purchid)).NumberSequence,true,false).num(); 

            purchaseTable.OrderAccount   = SS_CategoryTradeAgreement::find(tradeAgreementId).Vendor;
            purchaseTable.InvoiceAccount = SS_CategoryTradeAgreement::find(tradeAgreementId).Vendor;
            purchaseTable.PurchaseType   = PurchaseType::Purch;
            purchaseTable.PurchStatus    = PurchStatus::Backorder;
            purchaseTable.PurchTypeCode  = "Stock";
            purchaseTable.initFromVendTable(VendTable::find(SS_CategoryTradeAgreement::find(tradeAgreementId).Vendor));
            purchaseTable.PurchOrderType = PurchOrderType::Regular;
            purchaseTable.SS_JournalNo   = _journal;
            purchaseTable.CurrencyCode   = SS_CategoryTradeAgreement::find(tradeAgreementId).Currency;
            purchaseTable.insert();

            if(purchaseTable.PurchId)
            {
               this.createPOLines(purchaseTable.PurchId ,tradeAgreementId);

            }
            ttscommit;

        }
    }
}

void createPOLines(PurchId   _purchId ,SS_TradeAgreementId   _tradeAgreementId)
{
    InventDim             _inventDim;
    InventItemLocation    inventItemLocation;
    Inventlocation        inventLocation;
    InventTable           inventTable;
    ;
    purchaseLines.clear();
    purchaseLines.PurchId    = _purchId;
    purchaseLines.ItemId     = SS_CategoryTradeAgreement::find(_tradeAgreementId).ItemId;
    inventTable              = inventTable::find(purchaseLines.ItemId);
    purchaseLines.PurchUnit  = inventTable.purchUnitId();

    purchaseLines.PurchQty              = 1;
    purchaseLines.PurchPrice            = SS_CategoryTradeAgreement::find(_tradeAgreementId).Rate;

    purchaseLines.CurrencyCode          = SS_CategoryTradeAgreement::find(_tradeAgreementId).Currency;
    purchaseLines.PurchStatus           = PurchStatus::Backorder;
    purchaseLines.PurchaseType          = PurchaseType::Purch;
    purchaseLines.LineAmount            = purchaseLines.PurchQty * purchaseLines.PurchPrice;
    purchaseLines.LineNum               = PurchLine::lastLineNum(_purchId) + 1.0;


    select firstonly InventItemLocation
        where InventItemLocation.ItemId == purchaseLines.ItemId
        &&   InventItemLocation.inventDimId != "AllBlank";

    if(InventItemLocation.RecId)
    {
          _inventDim.InventSiteId    = InventLocation::find(InventDim::find(InventItemLocation.inventDimId).InventLocationId).InventSiteId;
          _inventDim.InventLocationId = InventDim::find(InventItemLocation.inventDimId).InventLocationId;

    purchaseLines.InventDimId =   InventDim::findOrCreate(_inventDim).inventDimId;
    purchaseLines.insert();
    }
    else
    {
    throw error(strfmt("Warehouse item setup could not find for the item :%1 ",purchaseLines.ItemId));
    }
}

static  SS_POCreate construct()
{
    return new SS_POCreate();
}