<?php

$status = array
(
    1 => '待支付',
    2 => '待审核',
    3 => '待发货',
    4 => '待收货确认',
    5 => '已完成',
    6 => '已完成(有退款)',
    7 => '已取消',
    8 => '已退款',
    11 => '已过期',
);

$out_status = array
(
    1 => '未导出',
    2 => '已导出',
);

$audit = array
(
    //1 => '待审核',
    2 => '审核通过',
    3 => '审核未通过',
);

$refund_status = array
(
    1 => '未申请',
    2 => '申请中',
    3 => '有退款',
    4 => '已退款',
);

$parent_type = array
(
    1 => '直接下单',
    2 => '平台店订单',
    3 => '代理商订单',
);

$excel = true;
$type = Dever::config('base')->source;
unset($type[3]);
$shop = function()
{
    $array = array
    (
        -1 => array('id' => 0, 'name' => '全部门店'),
    );
    $info = Dever::db('shop/info')->select();
    if($info)
    {
        $array += $info;
    }
    return $array;
};

$store = function()
{
    $array = array
    (
        -1 => array('id' => 0, 'name' => '全部仓库'),
    );
    $info = Dever::db('store/info')->select();
    if($info)
    {
        $array += $info;
    }
    return $array;
};

$store_list = function()
{
    $array = array();
    $info = Dever::db('store/info')->select();
    if($info)
    {
        $array += $info;
    }
    return $array;
};


$factory = function()
{
    $array = array
    (
        -1 => array('id' => 0, 'name' => '全部工厂'),
    );
    $info = Dever::db('factory/info')->select();
    if($info)
    {
        $array += $info;
    }
    return $array;
};

$stype = 1;
$noprice = 2;


$search_option_type = Dever::input('search_option_type');

$search_option_source_type = Dever::input('search_option_source_type');

