<?php

$jstype = array
(
    1 => '订货结算',
    2 => '退款结算',
);

$status = array
(
    1 => '未入账',
    2 => '已入账',
);

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


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

$audit_type = array
(
	1 => '系统',
	2 => '人工',
);

$source = Dever::config('base')->source;

$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;
};

# 1是门店 2是仓库 但仓库没有结算单 3是工厂
$search = Dever::input('search_option_source_type', 1);

if ($search == 1) {
    $search_name = '结算门店';
    $search_option = $shop;
    $search_url = 'shop/lib/manage.search';
} elseif ($search == 2) {
    $search_name = '结算仓库';
    $search_option = $store;
    $search_url = 'store/lib/manage.search';
} if ($search == 3) {
    $search_name = '结算工厂';
    $search_option = $factory;
    $search_url = 'factory/lib/manage.search';
}

return array
(
    # 表名
    'name' => 'order',
    # 显示给用户看的名称
    'lang' => '结算单',
    'order' => 100,
    'config_jstype' => $jstype,
    'config_status' => $status,
    'config_audit' => $audit,
    'config_audit_type' => $audit_type,
    'config_source' => $source,
    'end' => array
    (
        'insert' => 'cash/lib/set.orderUpdate_commit',
        'update' => 'cash/lib/set.orderUpdate_commit',
    ),
    # 数据结构
    'struct' => array
    (
    
        'id'        => array
        (
            'type'      => 'int-11',
            'name'      => 'ID',
            'default'   => '',
            'desc'      => '',
            'match'     => 'is_numeric',
            'search'    => 'order',
            'list'      => true,
        ), 

        'order_num'      => array
        (
            'type'      => 'varchar-100',
            'name'      => '结算单号',
            'default'   => '',
            'desc'      => '结算单号',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => 'fulltext',
            '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',
            'option'    => $search_option,
            //'update_search' => $search_url,
            'list'      => $search == 3 ? false : 'Dever::load("shop/lib/manage.buyInfo", "{type}", "{type_id}")',
        ),

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

        'source_id'      => array
        (
            'type'      => 'int-11',
            'name'      => $search_name,
            'default'   => '',
            'desc'      => '供货商',
            'match'     => 'is_numeric',
            'search'    => $search == 3 ? 'select' : '',
            'option'    => $search_option,
            //'update_search' => $search_url,
            'list'      => $search == 3 ? 'Dever::load("shop/lib/manage.buyInfo", "{source_type}", "{source_id}")' : false,
        ),

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

        'source_order_num'      => array
        (
            'type'      => 'varchar-100',
            'name'      => '订货单编号',
            'default'   => '',
            'desc'      => '订货单编号',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => 'fulltext',
            'list'      => 'Dever::load("shop/lib/manage.getOrderUrl", "{source_order_id}", "{source_order_num}", "'.$search.'")',
        ),

        'refund_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '退款id',
            'default'   => '-1',
            'desc'      => '退款id',
            'match'     => 'is_numeric',
        ),

        'num'        => array
        (
            'type'      => 'int-11',
            'name'      => '数量',
            'default'   => '',
            'desc'      => '数量',
            'match'     => 'is_numeric',
            'list'      => true,
        ),

        'cash'        => array
        (
            'type'      => 'varchar-50',
            'name'      => '结算金额',
            'default'   => '0',
            'desc'      => '结算金额',
            'match'     => 'option',
            'update'    => 'text',
            'list'      => $search == 1 ? true : false,
        ),

        'p_cash'        => array
        (
            'type'      => 'varchar-50',
            'name'      => '结算金额',
            'default'   => '0',
            'desc'      => '结算金额',
            'match'     => 'option',
            'update'    => 'text',
            'list'      => $search == 3 ? true : false,
        ),

        'jstype'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '结算类型',
            'default'   => '1',
            'desc'      => '结算类型',
            'match'     => 'is_numeric',
            'option'    => $jstype,
            'search'    => 'select',
            'list'      => true,
        ),

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

        'audit'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '审核状态',
            'default'   => '1',
            'desc'      => '审核状态',
            'match'     => 'is_numeric',
            'option'    => $audit_1,
            'search'    => 'select',
            'update'    => 'radio',
            'list'      => 'Dever::load("cash/lib/set.info", "{audit_type}", "{audit}")',
        ),

        'audit_type'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '审核类型',
            'default'   => '1',
            'desc'      => '审核类型',
            'match'     => 'is_numeric',
            'option'    => $audit_type,
        ),

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

        'operdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '结算时间',
            'default'   => '',
            'match'     => 'is_numeric',
            'desc'      => '',
            'search'    => 'date',
            'list'      => '"{operdate}" ? date("Y-m-d H:i:s", {operdate}) : ""',
        ),

        'desc'        => array
        (
            'type'      => 'varchar-500',
            'name'      => '审核备注',
            'default'   => '',
            'desc'      => '审核备注',
            'match'     => 'option',
            'update'    => 'textarea',
            'list'      => true,
        ),

        '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'    => 'date',
            'list'        => 'date("Y-m-d H:i:s", {cdate})',
            //'list'      => 'Dever::load("service/lib/manage.showOrderTime", "{id}")',
        ),
    ),

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

        'page_list' => 'order_list',

        'list_button' => array
        (
            'fast' => array('审核', '"order&where_id={id}&col=audit,desc&oper_save_jump=order&oper_table=order&oper_parent=order"', '{audit_type} == 2 && {audit} == 1'),
        ),
    ),

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

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

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

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

        'getAllByStore' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'type' => array('yes-type', '=', 'and (( '),
                'type_id' => array('yes-type_id', '=', 'and )'),
                'source_type' => array('yes-source_type', '=', 'or ( '),
                'source_id' => array('yes-source_id', '=', 'and )) '),
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'page' => array(10, 'list'),
            'col' => '*',
        ),

        'getAllNoPage' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'col' => '*',
        ),

        'getAllByStoreNoPage' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'type' => array('yes-type', '=', 'and (( '),
                'type_id' => array('yes-type_id', '=', 'and )'),
                'source_type' => array('yes-source_type', '=', 'or ( '),
                'source_id' => array('yes-source_id', '=', 'and )) '),
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'col' => '*',
        ),

        'getDataByOrderId' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'type' => 'yes',
                'type_id' => 'yes',
                'source_type' => 'yes',
                'source_id' => 'yes',
                'source_order_id' => array('yes', 'in'),
                'status' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'col' => '*',
        ),
    ),
);
