Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '1');
  4.  
  5. $Server = "XXXXXX";
  6. $User = "XXXXXX";
  7. $Pass = "XXXXXX";
  8. $DB = "XXXXXX";
  9.  
  10. //connection to the database
  11. $dbhandle = mssql_connect($Server, $User, $Pass)
  12. or die("Couldn't connect to SQL Server on $Server. Error: " . mssql_get_last_message());
  13.  
  14. //select a database to work with
  15. $selected = mssql_select_db($DB, $dbhandle)
  16. or die("Couldn't connect to SQL Server on $Server. Error: " . mssql_get_last_message());
  17.  
  18. //declare the SQL statement that will query the database
  19. $query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand n";
  20. $query .= "from Products I n";
  21. $query .= "left join ProductStockLocations St on (St.ProductID = I.ID) n";
  22. $query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) n";
  23. $query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) n";
  24. $query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) n";
  25. $query .= "where I.ID > 0 n";
  26. $query .= "and St.StockLocationID = 1 n";
  27. $query .= "and L.POSLocationID = 1 n";
  28. $query .= "and L.SoldAtLocation = 1 n";
  29.  
  30.  
  31. // execute the SQL query and return a result set
  32. // mssql_query() actually returns a resource
  33. // that you must iterate with (e.g.) mssql_fetch_array()
  34. $mssqlResult = mssql_query($query);
  35.  
  36. if($mssqlResult === FALSE) {
  37. die(mysql_error()); // TODO: better error handling
  38. }
  39.  
  40. // connect to the MySQL database
  41. mysql_connect("XXXXXX", "XXXXXX", "XXXXXX") or die(mysql_error());
  42. mysql_select_db("XXXXXX") or die(mysql_error());
  43.  
  44.  
  45. while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
  46. $mssqlCode = $mssqlRow['Code'];
  47. $mssqlOnHand = $mssqlRow['OnHand'];
  48. mysql_query(
  49. "UPDATE product_option_relation SET stock = $mssqlOnHand WHERE sku = '$mssqlCode'"
  50. // extra quotes may be required around $mssqlCode depending on the column type
  51. );
  52. mysql_query(
  53. "UPDATE product SET quantity = $mssqlOnHand WHERE sku = '$mssqlCode' AND has_option = '0' ");
  54. }
  55.  
  56. //close the connection
  57. mssql_close($dbhandle);
  58.  
  59. // Update product total with the total quantities of the product options
  60. $result = mysql_query("UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty")
  61. or die(mysql_error());
  62. ?>
  63.  
  64. <?php
  65. error_reporting(E_ALL);
  66. ini_set('display_errors', '1');
  67.  
  68. $Server = "XXXXXX";
  69. $User = "XXXXXX";
  70. $Pass = "XXXXXX";
  71. $DB = "XXXXXX";
  72.  
  73. //Connection to the database
  74. $connectionInfo = array( "Database"=>$DB, "UID"=>$User, "PWD"=>$Pass);
  75. @$conn = sqlsrv_connect( $Server, $connectionInfo);
  76.  
  77. if( $conn === false) {
  78. echo "Connection could not be established.<br />";
  79. die( print_r( sqlsrv_errors(), true));
  80. }
  81.  
  82.  
  83. //declare the SQL statement that will query the database
  84. $query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand n";
  85. $query .= "from Products I n";
  86. $query .= "left join ProductStockLocations St on (St.ProductID = I.ID) n";
  87. $query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) n";
  88. $query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) n";
  89. $query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) n";
  90. $query .= "where I.ID > 0 n";
  91. $query .= "and St.StockLocationID = 1 n";
  92. $query .= "and L.POSLocationID = 1 n";
  93. $query .= "and L.SoldAtLocation = 1 n";
  94.  
  95.  
  96. // execute the SQL query and return a result set
  97. // sqlsrv_query() actually returns a resource
  98. // that you must iterate with (e.g.) sqlsrv_fetch_array()
  99. @$mssqlResult = sqlsrv_query( $conn, $sql);
  100.  
  101.  
  102. if( $mssqlResult === false) {
  103. echo "No result resource after MSSQL query.<br />";
  104. die( print_r( sqlsrv_errors(), true) );
  105. }
  106.  
  107.  
  108. // connect to the MySQL database
  109. @$db = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "XXXXXX");
  110.  
  111.  
  112. if (mysqli_connect_error()) {
  113. echo "Error: Unable to connect to MySQL.<br />";
  114. exit;
  115. }
  116.  
  117.  
  118. while( $mssqlRow = sqlsrv_fetch_array( $mssqlResult, SQLSRV_FETCH_ASSOC) ) {
  119.  
  120. $mssqlCode = $mssqlRow['Code'];
  121. $mssqlOnHand = $mssqlRow['OnHand'];
  122.  
  123. $mysqlQuery = "UPDATE product_option_relation SET stock = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."'";
  124.  
  125. mysqli_query($db, $mysqlQuery);
  126.  
  127. $mysqlQuery = "UPDATE product SET quantity = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."' AND has_option = '0' ";
  128.  
  129. mysqli_query($db, $mysqlQuery);
  130. }
  131.  
  132. //close the MSSRV connections
  133. sqlsrv_free_stmt($mssqlResult);
  134. sqlsrv_close($conn);
  135.  
  136.  
  137. // Update product total with the total quantities of the product options
  138. $mysqlQuery = "UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty";
  139. @$result = mysqli_query($db, $mysqlQuery);
  140. if(!$result){
  141. echo "No result resource after MySQL query.<br />";
  142. }
  143.  
  144. //Close MySQL connection
  145. //mysqli_free_result($result);
  146. mysqli_close($db);
  147. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement