QuickBooks/Callbacks/SQL/Callbacks.php
public static function InventoryLevelsResponse($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents, $callback_config = array())
	{
		$Driver = QuickBooks_Driver_Singleton::getInstance();
		
		$col_defs = array();
		
		//mysql_query("INSERT INTO quickbooks_log ( msg, log_datetime ) VALUES ( 'TESTING', NOW() ) ") or die(mysql_error());
		
		// First, find the column definitions
		$tmp = $xml;
		$find = 'ColDesc';
		while ($inner = QuickBooks_Callbacks_SQL_Callbacks::_reportNextXML($tmp, $find))
		{
			$colID = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColID($inner);
			$type = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColType($inner);
			
			$col_defs[$colID] = $type;
		}
		
		//print_r($col_defs);
		//exit;
		
		$items = array();
		
		// Now, find the actual data
		$tmp = $xml;
		$find = 'DataRow';
		while ($inner = QuickBooks_Callbacks_SQL_Callbacks::_reportNextXML($tmp, $find))
		{
			$item = array(
				'FullName' => null, 
				'Blank' => null, 					// 
				'ItemDesc' => null, 				// 
				'ItemVendor' => null, 				// Pref Vendor
				'ReorderPoint' => null, 			// Reorder Pt
				'QuantityOnHand' => null, 			// On Hand
				'SuggestedReorder' => null, 		// Order
				'QuantityOnOrder' => null, 			// On PO
				'QuantityOnSalesOrder' => null, 	// On Sales Order 
				'EarliestReceiptDate' => null, 		// Next Deliv
				'SalesPerWeek' => null, 			// Sales/Week
				);
			
			$find2 = 'RowData';
			if ($tag = QuickBooks_Callbacks_SQL_Callbacks::_reportNextTag($inner, $find2))
			{
				$value = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColValue($tag);
				
				$item['FullName'] = $value;
			}
				
			$find3 = 'ColData';
			while ($tag = QuickBooks_Callbacks_SQL_Callbacks::_reportNextTag($inner, $find3))
			{
				$colID = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColID($tag);
				$value = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColValue($tag);
				
				if (array_key_exists($colID, $col_defs))
				{
					$item[$col_defs[$colID]] = $value;
				}
			}
			
			//$items[] = $item;
			
			/*
			Inventory for "another inventory": Array
			(
			    [FullName] => another inventory
			    [Blank] => another inventory
			    [ItemDesc] => 
			    [ItemVendor] => 
			    [ReorderPoint] => 5
			    [QuantityOnHand] => 35
			    [SuggestedReorder] => false
			    [QuantityOnOrder] => 0
			    [EarliestReceiptDate] => 
			    [SalesPerWeek] => 0
			)
			*/
			
			$Driver->log('Inventory for "' . $item['FullName'] . '": ' . print_r($item, true), null, QUICKBOOKS_LOG_DEBUG);
			//$errnum = null;
			//$errmsg = null;
			//mysql_query("INSERT INTO quickbooks_log VALUES ( msg, log_datetime) VALUES ( '" . mysql_real_escape_string(print_r($item, true)) . "', NOW() ) ");
			// UPDATE item SET QuantityOnHand = x WHERE FullName = y, resync = NOW() AND qbsql_resync_datetime = qbsql_modify_timestamp
			// if (!affected_rows)
			// 	UPDATE item SET QuantityOnHand = x WHERE FullName = y 		// this was a modified item, so it needs to stay modified
			
			$sql1 = "
				UPDATE 
					" . QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . "iteminventory 
				SET 
					QuantityOnHand = " . (float) $item['QuantityOnHand'] . ", 
					QuantityOnOrder = " . (float) $item['QuantityOnOrder'] . ", 
					QuantityOnSalesOrder = " . (float) $item['QuantityOnSalesOrder'] . ", 
					qbsql_resync_datetime = '%s', 
					qbsql_modify_timestamp = '%s'
				WHERE
					FullName = '%s' AND 
					qbsql_resync_datetime = qbsql_modify_timestamp ";
			
			$datetime = date('Y-m-d H:i:s');
			
			$vars1 = array( $datetime, $datetime, $item['FullName'] );
			
			$errnum = null;
			$errmsg = null;
			$Driver->query($sql1, $errnum, $errmsg, 0, 1, $vars1); 
			
			//$Driver->log($sql1, null, QUICKBOOKS_LOG_DEBUG);
			
			if (!$Driver->affected())
			{
				$sql2 = "
					UPDATE 
						" . QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . "iteminventory 
					SET 
						QuantityOnHand = " . (float) $item['QuantityOnHand'] . ", 
						QuantityOnOrder = " . (float) $item['QuantityOnOrder'] . ", 
						QuantityOnSalesOrder = " . (float) $item['QuantityOnSalesOrder'] . "
					WHERE
						FullName = '%s' ";
				
				$vars2 = array( $item['FullName'] );
				
				$errnum = null;
				$errmsg = null;
				$Driver->query($sql2, $errnum, $errmsg, 0, 1, $vars2); 
				
				//$Driver->log($sql2, null, QUICKBOOKS_LOG_DEBUG);
			}
			
			$hooks = array();
			if (isset($callback_config['hooks']))
			{
				$hooks = $callback_config['hooks'];
			}
			
			$Driver->log('CALLING THE HOOKS! ' . print_r($hooks, true), null, QUICKBOOKS_LOG_VERBOSE);
			
			// Call any hooks that occur when a record is updated 	
			$hook_data = array(
				'hook' => QuickBooks_SQL::HOOK_SQL_INVENTORY,
				'user' => $user,
				'table' => QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . 'iteminventory',
				'data' => $item, 
				);
								
			$err = null;
			QuickBooks_Callbacks_SQL_Callbacks::_callHooks($hooks, QuickBooks_SQL::HOOK_SQL_INVENTORY, $requestID, $user, $err, $hook_data, $callback_config);
		}
		
		//print_r($items);
		
		//$Driver->log('Inventory: ' . print_r($items, true), null, QUICKBOOKS_LOG_VERBOSE);
	}
