'AM' AND AAA.Descrizione_Web <> ''"; $sql_create_BBB="CREATE TEMPORARY TABLE BBB (select * from b2b_categoria)"; sc_exec_sql($sql_create_BBB, "conn_mysql_import"); sc_lookup(rs, $sql_categories, "conn_mysql_import"); $thevalues = 'INSERT INTO categories_temp (CategoryID, CategoryName, Description, CategoryUp, Descrizione_Web) VALUES '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; //echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS categories_temp LIKE categories"; $sql_temp_trunc="TRUNCATE TABLE categories_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO categories(CategoryID, CategoryName, Description, CategoryUp, Descrizione_Web) SELECT t.CategoryID, t.CategoryName, t.Description, t.CategoryUp, t.Descrizione_Web FROM categories_temp t ON DUPLICATE KEY UPDATE CategoryID = t.CategoryID , CategoryName = t.CategoryName, Description = t.Description , CategoryUp = t.CategoryUp , Descrizione_Web = t.Descrizione_Web "; sc_exec_sql($sql_update, "conn_mysql"); //return ok (true) or error (false); } function import_customer() { $sql_customers="SELECT CodAnag AS CustomerID, `Ragione sociale` AS CompanyName, NomeContatto AS ContactName, Indirizzo AS Address, Localita AS City, Provincia AS Region, CAP AS PostalCode, Nazione AS Country, Zona, NumeroTelefono AS Phone, NumeroFax AS Fax, Cell AS Mobile, PartitaIVA as VatCode, `Codice Fiscale` AS CodFisc, Email, Codag AS SalesmanID, `Id anag` AS Id_anag FROM b2b_anagrafica WHERE (((Left(CodAnag,1))='C')) ORDER BY CodAnag"; sc_lookup(rs, $sql_customers, "conn_mysql_import"); $thevalues = 'INSERT INTO customers_temp (CustomerID,CompanyName,ContactName,Address,City,Region,PostalCode,Country,Zona,Phone,Fax,Mobile,VatCode, CodFisc, Email,SalesmanID,Id_anag) values '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS customers_temp LIKE customers"; $sql_temp_trunc="TRUNCATE TABLE customers_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO customers(CustomerID,CompanyName,ContactName,Address,City,Region,PostalCode,Country,Zona,Phone,Fax,Mobile,VatCode, CodFisc, Email, User, Password, UserType, SalesmanID, Active, Admist_priviledge, Id_anag) SELECT t.CustomerID , t.CompanyName , t.ContactName , t.Address , t.City , t.Region , t.PostalCode , t.Country , t.Zona , t.Phone , t.Fax , t.Mobile , t.VatCode, t.CodFisc, t.Email , t.CustomerID, '12345' , 'CLI', t.SalesmanID , 'Y', 0, t.Id_anag FROM customers_temp t ON DUPLICATE KEY UPDATE CompanyName = t.CompanyName , ContactName = t.ContactName , Address = t.Address , City = t.City , Region = t.Region , PostalCode = t.PostalCode , Country = t.Country , Zona = t.Zona , Phone = t.Phone , Fax = t.Fax , Mobile = t.Mobile , VatCode = t.VatCode, CodFisc = t.CodFisc, Email = t.Email , UserType = 'CLI' , SalesmanID = t.SalesmanID , Active = 'Y', Admist_priviledge = 0, Id_anag = t.Id_anag"; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_orders() { $sql_order="SELECT b2b_ordini.`N Ord` AS Id_Ord, b2b_ordini.CodAnag AS CustomerID, b2b_ordini.CodAg AS SalesmanID, b2b_ordini.`Data Ord` AS OrderDate, b2b_ordini.Stato AS StatusID, b2b_ordini.B2B_IdOrdine AS OrderID FROM b2b_ordini GROUP BY b2b_ordini.`N Ord`, b2b_ordini.CodAnag, b2b_ordini.CodAg, b2b_ordini.`Data Ord`, b2b_ordini.Stato, b2b_ordini.B2B_IdOrdine, b2b_ordini.`Tipo Ord` HAVING (((b2b_ordini.B2B_IdOrdine) <> 0) AND ((b2b_ordini.`Tipo Ord`)='C9'))"; sc_lookup(rs, $sql_order, "conn_mysql_import"); $thevalues = 'INSERT INTO orders_temp (Id_Ord , CustomerID, SalesmanID, OrderDate, StatusID, OrderID ) values '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS orders_temp LIKE orders"; $sql_temp_trunc="TRUNCATE TABLE orders_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); //Creare routine che identifica le testate ordini esistenti , le trova e aggiunge l'id ord e data_ord di Dinamico. Solo se nuove // altrimenti le sovrascrive solo a quelle già nuove da Dinamico $sql_update="INSERT INTO orders(OrderID , CustomerID, SalesmanID, OrderDate, StatusID, Id_Ord) SELECT t.OrderID ,t.CustomerID , t.SalesmanID , t.OrderDate , t.StatusID , t.Id_Ord FROM orders_temp t ON DUPLICATE KEY UPDATE OrderID = t.OrderID , CustomerID = t.CustomerID , SalesmanID = t.SalesmanID , OrderDate = t.OrderDate , StatusID = t.StatusID , Id_Ord = t.Id_Ord "; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_order_details() { $sql_orders_details="SELECT b2b_ordini_righe.`Ordine` AS Order_row_ID, b2b_ordini_righe.B2B_IdOrdine AS OrderID, b2b_ordini.CodAnag AS CustomerID, b2b_ordini.CodAg AS SalesmanID, b2b_ordini.`Data Ord` AS OrderDate, b2b_ordini_righe.Codart AS ProductID, b2b_ordini_righe.Prezzo AS UnitPrice, 1 AS CurrencyID, b2b_ordini_righe.Quantita AS Quantity FROM b2b_ordini_righe INNER JOIN b2b_ordini ON b2b_ordini_righe.B2B_IdOrdine = b2b_ordini.B2B_IdOrdine"; /* $sql_orders_details="SELECT b2b_ordini_righe.`id sottoOrd` AS Order_row_ID, b2b_ordini_righe.B2B_IdOrdine AS OrderID, b2b_ordini.CodAnag AS CustomerID, b2b_ordini.CodAg AS SalesmanID, b2b_ordini.`Data Ord` AS OrderDate, b2b_ordini_righe.Codart AS ProductID, b2b_ordini_righe.Prezzo AS UnitPrice, 1 AS CurrencyID, b2b_ordini_righe.Quantita AS Quantity FROM b2b_ordini_righe INNER JOIN b2b_ordini ON b2b_ordini_righe.`Id ord` = b2b_ordini.`Id Ord` GROUP BY b2b_ordini_righe.`id sottoOrd`, b2b_ordini_righe.Ordine, b2b_ordini.CodAnag, b2b_ordini.CodAg, b2b_ordini.`Data Ord`, b2b_ordini_righe.Codart, b2b_ordini_righe.Prezzo, 1, b2b_ordini_righe.Quantita, b2b_ordini.B2B_IdOrdine HAVING (((b2b_ordini.B2B_IdOrdine) <> 0))"; */ sc_lookup(rs, $sql_orders_details, "conn_mysql_import"); $thevalues = 'INSERT INTO orders_details_temp (Order_row_ID , OrderID , CustomerID, SalesmanID , OrderDate , ProductID , UnitPrice , CurrencyID , Quantity ) values '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TABLE IF NOT EXISTS orders_details_temp LIKE orders_details"; $sql_temp_trunc="TRUNCATE TABLE orders_details_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); //Creare routine che cerca OrderID su Temp, le cancella con chiave su Order_Details e poi le inserisce leggendo da TEMP // Non posso usare Duplicate Key perchè se mi cancellano righe nel gestionale poi mi restano appese quelle di EosB2B. $sql_insert_rows="INSERT INTO orders_details (Order_row_ID, OrderID , CustomerID, SalesmanID , OrderDate , ProductID , UnitPrice , CurrencyID , Quantity) SELECT (SELECT Order_row_ID FROM orders_details WHERE OrderID = t.OrderID AND CustomerID = t.CustomerID AND SalesmanID = t.SalesmanID AND OrderDate = t.OrderDate AND ProductID = t.ProductID AND UnitPrice = t.UnitPrice AND CurrencyID = t.CurrencyID) AS Order_row_ID, t.OrderID , t.CustomerID, t.SalesmanID , t.OrderDate , t.ProductID , t.UnitPrice , t.CurrencyID , t.Quantity FROM orders_details_temp t ON DUPLICATE KEY UPDATE #Order_row_ID = t.Order_row_ID; OrderID = t.OrderID , CustomerID = t.CustomerID, SalesmanID = t.SalesmanID, OrderDate = t.OrderDate, ProductID = t.ProductID , UnitPrice = t.UnitPrice , CurrencyID = t.CurrencyID, Quantity = t.Quantity "; sc_exec_sql($sql_insert_rows, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_price_list() { $sql_price_list="SELECT CodAnag AS CustomerID, Codart AS ProductID, Prezzo AS UnitPrice, VALUTA AS CurrencyID, `N Ord` AS OrderNr, id_off AS OfferNr, DATE_FORMAT(Data_Off,'%Y-%m-%d') AS OfferDate, DATE_FORMAT (ValidoFinoAl,'%Y-%m-%d') AS DueDate, Anno AS RefYear FROM b2b_listino WHERE (((Codart)<>'-----')) ORDER BY CodAnag, Codart"; sc_lookup(rs, $sql_price_list, "conn_mysql_import"); $thevalues = 'INSERT INTO price_list_temp (CustomerID, ProductID, UnitPrice, CurrencyID, OrderNr, OfferNr, OfferDate, DueDate, RefYear) VALUES '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS price_list_temp LIKE price_list"; $sql_temp_trunc="TRUNCATE TABLE price_list_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO price_list (CustomerID, ProductID, UnitPrice, CurrencyID, OrderNr, OfferNr, OfferDate, DueDate, RefYear) SELECT t.CustomerID, t.ProductID, t.UnitPrice, t.CurrencyID, t.OrderNr, t.OfferNr, t.OfferDate, t.DueDate, t.RefYear FROM price_list_temp t ON DUPLICATE KEY UPDATE CustomerID = t.CustomerID , ProductID = t.ProductID , UnitPrice = t.UnitPrice , CurrencyID = t.CurrencyID , OrderNr = t.OrderNr, OfferNr = t.OfferNr , OfferDate = t.OfferDate , DueDate = t.DueDate , RefYear = t.RefYear "; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_price_list_std() { $sql_price_list="SELECT ProductID, StdPrice, CurrencyID, DATE_FORMAT( DueDate, '%Y-%m-%d' ) , RefYear FROM b2b_listino_Std WHERE ProductID <> '' ORDER BY ProductID"; sc_lookup(rs, $sql_price_list, "conn_mysql_import"); $thevalues = 'INSERT INTO price_list_std_temp (ProductID, StdPrice, CurrencyID, DueDate, RefYear) VALUES '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS price_list_std_temp LIKE price_list_std"; $sql_temp_trunc="TRUNCATE TABLE price_list_std_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO price_list_std(ProductID, StdPrice, CurrencyID, DueDate, RefYear) SELECT t.ProductID, t.StdPrice, t.CurrencyID, t.DueDate, t.RefYear FROM price_list_std_temp t ON DUPLICATE KEY UPDATE ProductID = t.ProductID , StdPrice = t.StdPrice , CurrencyID = t.CurrencyID , DueDate = t.DueDate , RefYear = t.RefYear "; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_products() { $sql_products="SELECT Codart AS ProductID, Descrizione AS ProductName, CodAnag AS SupplierID, Codcat AS CategoryID, If(Left(Codcat,2)='DI','Dirui','Eos') AS CompanyID, CodArt2 AS ProductID_altern, Um, IF( Codiva = '', 22, Codiva ) AS Codiva, Marca, CatStatistica FROM b2b_articoli WHERE (((Codart) Is Not Null)) ORDER BY Codart"; sc_lookup(rs, $sql_products, "conn_mysql_import"); $thevalues = 'INSERT INTO products_temp (ProductID , ProductName , SupplierID , CategoryID , CompanyID , ProductID_altern , Um , Codiva, Marca, CatStatistica ) values '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS products_temp LIKE products"; $sql_temp_trunc="TRUNCATE TABLE products_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO products(ProductID , ProductName , SupplierID , CategoryID , CompanyID , ProductID_altern , Um , Codiva, Marca, CatStatistica ) SELECT t.ProductID , t.ProductName , t.SupplierID , t.CategoryID , t.CompanyID , t.ProductID_altern , t.Um , t.Codiva, t.Marca, t.CatStatistica FROM products_temp t ON DUPLICATE KEY UPDATE ProductName = t.ProductName , SupplierID = t.SupplierID , CategoryID = t.CategoryID , CompanyID = t.CompanyID , ProductID_altern = t.ProductID_altern , Um = t.Um , Codiva = t.Codiva, Marca = t.Marca, CatStatistica = t.CatStatistica"; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_salesmen() { $sql_salesmen="SELECT CodAg AS CustomerID, `Rag Soc` AS CompanyName, Email FROM b2b_agenti"; sc_lookup(rs, $sql_salesmen, "conn_mysql_import"); $thevalues = 'INSERT INTO salesmen_temp (CustomerID,CompanyName,Email) values '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS salesmen_temp LIKE customers"; $sql_temp_trunc="TRUNCATE TABLE salesmen_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO customers(CustomerID,CompanyName,ContactName,Address,City,Region,PostalCode,Country,Zona,Phone,Fax,Mobile,Email, User, Password, UserType, SalesmanID, Active, Admist_priviledge, Id_anag) SELECT t.CustomerID , t.CompanyName , t.ContactName , t.Address , t.City , t.Region , t.PostalCode , t.Country , t.Zona , t.Phone , t.Fax , t.Mobile , t.Email , t.CustomerID, '12345' , 'AGE', t.CustomerID , 'Y', 0, t.Id_anag FROM salesmen_temp t ON DUPLICATE KEY UPDATE CompanyName = t.CompanyName , ContactName = '' , Address = '' , City = '' , Region = '' , PostalCode = '' , Country = '' , Zona = '' , Phone = '' , Fax = '' , Mobile = '' , Email = t.Email , UserType = 'AGE' , SalesmanID = t.CustomerID , Active = 'Y', Admist_priviledge = 0, Id_anag = ''"; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } function import_suppliers() { $sql_suppliers="SELECT CodAnag AS SupplierID, `Ragione sociale` AS CompanyName, NomeContatto AS ContactName, Indirizzo AS Address, Localita AS City, Provincia AS Region, CAP AS PostalCode, Nazione AS Country, NumeroTelefono AS Phone, NumeroFax AS Fax, Cell AS Mobile, Email, `Id anag` AS Id_anag FROM b2b_anagrafica WHERE (((Left(CodAnag,1))='F')) ORDER BY CodAnag"; sc_lookup(rs, $sql_suppliers, "conn_mysql_import"); $thevalues = 'INSERT INTO suppliers_temp (SupplierID, CompanyName, ContactName, Address, City, Region, PostalCode, Country, Phone, Fax, Mobile, Email , Id_anag) VALUES '; foreach($rs as $rec) { $thevalues1 = "("; foreach ($rec as $field) { $thevalues1 = $thevalues1 ."'" . str_replace("'", "\'", $field) . "',"; } $thevalues1 = substr($thevalues1, 0, strlen($thevalues1)-1); $thevalues1 = $thevalues1 . "),"; $thevalues = $thevalues.$thevalues1 ; } $thevalues = substr($thevalues, 0, strlen($thevalues)-1).";"; // echo $thevalues; $sql_temp_table="CREATE TEMPORARY TABLE IF NOT EXISTS suppliers_temp LIKE suppliers"; $sql_temp_trunc="TRUNCATE TABLE suppliers_temp"; sc_exec_sql($sql_temp_table, "conn_mysql"); sc_exec_sql($sql_temp_trunc, "conn_mysql"); sc_exec_sql($thevalues, "conn_mysql"); $sql_update="INSERT INTO suppliers(SupplierID, CompanyName, ContactName, Address, City, Region, PostalCode, Country, Phone, Fax, Mobile, Email, Id_anag) SELECT t.SupplierID , t.CompanyName , t.ContactName , t.Address , t.City , t.Region , t.PostalCode , t.Country , t.Phone , t.Fax , t.Mobile , t.Email , t.Id_anag FROM suppliers_temp t ON DUPLICATE KEY UPDATE CompanyName = t.CompanyName , ContactName = t.ContactName , Address = t.Address , City = t.City , Region = t.Region , PostalCode = t.PostalCode , Country = t.Country , Phone = t.Phone , Fax = t.Fax , Mobile = t.Mobile , Email = t.Email , Id_anag = t.Id_anag"; sc_exec_sql($sql_update, "conn_mysql"); //return true (everythin is ok) or false (an error occured); } } ?>