Esempio n. 1
0
        private void comarchOptimaImportOrderSTA(EventLog eventLog)
        {
            string akronim;

            try
            {
                string    orderId        = "";
                WebClient client         = new WebClient();
                string    prestaResponse = "";

                try
                {
                    NameValueCollection postData = new NameValueCollection()
                    {
                        { "action", "getXmlOrders" }
                    };
                    prestaResponse = Encoding.UTF8.GetString(client.UploadValues(Properties.Settings.Default.ordersGate, postData));
                }
                catch (Exception exPresta)
                {
                    eventLog.WriteEntry("Error 1 on order import:" + Environment.NewLine + exPresta.ToString(), EventLogEntryType.Error, 0);
                }

                XmlDocument ordersXML = new XmlDocument();
                ordersXML.LoadXml(prestaResponse);
                XmlElement ordersXMLroot = ordersXML.DocumentElement;

                if (ordersXMLroot.ChildNodes.Count > 0)
                {
                    foreach (XmlNode orderXML in ordersXMLroot.ChildNodes)
                    {
                        bool error = true;
                        try
                        {
                            Dictionary <string, List <XmlNode> > splitedOrder = new Dictionary <string, List <XmlNode> >();
                            orderId = orderXML["id"].InnerText;
                            XmlNode tmpNode = orderXML["associations"];
                            foreach (XmlNode orderXmlElement in tmpNode["order_rows"])
                            {
                                string        TwrTwrId = orderXmlElement["TwrTwrId"].InnerText;
                                SqlConnection conn     = new SqlConnection();
                                conn.ConnectionString =
                                    "Data Source=" + Properties.Settings.Default.serverName + ";" +
                                    "Initial Catalog=" + Properties.Settings.Default.dataBaseName + ";" +
                                    "User id=" + Properties.Settings.Default.userName + ";" +
                                    "Password="******";";
                                conn.Open();
                                SqlCommand getKntId = new SqlCommand("select twr_twrid, twr_kod, 7 from cdn.Towary where twr_twrid = " + TwrTwrId, conn);


                                SqlDataReader reader = getKntId.ExecuteReader();

                                while (reader.Read())
                                {
                                    if (!splitedOrder.ContainsKey(reader[0].ToString()))
                                    {
                                        splitedOrder.Add(reader[0].ToString(), new List <XmlNode>());
                                    }
                                    splitedOrder[reader[0].ToString()].Add(orderXmlElement);
                                }

                                reader.Close();
                                reader.Dispose();

                                conn.Close();
                            }


                            foreach (KeyValuePair <string, List <XmlNode> > orderContent in splitedOrder)
                            {
                                LogowanieAutomatyczne();
                                AdoSession Sesja = Login.CreateSession();


                                try {
                                    XmlNode xmlBilling  = orderXML.SelectSingleNode("address_invoice")["address"];
                                    XmlNode xmlShipping = orderXML.SelectSingleNode("address_delivery")["address"];
                                    XmlNode xmlCustomer = orderXML.SelectSingleNode("customer");

                                    try
                                    {
                                        Kontrahent knt;
                                        XmlNode    xmltmp = orderXML["address_invoice"]["address"];


                                        DefAtrybut defAtrybut = Sesja.CreateObject("CDN.DefAtrybuty").Item("DeA_Kod='B2BID'");
                                        string     KnA_DeAId  = "KnA_DeAId=" + defAtrybut.ID.ToString() + "";

                                        SqlConnection conn = new SqlConnection();
                                        conn.ConnectionString =
                                            "Data Source=" + Properties.Settings.Default.serverName + ";" +
                                            "Initial Catalog=" + Properties.Settings.Default.dataBaseName + ";" +
                                            "User id=" + Properties.Settings.Default.userName + ";" +
                                            "Password="******";";
                                        conn.Open();


                                        SqlCommand idKnt = new SqlCommand("SELECT    [KnA_PodmiotId]  ,[KnA_WartoscTxt]  FROM [CDN_Tomax].[CDN].[KntAtrybuty] where [KnA_DeAId] = '" + defAtrybut.ID.ToString() + "' AND [KnA_WartoscTxt] = '" + xmlBilling["id"].InnerText + "'", conn);


                                        SqlDataReader reader    = idKnt.ExecuteReader();
                                        int           podmiotId = 0;

                                        akronim = "B2B_";
                                        if (xmlBilling["company"].InnerText.Length > 0)
                                        {
                                            akronim += xmlBilling["company"].InnerText.Replace(" ", "");
                                        }
                                        akronim += xmlBilling["lastname"].InnerText + "_" + xmlBilling["id"].InnerText;

                                        if (reader.Read())
                                        {
                                            Int32.TryParse(reader["KnA_PodmiotId"].ToString(), out podmiotId);
                                            knt = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_kntid=" + podmiotId);
                                        }

                                        else
                                        {
                                            podmiotId = 0;

                                            try
                                            {
                                                knt = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_Kod='" + akronim + "'");
                                            }catch (Exception ex)
                                            {
                                                knt = null;
                                            }

                                            if (knt == null)
                                            {
                                                knt = Sesja.CreateObject("CDN.Kontrahenci").AddNew();

                                                knt.Akronim           = akronim;
                                                knt.Rodzaj_Odbiorca   = 1;
                                                knt.Rodzaj_Dostawca   = 0;
                                                knt.Nazwa1            = xmlBilling["company"].InnerText;
                                                knt.Nazwa2            = xmlBilling["firstname"].InnerText + " " + xmlBilling["lastname"].InnerText;
                                                knt.Adres.Ulica       = xmlBilling["address1"].InnerText;
                                                knt.Adres.NrDomu      = xmlBilling["address2"].InnerText;
                                                knt.Adres.Miasto      = xmlBilling["city"].InnerText;
                                                knt.Adres.KodPocztowy = xmlBilling["postcode"].InnerText;
                                                knt.Adres.Kraj        = "Polska";

                                                if (xmlCustomer["email"].InnerText.Length > 2)
                                                {
                                                    knt.Email = xmlCustomer["email"].InnerText;
                                                }

                                                if (xmlBilling["phone"].InnerText.Length > 5)
                                                {
                                                    knt.Telefon = xmlBilling["phone"].InnerText;
                                                }

                                                if (xmlBilling["company"].InnerText.Length > 0)
                                                {
                                                    knt.Nazwa1 = xmlBilling["company"].InnerText;
                                                }

                                                if (xmlBilling["address1"].InnerText.Length > 0)
                                                {
                                                    knt.Adres.Ulica = xmlBilling["address1"].InnerText;
                                                }



                                                knt.Adres.Kraj = "Polska";

                                                knt.Nip = xmlBilling["vat_number"].InnerText;



                                                if (xmlBilling["postcode"].InnerText.Length > 0)
                                                {
                                                    knt.Adres.KodPocztowy = xmlBilling["postcode"].InnerText;
                                                }
                                                if (xmlBilling["city"].InnerText.Length > 0)
                                                {
                                                    knt.Adres.Miasto = xmlBilling["city"].InnerText;
                                                }


                                                // DokAtrybut dostawa = dok.Atrybuty.AddNew();
                                                // dostawa.Kod = "METODADOSTAWY";
                                                // dostawa.Wartosc = orderXML["Header"]["DeliveryMethod"].InnerText;
                                                //DokAtrybut platnosc = dok.Atrybuty.AddNew();

                                                KntAtrybut b2bId = knt.Atrybuty.AddNew();


                                                b2bId.DefAtrybut = defAtrybut;
                                                b2bId.ROSaveMode = 1;
                                                b2bId.Wartosc    = xmlBilling["id"].InnerText;
                                                Sesja.Save();

                                                podmiotId = knt.ID;
                                            }
                                        }



                                        reader.Close();
                                        reader.Dispose();

                                        conn.Close();



                                        /*
                                         *
                                         * if (xmlBilling["vat_number"].InnerText.Length >= 10) {
                                         * string Knt_nip = "Knt_Nip='" + xmlBilling["vat_number"].InnerText + "'";
                                         * knt = Sesja.CreateObject("CDN.Kontrahenci").Item(Knt_nip);
                                         * }
                                         * else
                                         * {
                                         * knt = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_Kod='!NIEOKREŚLONY!'");
                                         * }
                                         */



                                        DokumentHaMag dok = Sesja.CreateObject("CDN.DokumentyHaMag").AddNew();
                                        dok.Rodzaj       = 308000;
                                        dok.TypDokumentu = 308;


                                        var rNumerator = dok.Numerator;

                                        DefinicjaDokumentu dokDef = Sesja.CreateObject("CDN.DefinicjeDokumentow").Item("DDf_Symbol='RO'");
                                        rNumerator.DefinicjaDokumentu = dokDef;
                                        knt         = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_kntid=" + knt.ID);
                                        dok.Podmiot = knt;

                                        dok.WalutaSymbol = "PLN";
                                        //  dok.OdbEmail = xmlShipping["Email"].InnerText;
                                        //   dok.Odbiorca =
                                        dok.OdbTelefon      = xmlShipping["phone"].InnerText;
                                        dok.OdbNazwa1       = xmlShipping["company"].InnerText;
                                        dok.OdbNazwa2       = xmlShipping["firstname"].InnerText + " " + xmlShipping["lastname"].InnerText;
                                        dok.OdbAdres.Ulica  = xmlShipping["address1"].InnerText;
                                        dok.OdbAdres.NrDomu = xmlShipping["address2"].InnerText;
                                        //dok.OdbAdres.NrLokalu = xmlShipping["Street3"].InnerText;
                                        dok.OdbAdres.Miasto      = xmlShipping["city"].InnerText;
                                        dok.OdbAdres.KodPocztowy = xmlShipping["postcode"].InnerText;
                                        dok.OdbAdres.Kraj        = "Polska";
                                        //dok.OdbAdres.Wojewodztwo = xmlShipping["Region"].InnerText;
                                        DokAtrybut dostawa = dok.Atrybuty.AddNew();
                                        dostawa.Kod     = "METODADOSTAWY";
                                        dostawa.Wartosc = "TEST";
                                        //DokAtrybut platnosc = dok.Atrybuty.AddNew();
                                        //platnosc.Kod = "METODAPLATNOSCI";
                                        // platnosc.Wartosc = orderXML["Header"]["PaymentMethod"].InnerText;
                                        dok.NumerObcy = orderId;
                                        // dok.MagazynZrodlowyID = int.Parse(orderContent.Key);
                                        ICollection pozycje = dok.Elementy;
                                        foreach (XmlNode orderItem in orderContent.Value)
                                        {
                                            ElementHaMag pozycja = pozycje.AddNew();
                                            pozycja.TowarID = Convert.ToInt32(orderItem["TwrTwrId"].InnerText);
                                            var product_price = orderItem["product_price"].InnerText.Replace(".", ",");
                                            pozycja.CenaT   = Convert.ToDecimal(product_price);
                                            pozycja.IloscJM = Convert.ToDouble(orderItem["product_quantity"].InnerText);
                                        }

                                        error = false;
                                        Sesja.Save();
                                    }
                                    catch (Exception exDokDef)
                                    {
                                        eventLog.WriteEntry("Error 2 on order import:" + Environment.NewLine + exDokDef.ToString(), EventLogEntryType.Error, 0);
                                    }
                                }
                                catch (Exception exPresta)
                                {
                                    error = true;
                                    //  eventLog.WriteEntry("Error on order import: " + orderXML["Header"]["OrderId"].InnerText + Environment.NewLine + "dzieki tej opcji wystapienie bledu importu zamowienia nie powinno zabijac optimy" + exMagento, EventLogEntryType.Error, 0);
                                    Wylogowanie();
                                }
                                Wylogowanie();
                            }
                        }
                        catch (Exception exPresta)
                        {
                            error = true;
                            //   eventLog.WriteEntry("Error 3 on order import: " + orderXML["Header"]["OrderId"].InnerText + Environment.NewLine + Environment.NewLine + exMagento.ToString(), EventLogEntryType.Error, 0);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                //eventLog.WriteEntry("Error 5 on order import:" + Environment.NewLine + ex.ToString(), EventLogEntryType.Error, 0);
            }
        }
Esempio n. 2
0
        private void comarchOptimaImportOrderSTA()
        {
            string akronim;

            try
            {
                string    reference      = "";
                WebClient client         = new WebClient();
                string    prestaResponse = "";

                try
                {
                    NameValueCollection postData = new NameValueCollection()
                    {
                        { "action", "getXmlOrders" }
                    };
                    prestaResponse = Encoding.UTF8.GetString(client.UploadValues(getOrdersGate(), postData));
                }
                catch (Exception exPresta)
                {
                    eventLog.WriteEntry("Error 1 on order import:" + Environment.NewLine + exPresta.ToString(), EventLogEntryType.Error, 0);
                }

                XmlDocument ordersXML = new XmlDocument();
                ordersXML.LoadXml(prestaResponse);
                XmlElement ordersXMLroot = ordersXML.DocumentElement;

                if (ordersXMLroot.ChildNodes.Count > 0)
                {
                    foreach (XmlNode orderXML in ordersXMLroot.ChildNodes)
                    {
                        bool error = true;
                        try
                        {
                            //eventLog.WriteEntry("Rozpoczynam import zamówień:" + Environment.NewLine , EventLogEntryType.Information, 0);

                            Dictionary <string, List <XmlNode> > splitedOrder = new Dictionary <string, List <XmlNode> >();
                            orderId     = orderXML["id"].InnerText;
                            reference   = orderXML["reference"].InnerText;
                            curOrderXML = orderXML;

                            if (CzyZaimportowane(reference)) //Jeśłi zamówienie zaimportowane to przerywam.
                            {
                                continue;
                            }

                            XmlNode tmpNode = orderXML["associations"];

                            LogowanieAutomatyczne(eventLog);
                            AdoSession Sesja = Login.CreateSession();


                            try {
                                XmlNode xmlBilling  = orderXML.SelectSingleNode("address_invoice")["address"];
                                XmlNode xmlShipping = orderXML.SelectSingleNode("address_delivery")["address"];
                                XmlNode xmlCustomer = orderXML.SelectSingleNode("customer");
                                XmlNode xmlCarrier  = orderXML.SelectSingleNode("carrier");
                                try
                                {
                                    XmlNode       xmltmp     = orderXML["address_invoice"]["address"];
                                    DefAtrybut    defAtrybut = Sesja.CreateObject("CDN.DefAtrybuty").Item("DeA_Kod='B2BID'");  //Pobranie id atrybutu
                                    string        KnA_DeAId  = "KnA_DeAId=" + defAtrybut.ID.ToString() + "";
                                    SqlConnection conn       = new SqlConnection();
                                    conn.ConnectionString = connectionString;
                                    conn.Open();
                                    SqlCommand idKnt = new SqlCommand("SELECT    [KnA_PodmiotId]  ,[KnA_WartoscTxt]  FROM [CDN].[KntAtrybuty] where [KnA_DeAId] = '" + defAtrybut.ID.ToString() + "' AND [KnA_WartoscTxt] = '" + xmlBilling["id"].InnerText + "'", conn);


                                    SqlDataReader reader    = idKnt.ExecuteReader();
                                    int           podmiotId = 0;


                                    try {
                                        if (reader.Read())
                                        {
                                            Int32.TryParse(reader["KnA_PodmiotId"].ToString(), out podmiotId);
                                            knt = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_kntid=" + podmiotId);
                                        }
                                        else
                                        {
                                            podmiotId = 0;

                                            try
                                            {
                                                knt = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_Kod='" + ZbudujAkronim(curOrderXML) + "'");
                                            }catch (Exception ex)
                                            {
                                                knt = null;
                                            }

                                            if (knt == null)
                                            {
                                                knt                 = Sesja.CreateObject("CDN.Kontrahenci").AddNew();
                                                knt.Akronim         = ZbudujAkronim(curOrderXML);;
                                                knt.Rodzaj_Odbiorca = 1;
                                                knt.Rodzaj_Dostawca = 0;

                                                PobierzDaneDoFaktury();
                                                KntAtrybut b2bId = knt.Atrybuty.AddNew();
                                                b2bId.DefAtrybut = defAtrybut;
                                                b2bId.ROSaveMode = 1;
                                                b2bId.Wartosc    = xmlBilling["id"].InnerText;
                                                knt.Kategoria    = Sesja.CreateObject("CDN.Kategorie").Item("Kat_KodOgolny='ALLEGRO MAJSTERKOWAN'");
                                                knt.Grupa        = "ALLEGRO MAJSTERKOWAN";
                                                Sesja.Save();
                                                podmiotId = knt.ID;
                                            }
                                        }
                                    }
                                    catch (Exception exPresta)
                                    {
                                        // eventLog.WriteEntry("Błąd przy tworzeniu Kontrahenta: " + orderId + Environment.NewLine + "" + exPresta, EventLogEntryType.Error, 0);

                                        throw;
                                    }



                                    reader.Close();
                                    reader.Dispose();

                                    conn.Close();


                                    DokumentHaMag dok = Sesja.CreateObject("CDN.DokumentyHaMag").AddNew();
                                    dok.Rodzaj       = 308000;
                                    dok.TypDokumentu = 308;


                                    var rNumerator = dok.Numerator;

                                    DefinicjaDokumentu dokDef = Sesja.CreateObject("CDN.DefinicjeDokumentow").Item("DDf_Symbol='RO'");
                                    rNumerator.DefinicjaDokumentu = dokDef;
                                    knt         = Sesja.CreateObject("CDN.Kontrahenci").Item("Knt_kntid=" + knt.ID);
                                    dok.Podmiot = knt;

                                    dok.WalutaSymbol = "PLN";

                                    dok.OdbEmail = xmlCustomer["email"].InnerText;
                                    dok.TypNB    = 2; /* 1 - Licz od netto, 2 -licz od brutto*/



                                    dok.OdbNazwa1            = ZbudujNazwe(xmlShipping["company"].InnerText, xmlShipping["firstname"].InnerText, xmlShipping["lastname"].InnerText).Nazwa1;
                                    dok.OdbNazwa2            = ZbudujNazwe(xmlShipping["company"].InnerText, xmlShipping["firstname"].InnerText, xmlShipping["lastname"].InnerText).Nazwa2;
                                    dok.OdbNazwa3            = ZbudujNazwe(xmlShipping["company"].InnerText, xmlShipping["firstname"].InnerText, xmlShipping["lastname"].InnerText).Nazwa3;
                                    dok.OdbAdres.Ulica       = xmlShipping["address1"].InnerText;
                                    dok.OdbAdres.NrDomu      = xmlShipping["address2"].InnerText;
                                    dok.OdbAdres.NrLokalu    = "";
                                    dok.OdbAdres.Poczta      = "";
                                    dok.OdbTelefon           = xmlShipping["phone"].InnerText ?? xmlShipping["phone_mobile"].InnerText;
                                    dok.OdbAdres.Miasto      = xmlShipping["city"].InnerText;
                                    dok.OdbAdres.KodPocztowy = xmlShipping["postcode"].InnerText;
                                    dok.OdbAdres.Kraj        = "Polska";
                                    //dok.OdbAdres.Wojewodztwo = xmlShipping["Region"].InnerText;
                                    DokAtrybut dostawa = dok.Atrybuty.AddNew();
                                    dostawa.Kod     = "METODADOSTAWY";
                                    dostawa.Wartosc = xmlCarrier["name"].InnerText;
                                    DokAtrybut platnosc = dok.Atrybuty.AddNew();
                                    platnosc.Kod     = "METODAPLATNOSCI";
                                    platnosc.Wartosc = orderXML["payment"].InnerText;

                                    DokAtrybut b2bIdDok = dok.Atrybuty.AddNew();
                                    b2bIdDok.Kod     = "B2BID";
                                    b2bIdDok.Wartosc = orderId;

                                    dok.NumerObcy = reference;
                                    // dok.MagazynZrodlowyID = int.Parse(orderContent.Key);

                                    Kategoria kategoria;
                                    if (orderXML["module"].InnerText == "allegro")
                                    {
                                        knt.Kategoria = Sesja.CreateObject("CDN.Kategorie").Item("Kat_KodOgolny='ALLEGRO MAJSTERKOWAN'");
                                        knt.Grupa     = "ALLEGRO MAJSTERKOWAN";
                                    }
                                    else
                                    {
                                        knt.Kategoria = Sesja.CreateObject("CDN.Kategorie").Item("Kat_KodOgolny='MAJSTERKOWANIE.EU'");
                                        knt.Grupa     = "MAJSTERKOWANIE.EU";
                                    }



                                    CDNBase.ICollection FormyPlatnosci = (CDNBase.ICollection)(Sesja.CreateObject("CDN.FormyPlatnosci", null));
                                    if (orderXML["module"].InnerText == "ps_cashondelivery" || orderXML["payment"].InnerText.IndexOf("collect_on_delivery") != -1)
                                    {
                                        dok.FormaPlatnosci = (OP_KASBOLib.FormaPlatnosci)FormyPlatnosci["FPl_FPlId=14"]; /*Pobranie IE*/
                                    }
                                    else
                                    {
                                        dok.FormaPlatnosci = (OP_KASBOLib.FormaPlatnosci)FormyPlatnosci["FPl_FPlId=13"];  /*Przedpłata IE*/
                                    }

                                    ICollection pozycje = dok.Elementy;

                                    foreach (XmlNode orderXmlElement in tmpNode["order_rows"])
                                    {
                                        ElementHaMag pozycja = pozycje.AddNew();
                                        int          TwrTwrId;
                                        if (Int32.TryParse(orderXmlElement["TwrTwrId"].InnerText, out TwrTwrId))
                                        {
                                            pozycja.TowarID = TwrTwrId;
                                        }
                                        else
                                        {
                                            pozycja.TowarID = Convert.ToInt32(getTwrIdException());
                                        }

                                        var product_price = orderXmlElement["unit_price_tax_incl"].InnerText.Replace(".", ",");
                                        pozycja.CenaT   = Convert.ToDecimal(product_price);
                                        pozycja.IloscJM = Convert.ToDouble(orderXmlElement["product_quantity"].InnerText);
                                    }

                                    ElementHaMag carrier = pozycje.AddNew();
                                    carrier.TowarID = Convert.ToInt32(getTwrIdCarrier());
                                    var total_shipping = orderXML["total_shipping"].InnerText.Replace(".", ",");
                                    carrier.CenaT   = Convert.ToDecimal(total_shipping);
                                    carrier.IloscJM = Convert.ToDouble(1);
                                    carrier.UstawNazweTowaru(xmlCarrier["name"].InnerText);
                                    carrier.WartoscZakupu = Convert.ToDecimal(total_shipping);



                                    error = false;
                                    Sesja.Save();
                                    eventLog.WriteEntry("Pomyślnie pobrano zamówienie nr:" + orderId + Environment.NewLine, EventLogEntryType.Information, 0);

                                    OznaczJakoPobrane(Convert.ToInt32(orderXML["id_optimaexportorders"].InnerText), Convert.ToInt32(orderXML["id"].InnerText));
                                }
                                catch (Exception exDokDef)
                                {
                                    eventLog.WriteEntry("Error 2 on order import: " + orderId + Environment.NewLine + exDokDef.ToString(), EventLogEntryType.Error, 0);
                                }
                            }
                            catch (Exception exPresta)
                            {
                                error = true;
                                eventLog.WriteEntry("Error on order import: " + orderId + Environment.NewLine + "dzieki tej opcji wystapienie bledu importu zamowienia nie powinno zabijac optimy" + exPresta, EventLogEntryType.Error, 0);
                                Wylogowanie();
                            }
                            Wylogowanie();
                        }
                        catch (Exception exPresta)
                        {
                            error = true;
                            eventLog.WriteEntry("Error 3 on order import: " + orderId + Environment.NewLine + Environment.NewLine + exPresta.ToString(), EventLogEntryType.Error, 0);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                eventLog.WriteEntry("Błąd ogólny:" + Environment.NewLine + exception.ToString(), EventLogEntryType.Error, 0);
            }
        }