<?php

namespace Mshop\Src;

use Dever;
use Mshop\Lib\Core;

class Data extends Core
{
	public function home()
	{
		$day = Dever::input('day');

		if (!$day) {
			$day = date('Y-m-d');
		}

		$where['start'] = Dever::maketime($day . ' 00:00:00');
		$where['end'] = Dever::maketime($day . ' 23:59:59');
		$where['shop_id'] = $this->shop_id;
        $where['status'] = '1,2,3,4';

		$this->data['num_order'] = round(Dever::db('shop/sell_order')->getOrderNum($where), 2);
		$cash = Dever::db('shop/sell_order')->getCashNum($where);
		if ($cash) {
			$this->data['num_cash'] = $cash['total'];
		}
		if (!$this->data['num_cash']) {
			$this->data['num_cash'] = 0;
		}
		$goods = Dever::db('shop/sell_order')->getGoodsNum($where);
		if ($goods) {
			$this->data['num_goods'] = $goods['total'];
		}
		if (!$this->data['num_goods']) {
			$this->data['num_goods'] = 0;
		}

		$this->data['num_cash'] = round($this->data['num_cash'], 2);
		$this->data['num_goods'] = round($this->data['num_goods'], 2);

		return $this->data;
	}

	# 销售订单列表
	public function sell_order()
	{
		return Dever::load('shop/lib/sell')->set(2, 1)->getList($this->shop_id);
	}

