<?php
class flow_fund_transferClassModel extends flowModel
{
	
    
	
  public function flowrsreplace($rs)
    {
        $uid=$this->adminid;
        $sql="account in ( select a.name from oa_c_fund_account as a where uid like concat({$uid},',%') or uid like concat('%,',{$uid}) or uid like concat('%,',{$uid},',%') )";
        $fund_detail = m('c_fund_detail')->getall($sql,'account');
        foreach ($fund_detail as $key => $value){
            $allaccount[]=$value['account'];
        }
         if($rs['business_type']){
                $data=$this->db->getone('oa_option',"num='".$rs['business_type']."' and pid=1350");
                $rs['business_type']=$data['name'];
            }
             if($rs['fund_rule']){
                $data=$this->db->getone('oa_option',"num='".$rs['fund_rule']."' and pid=1362");
                $rs['fund_rule']=$data['name'];
            }
       foreach ($rs as $k => &$v) {
                switch ($k) {
                    case 'opt_type':
                        if($v == 1) {
                            $v = '收入';
                          
                        } else if($v == 2) {
                            $v = '支出';
                        } else {
                            $v = '';
                        }
                        continue;
                        break;
                     case 'account':
                    if(!in_array($v,$allaccount)){
                        $rs['account_balance']='***';
                    }else{
                        $rs['account_balance']=$rs['account_balance'];
                    }
                    break;
                }
               
            }
            return $rs;
        }



	//提交时调用
	   protected function flowcheckfinsh($zt){
         
	// 转出账户扣钱

	// 转入账户加钱

	// 生成两条记录
       
		$name=$this->rs['account'];//转出账户

		$data=m('c_fund_account')->getone("`name`='$name'");
            //转出账户扣钱
			$zfarr = array(
			'account_balance'        =>$data['account_balance']-$this->rs['trans_amount'],
			);
			m('c_fund_account')->update($zfarr,"`name`='$name'");
            //转入账户加钱
            $toaccount=$this->rs['toaccount'];//转入账户
            $toaccount_list=m('c_fund_account')->getone("`name`='$toaccount'");
            $accountarr = array(
			'account_balance'        =>$toaccount_list['account_balance']+$this->rs['trans_amount'],
			);
			m('c_fund_account')->update($accountarr,"`name`='$toaccount'");

            $id=$this->rs['id'];
            $account_balance=$data['account_balance']-$this->rs['trans_amount'];//当前余额
            $detail = array(
            'account_balance'        =>$account_balance,
             'opt_type'        =>2,
            );
            m('c_fund_detail')->update($detail,"`id`='$id'");

           //记录账户明细

             $acc_detail = array(
                    'account'        => $this->rs['account'],
                    'opt_type'        =>2,
                    'amount'        => $this->rs['trans_amount'],
                    'account_balance'        =>$data['account_balance']-$this->rs['trans_amount'],
                    'trans_numb'        => $this->rs['trans_numb'],
                    'create_time'  =>$this->rs['create_time'],
                    'uid'  =>$this->rs['uid'],
            );
                m('c_account_detail')->insert($acc_detail);

                 $toacc_detail = array(
                    'account'        => $this->rs['toaccount'],
                    'opt_type'        =>1,
                    'amount'        => $this->rs['trans_amount'],
                    'account_balance'        =>$toaccount_list['account_balance']+$this->rs['trans_amount'],
                    'trans_numb'        => $this->rs['trans_numb'],
                    'create_time'  =>$this->rs['create_time'],
                    'uid'  =>$this->rs['uid'],
            );
                m('c_account_detail')->insert($toacc_detail);

           

                
	}
	 
}

