<?php

namespace Mshop\Src;

use Dever;
use Mshop\Lib\Core;

# 库存清单
class Goods extends Core
{
    public function getList()
    {
        $this->data['search'] = array();
        $this->data['search']['total'] = array
        (
            array('name' => '全部', 'value' => ''),
            array('name' => '无库存', 'value' => '1'),
            array('name' => '库存<3件', 'value' => '3'),
            array('name' => '库存<5件', 'value' => '5'),
            array('name' => '库存<10件', 'value' => '10'),
        );
        $this->data['data'] = Dever::load('shop/lib/info')->getGoodsSku($this->shop_id);

        return $this->data;
    }


    # 下单pdf
    public function pdf()
    {
        $manage_config = Dever::db('main/manage_config')->find();
        $sell_config = Dever::db('main/sell_config')->find();
        $main_config = Dever::db('main/config')->find();
        $data = Dever::load('shop/lib/info')->getGoodsSku($this->shop_id);


        $pdf = Dever::load('pdf/lib/base')->init();

        $pdf->hr('-', $main_config['name']);

        $pdf->br()->font(20)->center('库存清单');
        $pdf->font(10);
        $pdf->br(2);
        $pdf->left('门店名称：' . $this->shop['name']);
        $pdf->left('制单人：' . $this->user['name']);
        $pdf->left('制单时间：' . date('Y-m-d H:i:s'));

        $pdf->hr();

        $pdf->br()->left('商品名称', 150)->right('数量', 40);

        if ($data) {
            foreach ($data as $k => $v) {
                if (isset($v['price_array'])) {
                    $pdf->br()->left($v['name'], 150);
                    foreach ($v['price_array'] as $k1 => $v1) {
                        $pdf->br()->left('----' . $v1['name'], 150)->right('x ' . $v1['total'], 40);
                    }
                } else {
                    $pdf->br()->left($v['name'], 150)->right('x ' . $v['total'], 40);
                }
            }
        }

        $pdf->hr();

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

        $pdf->br(2)
        ->img($sell_config['applet'], 70)
        ->img($manage_config['applet'], 110);

        $pdf->br(3)->center('门店零售小程序                 门店管理小程序');

        $pdf->out('库存清单');
    }

    # 打印
    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 = Dever::load('shop/lib/info')->getGoodsSku($this->shop_id);

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

        $print->hr('-', $main_config['name'])->br();
        $print->text('库存清单', 'CB')->br();
        $print->text('门店名称：' . $this->shop['name']);
        $print->text('制 单 人：'.$this->user['name']);
        $print->text('制单时间：'.date('Y-m-d H:i:s'));
        
        if ($data) {
            $left = 70;
            $right = 30;
            $head = array('名称', '数量', $left, $right);
            $body = array();
            foreach ($data as $k => $v) {
                if (isset($v['price_array'])) {
                    $body[] = array($v['name'], '', $left, $right);
                    foreach ($v['price_array'] as $k1 => $v1) {
                        $name = '----' . $v1['name'];
                        $total = $v1['total'];
                        $body[] = array($name, $total, $left, $right);
                    }
                    
                } else {
                    $name = $v['name'];
                    $total = $v['total'];
                    $body[] = array($name, $total, $left, $right);
                }
            }
            $print->table($head, $body, '', 'lr');
        }

        $print->center('如遇任何问题请致电客服');
        $print->center('电话：'.$manage_config['phone']);
        $print->center($main_config['worktime']);
        $content = $print->getText();
        Dever::load('mshop/lib/feieyun')->printOrder($info['number'], $content, 1);

        return 'ok';
    }
}