Thursday, 28 April 2016

inventoryOnHandUpload on ax 2009

static void _kr_inventoryOnHandUpload(Args _args)
{
  Price price;
  InventJournalCheckPost inventJournalCheckPost;
  ItemId itemId;
  ASCIIIo file;
  Dialog dlg;
  FileNameOpen filename;
  Date conversionDate;
  DialogField dfFileName;
  DialogField dfConversionDate;
  container con;
  int linenum, read, added, skipped, last;
  str 4 slast;
  inventqty qty;
  str 10  whse;
  InventJournalCopy inventJournalCopy;

  InventJournalTable InventJournalTable, retInventJournalTable;
  InventJournalTrans InventJournalTrans;
  InventLocationId inventLocationId;
  InventDim InventDim;
  LedgerTrans ledgerTrans;
  ;
//  changecompany('t1uk')
  {
//     filename = 'C:\\Users\\DDyachenko\\Desktop\\1.txt';
//     conversiondate = today();
     linenum = 0;
     InventJournalTable = null; InventJournalTrans = null;inventDim = null;
     dlg = new Dialog('Open inventory onhand file');
     dfFileName = dlg.addFieldValue(typeid(FileNameOpen),filename);
     dfConversionDate = dlg.addFieldValue(typeid(TransDate),conversiondate, "Conversion date");
     if (!dlg.run())
       throw error('Cancelled by the user');
     ttsbegin;

     filename = dfFileName.value();
     conversionDate = dfConversionDate.value();
     if (conversionDate == datenull())
     {
        throw (error("Conversion date must be populated"));
     }
     file = new ASCIIIO(filename,'rt');
     file.inFieldDelimiter('\t');
     file.inRecordDelimiter('\n');
     info(strfmt('File %1',filename));
     select  inventJournalTable  order by journalid desc where inventjournaltable.journalid  like 'conv*';
     if (inventJournalTAble.RecId)
     {
           last = str2int(substr(inventJournalTable.JournalId,5,strlen(inventJournalTable.JournalId)-4));
           info(strfmt("Previous journal found %1",inventJournalTable.JournalId));

        slast = int2str(last);
        if (strlen(slast)<4)
           slast = strfmt("%1%2",strrep('0',4-strlen(slast)),slast);

        retInventJournaltable = InventJournalTable::find(strfmt("Conv%1rev",slast));
        if (!retInventJournalTable.RecId)
        {
           inventJournalCopy = InventJournalCopy::newStandard(null,inventJournalTable.JournalId);
           inventJournalCopy.krParmReverseSign(true);
           inventJournalCopy.krparmJournalIdTo(strfmt("Conv%1rev",slast));
           inventJournalCopy.run();
           retInventJournalTable = InventJournalTable::find(inventJournalCopy.krParmJournalIdTo());
           info(strfmt('Reversing journal %1 created',retInventJournalTable.JournalId));
           if (inventjournaltable.Posted)
           {
             inventJournalCheckPost = InventJournalCheckPost::newPostJournal(retinventJournalTable);
             inventJournalCheckPost.run();
             info(strfmt("Inventory journal %1 posted",retinventjournalTAble.JournalId));
           }
        }
        else
          info(strfmt("Reversing journal %1 exists",retInventJournaltable.JournalId));

     }
     else
       info('No previous journal found');

     last++;
     slast = int2str(last);
     if (strlen(slast)<4)
        slast = strfmt("%1%2",strrep('0',4-strlen(slast)),slast);
     info(strfmt("New journal %1",strfmt("Conv%1",slast)));
     InventJournalTable.clear();
     InventJournalTable.JournalId = strfmt("Conv%1",slast);
     InventJournalTable.Description = "Inventory on-hand balance";
     InventJournalTable.Reservation = ItemReservation::None;
     InventJournalTable.JournalType = InventJournalType::Movement;
     InventJournalTable.JournalNameId = "IMov";
     InventJournalTable.VoucherDraw = JournalVoucherDraw::Post;
     InventJournalTable.VoucherChange = InventJournalVoucherChange::DateChange;
     InventJournalTable.VoucherSeqId = "Inve_72";
     InventJournalTable.DetailSummary = DetailSummary::Summary;
     InventJournalTable.LedgerAccountIdOffset = "129450";
     if (inventjournaltable.validateWrite())
        InventJournalTable.insert();
     else
       throw error('script aborted');

     while (file.status() == IO_Status::Ok)
     {
        read++;

        con = file.read();
        if (read == 1)
         continue;
        if (conlen(con) ==0)
          continue;
        if (conlen(con)<5 )
        {
           warning(strfmt("line: %1, number of columns %2",read, conlen(con)));
           continue;
        }
        itemId = conpeek(con,1);
        if( !InventTable::find(itemid).RecId)
        {
            warning(strfmt('item %1 not found in the Item Master table',itemid));
            continue;
        }
        whse = conpeek(con,2);
        switch(whse)
        {
           case 'GHT':
             inventlocationid  = 'GHEA';
             break;
           default:
             inventLocationId = whse;
        }
        InventDim.clear();
        inventDim.inventSiteId = InventLocation::find(inventLocationId).InventSiteId;
        inventDim.InventLocationId = inventLocationId;
        inventDim = inventDim::findOrCreate(inventdim);
        qty = conpeek(con,3);
        if (qty ==0)
        {
           warning(strfmt("line %1, item %2 - zero qty",read, itemid));
           continue;
        }
        price = conpeek(con,4);

        linenum++;

        InventJournalTrans.clear();
        InventJournalTrans.initFromInventJournalTable(InventJournalTable);
        InventJournalTrans.LineNum = linenum;
        InventJournalTrans.TransDate = conversionDate;
        InventJournalTrans.Voucher = "Conversion";
        InventJournalTrans.ItemId = itemid;
        InventJournalTrans.initFromInventTable(InventTable::find(itemid));
        inventJournalTrans.InventDimId = inventDim.inventDimId;

        InventJournalTrans.Qty = qty;
        InventJournalTrans.CostPrice = price;
        inventJournalTrans.CostAmount  = InventJournalTrans.calcCostAmount();
        if (inventJournaltrans.validateWrite())
        {
           InventJournalTrans.insert();
           added++;
        }
        else
          throw error('script aborted');

     }
     if (added >0)
     {

       info(strfmt("New journal %1 created. %2 lines added",inventJournalTable.JournalId, added));
       inventJournaltable.NumOfLines = added;
       inventJournaltable.update();
       inventJournalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);
       inventJournalCheckPost.run();
       info(strfmt("Inventory journal %1 posted",inventjournalTAble.JournalId));

     }
     ttscommit;

  }
}

No comments:

Post a Comment