<?php
namespace Store\Admin;

use Dever;

class Factory_excel extends Core
{
    public function out($id, $user, $data)
    {
        if (!$data) {
            Dever::alert('无导出数据');
        }
        $info = Dever::db('store/info')->find($id);
        if (!$info) {
            Dever::alert('操作失败');
        }

        $method = 'out_' . $info['out_template'];
        $file = '入库信息表';
        return $this->$method($file, $user, $data);
    }

    # 模板模板
    public function out_1($file, $user, $data)
    {
        Dever::load('mshop/lib/buy')->set(2, 1)->excel($data);
    }

    # 安鲜达模板
    public function out_2($file, $user, $data, $name = '')
    {
        $file = $name ? $name : '安鲜达入库信息表';
        $body_0 = $body_1 = array();

        $header_0 = array('客户入库单号', '业务单据类型', '预计入库时间', '备注', '发件人', '发件人手机', '发件人电话', '发件人省', '发件人城市', '发件人区县', '发件人详细地址');

        $header_1 = array('客户入库单号', '货品编码', '应收数量', '库存类型');

        foreach ($data as $k => $v) {
            if (!is_array($v)) {
                continue;
            }

            Dever::load('mshop/lib/buy')->setOutStatus($v['id'], 1);

            if ($v['ps_info'] && isset($v['ps_info']['ydate_day']) && $v['ps_info']['ydate_day']) {
                $ps_date = $v['ps_info']['ydate_day'];
            } else {
                $ps_date = '-';
            }

            $info = Dever::db('area/province')->find($v['source_info']['province']);
            $v['source_info']['province_name'] = $info['name'];

            $info = Dever::db('area/city')->find($v['source_info']['city']);
            $v['source_info']['city_name'] = $info['name'];

            if (isset($v['source_info']['county']) && $v['source_info']['county']) {
                $info = Dever::db('area/county')->find($v['source_info']['county']);
                $v['source_info']['county_name'] = $info['name'];
            } else {
                $v['source_info']['county_name'] = '';
            }
            

            $body_0[] = array
            (
                $v['order_num'],
                '入库单',
                $ps_date,
                $v['info'],
                $v['source_info']['name'],
                $v['source_info']['mobile'],
                $v['source_info']['mobile'],
                $v['source_info']['province_name'],
                $v['source_info']['city_name'],
                $v['source_info']['county_name'],
                $v['source_info']['address'],
            );

            foreach ($v['goods'] as $k1 => $v1) {

                $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
                if (isset($goods_info['sku'])) {
                    $sku = $goods_info['sku']['string'];
                } else {
                    $sku = '';
                }

                $body_1[] = array
                (
                    $v['order_num'],
                    $goods_info['code'],
                    $v1['num'],
                    '正品',
                );
            }
        }

        $xls = Dever::excelExport($body_0, $header_0, $file, 0, '入库单主表', true);

        if ($xls) {
            Dever::excelExport($body_1, $header_1, $file, 1, '入库单明细', false, $xls);
        }
    }

    # 大连模板
    public function out_3($file, $user, $data, $name = '')
    {
        $header = array();

        $header = array('所属客户', '商品编码', '数量', '商品供应商', '计量单位', '重量(kg)', '批号', '体积(m3)', '备注');

        $file = $name ? $name : '大连仓入库信息表';

        $body = array();

        $i = 1;
        foreach ($data as $k => $v) {
            if (!is_array($v)) {
                continue;
            }

            Dever::load('mshop/lib/buy')->setOutStatus($v['id'], 1);
            if ($v['ps_info'] && $v['ps_info']['cdate_day']) {
                $ps_date = $v['ps_info']['cdate_day'];
            } else {
                $ps_date = '-';
            }

            if ($v['parent_type'] == 2) {
                $parent_order = Dever::db('shop/sell_order')->find($v['parent_order_id']);

                if ($parent_order && $parent_order['uid']) {
                    if ($parent_order['address_id']) {
                        $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
                        $v['type_info']['name'] = $address['contact'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = $address['address_info'];
                    } else {
                        $address = Dever::db('passport/user')->find($parent_order['uid']);
                        $v['type_info']['name'] = $address['username'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = '';
                    }
                }
            } elseif ($v['parent_type'] == 3) {
                $parent_order = Dever::db('agent/dh_order')->find($v['parent_order_id']);

                if ($parent_order && $parent_order['mid']) {
                    if ($parent_order['address_id']) {
                        $address = Dever::load('agent/address')->getOne($parent_order['mid'], $parent_order['address_id']);
                        $v['type_info']['name'] = $address['contact'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = $address['address_info'];
                    } else {
                        $address = Dever::db('agent/member')->find($parent_order['mid']);
                        $v['type_info']['name'] = $address['name'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = '';
                    }
                }
            }

            if (!isset($v['type_info']['sid'])) {
                $v['type_info']['sid'] = '';
            }
            
            foreach ($v['goods'] as $k1 => $v1) {

                $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
                if (isset($goods_info['sku'])) {
                    $sku = $goods_info['sku']['string'];
                } else {
                    $sku = '';
                }

                $d = array
                (
                    '厨人易料',
                    $goods_info['code'],
                    $v1['num'],
                    $v['source_info']['name'],
                    $goods_info['unit'],
                    $goods_info['weight'],
                    '',
                    '',
                    $v['info'],
                );

                $body[] = $d;

                $i++;
            }
        }

        Dever::excelExport($body, $header, $file);
    }
}