<?php
namespace Factory\Admin;

use Dever;

class Excel extends Core
{
    public function out($id, $user, $data)
    {
        if (!$data) {
            Dever::alert('无导出数据');
        }
        $info = Dever::db('factory/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_2($file, $user, $data, $file);
    }

    # 九阳模板
    public function out_2($file, $user, $data, $name = '')
    {
        $header = array();

        $header_0 = array('序号', '申请发货时间', '申请人', '产品名称', '数量', '订单号', '收件人姓名', '收件人电话', '收件地址', '发货时间', '发件物流', '运单编号', '备注');

        $header_1 = array('商品条码（必填）', '商品名称', '申请数量（必填）', '配销价(元）');

        $file = $name ? $name : '中食民安&九阳发货信息表';

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

            $service_name = array();
            $service_num = array();
            if (isset($v['ps_info']['service']) && $v['ps_info']['service']) {
                foreach ($v['ps_info']['service'] as $k1 => $v1) {
                    $service_name[] = $v1['service_name'];
                    $service_num[] = $v1['order_num'];
                }
            }

            $service_name = implode(',', $service_name);
            $service_num = implode(',', $service_num);

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

            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_0[] = array
                        (
                            $i,
                            $v['operdate_day'],
                            $user['name'],
                            $v2['name'],
                            $v2['num'],
                            $v['order_num'],

                            $v['type_info']['name'],
                            $v['type_info']['mobile'],
                            $v['type_info']['address'],

                            $ps_date,

                            $service_name,
                            $service_num,

                            $v['info'],
                            
                        );
                        $body_1[] = array
                        (
                            $v2['code'],
                            $v2['name'],
                            $v2['num'],
                            $v2['price'],
                        );

                        // $body[] = $d;
                    }
                } else {

                    $body_0[] = array
                    (
                        $i,
                        $v['operdate_day'],
                        $user['name'],
                        $goods_info['name'] . ' ' . $sku,
                        $v1['num'],
                        $v['order_num'],

                        $v['type_info']['name'],
                        $v['type_info']['mobile'],
                        $v['type_info']['address'],

                        $ps_date,

                        $service_name,
                        $service_num,

                        $v['info'],
                        
                    );
                    $body_1[] = array
                    (
                        $goods_info['code'],
                        $goods_info['name'],
                        $v1['num'],
                        $v1['price'],
                    );
                    // $body[] = $d;
                }

                $i++;
            }
        }
        $xls = Dever::excelExport($body_0, $header_0, $file, 0, '发货主信息', true);
        if($xls){
            return Dever::excelExport($body_1, $header_1, $file, 1 , '要货模板', false, $xls);
        }
        // return Dever::excelExport($body, $header, $file);
    }

    # 纯米模板
    public function out_3($file, $user, $data)
    {
        $header = array();

        $header_0 = array('序号', '申请人', '发货日期', '客户名称', '品名', '单位', '数量', '订单号', '收件人', '收件电话', '收件邮寄地址', '备注');
        $header_1 = array('商品条码（必填）', '商品名称', '申请数量（必填）', '配销价(元）');

        $file = '纯米发货信息表';

        $body_0 = $body_1 = 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']['truename'] = $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']['truename'] = $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']['truename'] = $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']['truename'] = $address['name'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $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_0[] = array
                        (
                            $i,
                            $user['name'],
                            $ps_date,
                            '中食民安（北京）',
                            $v2['name'],
                            $v2['unit'],
                            $v2['num'],

                            $v['order_num'],

                            $v['type_info']['truename'],
                            $v['type_info']['mobile'],
                            $v['type_info']['address'],

                            $v['info'],
                            
                        );
                        $body_1[] = array
                        (
                            $v2['code'],
                            $v2['name'],
                            $v2['num'],
                            $v2['price'],
                        );
                        // $body[] = $d;
                    }
                } else {

                    $body_0[] = array
                    (
                        $i,
                        $user['name'],
                        $ps_date,
                        '中食民安（北京）',
                        $goods_info['name'] . ' ' . $sku,
                        $goods_info['unit'],
                        $v1['num'],

                        $v['order_num'],

                        $v['type_info']['truename'],
                        $v['type_info']['mobile'],
                        $v['type_info']['address'],

                        $v['info'],
                        
                    );
                    $body_1[] = array
                    (
                        $goods_info['code'],
                        $goods_info['name'],
                        $v1['num'],
                        $v1['price'],
                    );

                    // $body[] = $d;
                }

                $i++;
            }
        }
        $xls = Dever::excelExport($body_0, $header_0, $file, 0, '发货主信息', true);
        if($xls){
            return Dever::excelExport($body_1, $header_1, $file, 1 , '要货模板', false, $xls);
        }
        // return Dever::excelExport($body, $header, $file);
    }
    # 安鲜达模板
    public function out_4($file, $user, $data, $name = '')
    {
        $file = $name ? $name : '安鲜达发货信息表';
        $body_0 = $body_1 = $body_2 = array();

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

        $header_0 = array('客户订单编号', '收件人姓名', '收件人手机', '收件省份(直辖市)', '收件市(州)', '收件区(县,县级市)', '收件详细地址', '收件人备注', '订单备注', '相关单号', '订单来源');

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

        $header_2 = 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']);
                if ($info) {
                    $v['type_info']['county_name'] = $info['name'];
                } else {
                    $v['type_info']['county_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'],
                            '',
                        );
                        $body_2[] = array
                        (
                            $v2['code'],
                            $v2['name'],
                            $v2['num'],
                            $v2['price'],
                        );
                    }
                } else {
                    $body_1[] = array
                    (
                        $v['order_num'],
                        $goods_info['code'],
                        $goods_info['name'] . ' ' . $sku,
                        $v1['num'],
                        '',
                    );
                    $body_2[] = array
                        (
                            $goods_info['code'],
                            $goods_info['name'],
                            $v1['num'],
                            $v1['price'],
                        );
                }
            }
        }

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

        if ($xls) {
            $xls =  Dever::excelExport($body_1, $header_1, $file, 1, '订单货品明细', true, $xls);
            return Dever::excelExport($body_2, $header_2, $file, 2, '要货模板', false, $xls);
        }
    }
}