<?php

$status = array
(
    1 => '待审核',
    2 => '已审核',
    3 => '取消',
);

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

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

# 1是门店 2是仓库
$search = Dever::input('search_option_type', 1);

if ($search == 1) {
    $search_name = '门店名称';
    $search_option = $shop;
    $search_url = 'shop/lib/manage.search';
    $out_type = function()
    {
        $array = array();
        $info = Dever::db('shop/out_type')->getAll(array('type' => '1,10'));
        if($info)
        {
            $array += $info;
        }
        return $array;
    };
    $search_table = 'shop';
    $excel = array
    (
        array('导出订单', '门店出库订单', ''),
        array('导出订单明细', '门店出库订单明细', 'shop/excel.out_order'),
    );
} elseif ($search != 1) {
    $search_name = '仓库名称';
    $search_option = $store;
    $search_url = 'store/lib/manage.search';
    $out_type = function()
    {
        $array = array();
        $info = Dever::db('shop/out_type')->getAll(array('type' => '2,10'));
        if($info)
        {
            $array += $info;
        }
        return $array;
    };
    $search_table = 'store';
    $excel = array
    (
        array('导出订单', '仓库出库订单', ''),
        array('导出订单明细', '仓库出库订单明细', 'shop/excel.store_out_order'),
    );
}

return array
(
    # 表名
    'name' => 'out_order',
    # 显示给用户看的名称
    'lang' => '出库订单',
    'order' => 98,
    'config_type' => $out_type,
    'config_status' => $status,
    # 数据结构
    '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'      => '拥有人类型',
            'search'    => 'hidden',
            'match'     => 'is_numeric',
        ),

        'type_id'      => array
        (
            'type'      => 'int-11',
            'name'      => $search_name,
            'default'   => '',
            'desc'      => '拥有人',
            'match'     => 'is_numeric',
            //'search'    => $search == 3 ? '' : 'select',
            'search'    => $search == 3 ? '' : array
            (
                'api' => $search_table . '/info-like',
                'col' => 'name',
                'result' => 'id',
            ),
            //'option'    => $search_option,
            //'update_search' => $search_url,
            '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'      => $search == 1 ? true : false,
            'list_order' => 1,
        ),

        'shop-info-type'=> array
        (
            'name'      => '门店类型',
            'default'   => '',
            'desc'      => '门店类型',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('type_id', 'id'),
            'list'      => $search == 1 ? 'Dever::load("shop/lib/manage.shopInfoType", {shop-info-type})' : false,
            'list_order' => 3,
        ),

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

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

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

        'price'      => array
        (
            'type'      => 'varchar-50',
            'name'      => '出库总金额',
            'default'   => '',
            'desc'      => '出库总金额',
            'match'     => 'option',
            'update'    => 'text',
            'list_name'      => $search == 1 ? '出库总金额' : '出库总金额',
            'list'      => $search == 1 ? true : true,
            'list_order' => 5,
        ),

        'num'        => array
        (
            'type'      => 'int-11',
            'name'      => '出库数量',
            'default'   => '',
            'desc'      => '出库数量',
            'match'     => 'is_numeric',
            'search'    => 'select',
            'list'        => true,
            'list_order' => 5,
        ),

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

        'member_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '操作人',
            'default'   => '',
            'desc'      => '操作人',
            'match'     => 'option',
            //'update'  => 'text',
        ),

        'out_type'        => array
        (
            'type'      => 'int-11',
            'name'      => '出库类型',
            'default'   => '1',
            'desc'      => '类型',
            'match'     => 'is_numeric',
            'option'    => $out_type,
            'search'    => 'select',
            'list'      => true,
            'list_order'    => 7,
        ),

        'status'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '出库状态',
            'default'   => '1',
            'desc'      => '状态',
            'match'     => 'is_numeric',
            'option'    => $status,
            'search'    => 'select',
            'list'      => true,
            'list_order' => 8,
        ),

        'state'     => array
        (
            'type'      => 'tinyint-1',
            'name'      => '状态',
            'default'   => '1',
            'desc'      => '请选择状态',
            'match'     => 'is_numeric',
        ),
        
        'cdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '出库日期',
            'match'     => array('is_numeric', time()),
            'desc'      => '',
            # 只有insert时才生效
            'insert'    => true,
            'search'    => 'sdate',
            'list'        => 'date("Y-m-d H:i:s", {cdate})',
            'list_order'    => 6,
            //'list'      => 'Dever::load("service/lib/manage.showOrderTime", "{id}")',
        ),
    ),

    'alter' => array
    (
        2 => array
        (
            array('update', 'out_type', 'out_type', 'int-11 1 出库类型'),
        ),
        3 => array
        (
            array('update', 'num', 'num', 'float-11,2 0 出库数量'),
        ),
        'version' => 3,
    ),

    'manage' => array
    (
        'delete' => false,
        'edit' => false,
        'insert' => false,
        'excel' => 'mshop/lib/out.excel',
        'excel' => $excel,

        'button' => array
        (
            //'导出订单' => array('excel', 'mshop/lib/out.excel'),
        ),

        'list_button' => array(
            //'edit' => array('退款', 'status,tk_time,tk_pic,tk_desc,tk_admin', '{status} == 2'),
            //'delete' => array('删除', '', '{status} == 1'),
            //'fast_list' => array('查看详情', '"out_order_goods&project=shop&order_id={id}&page_type=1"'),
            'list' => array('查看详情', '"out_order_goods&project=shop&order_id={id}&page_type=1"'),

            'oper'  => array('审核', '"mshop/lib/out.yes?id={type_id}&order_id={id}"', '{status} == 1 && {type} == 2'),

            'location' => array('导出', Dever::url('mshop/lib/out.excel_one?type='.$search)),
        ),
    ),

    'request' => array
    (
        'getAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'order_num' => array('yes', 'like'),
                'type' => 'yes',
                'type_id' => 'yes',
                'out_type' => 'yes',
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('cdate' => 'desc'),
            'page' => array(10, 'list'),
            'col' => '*',
        ),

        'getData' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'order_num' => array('yes', 'like'),
                'type' => 'yes',
                'type_id' => 'yes',
                'out_type' => 'yes',
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('cdate' => 'desc'),
            'col' => '*',
        ),

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

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

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