$struct = array
(

    'id'        => array
    (
        'type'      => 'int-11',
        'name'      => 'ID',
        'default'   => '',
        'desc'      => '',
        'match'     => 'is_numeric',
        'search'    => 'order',
        //'list'      => true,
    ), 

    'type'      => array
    (
        'type'      => 'int-11',
        'name'      => '收件人类型',
        'default'   => '',
        'desc'      => '收件人类型',
        'match'     => 'is_numeric',
        'search'    => 'hidden',
        'option'    => $type,
        'list'      => false,
        'list_order' => 3,
    ),

    'type_id'      => array
    (
        'type'      => 'int-11',
        'name'      => '收件人',
        'default'   => '',
        'desc'      => '收件人',
        'match'     => 'is_numeric',
        'search'    => 'hidden',
        'list'      => 'Dever::load("shop/lib/manage.buyInfo", "{type}", "{type_id}")',
        'list_order' => 2,
    ),

    'shop-info-sid'=> array
    (
        'name'      => '门店编号',
        'default'   => '',
        'desc'      => '门店编号',
        'match'     => 'option',
        # 读取另外表的关联方式
        'sync'      => array('type_id', 'id'),
        'list_order' => 1,
    ),

    'shop-info-type'=> array
    (
        'name'      => '门店类型',
        'default'   => '',
        'desc'      => '门店类型',
        'match'     => 'option',
        # 读取另外表的关联方式
        'sync'      => array('type_id', 'id'),
        'list_order' => 3,
    ),

    'area'       => array
    (
        'type'      => 'varchar-500',
        'name'      => '所在街道',
        'default'   => '',
        'desc'      => '所在街道',
        'match'     => 'option',
        'update'    => 'linkage',
        //'list'      => 'Dever::load("area/api.string", "{area}")',
    ),

    'source_type'      => array
    (
        'type'      => 'int-11',
        'name'      => '供货商类型',
        'default'   => '',
        'desc'      => '供货商类型',
        'match'     => 'is_numeric',
        'search'    => 'hidden',
        'value'     => $search_option_source_type,
    ),

    'source_id'      => array
    (
        'type'      => 'int-11',
        'name'      => '供货商',
        'default'   => '',
        'desc'      => '供货商',
        'match'     => 'is_numeric',
        'list'      => 'Dever::load("shop/lib/manage.buyInfo", "{source_type}", "{source_id}")',
    ),

    'name'      => array
    (
        'type'      => 'varchar-800',
        'name'      => '订单名称',
        'default'   => '',
        'desc'      => '订单名称',
        'match'     => 'is_string',
        'update'    => 'text',
    ),

    'parent_type'      => array
    (
        'type'      => 'int-11',
        'name'      => '原订单类型',
        'default'   => '1',
        'desc'      => '原订单类型',
        'match'     => 'is_numeric',
        'option'    => $parent_type,
        'search'    => 'hidden',
    ),

    'parent_order_id'      => array
    (
        'type'      => 'int-11',
        'name'      => '原订单id',
        'default'   => '',
        'desc'      => '原订单id',
        'match'     => 'is_numeric',
        'search'    => 'hidden',
    ),

    'order_num'      => array
    (
        'type'      => 'varchar-100',
        'name'      => '订货单号',
        'default'   => '',
        'desc'      => '订货单号',
        'match'     => 'is_string',
        'update'    => 'text',
        'search'    => 'fulltext',
        'list'      => true,
        'list_order' => 5,
    ),

    'price'      => array
    (
        'type'      => 'varchar-50',
        'name'      => '购买价格-采购价',
        'default'   => '0',
        'desc'      => '购买价格',
        'match'     => 'option',
        'update'    => 'text',
        'list_name'   => '订货单总金额',
        'list_order' => 7,
    ),

    'p_price'     => array
    (
        'type'      => 'varchar-50',
        'name'      => '购买价格-出厂价,给厂家看的',
        'default'   => '0',
        'desc'      => '购买价格',
        'match'     => 'option',
        'update'    => 'text',
        'list_name'   => '订货单总金额',
        'list_order' => 7,
    ),

    'num'        => array
    (
        'type'      => 'int-11',
        'name'      => '购买数量',
        'default'   => '',
        'desc'      => '购买数量',
        'match'     => 'is_numeric',
        'search'    => 'select',
        'list'        => true,
        'list_name'   => '订货总数量',
        'list_order' => 6,
    ),

    'refund_cash'      => array
    (
        'type'      => 'varchar-50',
        'name'      => '退款合计金额-采购价格',
        'default'   => '0',
        'desc'      => '退款合计金额',
        'match'     => 'option',
        'update'    => 'text',
        'list_name'   => '退款金额',
        'list_order' => 11,
    ),

    'refund_p_cash'      => array
    (
        'type'      => 'varchar-50',
        'name'      => '退款合计金额-出厂价格',
        'default'   => '0',
        'desc'      => '退款合计金额',
        'match'     => 'option',
        'update'    => 'text',
        'list_name'   => '已退商品数量',
        'list_order' => 11,
    ),

    'refund_status'        => array
    (
        'type'      => 'tinyint-1',
        'name'      => '申请退款状态',
        'default'   => '1',
        'desc'      => '申请退款状态',
        'match'     => 'is_numeric',
        'option'    => $refund_status,
        'search'    => 'select',
        'list_name'   => '退款状态',
        'list_order' => 10,
    ),

    'info'      => array
    (
        'type'      => 'varchar-300',
        'name'      => '订单备注',
        'default'   => '',
        'desc'      => '订单备注',
        'match'     => 'option',
        'update'    => 'textarea',
    ),

    'status'        => array
    (
        'type'      => 'tinyint-1',
        'name'      => '状态',
        'default'   => '1',
        'desc'      => '状态',
        'match'     => 'is_numeric',
        'option'    => $status,
        'search'    => 'select',
        'list'      => 'Dever::load("mshop/lib/buy.orderStatus", "{id}")',
        'list_name'   => '订货单状态',
        'list_order' => 9,
    ),

    'out_status'        => array
    (
        'type'      => 'tinyint-1',
        'name'      => '导出状态',
        'default'   => '1',
        'desc'      => '导出状态',
        'match'     => 'is_numeric',
        'option'    => $out_status,
    ),

    'out_date'     => array
    (
        'type'      => 'int-11',
        'name'      => '导出时间',
        'default'   => '',
        'match'     => 'is_numeric',
        'desc'      => '',
    ),

    'source_out_status'        => array
    (
        'type'      => 'tinyint-1',
        'name'      => '导出状态',
        'default'   => '1',
        'desc'      => '导出状态',
        'match'     => 'is_numeric',
        'option'    => $out_status,
    ),

    'source_out_date'     => array
    (
        'type'      => 'int-11',
        'name'      => '导出时间',
        'default'   => '',
        'match'     => 'is_numeric',
        'desc'      => '',
    ),

    'audit'        => array
    (
        'type'      => 'tinyint-1',
        'name'      => '审核状态',
        'default'   => '2',
        'desc'      => '审核状态',
        'match'     => 'is_numeric',
        'option'    => $audit,
        //'search'    => 'select',
        'update'    => 'radio',
    ),

    'audit_desc'        => array
    (
        'type'      => 'varchar-500',
        'name'      => '未通过原因',
        'default'   => '',
        'desc'      => '未通过原因',
        'match'     => 'option',
        'update'    => 'textarea',
    ),

    'state'     => array
    (
        'type'      => 'tinyint-1',
        'name'      => '状态',
        'default'   => '1',
        'desc'      => '请选择状态',
        'match'     => 'is_numeric',
    ),

    'fdate'     => array
    (
        'type'      => 'int-11',
        'name'      => '完成时间',
        'default'   => '',
        'match'     => 'is_numeric',
        'desc'      => '',
    ),

    'operdate'     => array
    (
        'type'      => 'int-11',
        'name'      => '审核时间',
        'default'   => '',
        'match'     => 'is_numeric',
        'desc'      => '',
    ),

    'shop-buy_order_ps-cdate'=> array
    (
        'name'      => '发货日期',
        'default'   => '',
        'desc'      => '发货日期',
        'match'     => 'option',
        # 读取另外表的关联方式
        'sync'      => array('id', 'order_id'),
        'list_order' => 9,
    ),

    'shop-buy_order_ps-ydate'=> array
    (
        'name'      => '收货日期',
        'default'   => '',
        'desc'      => '收货日期',
        'match'     => 'option',
        # 读取另外表的关联方式
        'sync'      => array('id', 'order_id'),
        'list_order' => 9,
    ),
    
    'cdate'     => array
    (
        'type'      => 'int-11',
        'name'      => '下单时间',
        'match'     => array('is_numeric', time()),
        'desc'      => '',
        # 只有insert时才生效
        'insert'    => true,
        'search'    => 'sdate',
        'list'        => 'date("Y-m-d H:i", {cdate})',
        'list_name'   => '订货日期',
        'list_order' => 8,
        //'list'      => 'Dever::load("service/lib/manage.showOrderTime", "{id}")',
    ),
);

