<?php
Doo::loadCore ( 'db/DooModel' );
/**
 * 费用培训班相关信息及其操作业务逻辑
 * @author CP.
 * @version 1.0
 * @namespace invoice
 * @package invoiceModel
 */
class receiptTraining extends DooModel {
	public $rtid;
	
	public $trainName;
	public $cid;
	public $categoryName;
	public $trainStartDate;
	public $trainEndDate;
	public $participants;
	public $trainAddress;
	public $trainRemarks;
	public $creator;
	public $creatorDate;
	public $identifyingTotal;

	public $_table = 'CLD_receiptTraining';
	public $_primarykey = 'rtid';
	public $_fields = array (
			'rtid',
			
			'trainName',
			'cid',
			'categoryName',
			'trainStartDate',
			'trainEndDate',
			'participants',
			'trainAddress',
			'trainRemarks',
			'creator',
			'creatorDate' ,
            'identifyingTotal'
	);
	
	/**
	 * 添加一个培训班
	 * @param array $item 培训班相关数据
	 * @return number 返回培训班ID
	 */
	public function addReceiptTraining($item = array()) {
		$itid = 0;
		if (is_array ( $item ) && ! empty ( $item )) {
			foreach ( $item as $key => $value ) {
				$this->$key = $value;
			}
			$itid = $this->insert ();
		}
		return $itid;
	}
	function getInvoiceTrainStatistics($itid = 0) {
		$itidCondition = ' and trainId=NULL ';
		if (! empty ( $itid ))
			$itidCondition = ' and trainId in (' . $itid . ') ';
		
		$sql = 'select sum(invoicePrice) as invoicePrice ,trainId
				from CLD_invoice
				where status=2 and printStatus=1 and (untreadStatus =0 or untreadStatus=3) ' . $itidCondition . '
				GROUP BY status';
		$query = Doo::db ()->query ( $sql );
		$result = $query->fetchAll ();
		
		if (! empty ( $result ))
			$detail = array (
					'invoicePrice' => $result [0] ['invoicePrice'] 
			);
		else
			$detail = array (
					'invoicePrice' => 0 
			);
			// print_r($result);
		$itidCondition = ' and a.trainId=NULL ';
		if (! empty ( $itid ))
			$itidCondition = ' and a.trainId in (' . $itid . ') ';
		$sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
				from CLD_invoice as a left join CLD_invoiceReceivables as b on find_in_set(b.irid,a.irid)
				where  a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!="" ' . $itidCondition . '
				GROUP BY a.status';
		$query = Doo::db ()->query ( $sql );
		$result = $query->fetchAll ();
		
		if (! empty ( $result )) {
			$detail += array (
					'receivablesPrice' => $result [0] ['receivablesPrice'] 
			);
			$detail += array (
					'rPrice' => $detail ['invoicePrice'] - $result [0] ['receivablesPrice'] 
			);
		} else {
			$detail += array (
					'receivablesPrice' => 0 
			);
			$detail += array (
					'rPrice' => $detail ['invoicePrice'] 
			);
		}
		return $detail;
	}
	function getInvoiceTrainingByTodo($select = "", $cid = 0, $sid = 0) {
		Doo::loadModel ( 'staff' );
		$staff = new staff ();
		Doo::loadModel ( 'L_category' );
		$lCategory = new L_category ();
		
		$condition = array (
				'where' => "(status= 0 or status=1) and ( cid=" . $cid . " or creator=" . $sid . " )",
				//'limit' => 8,
				'asArray' => TRUE 
		);
		if (! empty ( $select ))
			$condition += array (
					'select' => $select 
			);
		
		$list = $this->find ( $condition );
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		$itid = array ();
		foreach ( $list as $key => $value ) {
			$list [$key] ['trainingKey'] = $XDeode->encode ( $value ['itid'] );
			$detail = $staff->getStaffBySid ( $value ['creator'] );
			$list [$key] ['staff'] = $detail;
			$detail = $lCategory->getCategoryById ( $value ['cid'] );
			$list [$key] ['category'] = $detail;
			array_push ( $itid, $value ['itid'] );
		}
		
		// 获得开票金额合计 入账金额合计 入账完成度
		Doo::loadModel ( 'invoice' );
		$invoice = new invoice ();
		
		$invoiceList = $invoiceRecelvablesList = array ();
		if (! empty ( $itid )) {
			$itidSql = implode ( ',', $itid );
			$invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
			$invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
		}
		// print_r($invoiceRecelvablesList);
		
		foreach ( $list as $key => $value ) {
			
			$countInvoice=$invoice->getInvoiceByTrainingCount($value['itid']);
			$value ['invoiceTotal']=$countInvoice;
			$list [$key] ['invoiceTotal'] =$countInvoice;
			
			foreach ( $invoiceList as $k => $v ) {
				if ($value ['itid'] == $v ['trainId']) {
					$list [$key] ['invoiceTotalAmount'] = $v ['invoicePrice'];
					break;
				}
			}
			foreach ( $invoiceRecelvablesList as $i => $o ) {
				if ($value ['itid'] == $o ['trainId']) {
					$list [$key] ['invoiceArriveAmount'] = $o ['receivablesPrice'];
					$list [$key] ['arriveSchedule'] = "0";
					if ($value ['invoiceTotal'] != 0)
						$list [$key] ['arriveSchedule'] = round ( $o ['count'] / $value ['invoiceTotal'] * 100, 2 );
					
					break;
				}
			}
		}
		
		return $list;
	}
	