public static function InventoryLevelsResponse($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents, $callback_config = array())
	{
		$Driver = QuickBooks_Driver_Singleton::getInstance();
		
		$col_defs = array();
		
		//mysql_query("INSERT INTO quickbooks_log ( msg, log_datetime ) VALUES ( 'TESTING', NOW() ) ") or die(mysql_error());
		
		// First, find the column definitions
		$tmp = $xml;
		$find = 'ColDesc';
		while ($inner = QuickBooks_Callbacks_SQL_Callbacks::_reportNextXML($tmp, $find))
		{
			$colID = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColID($inner);
			$type = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColType($inner);
			
			$col_defs[$colID] = $type;
		}
		
		//print_r($col_defs);
		//exit;
		
		$items = array();
		
		// Now, find the actual data
		$tmp = $xml;
		$find = 'DataRow';
		while ($inner = QuickBooks_Callbacks_SQL_Callbacks::_reportNextXML($tmp, $find))
		{
			$item = array(
				'FullName' => null, 
				'Blank' => null, 					// 
				'ItemDesc' => null, 				// 
				'ItemVendor' => null, 				// Pref Vendor
				'ReorderPoint' => null, 			// Reorder Pt
				'QuantityOnHand' => null, 			// On Hand
				'SuggestedReorder' => null, 		// Order
				'QuantityOnOrder' => null, 			// On PO
				'QuantityOnSalesOrder' => null, 	// On Sales Order 
				'EarliestReceiptDate' => null, 		// Next Deliv
				'SalesPerWeek' => null, 			// Sales/Week
				);
			
			$find2 = 'RowData';
			if ($tag = QuickBooks_Callbacks_SQL_Callbacks::_reportNextTag($inner, $find2))
			{
				$value = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColValue($tag);
				
				$item['FullName'] = $value;
			}
				
			$find3 = 'ColData';
			while ($tag = QuickBooks_Callbacks_SQL_Callbacks::_reportNextTag($inner, $find3))
			{
				$colID = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColID($tag);
				$value = QuickBooks_Callbacks_SQL_Callbacks::_reportExtractColValue($tag);
				
				if (array_key_exists($colID, $col_defs))
				{
					$item[$col_defs[$colID]] = $value;
				}
			}
			
			//$items[] = $item;
			
			/*
			Inventory for "another inventory": Array
			(
			    [FullName] => another inventory
			    [Blank] => another inventory
			    [ItemDesc] => 
			    [ItemVendor] => 
			    [ReorderPoint] => 5
			    [QuantityOnHand] => 35
			    [SuggestedReorder] => false
			    [QuantityOnOrder] => 0
			    [EarliestReceiptDate] => 
			    [SalesPerWeek] => 0
			)
			*/
			
			$Driver->log('Inventory for "' . $item['FullName'] . '": ' . print_r($item, true), null, QUICKBOOKS_LOG_DEBUG);
			//$errnum = null;
			//$errmsg = null;
			//mysql_query("INSERT INTO quickbooks_log VALUES ( msg, log_datetime) VALUES ( '" . mysql_real_escape_string(print_r($item, true)) . "', NOW() ) ");
			// UPDATE item SET QuantityOnHand = x WHERE FullName = y, resync = NOW() AND qbsql_resync_datetime = qbsql_modify_timestamp
			// if (!affected_rows)
			// 	UPDATE item SET QuantityOnHand = x WHERE FullName = y 		// this was a modified item, so it needs to stay modified
			
			$sql1 = "
				UPDATE 
					" . QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . "iteminventory 
				SET 
					QuantityOnHand = " . (float) $item['QuantityOnHand'] . ", 
					QuantityOnOrder = " . (float) $item['QuantityOnOrder'] . ", 
					QuantityOnSalesOrder = " . (float) $item['QuantityOnSalesOrder'] . ", 
					qbsql_resync_datetime = '%s', 
					qbsql_modify_timestamp = '%s'
				WHERE
					FullName = '%s' AND 
					qbsql_resync_datetime = qbsql_modify_timestamp ";
			
			$datetime = date('Y-m-d H:i:s');
			
			$vars1 = array( $datetime, $datetime, $item['FullName'] );
			
			$errnum = null;
			$errmsg = null;
			$Driver->query($sql1, $errnum, $errmsg, 0, 1, $vars1); 
			
			//$Driver->log($sql1, null, QUICKBOOKS_LOG_DEBUG);
			
			if (!$Driver->affected())
			{
				$sql2 = "
					UPDATE 
						" . QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . "iteminventory 
					SET 
						QuantityOnHand = " . (float) $item['QuantityOnHand'] . ", 
						QuantityOnOrder = " . (float) $item['QuantityOnOrder'] . ", 
						QuantityOnSalesOrder = " . (float) $item['QuantityOnSalesOrder'] . "
					WHERE
						FullName = '%s' ";
				
				$vars2 = array( $item['FullName'] );
				
				$errnum = null;
				$errmsg = null;
				$Driver->query($sql2, $errnum, $errmsg, 0, 1, $vars2); 
				
				//$Driver->log($sql2, null, QUICKBOOKS_LOG_DEBUG);
			}
			
			$hooks = array();
			if (isset($callback_config['hooks']))
			{
				$hooks = $callback_config['hooks'];
			}
			
			$Driver->log('CALLING THE HOOKS! ' . print_r($hooks, true), null, QUICKBOOKS_LOG_VERBOSE);
			
			// Call any hooks that occur when a record is updated 	
			$hook_data = array(
				'hook' => QuickBooks_SQL::HOOK_SQL_INVENTORY,
				'user' => $user,
				'table' => QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . 'iteminventory',
				'data' => $item, 
				);
								
			$err = null;
			QuickBooks_Callbacks_SQL_Callbacks::_callHooks($hooks, QuickBooks_SQL::HOOK_SQL_INVENTORY, $requestID, $user, $err, $hook_data, $callback_config);
		}
		
		//print_r($items);
		
		//$Driver->log('Inventory: ' . print_r($items, true), null, QUICKBOOKS_LOG_VERBOSE);
	}
Handle an inventory stock status report from QuickBooks