$struct['refund_p_cash']['list'] = 'Dever::load("shop/lib/manage.buyInfoRefundNum", "{id}")';
$struct['shop-buy_order_ps-cdate']['list'] = '"{shop-buy_order_ps-cdate}" ? date("Y-m-d H:i", {shop-buy_order_ps-cdate}) : "-"';

$button = array();

if ($search_option_type) {
    $struct['source_id']['name'] = '供货商';
    $struct['source_id']['list_order'] = 4;

    $struct['type_id']['search'] = 'select';

    if ($search_option_type == 1) {
        # 门店采购查询
        $excel = array
        (
            array('导出订单', '门店采购订单', ''),
            array('导出订单明细', '门店采购订单明细', 'shop/excel.buy_order'),
        );

        $struct['area']['option'] = Dever::url('lib/area.get', 'shop');
        $struct['area']['search'] = 'linkage';

        $struct['refund_p_cash']['list'] = false;
        $struct['shop-buy_order_ps-cdate']['list'] = false;

        $struct['type_id']['name'] = '门店名称';
        $struct['type_id']['search'] = array
        (
            'api' => 'shop/info-like',
            'col' => 'name',
            'result' => 'id',
        );

        $struct['shop-info-sid']['search'] = array
        (
            'api' => 'shop/info-like',
            'col' => 'sid',
            'result' => 'id',
            'search' => 'type_id',//本表的字段,默认为当前的字段
        );
        $struct['shop-info-sid']['list'] = true;
        $struct['shop-info-type']['list'] = 'Dever::load("shop/lib/manage.shopInfoType", {shop-info-type})';

        $struct['source_id']['search'] = array
        (
            'api' => 'shop/lib/manage.searchFName',
            'col' => 'name',
            'result' => 'id',
        );

        $struct['price']['list'] = true;
        $struct['refund_cash']['list'] = true;

        $struct['refund_status']['list'] = 'Dever::load("shop/lib/manage.buyInfoRefundStatus", "{id}")';

    } elseif ($search_option_type == 2) {
        $excel = array
        (
            array('导出订单', '仓库入库订单', ''),
            array('导出订单明细', '仓库入库订单明细', 'shop/excel.store_buy_order'),
        );
        $search_table = 'store';
        $struct['type_id']['name'] = '仓库名称';
        $struct['type_id']['option'] = $store;
        $struct['shop-buy_order_ps-ydate']['list'] = '"{shop-buy_order_ps-ydate}" ? date("Y-m-d H:i", {shop-buy_order_ps-ydate}) : "-"';

        $struct['source_type']['search'] = 'select';
        $struct['source_type']['option'] = array(2 => '仓库', 3 => '工厂');
        $struct['source_id']['search'] = array
        (
            'api' => 'shop/lib/manage.searchFName',
            'col' => 'name',
            'result' => 'id',
        );

        $noprice = 1;
    } elseif ($search_option_type == 3) {
        $struct['refund_p_cash']['list'] = false;
        $struct['type_id']['option'] = $factory;
    }
}