	/**
	 * 根据参数字段更新相应字段（主键ID必须传）
	 * @param array $item 相关需要更新的字段信息
	 * @return number 返回发票ID
	 */
	public function setReceiptTrainByCondition($item = array(), $rtid = "") {
		$lid = 0;
		
		if (is_array ( $item ) && ! empty ( $item )) {
			foreach ( $item as $key => $value ) {
				$this->$key = $value;
			}
			
			if (! empty ( $isid )) {
				$condition = array (
						'where' => "rtid in (" . $rtid . ")",
						'asArray' => TRUE 
				);
				$this->update ( $condition );
			} else
				$lid = $this->update ();
		}
		return $lid;
	}
	
	/**
	 * 根据状态获取培训班
	 * @param number $status
	 */
	function getInvoiceTrainingByStatus($status = 0, $select = "", $cid = 0, $sid = 0) {
		Doo::loadModel ( 'staff' );
		$staff = new staff ();
		Doo::loadModel ( 'L_category' );
		$lCategory = new L_category ();
		
		$condition = array (
				'where' => "status= '" . $status . "' and  (cid=" . $cid . " or creator=" . $sid . " )",
				'limit' => 8,
				'asArray' => TRUE 
		);
		if (! empty ( $select ))
			$condition += array (
					'select' => $select 
			);
		
		$list = $this->find ( $condition );
		
		foreach ( $list as $key => $value ) {
			$detail = $staff->getStaffBySid ( $value ['creator'] );
			$list [$key] ['staff'] = $detail;
			$detail = $lCategory->getCategoryById ( $value ['cid'] );
			$list [$key] ['category'] = $detail;
		}
		
		return $list;
	}
	
	/**
	 * 根据ID获得培训班
	 * @param number $itid
	 */
	function getReceiptTrainingByRtid($rtid = 0) {
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		if (!is_numeric($rtid))
			$rtid = $XDeode->decode ( $rtid );
		
		$detail = array ();
		if (! empty ( $rtid ) && is_numeric ( $rtid )) {
			$detail = $this->getOne ( array (
					'where' => " rtid=" . $rtid,
					'asArray' => TRUE 
			) );
		}
		$detail ['rtidKey'] = '';
		if (! empty ( $detail ))
			$detail ['rtidKey'] = $XDeode->encode ( $detail ['rtid'] );
		
		return $detail;
	}
	
	/**
	 * 根据ID删除数据
	 * @param number $itid
	 */
	function delInvoiceTrainingByItid($itid = 0) {
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		$itid = $XDeode->decode ( $itid );
		if (! empty ( $itid ) && is_numeric ( $itid ))
			$this->delete ( array (
					'where' => 'itid=' . $itid 
			) );
			// 删除培训班下的发票
		Doo::loadModel ( 'invoiceStore' );
		$invoiceStore = new invoiceStore ();
		
		$invoiceStore->delete ( array (
				'where' => 'trainId=' . $itid 
		) );
	}
	
	/**
	 * 获得某办事处未完成的培训班
	 * @return string
	 */
	function getInvoiceTrainingByUnfinished($cid=0){
		$condition = array (
				'where' => "(status= 0 or status=1) and cid=".$cid,
				'asArray' => true 
		);
		
		$list = $this->find ( $condition );
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ( $list as $key => $value ) {
			$list[$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
		}
		
		return $list;
	}
	
	/**
	 * 获得所有培训班
	 */
	function getInvoiceTrainingBySubmitStatus() {
		$condition = array (
				'where' => "submitStatus=1",
				'asArray' => TRUE 
		);
		
		$list = $this->find ( $condition );
		
		Doo::loadClass ( 'XDeode' );
		$XDeode = new XDeode ( 5 );
		foreach ( $list as $key => $value ) {
			$list[$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
		}
		
		return $list;
	}
}

?>