<?php
Doo::loadCore ( 'db/DooModel' );
class RItem extends DooModel {
	public $riid;
	public $rid;
	public $identifying;
	public $item;
	public $price;
	
	public $date;
	public $inputer;
	public $describe;
	public $payType;
	
	public $creater;
	public $createrId;
	public $creatDate;
	
	public $_table = 'CLD_RItem';
	public $_primarykey = 'riid';
	public $_fields = array (
			'riid',
			'rid',
			'identifying',
			'item',
			'price',
			
			'date',
			'inputer',
			'describe',
			'payType',
			'creater',
			'createrId',
			'creatDate',
			
	);

    /**
     * 一组培训班结算支出项金额总和
     * @param string $rids
     */
	function getRItemPriceByRids($rids=''){
        if (empty($rids)){
            return 0;
        }
        $sum = $this->getOne ( array (
            'select' => 'sum(price) as price',
            'where' => " rid in (" .$rids.")",
            //'groupby' => 'cid,Month(date)',
            'asArray' => TRUE
        ) );
        return $sum['price'];
    }

	function getRItemByRiid($riid){
		$detail=$this->getOne(array (
				'where' => "riid=".$riid,
				'asArray' => TRUE
		));
	
		return $detail;
	}
	
	function addRItem($itemData,$receipt,$receiptDetail,$rid,$item,$price,$payType){
		
		if (is_array ( $itemData ) && ! empty ( $itemData )) {
			foreach ( $itemData as $key => $value ) {
				$this->$key = $value;
			}
			$lid = $this->insert ();
		}
		
		
		
		// 添加公司汇总
		// 报销详情
		$rInfo = $receipt->getReceiptByRid ( $rid );
		$rdInfo = $receiptDetail->getReceiptDetailByRIC ( $rid, $item, '培训班费用' );
		if (empty ( $rdInfo )) {
			$receiptDetail = new receiptDetail ();
			$receiptDetail->staff = $rInfo ['staff'];
			$receiptDetail->item = $item;
			$receiptDetail->itemCategory = '培训班费用';
			$receiptDetail->price =  $price;
			$receiptDetail->date = date ( "Y-m-d" );
			$receiptDetail->cid = $rInfo ['cid'];
			$receiptDetail->rid = $rid;
			$receiptDetail->status = 4;
			$receiptDetail->insert ();
		} else {
			$receiptDetail = new receiptDetail ();
			$rdInfo ['price'] +=  $price;
			$receiptDetail->price = $rdInfo ['price'];
			$receiptDetail->update ( array (
					'where' => 'rid=' . $rid . ' and item like "' . $item . '" and itemCategory like "培训班费用"'
			) );
		}
		//更新费用合计金额
		$receipt = new receipt ();
		$receipt->sum=$rInfo['sum']+ $price;
		$receipt->update ( array (
				'where' => 'rid=' . $rid
		) );
			
			
		//汇总金额--提交审批之后有 支出项时 需要更新汇总
		if($payType=='company'){
			Doo::loadModel ( "statistics" );
			$statistics = new statistics ();
		
		
			$dateArray = explode ( "-", $rInfo ['date'] );
			$dateCondition = " and Year(date) =" . $dateArray [0] . " and Month(date) = " . $dateArray [1];
		
			$stat = $statistics->getOne ( array (
					'where' => 'staff=' . $rInfo ['staff'] . $dateCondition,
					'asArray' => true
			) );
			if (!empty($stat)){
				//$statistics->agPrice = $stat ['agPrice'] + $price;
				$statistics->rePrice = $stat ['rePrice'] + $price;
					
				$statistics->update ( array (
						'where' => 'sid=' . $stat ['sid']
				) );
			}
		}
		
		
		
	}
	
	/**
	 * 根据rid获得支出项详情
	 * @param number $rid
	 */
	function getRItemByRid($rid=0,$name=''){
		
		$con='';
		if(!empty($name)){
			$con=' ORDER BY case WHEN item like "%'.$name.'%" then 1 end desc';
		}else{
			$con=' ORDER BY creatDate  desc';
		}
		
		$sql = 'select (@rowNO := @rowNo+1) AS  i, a.* from ' . $this->_table . '  a  , (select @rowNO :=0) b where rid='.$rid.$con;
		
		$query = Doo::db ()->query ( $sql );
		$list = $query->fetchAll ();
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ($list as $key=>$value){
			
			$list[$key]['riidKey']=$XDeode->encode($value['riid']);
			
// 			if($value['item']=='税款'){
// 				$list[$key]['price']=$value['price']*0.07;
// 			}
		}
		
		return $list;
	}
	
}

?>