<?php
namespace Store\Admin;

use Dever;

class 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)
    {
        $this->out_3($file, $user, $data, $file);
    }

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

        $type = Dever::input('type');
        $parent_type = Dever::input('parent_type');
        $source_type = Dever::input('source_type');

        if ($type == 1 && $parent_type != 1) {
            $file .= '-平台商城订单';
            $header_0 = array('客户订单编号', '收件人姓名', '收件人手机', '收件省份(直辖市)', '收件市(州)', '收件区(县,县级市)', '收件详细地址', '收件人备注', '订单备注', '相关单号', '订单来源');

            $header_1 = array('客户订单编号', '客户货品编码', '销售平台货品编码', '数量', '备注');

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

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

                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'];
                            $v['type_info']['province_name'] = $address['province_name'];
                            $v['type_info']['city_name'] = $address['city_name'];
                            $v['type_info']['county_name'] = $address['county_name'];

                        } 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'] = '';
                            $v['type_info']['province_name'] = $v['type_info']['city_name'] = $v['type_info']['county_name'] = '';
                        }
                    }
                } 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'];
                            $v['type_info']['province_name'] = $address['province_name'];
                            $v['type_info']['city_name'] = $address['city_name'];
                            $v['type_info']['county_name'] = $address['county_name'];

                        } 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'] = '';
                            $v['type_info']['province_name'] = $v['type_info']['city_name'] = $v['type_info']['county_name'] = '';
                        }
                    }
                } else {
                    $info = Dever::db('area/province')->find($v['type_info']['province']);
                    $v['type_info']['province_name'] = $info['name'];

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

                    $info = Dever::db('area/county')->find($v['type_info']['county']);
                    $v['type_info']['county_name'] = $info['name'];
                }

                $body_0[] = array
                (
                    $v['order_num'],
                    $v['type_info']['name'],
                    $v['type_info']['mobile'],
                    $v['type_info']['province_name'],
                    $v['type_info']['city_name'],
                    $v['type_info']['county_name'],
                    $v['type_info']['address'],
                    '',
                    $v['info'],
                    '',
                    '',
                );

                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 = '';
                    }

                    if ($goods_info['price_type'] > 2 && isset($goods_info['goods']) && is_array($goods_info['goods'])) {
                    
                        foreach ($goods_info['goods'] as $v2) {
                            $p = $v2['price'];
                            if (is_numeric($v2['num']) && is_numeric($v2['price'])) {
                                $p = $v2['price']*$v2['num'];
                            }
                            $body_1[] = array
                            (
                                $v['order_num'],
                                $v2['code'],
                                $v2['name'],
                                $v2['num'],
                                '',
                            );
                        }
                    } else {
                        $body_1[] = array
                        (
                            $v['order_num'],
                            $goods_info['code'],
                            $goods_info['name'] . ' ' . $sku,
                            $v1['num'],
                            '',
                        );
                    }
                }
            }

            $xls = Dever::excelExport($body_0, $header_0, $file, 0, '订单主信息', true);

            if ($xls) {
                return Dever::excelExport($body_1, $header_1, $file, 1, '订单货品明细', false, $xls);
            }
        } else {
            

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

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

            if ($type == 2 && $source_type == 2) {
                $file .= '-仓库调拨订单';
                $header_0[4] = '仓库名称';
            } else {
                $file .= '-门店订单';
            }

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

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

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

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

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

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

                $body_0[] = array
                (
                    $v['order_num'],
                    '出库单',
                    $ps_date,
                    $v['info'],
                    $v['type_info']['name'],
                    $v['type_info']['truename'],
                    $v['type_info']['mobile'],
                    $v['type_info']['mobile'],
                    $v['type_info']['province_name'],
                    $v['type_info']['city_name'],
                    $v['type_info']['county_name'],
                    $v['type_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 = '';
                    }

                    if ($goods_info['price_type'] > 2 && isset($goods_info['goods']) && is_array($goods_info['goods'])) {
                    
                        foreach ($goods_info['goods'] as $v2) {
                            $p = $v2['price'];
                            if (is_numeric($v2['num']) && is_numeric($v2['price'])) {
                                $p = $v2['price']*$v2['num'];
                            }
                            $body_1[] = array
                            (
                                $v['order_num'],
                                $v2['code'],
                                $v2['num'],
                                '',
                                '',
                            );
                        }
                    } else {
                        $body_1[] = array
                        (
                            $v['order_num'],
                            $goods_info['code'],
                            $v1['num'],
                            '',
                            '',
                        );
                    }
                }
            }

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

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

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

        $header = array('所属客户', '所属仓库', '出库时间', '收货人编码', '包装箱数', '备注', '货位编码', '商品编码', '计量单位', '批号', '数量', '明细备注');

        $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'], 2);
            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 = '';
                }

                if ($goods_info['price_type'] > 2 && isset($goods_info['goods']) && is_array($goods_info['goods'])) {
                    
                    foreach ($goods_info['goods'] as $v2) {
                        $p = $v2['price'];
                        if (is_numeric($v2['num']) && is_numeric($v2['price'])) {
                            $p = $v2['price']*$v2['num'];
                        }
                        $d = array
                        (
                            '简单生活（中食）有限公司',
                            $v['type_info']['name'],
                            $ps_date,
                            $v['type_info']['sid'],
                            '',
                            $v['info'],
                            '',
                            $v2['code'],
                            $v2['unit'],
                            '',
                            $v2['num'],
                            $v2['name'],
                            
                        );
                        $body[] = $d;
                    }
                } else {
                    $d = array
                    (
                        '简单生活（中食）有限公司',
                        $v['type_info']['name'],
                        $ps_date,
                        $v['type_info']['sid'],
                        '',
                        $v['info'],
                        '',
                        $goods_info['code'],
                        $goods_info['unit'],
                        '',
                        $v1['num'],
                        $goods_info['name'] . ' ' . $sku,
                        
                    );
                    $body[] = $d;
                }

                $i++;
            }
        }

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