UFFI IMPORT ORDERS Sample code

Patrice Vigier

I looked on this forum to find code to import orders into SAP B1 but I did not find it, so here is what I have done. I hope this help you develop your own code. My code has few commented line, some are my comments, some are just so I keep records of code.

using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.Xml;
using Boyum.Utilities.FileHandler.Model;
using SAPbobsCOM;
using System.IO;
using SBO.DI;
using System;
//using SAPbouiCOM;


// ///  Change Log   ///

// 1. Could we move the defintion of which document up to the top here
// 2. Move service vs item type up here too


//document = B1BusinessObjects.Documents.Drafts.TemplateObject;
//document = B1BusinessObjects.Documents.Sales.Orders;  // Modify for other type of document ie Sales Order
//document.DocObjectCode = BoObjectTypes.oPurchaseInvoices;
//document.DocObjectCode = BoObjectTypes.oOrders;  // Modify for other type of document ie Sales Order
//document.DocType = BoDocumentTypes.dDocument_Service;
//document.DocType = BoDocumentTypes.dDocument_Items;  // Modify to change to item type document

// If statements

//documentLines.TaxCode = Double.Parse(values[9]) > 0.0f ? "TAXAB" : "TAXNO"; 

namespace UFFITemplate
{
    public class UffiRunner : UniversalFunctions.Public.FileImporter.IImporter
    {
        //Permet de faire une recherche dans une table
        public string ExecuteQueryString(string sQuery) //Pour requete SQL
        {
            SAPbobsCOM.Recordset oRecordSet = (SAPbobsCOM.Recordset) SBO.DI.Connection.SboCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            oRecordSet.DoQuery(sQuery);

            if (!oRecordSet.EoF)
            {
                return oRecordSet.Fields.Item(0).Value.ToString();
            }
            return "ERROR_ExeQS";
        }

        //Exemple Calcul de taxe
        //    double totalTax;
        /*    public void AddTaxLine(ref SAPbobsCOM.Document_Lines documentLines, double totalTax)
        {
        //Calculate Total taxes
        //totalTax = totalTax / 0.13f;
            
        //documentLines.Add();
            
        //documentLines.ItemDescription = "HST TAX TOTAL";                    // Description
        //documentLines.AccountCode = ExecuteQueryString("SELECT TOP 1 T0.[AcctCode] FROM OACT T0 WHERE T0.[Segment_0] + '-' + T0.[Segment_1] + '-' + T0.[Segment_2] + '-' + T0.[Segment_3] = '2434-00-1-00'");    // GL Account
        //documentLines.TaxCode = "HSTON"; // Tax Code
        //documentLines.LineTotal = totalTax; // Line Total
        //documentLines.TaxOnly = BoYesNoEnum.tYES;    // Tax Only 
            
        }*/

