<?php

namespace Mshop\Src;

use Dever;
use Mshop\Lib\Core;

class Out extends Core
{
    # 获取所有商品列表
    public function getGoods()
    {
        $this->data['data'] = Dever::load('shop/lib/info')->getGoods($this->shop_id, false, '1,2,3');

        return $this->data;
    }

    # 采购订单列表
    public function getList()
    {
        return Dever::load('mshop/lib/out')->set(1,1)->getList($this->shop_id);
    }

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

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

    # 下单填写用途信息
    public function info()
    {
        $this->data['user'] = $this->user;
        $type = Dever::db('shop/out_order')->config['config_type'];
        $type = $type();
        $this->data['type'] = array();
        foreach ($type as $k => $v) {
            $this->data['type'][$v['id']] = $v['name'];
        }
        return $this->data;
    }

    # 确认订单页面
    public function confirm()
    {
        $this->goods();

        return $this->data;
    }

    # 得到商品和总价
    private function goods()
    {
        $goods_id = Dever::input('goods_id');
        if (!$goods_id) {
            Dever::alert('请传入商品');
        }
        $goods_id = explode(',', $goods_id);

        $sku_id = Dever::input('price_id');
        if ($sku_id) {
            $sku_id = explode(',', $sku_id);
        }
        
        $num = Dever::input('num');
        if (!$num) {
            Dever::alert('请传入商品数量');
        }
        $num = explode(',', $num);

        $this->data['price'] = 0;
        $this->data['num'] = 0;
        $this->data['name'] = array();
        # 计算总价格
        foreach ($goods_id as $k => $v) {
            $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
            $n = isset($num[$k]) ? $num[$k] : 1;
            $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s);
            $this->data['list'][$k]['num'] = $n;

            # 2是库存不足
            $this->data['list'][$k]['ku_state'] = 1;

            # 验证是否有货
            $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $this->shop_id, $s, 3);

            if ($total <= 0) {
                $this->data['list'][$k]['ku_state'] = 2;
                $this->data['list'][$k]['buy_num'] = 0;
            }

            if ($this->data['list'][$k]['ku_state'] == 1) {
                $this->data['list'][$k]['buy_num'] = $n;
                $this->data['num'] += $n;
                $this->data['price'] += $this->data['list'][$k]['price'] * $n;
                $this->data['name'][] = $this->data['list'][$k]['name'];
            }
        }

        $this->data['name'] = implode(',', $this->data['name']);

        $this->data['price'] = round($this->data['price'], 2);
    }

    # 出库下单
    public function action_commit()
    {
        $info = Dever::input('info');
        $type = Dever::input('type', 1);

        $this->goods();

        return Dever::load('mshop/lib/out')->set(1,1)->action($this->shop_id, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $this->uid, $type, $info, 2, $this->shop['area']);
    }

    # 打印
    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('请选择打印机');
        }

        $manage_config = Dever::db('main/manage_config')->find();
        $sell_config = Dever::db('main/sell_config')->find();
        $main_config = Dever::db('main/config')->find();
        $data = $this->view();

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

        $config = Dever::db('shop/out_order')->config;

        $status = $config['config_status'][$data['status']];

        $out_type = $config['config_type'][$data['out_type']];

        $print->hr('-', $main_config['name'])->br();
        $print->text('单号：' . $data['order_num'], 'CB')->br();
        $print->text('门店名称：' . $this->shop['name']);
        $print->text('制 单 人：'.$this->user['name']);
        $print->text('制单时间：'.date('Y-m-d H:i'));
        $print->text('出库时间：'.$data['cdate']);
        
        if ($data && $data['goods']) {

            $left = 70;
            $right = 30;
            $head = array('名称', '数量', $left, $right);
            $body = array();
            $body_total = array();
            $body_total['num'] = 0;
            foreach ($data['goods'] as $k => $v) {
                $sku = isset($v['info']['sku_name']) ? '-' . str_replace(',', '', $v['info']['sku_name']) : '';
                $d  = array
                (
                    $v['info']['name'] . $sku,
                    $v['num'],
                    $left,
                    $right
                );
                $body[] = $d;
                $body_total['num'] += $v['num'];
            }
            $print->table($head, $body, '', 'lr');

            $print->right('共'.$body_total['num'].'件商品');
        }

        $print->hr();

        $print->text('出库类别：' . $out_type);
        $print->text('原因备注：' . $data['info']);

        $print->hr();

        $print->center('如遇任何问题请致电客服');
        $print->center('电话：'.$manage_config['phone']);
        $print->center($main_config['worktime']);

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

        return 'ok';
    }
}