if ($search_option_source_type) {
    $struct['source_id']['name'] = '供货商';
    $struct['source_id']['list_order'] = -1;

    if ($search_option_source_type == 2) {
        $noprice = 1;

        if ($search_option_type == 2) {
            $button = array
            (
                '新建库存调拨单' => array('location', 'store/lib/manage.cashier'),
            );

            $struct['source_id']['name'] = '发货仓库';
            $struct['source_id']['search'] = 'select';
            $struct['source_id']['option'] = $store_list;
            $struct['type_id']['name'] = '收货仓库';
            $struct['type_id']['search'] = 'select';
            $struct['type_id']['option'] = $store_list;
            $excel = array
            (
                array('导出订单', '仓库调拨订单', ''),
                array('导出订单明细', '仓库调拨订单明细', 'shop/excel.store_buy_order_diao'),
            );
        } else {
            $struct['source_id']['name'] = '仓库名称';
            $struct['source_id']['search'] = 'select';
            $struct['source_id']['option'] = $store;
            $excel = array
            (
                array('导出订单', '仓库发货订单', ''),
                array('导出订单明细', '仓库发货订单明细', 'shop/excel.store_buy_order_fa'),
            );
        }

    } elseif ($search_option_source_type == 3) {
        $struct['type']['search'] = 'select';
        $struct['type']['list'] = true;

        $struct['source_id']['name'] = '工厂名称';
        $struct['source_id']['search'] = 'select';
        $struct['source_id']['option'] = $factory;

        $struct['type_id']['name'] = '收件人名称';
        $struct['type_id']['search'] = array
        (
            'api' => 'shop/lib/manage.searchName',
            'col' => 'name',
            'result' => 'id',
        );

        $struct['source_id']['list_order'] = -1;

        $struct['p_price']['list'] = true;

        $struct['type_id']['option'] = $factory;

        $excel = array
        (
            array('导出订单', '工厂发货订单', ''),
            array('导出订单明细', '工厂发货订单明细', 'shop/excel.factory_buy_order_fa'),
        );
    }
}