        public FileHandlerResultEntry Import(FileHandlerParameters parameters)
        {
            try
            {
                SortedList<string, string> poOrders = new SortedList<string, string>();
                string fileName = parameters.File;
                bool first = true;

                using (var reader = new StreamReader(fileName))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        var values = line.Split(',');

                        //Pour le No de linge import 
                        string temp;
                        if (values[0] != "#")  //On ne prend pas la 1ere ligne
                        {
                            temp = values[0];
                            poOrders.Add(temp, line);
                        }
                    }

                    //*****Initialisation******

                    string currClient = "";
                    
                    /*    //Commandes draft
                    var    document = B1BusinessObjects.Documents.Drafts.TemplateObject;
                    document.DocObjectCode = BoObjectTypes.oOrders;*/

                    //Commandes
                    var document = B1BusinessObjects.Documents.Sales.Orders.TemplateObject;
                    document.DocObjectCode = BoObjectTypes.oOrders;

                    //Type de document
                    //document.DocType = BoDocumentTypes.dDocument_Service;
                    document.DocType = BoDocumentTypes.dDocument_Items;

                    var documentLines = document.Lines;

                    foreach (var line in poOrders) //parcours les lignes du fichier
                    {
                        var values = line.Value.Split(',');
                        if (currClient != values[12] && !first) //A vérifier mais c'est pour ignorer la 1ere ligne
                        {
                            first = true;
                        }

                        if (first)
                        {
                            currClient = values[12];     // Controls the grouping per document in this case CardCode
                            
                            //    totalTax = 0;

                            // *********Document Header*************
                            //    document.CardCode = ExecuteQueryString("SELECT TOP 1 T0.\"CardCode\" FROM OCRD T0 WHERE T0.\"CardCode\" = '" + values[2] + "'");     // CardCode
                            document.CardCode = values[12];
                            document.DocDate = DateTime.Parse(values[9]);  // Posting Date
                            document.DocDueDate = DateTime.Parse(values[9]);  // Due Date
                            document.NumAtCard = "Référence commande sur site Vigier Parts : " + values[2];   // Ref No
                            document.Comments = "Import Vigier Parts :" + values[2];   // 
                            document.UserFields.Fields.Item("U_VG_Comment_Izorder").Value = values[10];   // Comments
                            document.UserFields.Fields.Item("U_VG_Id_Order").Value = int.Parse(values[1]); //No de commande du shop
                            //document.DocNum = int.Parse(values[6]); //est attribué automatiquement                    
                            //    string DocEntry = "$[$[BOY_TABLE].DocEntry]";    OSCL.Expenses.DocEntry = int.Parse(DocEntry);    OSCL.Expenses.DocumentType = BoSvcEpxDocTypes.edt_Invoice;




                            //*****Transport*****
                            //4 =DPD
                            //6 = Lettre suivie
                            //1 = Chronopost
                            //23 = DHL
                            //24 = La poste
                            //Le No de transport est convertit dans le programme qui exporte les facture vparts-import-export.exe
                            document.TransportationCode = int.Parse(values[4]);
                            document.Expenses.ExpenseCode = 1; //champ Code transport HT
                            if (Convert.ToDouble(values[38]) != 0) // On ne prend pas la ligne si 0
                            {
                                document.Expenses.LineTotal = Convert.ToDouble(values[38]);//Cout transport
                            }

                            documentLines = document.Lines;
                            first = false;
                        }
                        else
                        {
                            documentLines.Add();

                        }

                        //*** Document Lines ***
                        documentLines.ItemCode = values[34];

                        documentLines.Quantity = double.Parse(values[35]);
                        documentLines.Price = double.Parse(values[36]);

                        //**Livraison**
                        if(values[15].Trim() == "")
                        {
                            document.AddressExtension.ShipToAddress2 = values[14].Trim(); // Nom
                        }
                        else
                        {
                            document.AddressExtension.ShipToAddress2 = values[15].Trim() + " / " + values[14].Trim(); // Société + Nom
                        }
                        
                        document.AddressExtension.ShipToStreet = values[16].Trim(); //S'imprime
                    //    document.AddressExtension.ShipToBuilding = values[].Trim(); //S'imprime
                        document.AddressExtension.ShipToBlock = values[17].Trim(); //S'imprime
                    //    document.AddressExtension.ShipToCounty = values[].Trim(); //S'imprime
                        document.AddressExtension.ShipToZipCode = values[18].Trim(); //S'imprime
                        document.AddressExtension.ShipToCity = values[19].Trim(); //S'imprime
                    //    document.AddressExtension.ShipToState = values[].Trim(); // code 3digit  //Ne s'imprime pas
                        document.AddressExtension.ShipToCountry = values[20]; // code 3digit  //S'imprime
                
                        //**Facturation**
                        if(values[25].Trim() == "")
                        {
                            document.AddressExtension.BillToAddress2 = values[24].Trim(); // Nom
                        }
                        else
                        {
                            document.AddressExtension.BillToAddress2 = values[25].Trim() + " / " + values[24].Trim(); // Société + Nom
                        }
                        
                        document.AddressExtension.BillToStreet = values[26].Trim(); //S'imprime
                    //    document.AddressExtension.BillToBuilding = values[].Trim(); //Ne s'imprime pas
                        document.AddressExtension.BillToBlock = values[27].Trim(); //Ne s'imprime pas
                    //    document.AddressExtension.BillToCounty = values[].Trim(); //Ne s'imprime pas
                        document.AddressExtension.BillToZipCode = values[28].Trim(); //S'imprime
                        document.AddressExtension.BillToCity = values[29].Trim(); //S'imprime
                    //    document.AddressExtension.BillToState = values[].Trim(); // code 3digit  //S'imprime
                        document.AddressExtension.BillToCountry = values[30].Trim(); // code 3digit  //S'imprime 


                        //document.SendNotification = BoYesNoEnum.tYES;
                        //    document.SpecialLines.Freight1 = 1;
                        //    document.SpecialLines.Freight1FC = 11;
                        //    document.SpecialLines.Freight1SC = 111;


                        //************Utile**************//
                        //    document.TrackingNumber=
                        //document.DocEntry

                        document.SpecialLines.LineText = "texte  ";
                        //    document.SpecialLines.GrossTotal = 200;
                        //document.DocTotal = 150;
                        //    document.PaymentMethod
                        //    document.SalesPersonCode

                        //    document.UserFields
                        //    document.DocumentStatus = "O"; //read only




                        //document.PaymentMethod

                        //    documentLines.ItemDescription = values[3];                    // Description
                        //    documentLines.AccountCode = ExecuteQueryString("SELECT TOP 1 T0.[AcctCode] FROM OACT T0 WHERE T0.[Segment_0] + '-' + T0.[Segment_1] + '-' + T0.[Segment_2] = '" + values[4] + "'");    // GL Account
                        //    documentLines.DiscountPercent = Double.Parse(values[6]);        // Discount
                        //    documentLines.TaxCode = values[7];                        // Tax Code
                        //    documentLines.LineTotal = Double.Parse(values[8]);         // Line Total

                        //    totalTax += Double.Parse(values[9]);
                    }

                    if (documentLines.Count >= 1)
                    {
                        //    AddTaxLine(ref documentLines, totalTax);
                        if (document.Add() != 0)
                        {
                            return new FileHandlerResultEntry(FileHandlerResultStatus.Error, GetLastError.ErrorNumberAndDescription + " Client :" + currClient);
                        }

                    }
                    return new FileHandlerResultEntry(FileHandlerResultStatus.Success);
                }

            }
            catch (Exception e)
            {
                return new FileHandlerResultEntry(FileHandlerResultStatus.Error, "Exception caught", e);
            }
        }
    }
}

I cannot attache the imported file, sorry about this, you will have to figure it out. Just know that the first line is for field name (or anything) and it must start with #. The first column is dedicated to line number.

 

Comentarios

0 comentarios

Iniciar sesión para dejar un comentario.