<?php

namespace Shop\Lib;

use Dever;

class Record
{

    #生成订单号
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('R');
        $where['clear'] = true;
        $state = Dever::db('shop/record')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
    public function pingtai($shop_id,$sid,$type,$type_id,$price,$yue,$explan=false){
    	$where['shop_id'] = $shop_id;
    	$where['sid'] = $sid;
    	$where['type'] = $type;
    	$where['type_id'] = $type_id;
    	$where['price'] = $price;
    	$where['yue'] = $yue;
    	$where['order_num'] = $this->getOrderId();
    	if($explan){
    		$where['explan'] = $explan;
    	}
    	if($type == 4){
    		$where['explan'] = '采购商品';
    	}
        if($type == 4){
            $where['price'] = -1*$price;
        }
    	$where['status'] = 2;
    	$where['state'] = 1;
    	$where['cdate'] = time();
    	// print_R($where);die;
    	$ids = Dever::db('shop/record')->insert($where);

        $update['where_id'] = $shop_id;
        $update['set_price'] = $where['price'];
        Dever::db('shop/info')->upCash($update);
    	return $ids;
    }
    public function insertRecord($id,$name,$data){
        $sid= Dever::param('sid',$data);
        $type = Dever::param('type',$data);
        $price = Dever::param('price',$data);
        $shop = Dever::db('shop/info')->find(array('sid'=>$sid));
        
        if($type && $type == 2 && $price){
            if($shop['price']<$price){
                Dever::alert('提现大于余额');
            }
        }
    }
    #充值和提现
    public function updateRecord($id,$name,$data){
    	$audit = Dever::input('audit',$data);
    	$sid = Dever::input('sid',$data);
        $type = Dever::input('type',$data);
        $price = Dever::input('price',$data);
        $info = Dever::db('shop/record')->find($id);
       	$shop = Dever::db('shop/info')->find(array('sid'=>$info['sid']));        
        if ($info) {
            $where['where_id'] = $shop['id'];
            $where['price'] = $info['price'];
            if($info['type'] ==2){
            	if($info['price']>$shop['price']){
            		Dever::alert('提现大于余额');
            	}
                
            	$insert['yue'] = $shop['price']+(-1*$info['price']);

            	$insert['type_id'] = Dever::load('bill/lib/tixian')->up($shop['id'], $info['price'], $info['price'], $info['pic'], 2,$id);
                
                $info['price'] = -1*$info['price'];
                $where['price'] = $info['price'];
                
            }
            Dever::db('shop/info')->upCash($where);
            $ids = Dever::db('shop/info')->find(array('sid'=>$info['sid'],'s'=>1));
            if($info['type'] == 1){
            	$insert['yue'] = $ids['price'];
            }
            $insert['where_id'] = $info['id'];
            $insert['shop_id'] = $shop['id'];
            $insert['order_num'] = $this->getOrderId();
            if($info['type'] == 1){
            	$insert['status'] = 2;
            	$insert['operdate'] = time();
            }elseif($info['type'] == 2){
                $insert['price'] =$info['price'];
            	$insert['status'] = 1;
            	$insert['audit'] = 1;
            	$insert['fafang'] = 1;
            }   
            // print_R($insert);die;         
            Dever::db('shop/record')->update($insert);
        }
    }
    #审核和发放
    public function RecordAuditUpdate($id, $name, $data)
    {
    	Dever::config('base')->hook = true;
        $audit = Dever::param('audit', $data);
        if ($audit > 1) {
            $mul_type = Dever::config('base')->mul_type;
            if ($mul_type == 2) {
                $list = Dever::db('shop/record')->select(array('status' => 1));
            } else {
                $list = explode(',', $id);
            }

            $info  = Dever::db('shop/record')->find(array('id' => $id));
            #已审核
            if ($audit == 2) {
            	$shop = Dever::db('shop/info')->find($info['shop_id']);

                $info = Dever::db('shop/record')->find($id);
                if ($info) {
                    $where['where_id'] = $info['shop_id'];
                    
                    if($info['type'] == 2){
                    	if($info['price']>$shop['price']){
                    		Dever::alert('提现大于余额');
                    	}
                    	$where['price'] = $info['price'];
                    	
                		$yue = $shop['price'];
                		Dever::db('shop/record')->update(array('where_id' => $id, 'yue'=>$yue,'status' => 2, 'operdate' => time()));
                		$tixian = Dever::db('bill/tixian')->find($info['type_id']);
                		if($tixian){
                			Dever::db('bill/tixian')->update(array('where_id'=>$tixian['id'],'status'=>1));
                		}
                    }else{
                    	$where['price'] = $info['price'];
                    	Dever::db('shop/record')->update(array('where_id' => $id, 'status' => 2, 'operdate' => time()));
                    }
                }
            }else {
                #作废
                
                $info = Dever::db('shop/record')->find($id);
                if ($info && $info['type'] == 2) {
                    $where['where_id'] = $info['shop_id'];
                    $where['set_price'] = -1*$info['price'];
                    $ids = Dever::db('shop/info')->upCash($where);
                    $shop = Dever::db('shop/info')->find($ids);
                    Dever::db('shop/record')->update(array('where_id' => $id, 'status' => 3, 'operdate' => time(),'yue'=>$shop['price']));
                }
            }
        }
    }
    #列表页展示
    public function order_num($id){
    	$info = Dever::db('shop/record')->find($id);
    	$data = array();
    	$data['num'] = $info['order_num'].'<br/>'.date('Y-m-d H:i',$info['cdate']);
    	$shop = Dever::db('shop/info')->find($info['shop_id']);
    	$data['name'] = $shop['name'].'<br/>'.$shop['sid'];
    	$data['price'] = $info['price'].'<br/>'.$info['yue'];
    	return $data;
    }
    #详情页展示
    public function show(){
    	$id = Dever::input('id');
    	$info = Dever::db('shop/record')->find($id);
    	$shop = Dever::db('shop/info')->find($info['shop_id']);
    	$typeconfig = Dever::db('shop/record')->config['config_type'];
		$type = Dever::status($typeconfig,$info['type']);
    	$config = Dever::db('shop/record')->config['config_status'];
		$status = Dever::status($config,$info['status']);
		$operdate = '-';
		if($info['operdate']){
			$operdate = date('Y/m/d H:i',$info['operdate']);
		}
		$audit_name = '-';
		if($info['audit'] == 1){
			$audit_name = '待审核';
		}elseif($info['audit'] == 2){
			$audit_name = '已审核';
		}else{
			$audit_name = '已作废';
		}
		// $f_name = '-';
		// if($info['fafang'] == 1){
		// 	$f_name = '待发放';
		// }elseif($info['fafang'] == 2){
		// 	$f_name = '已发放';
		// }else{
		// 	$f_name = '已作废';
		// }
		
    	$result = array();
    	$result['门店信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
            	array
            	(
            		array('门店名称', $shop['name']),
	                array('门店编号', $shop['sid']),
	                array('负责人', $shop['truename']),
	                array('联系电话', $shop['mobile']),
            	),
            ),
        );
        $result['交易信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
            	array
            	(
            		array('流水号', $info['order_num']),
	                array('交易时间', date('Y/m/d H:i',$info['cdate'])),
	                array('交易类型', $type),
	                array('交易状态', $status),
	                array('交易金额', $info['price']),
	                array('交易后金额', $info['yue']),
	                array('交易说明', $info['explan']),
            	),
            ),
        );
        if($info['type'] == 2){
        	$result['提现信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
            	array
            	(
            		array('审核时间', $operdate),
	                array('审核状态', $audit_name),
	                array('备注', $info['audit_desc']),
	                // array('发放状态', $f_name),
	                // array('备注', $info['fafang_desc']),
            	),
            	array
            	(
            		array('支付凭证', $info['pic']),
            	),
            ),
        );
        }
        $button = array();
        if (Dever::load('manage/auth')->checkFunc('bill.tixian', 'edit', '审核')) {
        	$purl = Dever::url('project/database/update?project=shop&table=record&where_id='.$info['id'].'&status=1&col=id,audit,audit_desc', 'manage');
        	if($info['audit'] == 1){
        		$button[] = array
	            (
	                'type' => 'edit',
	                'link' => $purl,
	                'name' => '审核',
	            );
            }
        }
        if ($info['type'] == 2) {
        	$tixian = Dever::db('bill/tixian')->find($info['type_id']);
        	// print_R($tixian);die;
            if ($tixian) {
                $config = Dever::db('bill/tixian')->config['set'];
                $status = $config['status'][$tixian['status']];

                if (Dever::load('manage/auth')->checkFunc('bill.tixian', 'edit1', '发放')) {
                    if ($info['status'] == 2 && $tixian['status'] == 1) {
                        $button[] = array
                        (
                            'type' => 'edit',
                            'link' => Dever::url('project/database/update?project=bill&table=tixian&where_id='.$tixian['id'].'&col=audit,audit_desc,pic&oper_save_jump=tixian&oper_table=tixian&oper_parent=tixian', 'manage'),
                            'name' => '发放',
                        );
                    }
                }
                if ($tixian['status']) {
                    if ($tixian['operdate']) {
                        $opertime = date('Y-m-d H:i', $tixian['operdate']);
                    } else {
                        $opertime = '';
                    }
                    $pic = '';
                    if ($tixian['pic']) {
                        $temp = explode(',', $tixian['pic']);
                        foreach ($temp as $k => $v) {
                            $pic .= '<a href="'.Dever::pic($v).'" target="_blank"><img src="'.Dever::pic($v).'" width="150" /></a>';
                        }
                    }
                    $result['发放信息'] = array
                    (
                        'type' => 'info',
                        'content' => array
                        (
                            array
                            (
                                array('发放时间', $opertime),
                                array('发放状态', $status),
                                array('备注', $tixian['audit_desc']),
                            ),
                            array
                            (
                                array('凭证', $pic),
                            ),
                        )
                    );
                }
            }
        }
       
        $head = array
        (
            'name' => '基本资料',
            'btn' => $button,
        );
        $foot = $button;
        $html = Dever::show($head, $result, $foot);

        return $html;
    }

    public function search_api()
    {
        $value = Dever::input('value');
        if(!$value){
                return '店铺编号不正确';
            }
        if ($value) {
            $data = Dever::db('shop/info')->one(array('sid' => $value,'state' =>1));
            $table = array();
            if ($data) {
                $table = array();
                $table['head'] = array('序列号', '门店编号','姓名', '手机号');
                $table['body'] = array();
                $table['body'] = array($data['id'], $data['sid'],$data['name'], $data['mobile']);
                
                $result[] = array
                (
                    'type' => 'table',
                    'content' => $table,
                );

                $html = Dever::show('', $result, false, false);
                return $html;
            }else{
                return '店铺不存在';
            }
        } else {
            return '';
        }
    }
    public function out_record($data){
    	$header = array('交易流水号','交易时间','门店名称','门店编号','门店手机号','交易类型','交易金额','交易后余额','交易状态','备注');
    	$body = array();
    	foreach($data as $k => $v){
    		$shop = Dever::db('shop/info')->find($v['shop_id']);
    		$typeconfig = Dever::db('shop/record')->config['config_type'];
			$type = Dever::status($typeconfig,$v['type']);
	    	$config = Dever::db('shop/record')->config['config_status'];
			$status = Dever::status($config,$v['status']);
			$d = array
			(
				$v['order_num'],
				date('Y-m-d H:i',$v['cdate']),
				$shop['name'],
				$shop['sid'],
				$shop['mobile'],
				$type,
				$v['price'],
				$v['yue'],
				$status,
				$v['audit_desc'],
			);
			$body[] = $d;
    	}
    	$file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }
    public function getString($shop_id){
    	$info = Dever::db('shop/info')->find($shop_id);
    	return '门店信息：'.$info['name'].' '.$info['sid'].' '.$info['truename'].' '.$info['mobile'];
    }
    
}