<?php namespace Pay\Yspay;
use Dever;

class Cash
{
	# 百分制
	private $num = 100;
	# 备付金
	private $bfj = 100;
	# 入账:待确认
    public function add($merchant_id, $amount, $order_num, $source_order_num, $fenzhang = 0)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
    	if ($merchant) {
    		$data = array();
    		$data['account_id'] = $merchant['account_id'];
			$data['merchant_id'] = $merchant['id'];
    		$data['order_num'] = $order_num;
	        $data['source_order_num'] = $source_order_num;
	        $info = Dever::db('pay/yspay_cash')->find($data);
	        if ($amount && $amount > 0) {
	        	$this->getCash($amount, $merchant, $data, $fenzhang);
	        }
	        
	        $data['status'] = 1;
	        if ($info) {
	        	$data['where_id'] = $id = $info['id'];
	        	Dever::db('pay/yspay_cash')->update($data);
	        } else {
	        	$id = Dever::db('pay/yspay_cash')->insert($data);
	        }
	        
	        return $id;
    	}
    	return false;
    }

    # 获取账户信息
    public function getInfo($merchant_id)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
	    $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'];
	    $yue = $yue/$this->num;
	    $beifujin = 0;
	    if ($yue >= $this->bfj) {
	        $beifujin = $this->bfj;
	    }
	    $tixian = $yue - $beifujin;
	    $info = '当前账户余额:¥' . Dever::number($yue, 2) . '&nbsp;&nbsp;&nbsp;&nbsp;备付金:¥'.Dever::number($beifujin, 2).'&nbsp;&nbsp;&nbsp;&nbsp;可提现余额:¥' . Dever::number($tixian, 2);
	    return $info;
    }


    # 修改状态
    public function up($id, $status = 2, $amount = false, $fenzhang = 0)
    {
    	if ($amount) {
    		$amount = $amount * 100;
    	}
    	if (is_numeric($id)) {
    		$where['id'] = $id;
    	} else {
    		$where['source_order_num'] = $id;
    	}
    	
    	$info = Dever::db('pay/yspay_cash')->find($where);
    	if (!$info) {
    		$where['source_order_num'] = $id . '_O';
    		$info = Dever::db('pay/yspay_cash')->find($where);
    	}
    	if ($info) {
    		$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
    		if ($merchant) {
    			$update = array('status' => $status, 'where_id' => $info['id']);
	    		if ($status == 2) {
	    			if ($amount && $amount > 0 && $info['ycash'] != $amount) {
		        		$this->getCash($amount, $merchant, $update, $fenzhang);
	    			}
	    			$update['fdate'] = time();
		        }
	    		$state = Dever::db('pay/yspay_cash')->update($update);
	    		if ($state) {
	    			if ($status == 2) {
	    				$info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));

	    				$total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
	    				if ($total) {
	    					$name = '';
	    					$admin = Dever::load('manage/auth.info');
	    					if ($admin) {
	    						$name = $admin['username'];
	    					}

	    					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']));

	    					Dever::load('pay/yspay/merchant')->log($info['merchant_id'], $info['account_id'], 1, $info['hf_cash'], $name);
	    				}
	    				$info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
	    				if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
	    					$state = $this->fenzhang_act($info);
		    				if ($state == 1) {
		    					$update = array();
			    				$update['where_id'] = $info['id'];
			    				if ($state == 1) {
			    					$update['fenzhang_status'] = 2;
			    				} else {
			    					$update['fenzhang_status'] = 3;
			    				}
			    				$update['rdate'] = time();
			    				Dever::db('pay/yspay_cash')->update($update);
		    				}
	    				}
	    			}
	    			return $state;
	    		}
    		}
    	}
    	return false;
    }

    private function getCash($amount, $merchant, &$data, $fenzhang = 0)
    {
    	$fenzhang = $fenzhang*$this->num;
    	$yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
		if ($yspay) {
			$cash_per = 0;
			if ($merchant['type'] == 1 && $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;
    		$cash_jy_per = $yspay['cash_jy_per']/100;

        	$data['ycash'] = $amount;
        	$data['yl_cash'] = round($data['ycash'] * $per);
        	$data['pt_cash'] = round($data['ycash'] * $cash_jy_per);
	        $data['cash'] = $data['ycash'] - $data['yl_cash'] - $data['pt_cash'];

	        /*
	        if ($yspay['cash_type'] == 1) {
	        	$fz_cash = $data['ycash'];
	        } else {
	        	$fz_cash = $data['cash'];
	        }
	        $data['fz_cash'] = $fz_cash*$cash_per;
	        */
	        $data['fz_cash'] = 0;
	        if ($fenzhang && $fenzhang > 0) {
	        	$data['fz_cash'] += $fenzhang;
	        	$data['hf_cash'] = $data['cash'] - $data['fz_cash'];
	        } else {
	        	$data['hf_cash'] = $data['cash'];
	        	unset($data['fz_cash']);
	        }
	    }
    }

    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')->huafu($config, $merchant, $info);
    		}
    	}

    	return false;
    }

    private function fenzhang_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) {
    			$yspay_main = Dever::db('pay/yspay_main')->one($merchant['yspay_main_id']);
    			return Dever::load('pay/yspay/multi')->fenzhang($config, $yspay_main, $merchant, $info);
    		}
    	}

    	return false;
    }

    # 重新分账
    public function fz_api()
    {
    	$where = array();
    	$where['status'] = 2;
    	$where['fenzhang_status'] = 2;
    	$no_order_num = Dever::input('no_order_num');
    	$order_num = Dever::input('order_num');
    	if ($order_num) {
    		$where['order_num'] = $order_num;
    	}
    	$data = Dever::db('pay/yspay_cash')->select($where);
    	if ($data) {
    		$test = Dever::input('test');
    		if ($test == 1) {
    			print_r($data);die;
    		}
    		foreach ($data as $info)
    		{
    			if ($no_order_num && strstr($no_order_num, $info['order_num'])) {
    				continue;
    			} elseif ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] == 2) {
					$this->fenzhang_act($info);
				}
    		}
    	}
    	return 'ok';
    }

    /*
    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_edit_api()
    {
    	$order_num = Dever::input('order_num');
    	/*
    	
    	$price = Dever::input('price');
    	$fenzhang = Dever::input('fenzhang');
    	Dever::load('pay/yspay/cash')->up($order_num, 2, $price, $fenzhang);
    	*/

    	$info = Dever::db('pay/yspay_cash')->find(array('source_order_num' => $order_num, 'clear' => true));
		if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
			$state = $this->fenzhang_act($info);
			if ($state == 1) {
				$update = array();
				$update['where_id'] = $info['id'];
				if ($state == 1) {
					$update['fenzhang_status'] = 2;
				} else {
					$update['fenzhang_status'] = 3;
				}
				$update['rdate'] = time();
				Dever::db('pay/yspay_cash')->update($update);
			}
		}
    	return 'ok';
    }

    # 查询余额
    public function test_yue_api()
    {
    	$info['merchant_id'] = Dever::input('merchant_id');
		if ($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')->yue($config, $merchant);
	    		}
	    	}
		}
    	return 'ok';
    }

    # 直接划付
    public function test_tixian_api()
    {
    	$info['merchant_id'] = Dever::input('merchant_id');
		$info['hf_cash'] = Dever::input('hf_cash');
		$info['order_num'] = Dever::order('TX');
		if ($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')->huafu($config, $merchant, $info);
	    		}
	    	}
		}
    	return 'ok';
    }

    # 查询订单
    public function test_query_api()
    {
    	$info['merchant_id'] = Dever::input('merchant_id');
    	$orderType = Dever::input('orderType', 1);
    	$transDate = Dever::input('transDate');
    	$page = Dever::input('page', 1);
		if ($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')->query($config, $merchant, $orderType, $transDate, $page);
	    		}
	    	}
		}
    	return 'ok';
    }

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

    # 随机获取平台商户
    public function getMid($account_id, $yspay_main_id = 1)
    {
    	$merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'yspay_main_id' => $yspay_main_id, 'status' => 1));

    	if ($merchant) {
    		$key = array_rand($merchant);
    		if (isset($merchant[$key])) {
    			return $merchant[$key];
    		}
    	}
    	return false;
    }

    public function tixian($id, $name, $data)
    {
    	$merchant_id = Dever::param('merchant_id', $data);
    	$cash = Dever::param('cash', $data);
    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
    	if ($merchant) {
    		$admin = Dever::load('manage/auth.info');
    		$this->tixian_act($merchant, $cash, $admin['username']);
    	}
    	return false;
    }

    public function tixian_act($merchant, $cash, $name = '')
    {
    	if ($cash <= 0) {
    		Dever::alert('提现金额应大于0');
    	}
    	$cash = $cash*$this->num;
		$yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
		$cash = intval($cash);
		$yue = intval($yue);
		if ($yue < $cash) {
			Dever::alert('余额不足');
		}

		$info['merchant_id'] = $merchant['id'];
		$info['hf_cash'] = $cash;
		$info['order_num'] = Dever::order('TX');
		$status = $this->huafu_act($info);
		if ($status == 1) {
			Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant['id'], 'hf_tx_cash' => $cash));
			Dever::load('pay/yspay/merchant')->log($merchant['id'], $merchant['account_id'], 3, $cash, $name);
		}
    }

    public function total_api()
    {
    	$merchant_id = Dever::input('mid');
    	$total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $merchant_id));
		if ($total) {
			Dever::db('pay/yspay_merchant')->update(array('where_id' => $merchant_id, 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
		}

		return 'ok';
    }
}