<?php
/**
 * simulate sales for ABC Co. and log to database
 *
 * Please note this script relies on U.S. Census data 
 * downloaded from http://www.census.gov/geo/www/gazetteer/places2k.html.  
 * This data is used to distribute sales by state in proportion with 
 * the population in each state.  Complete details on loading this 
 * census data and using JpGraph and Smarty to present the data 
 * is included in the PHP Graphics book.
 *
 * @author	Jason E. Sweat
 * @since	2002-12-15
 */
error_reporting(E_ALL);
set_time_limit(0);

require_once 'phpa_db.inc.php';
require_once 'adodb/adodb.inc.php' ;

define('START_TIME',mktime(0,0,0,1,1,2002));
define('END_TIME',mktime(0,0,0,12,15,2002));
define('TIME_STEP', 86400);
define('INS_SQL','INSERT INTO abc_sales (`date`, `channel_id`, `item_id`, `state`, `qty`, `rev`) VALUES (?, ?, ?, ?, ?, ?)');
define('BASE_NUM_SALES',30);
define('SALES_RAND_FACTOR',20);

$sql = <<<EOS
SELECT id, unit_price
FROM abc_catalog
EOS;

$rs = $conn->Execute($sql);
if ($rs && !$rs->EOF) {
	$itemPrice = $rs->GetAssoc();
}

$itemStdQty = array(0,100,200,50,10,5);
$itemStdRnd = array(0,10,20,5,2,5);
$itemStdInc = array(0,5,5,5,5,1);

foreach ($itemPrice as $key => $value) {
	$itemInfo[$key]['price'] = (float)$value;
	$itemInfo[$key]['qty'] = $itemStdQty[$key];
	$itemInfo[$key]['rnd'] = $itemStdRnd[$key];
	$itemInfo[$key]['inc'] = $itemStdInc[$key];
}

$sql = <<<EOS
SELECT s.state_abbr, round( sum( d.population ) / 27958.3437 ) AS pct
FROM `census_data` d, abc_state_region s
WHERE s.state_abbr = d.state
GROUP BY s.state_abbr
EOS;

$conn->debug=true;

$stateData = array();
$sum = 0;
$rs = $conn->Execute($sql);
if ($rs && !$rs->EOF) {
	while ($row = $rs->fetchRow()) {
		$sum += $row['pct'];
		$add['value'] = $sum;
		$add['state'] = $row['state_abbr'];
		$stateData[] = $add;
	}
}

//Commented by JVN 2004.02.14
/*print '<pre>';var_dump($stateData);
print "\n\n\$stateData=unseriealize('".serialize($stateData)."');\n\n";
exit;*/


for ($i=START_TIME; $i<END_TIME; $i+=TIME_STEP) {
	$orders = BASE_NUM_SALES + rand(1,SALES_RAND_FACTOR) - rand(1,SALES_RAND_FACTOR);
	print "working on ". strftime('%D',$i) . " $orders orders<br>";
	flush();
	for ($k=0; $k<$orders; $k++) {
		$sale = new sale($i);
	}
}

print "Done!";

class sale {
	var $_state;
	var $_channel;
	var $_item;
	var $_qty;
	var $_rev;
	
	function sale($dt) {
		global $itemInfo;
		
		//set channel
		$r = rand(1,100);
		if ($r < 20) {
			$this->_channel = 2; //phone
		} elseif ($r < 40 + strftime('%m',$dt)*2 ) {
			$this->_channel = 1; //web eating into retail
		} else {
			$this->_channel = 3; //retail
		}
		
		$this->_item = rand(1,5);
		$this->_qty = $itemInfo[$this->_item]['qty'] 
					+ $itemInfo[$this->_item]['inc'] * rand(1, $itemInfo[$this->_item]['rnd'])
					- $itemInfo[$this->_item]['inc'] * rand(1, $itemInfo[$this->_item]['rnd']);
		if ($this->_qty < 1) {
			$this->_qty = 1;
		}
		
		$this->_rev = $this->_qty * $itemInfo[$this->_item]['price'];
		
		$this->_pick_state();
		
		$this->_log_sale($dt);
	}
	
	function _pick_state() {
		global $stateData;
		
		$pick = rand (1,10000);
		$last = $stateData[0]['state'];
		for ($i=0,$j=count($stateData); $i<$j; $i++) {
			$last = $stateData[$i]['state'];
			if ($pick <= $stateData[$i]['value']) {
				break;
				//$i=$j;
			}
		}
		$this->_state = $last;
	}
	
	function _log_sale($dt) {
		global $conn;

		$conn->Execute(INS_SQL, array(strftime('%Y-%m-%d',$dt), $this->_channel, $this->_item, $this->_state, $this->_qty, $this->_rev));
	}
}

?>