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;
}