<?php

// register global/PHP functions to be used with your template files
// You can move this to common.conf.php $config['TEMPLATE_GLOBAL_TAGS'] = array('isset', 'empty');
// Every public static methods in TemplateTag class (or tag classes from modules) are available in templates without the need to define in TEMPLATE_GLOBAL_TAGS
Doo::conf ()->TEMPLATE_GLOBAL_TAGS = array (
		'isTimeTR',
		'isInvoiceNew',
		'upper',
		'tofloat',
		'sample_with_args',
		'debug',
		'url',
		'url2',
		'function_deny',
		'isset',
		'empty',
		'make_date',
		'inarray',
		'getGlobals',
		'isInvoiceMoldShow'
);



function isTimeTR($day = 3,$cTime) {
	$flag=false;
	$thenTime=time();
	$futureTime=strtotime('+'.$day.' day',strtotime($cTime));
	if($thenTime<$futureTime)
		$flag=true;
	
	return $flag;
}
function isInvoiceNew($type = "GLOBAL") {
	Doo::loadModel ( 'invoice' );
	$invoice = new invoice ();
	Doo::loadModel ( 'invoiceReceivables' );
	$invoiceReceivables = new invoiceReceivables ();
	Doo::loadModel ( 'staff' );
	$staff = new staff ();
	
	$st = $staff->getUserByIdList ( $_COOKIE ["staff"] );
	
	
	// 我的发票待处理
	$pendingInvoice = $invoice->getOne ( array (
			'where' => "(status=3 or status=4 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )  )  and isDelete=0 and sid=" . $st[0]['sid'],
			'desc' => 'iid',
			'asArray' => TRUE 
	) );
	// 我的收款待入账
	$myClaim = $invoiceReceivables->getOne ( array (
			'where' => " receivablesStaff like '" . $st[0]['sid'] . "-%' and receivablesCategory like '" . $st [0] ['cid'] . ":%' and bindStatus=0",
			'desc' => 'irid',
			'asArray' => TRUE 
	) );
	// 发票审批
	$approval = $invoice->getOne ( array (
			'where' => 'status=1 and pendingApprovals=' . $st[0]['sid'],
			'desc' => 'iid',
			'asArray' => true 
	) );
	// 发票邮寄
	$post = $invoice->getOne ( array (
			'where' => "postStatus=0 and doPost=1 and status=2",
			'asArray' => TRUE 
	) );
	// 发票打印
	$print = $invoice->getOne ( array (
			'where' => "status=2 and printStatus=0 and moldManage like '%[\"" . $st[0]['sid'] . "\",%'",
			'desc' => 'iid',
			'asArray' => TRUE 
	) );
	// 发票退票
	$untread = $invoice->getOne ( array (
			'where' => " printStatus=1 and untreadStatus=1 and status=2 ",
			'asArray' => TRUE 
	) );
	
	$new = array (
			'GLOBAL' => FALSE,
			'pendingInvoice' => FALSE,
			'myClaim' => FALSE,
			'approval' => FALSE,
			'post' => FALSE,
			'print' => FALSE,
			'untread' => FALSE 
	);
	
	if (! empty ( $pendingInvoice ) || ! empty ( $myClaim ) || ! empty ( $approval ) || ! empty ( $post ) || ! empty ( $print ) || ! empty ( $untread ))
		$new ['GLOBAL'] = TRUE;
	if (! empty ( $pendingInvoice ))
		$new ['pendingInvoice'] = TRUE;
	if (! empty ( $myClaim ))
		$new ['myClaim'] = TRUE;
	if (! empty ( $approval ))
		$new ['approval'] = TRUE;
	if (! empty ( $post ))
		$new ['post'] = TRUE;
	if (! empty ( $print ))
		$new ['print'] = TRUE;
	if (! empty ( $untread ))
		$new ['untread'] = TRUE;
	return $new [$type];
}

/**
 * 判断发票的审批权限
 * @param number $sid 用户ID
 * @param string $mold 项目类型
 * @return boolean
 */
