<?php
#!/usr/bin/php -q

/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	
	require('includes/sms_application_top.php');
	require(DIR_WS_CLASSES . 'currencies.php');

	$currencies=new currencies();

require(DIR_WS_CLASSES . 'fulfiller.php');
require(DIR_WS_CLASSES . 'xml_parser.php');

$parser=new xml_parser_class;
$parser->store_positions=0;
$parser->case_folding=0;
$parser->target_encoding="ISO-8859-1";
$parser->simplified_xml=0;
$parser->fail_on_non_simplified_xml=0;

$fulfiller= new fulfiller('oneworld');

$error="";
$result=$fulfiller->process_out_of_stock();
if ($result["error"]==""){
	if (count($result["content"])<=0) $error="Error: No products found to update";
} else {
	$error=$result["error"];
}
if($error=="")
{
	while(list($key,$value)=each ($result["content"]))
	{
		if ($key=="") continue;
		if($value<=0) //out of stock
		{
			$sql="update " . TABLE_PRODUCTS . " set products_status=0 where products_sku='" . tep_db_input($key) . "'";
			tep_db_query($sql);
			$sql="update " . TABLE_PRODUCTS_STOCK . " set attribute_status=0 where sku='" . tep_db_input($key) . "'";
			tep_db_query($sql);
		}
		else
		{
			$sql="update " . TABLE_PRODUCTS . " set products_status=1 where products_sku='" . tep_db_input($key) . "'";
			tep_db_query($sql);
			$sql="update " . TABLE_PRODUCTS_STOCK . " set attribute_status=1 where sku='" . tep_db_input($key) . "'";
			tep_db_query($sql);
		}
	}
	echo "Update Successfully";
}
else{
	$error_array = array(
						'cron_jobs_error_action' => basename($HTTP_SERVER_VARS['PHP_SELF']),
						'cron_jobs_error_text' => $error,
						'cron_jobs_error_date' => 'now()'
						);
	tep_db_perform(TABLE_CRON_JOBS_ERROR,$error_array);
	echo "<font color='red'>" . $error . '</font>';
}
	
require('includes/application_bottom.php');
?>