    # 查看订单详情
    public function order_view()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('shop/lib/sell')->set(2, 2)->getView($this->shop_id, $order_id);
    }

    # 获取配送服务商
    public function get_service()
    {
        $data['service'] = Dever::db('shop/service')->select(array('type' => 2));

        return $data;
    }

    # 通知用户取件或者配送
    public function notice()
    {
        $order_id = Dever::input('order_id');
        $ps_order_num = Dever::input('ps_order_num');
        $ps_service_id = Dever::input('ps_service_id');

        $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));

        if ($info && $info['shop_id'] == $this->shop_id && $info['status'] < 3) {

            Dever::load('shop/lib/sell')->notice($info);
            
            if ($info['method'] == 2) {
                Dever::load('shop/lib/sell')->updatePs($info, 2, $ps_service_id, $ps_order_num);
            }
            
            return 'ok';
        } else {
            Dever::alert('您没有权限');
        }
    }

    # 核销取件码，完成
    public function finish_commit()
    {
        $code = Dever::input('code');

        if (!$code) {
            Dever::alert('请输入自提码');
        }

        $order_id = Dever::input('order_id');

        $info = Dever::db('shop/sell_order')->find(array('code' => $code, 'id' => $order_id));

        return Dever::load('shop/lib/sell')->finish($info, $this->shop_id);
    }

    # 取消订单
    public function order_cancel_commit()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('shop/lib/sell')->set(2, 2)->cancel($this->shop_id, $order_id);
    }

    # 获取订单退款记录
    public function order_tui_log()
    {
        $order_id = Dever::input('order_id');

        return Dever::load('shop/lib/refund')->set('sell')->getList(2, $this->shop_id, $order_id);
    }

    # 门店全部退款
    public function order_tui_commit()
    {
        $order_id = Dever::input('order_id');
        $status = Dever::input('status');
        $desc = Dever::input('desc');

        return Dever::load('shop/lib/refund')->set('sell')->apply(2, $this->shop_id, $order_id, false, $status, 0, $desc);
    }

    # 门店部分退款信息
    public function order_tui_one_info()
    {
        $order_id = Dever::input('order_id');
        $order_goods_id = Dever::input('order_goods_id');

        return Dever::load('shop/lib/refund')->set('sell')->getInfo(2, $this->shop_id, $order_id, $order_goods_id);
    }

    # 门店部分退款
    public function order_tui_one_commit()
    {
        $order_id = Dever::input('order_id');
        $order_goods_id = Dever::input('order_goods_id');
        $num = Dever::input('num');
        $status = Dever::input('status');
        $desc = Dever::input('desc');

        return Dever::load('shop/lib/refund')->set('sell')->apply(2, $this->shop_id, $order_id, $order_goods_id, $status, $num, $desc);
    }

    # 根据店铺获取商品列表
    public function getGoods()
    {
        return Dever::load('shop/lib/goods')->getList();
    }

    # 获取店铺的优惠券
    public function getCoupon()
    {
        $this->data = Dever::db('shop/coupon')->getAllPage(array('shop_id' => $this->shop_id));

        if ($this->data) {
            $time = time();
            foreach ($this->data as $k => $v) {
                $this->data[$k]['info'] = Dever::db('goods/coupon')->find($v['coupon_id']);
                $this->data[$k]['shop'] = array();
                $shop_num = Dever::db('shop/coupon')->total(array('coupon_id' => $v['coupon_id'], 'city' => $v['city']));

                $this->data[$k] = Dever::load('shop/lib/coupon')->getShop($this->data[$k], false, true);
                /*
                $shop = Dever::db('shop/info')->getOne($v['shop_id']);
                if ($this->data[$k]['info']['method'] == 1) {
                    $this->data[$k]['shop_name'] = '平台通用';
                } elseif ($this->data[$k]['info']['method'] == 2) {
                    $this->data[$k]['shop_name'] = $shop['name'];
                    if ($shop_num > 1) {
                        $this->data[$k]['shop_name'] .= '多店通用';
                    }
                } elseif ($this->data[$k]['info']['method'] == 3) {
                    $this->data[$k]['shop_name'] = $shop['name'];
                }
                */
            }
        }

        return $this->data;
    }

    # 赠送优惠券
    public function giveCoupon_commit()
    {
        $coupon_id = Dever::input('coupon_id');
        $coupon = explode(',', $coupon_id);

        $num = Dever::input('num');
        $coupon_num = explode(',', $num);

        $order_id = Dever::input('order_id');

        $info = Dever::db('shop/sell_order')->find(array('shop_id' => $this->shop_id, 'id' => $order_id));

        if ($info['uid'] && $info['uid'] > 0) {
            foreach ($coupon as $k => $v) {
                $num = isset($coupon_num[$k]) ? $coupon_num[$k] : 1;
                Dever::load('shop/lib/coupon')->getOne($info['uid'], $this->shop, $v, 1, $num, false);
            }
        }

        return 'ok';

    }

    # 获取购物车的数据
    public function getCart()
    {
        $where['shop_id'] = $this->shop_id;

        $this->data['cart'] = Dever::db('shop/shop_cart')->select($where);

        if ($this->data['cart']) {
            foreach ($this->data['cart'] as $k => $v) {
                $this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);

                if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
                    $this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
                }
            }
        }

        return $this->data['cart'];
    }

    # 扫码加入购物车
    public function scan()
    {
        $goods = Dever::load('goods/lib/info')->getGoodsByCode($this->shop_id, 1, true);
        if ($goods) {
            Dever::setInput('goods_id', $goods['id']);
            Dever::setInput('price_id', $goods['sku']['id']);

            return $this->addCart();
        }
        Dever::alert('错误的商品');
    }

    # 加入到购物车
    public function addCart()
    {
        $where['shop_id'] = $this->shop_id;
        $where['goods_id'] = Dever::input('goods_id');
        $where['sku_id'] = Dever::input('price_id');
        if (!$where['goods_id']) {
            Dever::alert('错误的商品');
        }

        $info = Dever::db('shop/shop_cart')->find($where);

        $where['num'] = Dever::input('num', 1);
        if (!$info) {
            # 验证库存
            Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
            $state = Dever::db('shop/shop_cart')->insert($where);
        } else {
            $where['num'] += $info['num'];
            # 验证库存
            Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
            $where['where_id'] = $info['id'];
            $state = Dever::db('shop/shop_cart')->update($where);
        }

        return $this->getCart();
    }

    # 清空购物车
    public function dropCart()
    {
        $where['shop_id'] = $this->shop_id;

        $state = Dever::db('shop/shop_cart')->delete($where);

        return $this->alert($state);
    }

    # 更新购物车数量
    public function upCart()
    {
        $where['shop_id'] = $this->shop_id;
        $where['id'] = Dever::input('cart_id');

        $status = Dever::input('status', 1);
        $type = Dever::input('type', 1);
        $num = Dever::input('num');

        $info = Dever::db('shop/shop_cart')->find($where);

        $state = false;

        if ($info) {
            if ($type == 1) {
                $num = $info['num'] + $num;
            } else {
                $num = $info['num'] - $num;
            }
            $where = array();
            $where['where_id'] = $info['id'];
            if ($num <= 0) {
                $state = Dever::db('shop/shop_cart')->delete($info['id']);
            } else {
                $where['status'] = $status;
                $where['num'] = $num;
                
                # 验证库存
                $total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);

                if ($where['num'] > $total) {
                    # 库存不足
                    $where['num'] = $total;
                    $state = Dever::db('shop/shop_cart')->update($where); 
                    return array('cart' => 2, 'data' => $total);
                }
                $state = Dever::db('shop/shop_cart')->update($where); 
            }
        }

        return array('cart' => 1, 'data' => $this->getCart());
    }

    # 确认订单页面
    public function confirm()
    {
        $this->data['shop'] = $this->shop;
        Dever::load('shop/lib/sell')->goods($this->data);
        Dever::load('shop/lib/sell')->coupon($this->data);

        return $this->data;
    }

    # 开始下单
    public function pay()
    {
        $refer = Dever::input('refer');
        $cart = Dever::input('cart', 2);
        $address_id = Dever::input('address_id');
        $invoice_id = Dever::input('invoice_id');
        $pay_type = Dever::input('pay_type', 1);
        $mobile = Dever::input('mobile');
        $cash = Dever::input('cash', 0);
        Dever::setInput('pay_method', 2);

        if (!$mobile) {
            Dever::alert('手机号不能为空');
        }
        
        $info = Dever::input('info');

        $this->data['shop'] = $this->shop;

        Dever::load('shop/lib/sell')->goods($this->data);
        Dever::load('shop/lib/sell')->coupon($this->data, 3);

        if ($this->data['pay_method'] > 1) {
            $cart = false;
        }
        $cart = 2;

        if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
            Dever::alert('已售空');
        }

        if ($this->data['method'] == 2 && !$address_id) {
            Dever::alert('收货地址不能为空');
        }

        if (!isset($this->data['card'])) {
            $this->data['card'] = false;
        }

        $this->user = array
        (
            'id' => -1,
            'mobile' => $mobile,
        );

        $this->data['price'] -= $cash;
        $pay = Dever::load('shop/lib/sell')->pay($this->data['method'], $this->data['pay_method'], $pay_type, $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['oprice'], $this->data['price'], $cash, $address_id, $invoice_id, $info, $this->data['card'], 0, $this->data['coupon_id'], $this->data['user_coupon_id'], $this->data['coupon_cash'], $cart, 4, $refer);
        return $pay;
    }

    # 再次付款
    public function r_pay()
    {
        $refer = Dever::input('refer');
        $order_id = Dever::input('order_id');

        $pay = Dever::load('shop/lib/sell')->rpay($order_id, 4, $refer);

        return $pay;
    }

    # 查询是否支付
    public function c_pay()
    {
        $refer = Dever::input('refer');
        $order_id = Dever::input('order_id');

        $pay = Dever::load('shop/lib/sell')->cpay($order_id, 4, $refer);

        return $pay;
    }


    # 销量统计
    public function stat()
    {
        $where['shop_id'] = $this->shop_id;
        $where['end'] = time();
        $where['start'] = $where['end'] - 86400*30;
        $this->data['total'] = array();
        $this->data['total']['cash'] = 0;
        $this->data['total']['order'] = 0;
        $this->data['total']['goods'] = 0;
        $this->data['data'] = Dever::db('shop/sell_stat')->getAll($where);

        $this->data['day'] = $this->data['cash'] = $this->data['order'] = $this->data['goods'] = array();

        if ($this->data['data']) {
            foreach ($this->data['data'] as $k => $v) {
                if (!$v['cash']) {
                    $this->data['data'][$k]['cash'] = 0;
                }
                if (!$v['order']) {
                    $this->data['data'][$k]['order'] = 0;
                }
                if (!$v['goods']) {
                    $this->data['data'][$k]['goods'] = 0;
                }

                $this->data['data'][$k]['cash'] = floatval($this->data['data'][$k]['cash']);
                $this->data['data'][$k]['order'] = floatval($this->data['data'][$k]['order']);
                $this->data['data'][$k]['goods'] = floatval($this->data['data'][$k]['goods']);

                $this->data['data'][$k]['day'] = date('m-d', $v['day']);
                $this->data['total']['cash'] += $this->data['data'][$k]['cash'];
                $this->data['total']['order'] += $this->data['data'][$k]['order'];
                $this->data['total']['goods'] += $this->data['data'][$k]['goods'];

                $this->data['day'][] = $this->data['data'][$k]['day'];
                $this->data['cash'][] = $this->data['data'][$k]['cash'];
                $this->data['order'][] = $this->data['data'][$k]['order'];
                $this->data['goods'][] = $this->data['data'][$k]['goods'];
            }
        }

        return $this->data;
    }

    # 打印
    public function printer()
    {
        $id = Dever::input('printer_id');
        if (!$id) {
            Dever::alert('请选择打印机');
        }
        $where['id'] = $id;
        $where['shop_id'] = $this->shop_id;
        $info = Dever::db('shop/print')->find($where);
        if (!$info) {
            Dever::alert('请选择打印机');
        }

        $order_id = Dever::input('order_id');

        $order = Dever::load('shop/lib/sell')->set(2, 2)->getView($this->shop_id, $order_id);

        $manage_config = Dever::db('main/manage_config')->find();
        $sell_config = Dever::db('main/sell_config')->find();
        $main_config = Dever::db('main/config')->find();
        $config = Dever::db('shop/sell_order')->config;
        $shop = Dever::db('shop/info')->find($order['shop_id']);

        $user = $this->user;

        $left = 70;
        $right = 30;
        $head = array('名称', '数量', '单价', $left, $right);
        $goods = array();
        $tgoods = array();
        foreach ($order['goods'] as $k => $v) {
            $sku = isset($v['info']['sku_name']) ? '-' . str_replace(',', '', $v['info']['sku_name']) : '';
            $d  = array
            (
                $v['info']['name'] . $sku,
                $v['num'],
                $v['price'],
                $left,
                $right
            );
            $goods[] = $d;
            if ($v['status'] == 3) {
                $tgoods[] = $d;
            }
        }

        $print = Dever::load('mshop/lib/feieyun');

        $print->hr('-', $main_config['name'])->br();
        $print->center($config['method'][$order['method']]);
        $print->text('单号：' . $order['order_num'], 'CB')->br();
        $print->text('门店名称：' . $shop['name']);
        $print->text('制 单 人：'.$user['name']);
        $print->text('下单时间：'.$order['cdate']);
        if ($order['fdate']) {
            $print->text('完成时间：'.$order['fdate']);
        }
        
        if ($goods) {
            $print->table($head, $goods, '订单商品');
        }
        
        if ($order['card_code_id']) {
            $card = Dever::db('goods/card_code')->find($order['card_code_id']);
            if ($card) {
                $print->text('礼品卡：'.$card['card']);
            }
        }
        $print->lr('优    惠：', $order['coupon_cash']);
        if (isset($order['card_code_id']) && $order['card_code_id'] > 0) {
            $card = substr($order['card_code_card'], -4);
            $print->lr('礼品卡：', $order['card_code_cash']);
        }
        if (isset($order['ps_cash']) && $order['ps_cash']) {
            $print->lr('配送费：', $order['ps_cash']);
        }
        $print->lr('应    付：', $order['price']);
        $print->hr();
        $print->lr($order['pay_type_name'] . '支付：', $order['price']);
        $print->lr('找    零：', $order['kou_cash']);
        
        if ($tgoods) {
            $print->table($head, $tgoods, '退款商品');
            $print->lr('退款金额：', $order['coupon_cash']);
        }

        $print->hr();

        if (isset($order['address']) && $order['address']) {
            $print->text('配送地址');
            $print->text($order['address']['contact'] . '，' . $order['address']['mobile']);

            $area = Dever::load("area/api.string", $order['address']['area'], '');
            $print->text($area . $order['address']['address'] . $order['address']['house_number']);
            $print->hr();
        }

        if ($order['info']) {
            $print->lr('备    注：', $order['info']);

            $print->hr();
        }
        
        $print->center('如遇任何问题请致电客服');
        $print->center('电话：'.$manage_config['phone']);
        $print->center($main_config['worktime']);
        //$content .= '<QR>http://www.feieyun.com</QR>';//把二维码字符串用标签套上即可自动生成二维码

        $content = $print->getText();
        $print->printOrder($info['number'], $content, 1);

        return 'ok';
    }
}