$config = array
(
    # 表名
    'name' => 'buy_order',
    # 显示给用户看的名称
    'lang' => '采购订单',
    'order' => 99,
    'status' => $status,
    'refund_status' => $refund_status,
    'config_type' => $type,
    'out_status' => $out_status,
    'end' => array
    (
        'insert' => 'shop/lib/manage.buyOrderUpdate',
        'update' => 'shop/lib/manage.buyOrderUpdate',
    ),
    # 数据结构
    'struct' => $struct,

    'manage' => array
    (
        'delete' => false,
        'edit' => false,
        'insert' => false,
        'excel' => $excel,

        'button' => $button,

        'list_button' => array(
            //'edit' => array('退款', 'status,tk_time,tk_pic,tk_desc,tk_admin', '{status} == 2'),
            //'delete' => array('删除', '', '{status} == 1'),

            'list' => array('查看详情', '"buy_order_goods&project=shop&order_id={id}&page_type=1&type='.$stype.'&noprice='.$noprice.'"'),

            //'oper'  => array('审核', '"mshop/lib/manage.audit?order_id={id}"', '{status} == 2'),
            'fast' => array('审核', '"buy_order&where_id={id}&col=audit,audit_desc&oper_save_jump=buy_order&oper_table=buy_order&oper_parent=buy_order"', '{status} == 2'),

            //'oper1'  => array('发货', '"mshop/lib/manage.send?order_id={id}"', '{status} == 3'),

            //'fast1' => array('发货', '"buy_order_ps&where_id={id}&search_option_order_id={id}&oper_save_jump=buy_order&oper_table=buy_order&oper_parent=buy_order"', '{status} == 3'),
        ),
    ),

    'alter' => array
    (
        2 => array
        (
            array('update', 'parent_order_id', 'parent_order_id', 'int-11 0 原订单id'),
        ),
        'version' => 2,
    ),

    'request' => array
    (
        'getAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'name' => array('yes', 'like'),
                'order_num' => array('yes', 'like'),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'parent_type' => 'yes',
                'out_status' => 'yes',
                'source_out_status' => 'yes',
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'start_o' => array('yes-operdate', '>='),
                'end_o' => array('yes-operdate', '<='),
                'start_f' => array('yes-fdate', '>='),
                'end_f' => array('yes-fdate', '<='),
                'status' => array('yes', 'in'),
                'refund_status' => 'yes',
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('cdate' => 'desc'),
            'page' => array(20, 'list'),
            'col' => '*',
        ),

        'getData' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'name' => array('yes', 'like'),
                'order_num' => array('yes', 'like'),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'parent_type' => 'yes',
                'out_status' => 'yes',
                'source_out_status' => 'yes',
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'start_o' => array('yes-operdate', '>='),
                'end_o' => array('yes-operdate', '<='),
                'start_f' => array('yes-fdate', '>='),
                'end_f' => array('yes-fdate', '<='),
                'status' => array('yes', 'in'),
                'refund_status' => 'yes',
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('cdate' => 'desc'),
            'col' => '*',
        ),

        # 获取订单数量
        'getOrderNum' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'start_o' => array('yes-operdate', '>='),
                'end_o' => array('yes-operdate', '<='),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'count',
            'col' => '*',
        ),

        # 获取总金额
        'getCashNum' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'start_o' => array('yes-operdate', '>='),
                'end_o' => array('yes-operdate', '<='),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'one',
            'col' => 'sum(price-refund_cash) as total',
        ),

        # 获取总金额
        'getPCashNum' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'start_o' => array('yes-operdate', '>='),
                'end_o' => array('yes-operdate', '<='),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'one',
            'col' => 'sum(p_price-refund_p_cash) as total',
        ),

        # 获取商品数量
        'getGoodsNum' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'start_o' => array('yes-operdate', '>='),
                'end_o' => array('yes-operdate', '<='),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'one',
            'col' => 'sum(num) as total',
        ),

        # 获取提交订单超过12个小时
        'getDataByTime' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'cdate' => array('yes', '<='),
                'status' => 'yes',
                'notice' => 'yes',
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('cdate' => 'desc'),
            'col' => '*',
        ),
        # 获取商品数量
        'getGoodsTotal' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'type' => 'yes',
                'type_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'one',
            'col' => 'sum(num) as total',
        ),

        'getDataToId' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'parent_type' => 'yes',
                'parent_order_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('cdate' => 'desc'),
            'col' => '*|id',
        ),
    ),
);

return $config;