mardi 24 janvier 2012

How to insert serial and batch numbers of items and transactions journal lines

Here is the job 
static void import_StunDimmension(Args _args)
{
    Dimensions                                  dimensions;
    container                                   c;
    TextIo                                      io;
    str 250                                     fileName;
    int                                         inc;
    TextBuffer                                  b;
    str 40                                      numSeriel,numBatch,ItemNum,fourSerial;
    DimensionCode                               DimensionCode;
    str 50                                      Config,Size,Color,Seriel;
    real                                        Price,counted;
    InventSerial                                inventSerial;
    InventBatch                                 InventBatch;
    real lineNumber = 0;
    str 20                                      iSize,iConfig,iColor,iJournalId,iJournalnameId,iSite,iInventLocation,iWmsLocation;
    InventDimCombination                        InventDimCombination;
    InventJournalTrans                          InventJournalTrans;
    InventDim inventDim1;
    InventDim inventDim2;

    InventJournalTable                          InventJournalTable;
    InventLocation                              inventLocation;
    Inventtable                                 inventtable;
    InventTableModule                           inventTableModule;

    ;
    fileName = @"D:\Export\Import\MAJ_BOBPRF-INV-STK000005-STK000048.csv";

     iJournalnameId = "INV";

     b=new Textbuffer();

     io = SysLicenseCodeReadFile::openFile(fileName,'r');

     if (!io)
        throw error(strfmt("@SYS18678",fileName));
     io.inFieldDelimiter(";");
     c = io.read();

     ttsbegin;

     while (io.status() == IO_Status::Ok)
     {
         c = io.read();
         inc++;
         if (io.status() != IO_Status::Ok)
         break;

         ItemNum  = conpeek(c,1);
         numSeriel = strRTrim(strLtrim(conpeek(c,2)));
         fourSerial = conpeek(c,3);
         numBatch = strRTrim(strLtrim(conpeek(c,4)));
         iSize = conpeek(c,5);
         iConfig = conpeek(c,6);
         iColor = conpeek(c,7);
         counted = conpeek(c,9);
         price = conpeek(c,8);
         iSite = conpeek(c,10);
         iInventLocation = conpeek(c,11);
         iWmsLocation = conpeek(c,12);
         iJournalId = conpeek(c,13);

        select firstonly forupdate inventTableModule
            where inventTableModule.ItemId ==  ItemNum
                && inventTableModule.ModuleType ==  ModuleInventPurchSales::Invent;

        if (inventTableModule)
        {
            inventTableModule.Price = price;
            inventTableModule.PriceUnit =1;
            inventTableModule.update();

        }

        inventDim1.clear();
        inventDim1.initValue();

        inventDim1.configId = iConfig;
        inventDim1.wMSLocationId = iWmsLocation;
        inventDim1.InventLocationId = iInventLocation;
        inventDim1.InventColorId = iColor;
        inventDim1.inventSerialId = numSeriel;
        inventDim1.InventSiteId = iSite;
        inventDim1.inventBatchId = numBatch;
        inventDim1.InventSizeId = iSize;

        inventDim2.clear();
        inventDim2 = InventDim::findOrCreate(inventDim1);

        if(inventDim2 != null)
        {
             if ((numSeriel != ""))
             {
                select firstOnly forupdate inventSerial
                    where inventSerial.InventSerialId == numSeriel
                       && inventSerial.ItemId == ItemNum;
                if (! inventSerial)
                {
                    inventSerial.InventSerialId = numSeriel;
                    inventSerial.ItemId = ItemNum;
                    inventSerial.ProdDate = systemdateget();
                    inventSerial.insert();
                }

            }

           if ((numBatch != ""))
           {
                select firstOnly forupdate InventBatch
                    where InventBatch.inventBatchId == numBatch
                        && InventBatch.itemId == ItemNum;
                if (! InventBatch)
                {
                    InventBatch.inventBatchId = numBatch;
                    InventBatch.ItemId = ItemNum;
                    InventBatch.ProdDate = systemdateget();
                    InventBatch.insert();
                }

            }
            InventJournalTable = InventJournalTable::find(iJournalId,true);

            try
            {
                InventJournalTrans.clear();
                InventJournalTrans.initValue();
                InventJournalTrans.initFromInventJournalTable(InventJournalTable);
                InventJournalTrans.initFromInventTable(InventTable::find(ItemNum));

                InventJournalTrans.JournalType = InventJournalType::Count;
                InventJournalTrans.JournalId   = iJournalId;
                InventJournalTrans.LineNum = InventJournalTrans::lastLineNum(InventJournalTrans.JournalId) + 1;
                InventJournalTrans.InventDimId = inventDim2.inventDimId;
                InventJournalTrans.Counted = counted;
                InventJournalTrans.Qty = counted;
                InventJournalTrans.CostPrice = price;
                InventJournalTrans.inventMovement().journalSetQty();
                InventJournalTrans.CostAmount = InventJournalTrans.calcCostAmount();
                InventJournalTrans.ProjTaxItemGroupId ="TVA-18";
                InventJournalTrans.PriceUnit =1; // Pas paa

                InventMoveMent::setAutoReserving(InventJournalTrans);

                InventJournalTrans.validateWrite();

                InventJournalTrans.insert();

                InventJournalTable.NumOfLines = InventJournalTable.NumOfLines +1;
                InventJournalTable.update();

               // InventItemLocation::updateStartCounting(InventJournalTrans.ItemId,InventJournalTrans.inventDim(), InventJournalTrans.JournalId);

            } catch (Exception::Error)
            {
                throw (Exception::Error);
            }

        } else
        {
         info("Missing:"+ItemNum+" config:"+iConfig+" Size:"+iSize+ " color:"+iColor);
        }
      }
      ttscommit;
      pause;
}