function isInvoiceMoldShow($sid = 0, $mold = '') {
	if (! empty ( $sid ) && ! empty ( $mold )) {
		Doo::loadModel ( "invoiceManage" );
		$invoiceManage = new invoiceManage ();
		$imList = $invoiceManage->getInvoiceByMold ( $mold );
		$list = array ();
		foreach ( $imList ['staffList'] as $k => $v ) {
			array_push ( $list, $v [0] );
		}
		$fileInvoice = file_get_contents ( "protected/config/invoice/invoice.ini" );
		$fileInvoice = json_decode ( $fileInvoice, true );
		$delIM = array ();
		if (isset ( $fileInvoice [$mold] ))
			$delIM = explode ( ',', $fileInvoice [$mold] );
		foreach ( $delIM as $key => $value ) {
			if (! empty ( $value ))
				array_push ( $list, $value );
		}
		$list = array_unique ( $list );
		return inarray ( $sid, $list );
	} else
		return false;
}
function getGlobals($var) {
	if ($var == 'NEW')
		getReceiptCount ();
	elseif ($var == 'NEW2')
		getExeCount ();
		// echo $GLOBALS[$var].'/'.$var.'<br/>';
	return $GLOBALS [$var];
}
function inarray($v1, $v2) {
	return in_array ( $v1, $v2 );
}
function make_date() {
	return date ( "Y-m" );
}
function upper($str) {
	return strtoupper ( $str );
}
function tofloat($str) {
	return sprintf ( "%.2f", $str );
}
function sample_with_args($str, $prefix) {
	return $str . ' with args: ' . $prefix;
}
function debug($var) {
	if (! empty ( $var )) {
		echo '<pre>';
		print_r ( $var );
		echo '</pre>';
	}
}

// This will be called when a function NOT Registered is used in IF or ElseIF statment
function function_deny($var = null) {
	echo '<span style="color:#ff0000;">Function denied in IF or ElseIF statement!</span>';
	exit ();
}

// Build URL based on route id
function url($id, $param = null, $addRootUrl = false) {
	Doo::loadHelper ( 'DooUrlBuilder' );
	// param pass in as string with format
	// 'param1=>this_is_my_value, param2=>something_here'
	
	if ($param != null) {
		$param = explode ( ', ', $param );
		$param2 = null;
		foreach ( $param as $p ) {
			$splited = explode ( '=>', $p );
			$param2 [$splited [0]] = $splited [1];
		}
		return DooUrlBuilder::url ( $id, $param2, $addRootUrl );
	}
	
	return DooUrlBuilder::url ( $id, null, $addRootUrl );
}

// Build URL based on controller and method name
function url2($controller, $method, $param = null, $addRootUrl = false) {
	Doo::loadHelper ( 'DooUrlBuilder' );
	// param pass in as string with format
	// 'param1=>this_is_my_value, param2=>something_here'
	
	if ($param != null) {
		$param = explode ( ', ', $param );
		$param2 = null;
		foreach ( $param as $p ) {
			$splited = explode ( '=>', $p );
			$param2 [$splited [0]] = $splited [1];
		}
		return DooUrlBuilder::url2 ( $controller, $method, $param2, $addRootUrl );
	}
	
	return DooUrlBuilder::url2 ( $controller, $method, null, $addRootUrl );
}
/**
 * 获得正在审批的费用单数量
 */
function getReceiptCount() {
	$status = 2;
	$year = date ( 'Y' );
	
	Doo::loadModel ( 'receipt' );
	$receipt = new receipt ();
	Doo::loadClass ( 'XDeode' );
	$XDeode = new XDeode ( 5 );
	$st[0]['sid'] = $XDeode->decode ( $_COOKIE["staff"] );
	
	$dateCondition = " and Year(date) =" . $year;
	$approvalCondition = ' and (nowStaff like "%,' . $st[0]['sid'] . '%" or nowStaff like "%' . $st[0]['sid'] . ',%" or nowStaff=' . $st[0]['sid'] . ' ) ';
	$receiptList = $receipt->find ( array (
			'where' => ' status=' . $status . $dateCondition . $approvalCondition,
			'desc' => 'rid',
			'asArray' => true 
	) );
	$GLOBALS ['NEW'] = count ( $receiptList );
}
/**
 * 获得执行费用的数量
 */
function getExeCount() {
	Doo::loadModel ( 'receipt' );
	$receipt = new receipt ();
	Doo::loadClass ( 'XDeode' );
	$XDeode = new XDeode ( 5 );
	$st[0]['sid'] = $XDeode->decode ( $_COOKIE["staff"] );
	$receiptList = $receipt->find ( array (
			'where' => '(executeCopy like \'%["' . $st[0]['sid'] . '%\' and executeStaff NOT LIKE \'%' . $st[0]['sid'] . '%\' ) and (status=1 or status=6)',
			'desc' => 'rid',
			'asArray' => true 
	) );
	
	$GLOBALS ['NEW2'] = count ( $receiptList );
}
?>