<?php

//global $Debug; $Debug = true;

class OrderManagerView extends Slot {

	var $ModuleID = 3;
	var $SectionID = 11;
	var $orderby = array(
		'Date' => array(
			'sortColumn' => 'o.date',
			'sortType' => 'desc',
		),
		'Status' => array(
			'sortColumn' => 'o.status',
			'sortType' => 'desc',
		),
	);
	var $sort_array = array();
	var $sortby = '';

	function __construct($pquery) {
		for($i = 1; $i < 13; $i++) $this->month[date2pg($i)] = date2pg($i);
		for($i = '2009'; $i < date('Y',time())+1; $i++) $this->year[$i] = $i;
		for($i = 1; $i < 32; $i++) $this->day[date2pg($i)] = date2pg($i);
		parent::__construct($pquery);
	}

	function init() {
		switch(@$this->LocalTarget[0]) {
			case 'Search':
				$this->getGuides();
				$request = array('pageRecordsQty'=>'25','curPageNumber'=> !empty($this->LocalTarget[5])? $this->LocalTarget[5]+1 : 1);
				if(!empty($this->Request)) {
					if(is_Date($this->Request['form']['begin']['year'],$this->Request['form']['begin']['month'],$this->Request['form']['begin']['day']) && validate($this->getRequestFormField('check')))
						$begin_date = $this->Request['form']['begin']['year'].'-'.$this->Request['form']['begin']['month'].'-'.$this->Request['form']['begin']['day'];
					else $begin_date = '2009-05-20';
					if(is_Date($this->Request['form']['end']['year'],$this->Request['form']['end']['month'],$this->Request['form']['end']['day']) && validate($this->getRequestFormField('check')))
						$end_date = $this->Request['form']['end']['year'].'-'.$this->Request['form']['end']['month'].'-'.$this->Request['form']['end']['day'];
					else $end_date = date("Y-m-d");
					header("Location: ".$this->sHome.'/OrderRegistar/OrderManager/Search/'.$this->getRequestFormField('customer').'/'.$this->getRequestFormField('product').'/'.$begin_date.'/'.$end_date.$this->SID);
				}
				else if(isset($this->LocalTarget[1]) || isset($this->LocalTarget[2]) || isset($this->LocalTarget[3]) || isset($this->LocalTarget[4])) {
					if(!empty($this->LocalTarget[1])) $search['customer'] = $this->LocalTarget[1];
					if(!empty($this->LocalTarget[2])) $search['product'] = $this->LocalTarget[2];
					if(!empty($this->LocalTarget[3])) $search['begin_date'] = $this->LocalTarget[3];
					if(!empty($this->LocalTarget[4])) $search['end_date'] = $this->LocalTarget[4];
					$request = array('conditions' => $search,'pageRecordsQty'=>'20','curPageNumber'=>(isset($this->LocalTarget[5])? $this->LocalTarget[5]+1 : 1));
					$this->sortby = !empty($this->LocalTarget[6]) && in_array($this->LocalTarget[6],array_keys($this->orderby))? $this->LocalTarget[6] : 'Date';
					$request['sortColumn'] = $this->orderby[$this->sortby]['sortColumn'];
					$request['sortType'] = $this->orderby[$this->sortby]['sortType'];
					$this->Action->processEvent($this->SectionID,'select_orders',$request,$this);
					$this->getPageControl($this->sHome.'/OrderRegistar/OrderManager/Search/'.$this->LocalTarget[1].'/'.$this->LocalTarget[2].'/'.$this->LocalTarget[3].'/'.$this->LocalTarget[4],$this->sortby);
					//if(empty($this->PreparedTemplates['curPage'])) $this->Scheme = 'empty';
				}
				else header("Location: ".$this->sHome.'/OrderRegistar/OrderManager'.$this->SID);
			break;
			case 'View':
				if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1]))) {
					$this->Scheme = 'view';
					$this->Action->processEvent($this->SectionID,'open',array('id' => $this->LocalTarget[1]),$this);
					//printArray($this->PreparedTemplates);
					if(empty($this->PreparedTemplates['customer']['id'])) header("Location: ".$this->sHome.'/OrderRegistar/OrderManager'.$this->SID);
				}
				else header("Location: ".$this->sHome.'/OrderRegistar/OrderManager'.$this->SID);
			break;
			default:
				if(!empty($this->Request)) {
					//printArray($this->Request);
					if($this->getRequestField('step')=='edit') {
						if(validate($this->getRequestFormField('id')) && $this->getRequestFormField('status')=='1') {
							$this->Action->processEvent($this->SectionID,'edit',array('id' => (int)$this->getRequestFormField('id')),$this)
							and header("Location: ".$this->sHome.'/OrderRegistar/OrderManager/View/'.$this->getRequestFormField('id').$this->SID);
						}
						else header("Location: ".$this->sHome.'/OrderRegistar/OrderManager'.$this->SID);
					}
					else header("Location: ".$this->sHome.'/OrderRegistar/OrderManager'.$this->SID);
				}
				$this->getGuides();
				$request = array('pageRecordsQty'=>'20','curPageNumber'=>(isset($this->LocalTarget[0])? $this->LocalTarget[0]+1 : 1));
				$this->sortby = !empty($this->LocalTarget[1]) && in_array($this->LocalTarget[1],array_keys($this->orderby))? $this->LocalTarget[1] : 'Date';
				$request['sortColumn'] = $this->orderby[$this->sortby]['sortColumn'];
				$request['sortType'] = $this->orderby[$this->sortby]['sortType'];
				$this->Action->processEvent($this->SectionID,'select_orders',$request,$this);
				$this->getPageControl($this->sHome.'/OrderRegistar/OrderManager',$this->sortby);
				//printArray($this->PreparedTemplates);
			break;
		}
		//printArray($this->Request);
		$this->setSchemeTemplate($this->Scheme);
	}

	function getGuides() {
		$this->product = mergeArrays(array('0' => ' All Products'),$this->Db->SelectGuide(array('parentitem g','product p','orderlineitem o'),array('p.id','p.name'),array('g.id' => 'p.ownerid','o.product' => 'p.id')));
		$this->customer = mergeArrays(array('0' => ' All Customers'),$this->Db->SelectGuide(array('lorder g','customer c'),array('c.id','(c.first_name || \' \' || c.last_name) as name'),array('g.customer' => 'c.id')));
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'view': $this->fillView(); break;
			case 'default': $this->fillList(); break;
		}
	}

	function fillView() {
		global $REGISTRY;
//printArray($this->PreparedTemplates);
		$array = $this->PreparedTemplates;
		$this->PreparedTemplates['slip_total'] = 0;
		foreach($this->PreparedTemplates['orderlineitems'] as $key => $value) {
			$this->PreparedTemplates['slip_total'] += $value['qty'];
			//$this->PreparedTemplates['orderlineitems'][$key]['price'] = number_format($value['price'],2);
			$this->PreparedTemplates['orderlineitems'][$key]['price'] = empty($this->PreparedTemplates['promocode']['id'])?number_format($value['price']*$value['qty'],2):	number_format($value['price']*$value['qty']- ($value['price']*$value['qty']*$this->PreparedTemplates['promocode']['percentage']/100),2);
			
			$this->PreparedTemplates['orderlineitems'][$key]['product'] = array();
			$this->PreparedTemplates['orderlineitems'][$key]['product'][0] = $value['product'];
			$this->PreparedTemplates['orderlineitems'][$key]['product'][0]['generation'] = $value['product']['generation']['name'];
			$this->PreparedTemplates['orderlineitems'][$key]['product'][0]['price'] = number_format($value['price'],2);
			$this->PreparedTemplates['orderlineitems'][$key]['num'] = $key+1;
		}
		//---Promo
		 if (!empty($this->PreparedTemplates['promocode']['id'])) {
		 	$this->PreparedTemplates['Promo_yousave'][] = array ("discount" => number_format(($array['product_price']*$this->PreparedTemplates['promocode']['percentage']/100),2),
		 														 "percentage" => $this->PreparedTemplates['promocode']['percentage'],
		 														 );
		 }
		//---Promo END -------
		
		//$this->PreparedTemplates['product_price'] = number_format($array['product_price'],2);
		$this->PreparedTemplates['product_price'] = empty($this->PreparedTemplates['promocode']['id'])? number_format($array['product_price'],2): number_format($array['product_price']-($array['product_price']*$this->PreparedTemplates['promocode']['percentage']/100),2);
		$ship_price = $this->PreparedTemplates['ship_price'];
//		$this->PreparedTemplates['shipping_price'] = $this->PreparedTemplates['_shippingtype']=='2'? 'Attention! This is order with International Shipping Address.<br>You shoul calculate shipping cost and contact customer.' : '$ '.number_format($ship_price,2);
		$this->PreparedTemplates['shipping_price'] =  '$ '.number_format($ship_price,2);
		$this->PreparedTemplates['tax_price'] = number_format($this->PreparedTemplates['tax_price'],2);
		//$this->PreparedTemplates['total_price'] = number_format($this->PreparedTemplates['total_price'],2);
		$this->PreparedTemplates['total_price'] = number_format(($this->PreparedTemplates['total_price']-    ($array['product_price']*$this->PreparedTemplates['promocode']['percentage']/100)   ),2);
		$customer = $this->PreparedTemplates['customer'];
		$this->PreparedTemplates['customer'] = array();
		$this->PreparedTemplates['customer'][0] = $customer;
		$this->PreparedTemplates['customer'][0]['bill_state'] = $this->PreparedTemplates['customer'][0]['bill_state']['shortname'];
		$this->PreparedTemplates['customer'][0]['bill_country'] = $this->PreparedTemplates['customer'][0]['bill_country']['name'];
		$this->PreparedTemplates['customer'][0]['state'] = $this->PreparedTemplates['customer'][0]['state']['shortname'];
		$this->PreparedTemplates['customer'][0]['country'] = $this->PreparedTemplates['customer'][0]['country']['name'];
		$shipcosttype = $this->PreparedTemplates['shipcosttype'];
		$this->PreparedTemplates['shipcosttype'] = array();
		$this->PreparedTemplates['shipcosttype'][0] = $shipcosttype;
//		if($this->PreparedTemplates['_shippingtype']=='2') $this->PreparedTemplates['intertext'] = array(array());
//		   $this->PreparedTemplates['shippingtype'] = $this->PreparedTemplates['_shippingtype']=='2'? 'International' : '';
		$this->PreparedTemplates['status'] = $this->PreparedTemplates['status']=='t'? 'Paid' : inputSelect('form[status]',array('0' => 'Placed','1' => 'Paid'),0);
		$tok = explode(".", $this->PreparedTemplates['date']);
		$this->PreparedTemplates['date'] = $tok[0];
		$this->PreparedTemplates['customer'][0]['card_display'] = $this->PreparedTemplates['status']=='Paid'? 'none' : 'inline';
		$this->PreparedTemplates['card_display'] = $this->PreparedTemplates['status']=='Paid'? 'none' : 'inline';
		$this->TempFillEls = $this->PreparedTemplates;
	}

	function fillList() {
		if(equals(@$this->LocalTarget[0],'Search'))
			$this->sort_array = array(
				'date' => $this->sortby=='Date'? '<span class=first> Date </span>' : makeLink($this->sHome.'/OrderRegistar/OrderManager/Search/'.$this->LocalTarget[1].'/'.$this->LocalTarget[2].'/'.$this->LocalTarget[3].'/'.$this->LocalTarget[4].'/'.(!empty($this->LocalTarget[5])? $this->LocalTarget[5] : 0).'/Date','Date'),
				'status' => $this->sortby=='Status'? '<span class=first> Order Status </span>' : makeLink($this->sHome.'/OrderRegistar/OrderManager/Search/'.$this->LocalTarget[1].'/'.$this->LocalTarget[2].'/'.$this->LocalTarget[3].'/'.$this->LocalTarget[4].'/'.(!empty($this->LocalTarget[5])? $this->LocalTarget[5] : 0).'/Status','Order Status'),
				//'price' => $this->sortby=='Price'? '<span class=first> Total Price </span>' : makeLink($this->sHome.'/OrderAdmin/OrderManager/Search/'.$this->LocalTarget[1].'/'.$this->LocalTarget[2].'/'.$this->LocalTarget[3].'/'.$this->LocalTarget[4].'/'.(!empty($this->LocalTarget[5])? $this->LocalTarget[5] : 0).'/Price','Total Price'),
			);
		else $this->sort_array = array(
			'date' => $this->sortby=='Date'? '<span class=first> Date </span>' : makeLink($this->sHome.'/OrderRegistar/OrderManager/'.(validate(intval(@$this->LocalTarget[0]))? $this->LocalTarget[0] : 0).'/Date','Date'),
			'status' => $this->sortby=='Status'? '<span class=first> Order Status </span>' : makeLink($this->sHome.'/OrderRegistar/OrderManager/'.(validate(intval(@$this->LocalTarget[0]))? $this->LocalTarget[0] : 0).'/Status','Order Status'),
			//'price' => $this->sortby=='Price'? '<span class=first> Total Price </span>' : makeLink($this->sHome.'/OrderAdmin/OrderManager/'.(validate(intval(@$this->LocalTarget[0]))? $this->LocalTarget[0] : 0).'/Price','Total Price'),
		);
		$this->TempFillEls['sort'][] = $this->sort_array;
		foreach($this->PreparedTemplates['curPage'] as $key => $value) {
			$tok = explode(".", $value['date']);
			$this->TempFillEls['orders'][$key] = array(
				'id' => $value['id'],
				'customer' => $value['first_name'].' '.$value['last_name'],
				'date' => $tok[0],
				'price' => number_format($value['total_price'],2),
				'status' => $value['status']=='t'? 'Paid' : 'Placed',
			);
		}
		empty($this->PreparedTemplates['curPage']) and $this->TempFillEls['navbar'] = 'Sorry, there aren\'t records according to your query';
		!empty($this->PreparedTemplates['curPage']) and $this->TempFillEls['orders_header'][] = array();
		$this->TempFillEls['check'] = inputCheckbox('form[check]',((!empty($this->LocalTarget[3]) && $this->LocalTarget[3]!='2004-02-01') || (!empty($this->LocalTarget[4]) && $this->LocalTarget[4]!=date("Y-m-d"))? 1 : 0),'',1);
		$this->TempFillEls['begin'][0] = array(
			'month' => inputSelect('form[begin][month]',$this->month,(!empty($this->LocalTarget[3])? substr($this->LocalTarget[3],5,2) : 0)),
			'day' => inputSelect('form[begin][day]',$this->day,(!empty($this->LocalTarget[3])? substr($this->LocalTarget[3],8,2) : 0)),
			'year' => inputSelect('form[begin][year]',$this->year,(!empty($this->LocalTarget[3])? substr($this->LocalTarget[3],0,4) : 0)),
		);
		$this->TempFillEls['end'][0] = array(
			'month' => inputSelect('form[end][month]',$this->month,(!empty($this->LocalTarget[4])? substr($this->LocalTarget[4],5,2) : 0)),
			'day' => inputSelect('form[end][day]',$this->day,(!empty($this->LocalTarget[4])? substr($this->LocalTarget[4],8,2) : 0)),
			'year' => inputSelect('form[end][year]',$this->year,(!empty($this->LocalTarget[4])? substr($this->LocalTarget[4],0,4) : 0)),
		);
		$this->TempFillEls['product'] = inputSelect('form[product]',$this->product,(!empty($this->LocalTarget[2])? $this->LocalTarget[2] : 0));
		$this->TempFillEls['customer'] = inputSelect('form[customer]',$this->customer,(!empty($this->LocalTarget[1])? $this->LocalTarget[1] : 0));
	}
}
?>