jeudi 29 décembre 2011

How to display system date and time in a report

display String30 clearanceDate()
{
    return date2str(systemdateget(),123,2,DateSeparator::Slash,2, DateSeparator::Slash,4)
             + "   " + time2str(timenow(),1,1) ;
}

How to use pack/unpack in a form

We can also use pack/unpack mechanism in a form like the ones used in Runbase Classes

Refer to this link  
http://daxguy.blogspot.com/2006/12/use-packunpack-on-form.html

When to use "ParentVersion" and when should I use "CurrentVersion" in pack/unpack method

If you're making your own, you should use CurrentVersion.  ParentVersion is likely a way to support older versions of packed information, or it's doing something fancy.  Here's an example of multiple version support in an unpack():

boolean unpack(container _packedClass)
{
int version = RunBase::getVersion(_packedClass); // this is a better way of conpeek(_packedClass, 1)
boolean ret = true;
;
switch (version)
{
case #CurrentVersion:
[version, #CurrentList] = _packedClass;
break;
case 2:
[version, startDate, endDate] = _packedClass;
break;
default:
ret = false;
break;
}
return ret;
}

It's just a switch on the version number so old data can be handled at least partially.

Why, When and How to use pack and unpack methods

An object has a particular state. You want to work with the same object at a later time or another place (such as on another tier). 
Make it possible to save the state of the object in a form that can be persistently saved or transported to another tier. Moreover, make it possible to reinstantiate the object.  

Basically, the pack method serializes the object, and unpack deserializes it.
What this gives you is the ability to automatically have all the variables set to specific values next time the job (class) is run.

For example, you have a class that prompts the user for an ItemId and does something with this item.
Each time the job is run, the user would have to select the item again, if it were not for the pack/unpack methods.

All you have to do to use them is declare the needed variables in the classDeclaration, add a macro defining the CurrentVersion of the parameterList and implement the pack and unpack methods. (the code is in the RunBase class - just copy it (removing #if.never, of course)

the information that is saved / restored is per user & per company basis.
The important thing to remember is - version id.  If you change the contents of container, you should remember to increment the version id. 

refer to this link to find an example.
https://community.dynamics.com/product/ax/f/33/p/67248/122709.aspx

and here is a link from MSDN tutorials
 http://msdn.microsoft.com/en-us/library/aa879675.aspx