<?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 => '已申请',
);

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

$type = Dever::config('base')->source;
unset($type[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;
};

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

$stype = 1;
$noprice = 2;

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

if ($search_option_type == 1) {
    $search_name = '门店名称';
    $search_option = $shop;
    $search_url = 'shop/lib/manage.search';
    $search_table = 'shop';
    $search = $search_table . '/info-like';
    $excel = array
    (
        array('导出订单', '门店采购订单', ''),
        array('导出订单明细', '门店采购订单明细', 'shop/excel.buy_order'),
    );
    $source_name = '供货商';
    $source_order = 4;
} elseif ($search_option_type == 2) {
    $search_name = '仓库名称';
    $search_option = $store;
    $search_url = 'store/lib/manage.search';
    $search_table = 'store';
    $search = $search_table . '/info-like';
    $excel = array
    (
        array('导出订单', '仓库入库订单', ''),
        array('导出订单明细', '仓库入库订单明细', 'shop/excel.store_buy_order'),
    );
    $source_name = '供货商';
    $source_order = 4;
    $noprice = 1;
}

$search_option_source_type = Dever::input('search_option_source_type');
if ($search_option_source_type == 2) {
    $noprice = 1;
    if ($search_option_type == 2) {
        $source_name = '发货仓库';
        $search_name = '收货仓库';
        $source_order = -1;
        $excel = array
        (
            array('导出订单', '仓库调拨订单', ''),
            array('导出订单明细', '仓库调拨订单明细', 'shop/excel.store_buy_order_diao'),
        );
    } else {
        $source_name = '仓库名称';
        $source_order = -1;
        $excel = array
        (
            array('导出订单', '仓库发货订单', ''),
            array('导出订单明细', '仓库发货订单明细', 'shop/excel.store_buy_order_fa'),
        );
    }
} elseif ($search_option_source_type == 3) {
    $search_option_type = 3;
    $search_name = '收件人名称';
    $search_table = 'store';
    $search = $search_table . '/info-like';
    $source_name = '工厂名称';
    $source_order = -1;
    $search_option = $factory;
    $excel = array
    (
        array('导出订单', '工厂发货订单', ''),
        array('导出订单明细', '工厂发货订单明细', 'shop/excel.factory_buy_order_fa'),
    );
}

return 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' => 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'    => $search_option_source_type == 3 ? 'select' : 'hidden',
            'option'    => $type,
            'list'      => $search_option_source_type == 3 ? true : false,
            'list_order' => 3,
        ),

        'type_id'      => array
        (
            'type'      => 'int-11',
            'name'      => $search_name,
            'default'   => '',
            'desc'      => '拥有人',
            'match'     => 'is_numeric',
            //'search'    => $search_option_type == 3 ? '' : 'select',
            'search'    => $search_option_type == 3 ? '' : array
            (
                'api' => $search,
                'col' => 'name',
                'result' => 'id',
            ),
            'search'    => $search_option_type == 3 ? '' : 'select',
            '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'),
            'search'    => $search_option_type == 1 ? array
            (
                'api' => 'shop/info-like',
                'col' => 'sid',
                'result' => 'id',
                'search' => 'type_id',//本表的字段,默认为当前的字段
            ) : false,
            'list'      => $search_option_type == 1 ? true : false,
            'list_order' => 1,
        ),

        'shop-info-type'=> array
        (
            'name'      => '门店类型',
            'default'   => '',
            'desc'      => '门店类型',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('type_id', 'id'),
            
            'list'      => $search_option_type == 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'    => $search_option_type == 1 && !$search_option_source_type ? 'linkage' : false,
            'update'    => 'linkage',
            'option'    => Dever::url('lib/area.get', $search_table),
            //'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'      => $source_name,
            'default'   => '',
            'desc'      => '供货商',
            'match'     => 'is_numeric',
            'searchs'    => $search_option_source_type == 2 ? array
            (
                'api' => 'store/info-like',
                'col' => 'name',
                'result' => 'id',
            ) : array
            (
                'api' => 'factory/info-like',
                'col' => 'name',
                'result' => 'id',
            ),
            //'search'    => $search_option_type != 1 ? 'select' : false,
            'option'    => $search_option,
            'list'      => 'Dever::load("shop/lib/manage.buyInfo", "{source_type}", "{source_id}")',
            'list_order' => $source_order,
        ),

        '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,
        ),

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

        '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'      => $search_option_type == 1 ? true : false,
            'list_name'   => '订货单总金额',
            'list_order' => 7,
        ),

        'p_price'     => array
        (
            'type'      => 'varchar-50',
            'name'      => '购买价格-出厂价,给厂家看的',
            'default'   => '0',
            'desc'      => '购买价格',
            'match'     => 'option',
            'update'    => 'text',
            'list'      => $search_option_source_type == 3 ? true : false,
            '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'      => ($search_option_type == 1 || $search_option_source_type == 3) ? true : false,
            'list_name'   => '退款金额',
            'list_order' => 11,
        ),

        'refund_p_cash'      => array
        (
            'type'      => 'varchar-50',
            'name'      => '退款合计金额-出厂价格',
            'default'   => '0',
            'desc'      => '退款合计金额',
            'match'     => 'option',
            'update'    => 'text',
            'list'      => ($search_option_type == 2 || $search_option_source_type == 2 || $search_option_source_type == 3) ? 'Dever::load("shop/lib/manage.buyInfoRefundNum", "{id}")' : false,
            'list_name'   => '已退商品数量',
            'list_order' => 11,
        ),

        'refund_status'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '退款状态',
            'default'   => '1',
            'desc'      => '退款状态',
            'match'     => 'is_numeric',
            'option'    => $refund_status,
            //'search'    => 'select',
            'list'      => $search_option_type == 1 ? 'Dever::load("shop/lib/manage.buyInfoRefundStatus", "{id}")' : false,
            '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'      => ($search_option_type == 2 || $search_option_source_type == 2 || $search_option_source_type == 3) ? '"{shop-buy_order_ps-cdate}" ? date("Y-m-d H:i", {shop-buy_order_ps-cdate}) : "-"' : false,
            'list_order' => 9,
        ),

        'shop-buy_order_ps-ydate'=> array
        (
            'name'      => '收货日期',
            'default'   => '',
            'desc'      => '收货日期',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('id', 'order_id'),
            'list'      => $search_option_type == 2 ? '"{shop-buy_order_ps-ydate}" ? date("Y-m-d H:i", {shop-buy_order_ps-ydate}) : "-"': false,
            '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}")',
        ),
    ),

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

        'button' => array
        (
            //'导出订单' => array('location', 'user/lib/manage.out'),
        ),

        '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',
        ),
    ),
);