<?php namespace Pay\Yspay;
use Dever;

class Cash
{
	# 入账：待确认
    public function add($order_id, $order, $info)
    {
    	$mid = $info['mid'];
    	$merchant = Dever::db('pay/yspay_merchant')->one(array('mid' => $mid));
    	if ($merchant) {
    		$yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
    		if ($yspay) {
    			$cash_per = 0;
    			if ($yspay['type'] == 2) {
    				$cash_per = $merchant['cash_per'];
		    		if (!$cash_per) {
		    			if ($yspay && $yspay['cash_per']) {
		    				$cash_per = $yspay['cash_per'];
		    			}
		    		}
		    		if (!$cash_per || $cash_per <= 0) {
		    			$cash_per = 0;
		    		} else {
		    			$cash_per = $cash_per/100;
		    		}
    			}
    			
	    		$per = $yspay['per']/100;
	    		
				$data['merchant_id'] = $merchant['id'];
	    		$data['order_num'] = $info['merOrderId'];
		        $data['source_order_num'] = $order_id;
		        $data['ycash'] = $info['amount'];
		        $data['cash'] = round($info['amount'] - $info['amount'] * $per, 2);
		        if ($yspay['cash_type'] == 1) {
		        	$fz_cash = $data['ycash'];
		        } else {
		        	$fz_cash = $data['cash'];
		        }
		        $data['fz_cash'] = round($fz_cash*$cash_per, 2);
		        $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 2);
		        $data['status'] = 1;
		        $id = Dever::db('pay/yspay_cash')->insert($data);
		        return $id;
    		}
    	}
    	return false;
    }

    # 修改状态
    public function up($id, $status = 2)
    {
    	if (is_numeric($id)) {
    		$where['id'] = $id;
    	} else {
    		$where['source_order_num'] = $id;
    	}
    	
    	$info = Dever::db('pay/yspay_cash')->find($where);
    	if ($info) {
    		$update = array('status' => $status, 'where_id' => $info['id']);
    		if ($status == 3) {
    			# 开始划付
    			$state = $this->huafu_act($info);
    			if ($state != 'ok') {
    				return false;
    			}
    			$update['rdate'] = time();
    		}
    		if ($status == 4) {
    			# 开始提现
    			$state = $this->tixian_act($info);
    			if ($state != 'ok') {
    				return false;
    			}
    			$update['tdate'] = time();
    		}
    		$state = Dever::db('pay/yspay_cash')->update($update);
    		if ($state) {
    			if ($status == 3) {
    				$total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 3, 'merchant_id' => $info['merchant_id']));
    				if ($total) {
    					Dever::db('pay/yspay_merchant')->update(array('where_id' => $info['merchant_id'], 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
    				}
    			}
    			return $state;
    		}
    	}
    	return false;
    }

    private function huafu_act($info)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    	if ($merchant) {
    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
    		if ($config) {
    			return Dever::load('pay/yspay/multi')->act($config, $merchant, $info);
    		}
    	}

    	return false;
    }

    private function tixian_act($info)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    	if ($merchant) {
    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
    		if ($config) {
    			return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
    		}
    	}

    	return false;
    }

    # 定时获取总额

    # 测试数据
    public function test_add_api()
    {
    	$info['mid'] = 'test';
    	$info['merOrderId'] = 'test123';
    	$info['amount'] = 10000;

    	$this->add('test123', array(), $info);
    	return 'ok';
    }

    # 将测试数据改成待入账
    public function test_edit_api()
    {
    	$data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
    	if ($data) {
    		foreach ($data as $k => $v) {
    			$this->up($v['id'], 2);
    		}
    	} 
    	return 'ok';
    }

    # 资金划付
    public function huafu_commit_api()
    {
    	$data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 2));
    	if ($data) {
    		foreach ($data as $k => $v) {
    			$this->up($v['id'], 3);
    		}
    	}
    	return 'ok';
    }
}