<?php

Doo::loadCore('db/DooModel');

class verify extends DooModel {

    public $vid;
    public $description;
    public $staff;
    public $subordinate;
    
    public $_table = 'CLD_verify';
    public $_primarykey = 'vid';
    
    public $_fields = array('vid', 'description', 'staff','subordinate');
	
    /**
     * 获得所有审批组
     */
    function getVerifyByAll(){
    	Doo::loadModel ( "role" );
    	$role = new role ();
    	$veList = $this->find ( array (
    			'asArray' => true
    	) );
    	foreach ( $veList as $key => $value ) {
    		$v = json_decode ( $value ['staff'] );
    		$list = array ();
    		foreach ( $v as $n => $m ) {
    			if ($m ['1'] == 'ROLE') {
    				$roleInfo = $role->getOne ( array (
    						'where' => 'rid=' . $m [0],
    						'asArray' => true
    				) );
    				$v [$n] [1] = $roleInfo ['name'];
    				array_push ( $list, $roleInfo ['name'] );
    			} else {
    				array_push ( $list, $v [$n] [1] );
    			}
    		}
    		$veList [$key] ['staff'] = implode ( '>', $list );
    	}
    	return $veList;
    }
    
    
    function getVerifyBySubordinate($subordinate='',$subordinate2=''){
    	if (empty($subordinate)){
    		return array();
    	}
    	$con='subordinate like "%'.$subordinate.'%"';
    	if (!empty($subordinate2)){
    		$con='subordinate like "%'.$subordinate.'%" or subordinate like "%'.$subordinate2.'%"';
    	}
    	$veList = $this->find ( array (
    			'where'=>$con,
    			'asArray' => true
    	) );
    	Doo::loadModel ( "role" );
    	$role = new role ();
    	foreach ( $veList as $key => $value ) {
    		$v = json_decode ( $value ['staff'] );
    		$list = array ();
    		foreach ( $v as $n => $m ) {
    			if ($m ['1'] == 'ROLE') {
    				$roleInfo = $role->getOne ( array (
    						'where' => 'rid=' . $m [0],
    						'asArray' => true
    				) );
    				$v [$n] [1] = $roleInfo ['name'];
    				array_push ( $list, $roleInfo ['name'] );
    			} else {
    				array_push ( $list, $v [$n] [1] );
    			}
    		}
    		$veList [$key] ['staff'] = implode ( '>', $list );
    	}
    	return $veList;
    }
    
    /**
     * 获得某费用单的审批信息
     */
    function getVerifyByRid($rid=0){
    	
    	if (empty($rid))
    		return array();
    	// 获得审批组
    	Doo::loadModel ( 'role' );
    	$role = new role ();
    	Doo::loadModel ( 'loanReceipt' );
    	$loanReceipt = new loanReceipt ();
    	Doo::loadModel ( 'staff' );
    	$staff = new staff ();
    	Doo::loadModel ( 'receipt' );
    	$receipt = new receipt ();
    	
    	$receiptDetail = $receipt->getReceiptByRid ( $rid );
    	
    	$loanList = $loanReceipt->find ( array (
    			'where' => 'rid=' . $receiptDetail ['loanRid'],
    			'desc' => 'rid',
    			'asArray' => true
    	) );
    	if ($receiptDetail ['status'] == 3 || $receiptDetail ['status'] == 1 || $receiptDetail ['status'] == 8 || $receiptDetail ['status'] == 9) {
    		if ($receiptDetail ['status'] == 8 && $receiptDetail ['Rtype'] == 1)
    			$verifyList ['staff'] = $loanList [0] ['verifyBreakup'];
    			else
    				$verifyList ['staff'] = $receiptDetail ['verifyBreakup'];
    	} else {
    		$verifyList = $this->getOne ( array (
    				'where' => 'vid=' . $receiptDetail ['verify'],
    				'asArray' => true
    		) );
    	}
    	$verifyList = json_decode ( $verifyList ['staff'], true );
    	$verifyStaff = json_decode ( $receiptDetail ['verifyStaff'], true );
    	
    	$roleArrayCopy = array ();
    	$flag = true;
    	
    	foreach ( $verifyList as $k => $v ) {
    		if ($v [1] == 'ROLE') {
    			$roleVerify = json_decode ( $v [3], true );
    			$roleKey = $k;
    			$roleArray = array ();
    			$rolename = $role->getOne ( array (
    					'where' => 'rid=' . $v [0],
    					'asArray' => true
    			) );
    			foreach ( $roleVerify as $o => $p ) {
    				$staffID = explode ( '_', $p );
    				$staffInfo = $staff->getOne ( array (
    						'where' => 'sid=' . $staffID [0],
    						'asArray' => true
    				) );
    				$exp = array (
    						0 => $staffInfo ['sid'],
    						1 => $staffID [1],
    						2 => $staffInfo ['avatar'],
    						'date' => '',
    						'date' => '',
    						'opinion' => '',
    						'status' => '',
    						'rolename' => $rolename ['name']
    				);
    				array_push ( $roleArray, $exp );
    			}
    			$roleArrayCopy [$v [0]] = $roleArray;
    		}
    		$verifyList [$k] ['date'] = "";
    		$verifyList [$k] ['opinion'] = "";
    		$verifyList [$k] ['status'] = "";
    		if (empty ( $verifyStaff )) {
    			$verifyList [$k] ['date'] = "";
    			$verifyList [$k] ['opinion'] = "";
    			if (($verifyList [0] [0] == $this->staff [0] ['sid']) && $flag) {
    				$verifyList [$k] ['status'] = 4;
    				$flag = false;
    			}
    		} else {
    			foreach ( $verifyStaff as $m => $u ) {
    				if ($v [0] == $m) {
    					$verifyList [$k] ['date'] = $u ['date'];
    					$verifyList [$k] ['opinion'] = $u ['opinion'];
    					$verifyList [$k] ['status'] = $u ['status'];
    					$flag = false;
    					break;
    				}
    			}
    		}
    	}
    	foreach ( $roleArrayCopy as $P => $A ) {
    		foreach ( $A as $MKI => $AVL ) {
    			foreach ( $verifyStaff as $m => $u ) {
    				$AVLFK = $AVL [0] . '_R';
    				if (strcmp ( $AVLFK, $m ) == 0) {
    					$roleArrayCopy [$P] [$MKI] ['date'] = $u ['date'];
    					$roleArrayCopy [$P] [$MKI] ['opinion'] = $u ['opinion'];
    					$roleArrayCopy [$P] [$MKI] ['status'] = $u ['status'];
    					break;
    				}
    			}
    		}
    	}
    	foreach ( $roleArrayCopy as $k => $v ) {
    		foreach ( $verifyList as $m => $n ) {
    			if ($n [0] == $k) {
    				array_splice ( $verifyList, $m, 1, $v );
    			}
    		}
    	}
    	
    	return $verifyList;
    }
    
}

?>