mercredi 21 décembre 2011

How to import vendors from a csv file

Here is an example of a job that can be used to import vendors

static void import_fournisseurs_DIST(Args _args)
{
    Dirpartytable                               dirpartytable, _dirpartytable;
    container                                   c;
    TextIo                                      io;
    str 130                                     fileName;
    TextBuffer                                  b;
    str                                         s,stramount;
    integer                                     inc;
    DirPartyid                                  partyid;
    Vendtable                                   vendTable;
    SmmBusRelSectorTable                        smmBusRelSectorTable;
    TaxVATNumTable                              taxVATNumTable;
    Address                                     addressTab;
    Integer                                     counter;
    ;

    delete_from vendTable;
    delete_from dirpartytable
        where Dirpartytable.Type == Dirpartytype::Organization;
    delete_from smmBusRelSectorTable;

    fileName = @"C:\\FRS_DIST\FRS_LOC_DIST.csv";
    io = SysLicenseCodeReadFile::openFile(fileName,'r');
    if (!io)
        throw error(strfmt("@SYS18678",fileName));
    io.inFieldDelimiter(";");
    c = io.read();
    while (io.status() == IO_Status::Ok)
    {
        c = io.read();
        inc++;
        if (io.status() != IO_Status::Ok)
        break;

        partyid                 = conpeek(c,1);
        Dirpartytable.PartyId   = partyid;
        Dirpartytable.Type      = DirPartyType::Organization;
        Dirpartytable.NameAlias = conpeek(c,4);
        DirpartyTable.Name      = conpeek(c,3);

        if (DirpartyTable.Name != "" )
        {
            Dirpartytable.insert();
        }

        vendTable.AccountNum = conpeek(c,1);
        vendTable.name       = conpeek(c,2);
        vendtable.PartyId    = partyid;
        vendTable.NameAlias  = conpeek(c,3);

        vendTable.Currency              = conpeek(c,8);
        vendTable.InvoiceAccount        = conpeek(c,5);

        vendTable.VendGroup  = conpeek(c,6);
        vendTable.LanguageId = "fr";
        vendTable.PartyType  = DirPartyType::Organization;
        vendTable.Phone      = conpeek(c,11);
        vendTable.TeleFax    = conpeek(c,12);
        vendTable.Email      = conpeek(c,13);
        vendTable.VATNum     = conpeek(c,9);
        if (vendTable.name   != "0")
        {
            VendTable.insert();
        }
        print dirpartytable.PartyId;
        select recId from _dirpartytable
            where _dirpartytable.RecId   == dirpartytable.RecId
                && dirpartytable.PartyId == partyid;
                print _dirpartytable.RecId;
        addressTab.AddrRecId        = _dirpartytable.RecId;
        addressTab.AddrTableId      = 2303;
        addressTab.Street           = conpeek(c,10);

        addressTab.insert();
       
        if (conpeek(c,7) != '')
        {
            smmBusRelSectorTable.PartyId                = partyId;
            smmBusRelSectorTable.BusinessSectorId       = conpeek(c,7);
            smmBusRelSectorTable.KeyId                  = partyId;

            smmBusRelSectorTable.insert();
        }
        if ( partyid == '')
        break;
    }
    info("inserted");
}

Aucun commentaire:

Enregistrer un commentaire