<?php
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--	Application Market Inbound
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */


function funcMarketInboundAddProcess($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());


	$returns = funcMarketInboundAdd($params);

	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}
function funcMarketInboundAdd($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());

	/* 写入 */
	$inserts = array();
	if(isset($params['join_inbound_m_product_id']))$inserts['join_inbound_m_product_id'] = $params['join_inbound_m_product_id'];
	if(isset($params['join_inbound_m_customer_id']))$inserts['join_inbound_m_customer_id'] = $params['join_inbound_m_customer_id'];
	if(isset($params['join_inbound_m_site_id']))$inserts['join_inbound_m_site_id'] = $params['join_inbound_m_site_id'];
	if(isset($params['join_inbound_m_member_id']))$inserts['join_inbound_m_member_id'] = $params['join_inbound_m_member_id'];
	if(isset($params['join_inbound_operate_m_member_id']))$inserts['join_inbound_operate_m_member_id'] = $params['join_inbound_operate_m_member_id'];
	if(isset($params['m_inbound_status']))$inserts['m_inbound_status'] = $params['m_inbound_status'];
	if(isset($params['m_inbound_category']))$inserts['m_inbound_category'] = $params['m_inbound_category'];
	if(isset($params['m_inbound_card_num']))$inserts['m_inbound_card_num'] = $params['m_inbound_card_num'];
	if(isset($params['m_inbound_times']))$inserts['m_inbound_times'] = $params['m_inbound_times'];
	if(isset($params['m_inbound_num']))$inserts['m_inbound_num'] = $params['m_inbound_num'];
	if(isset($params['m_inbound_added']))$inserts['m_inbound_added'] = $params['m_inbound_added'];
	if(isset($params['m_inbound_order']))$inserts['m_inbound_order'] = $params['m_inbound_order'];
	if(isset($params['m_inbound_remark']))$inserts['m_inbound_remark'] = $params['m_inbound_remark'];
	if(isset($params['m_inbound_extend_json']))$inserts['m_inbound_extend_json'] = $params['m_inbound_extend_json'];
	if(isset($params['m_inbound_addtimes']))$inserts['m_inbound_addtimes'] = $params['m_inbound_addtimes'];
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_inbound"));
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('m_inbound_id' => $idx);
	}

	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketInboundUpdProcess($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());


	$returns = funcMarketInboundUpd($params);

	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}
function funcMarketInboundUpd($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());

	/* 写入 */
	$data = array();
	if(isset($params['join_inbound_m_product_id']))$data['join_inbound_m_product_id'] = $params['join_inbound_m_product_id'];
	if(isset($params['join_inbound_m_customer_id']))$data['join_inbound_m_customer_id'] = $params['join_inbound_m_customer_id'];
	if(isset($params['join_inbound_m_site_id']))$data['join_inbound_m_site_id'] = $params['join_inbound_m_site_id'];
	if(isset($params['join_inbound_m_member_id']))$data['join_inbound_m_member_id'] = $params['join_inbound_m_member_id'];
	if(isset($params['join_inbound_operate_m_member_id']))$data['join_inbound_operate_m_member_id'] = $params['join_inbound_operate_m_member_id'];
	if(isset($params['m_inbound_status']))$data['m_inbound_status'] = $params['m_inbound_status'];
	if(isset($params['m_inbound_category']))$data['m_inbound_category'] = $params['m_inbound_category'];
	if(isset($params['m_inbound_card_num']))$data['m_inbound_card_num'] = $params['m_inbound_card_num'];
	if(isset($params['m_inbound_times']))$data['m_inbound_times'] = $params['m_inbound_times'];
	if(isset($params['m_inbound_num']))$data['m_inbound_num'] = $params['m_inbound_num'];
	if(isset($params['m_inbound_added']))$data['m_inbound_added'] = $params['m_inbound_added'];
	if(isset($params['m_inbound_order']))$data['m_inbound_order'] = $params['m_inbound_order'];
	if(isset($params['m_inbound_remark']))$data['m_inbound_remark'] = $params['m_inbound_remark'];
	if(isset($params['m_inbound_extend_json']))$data['m_inbound_extend_json'] = $params['m_inbound_extend_json'];
	if(isset($params['m_inbound_addtimes']))$data['m_inbound_addtimes'] = $params['m_inbound_addtimes'];

	if(!isset($params['wheres'])){
		$params['wheres'] = "m_inbound_id = " . $params['m_inbound_id'];
	}

	$sql = executeSQL($data, array('mode' => "update", 'table' => "yx_market_inbound", 'where' => $params['wheres']));
	if(execSQL($sql)){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
	}

	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketInboundDelProcess($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());


	$returns = funcMarketInboundDel($params);

	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}
function funcMarketInboundDel($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array(), 'page' => array());

	/* 执行 */	
	$sql  = "delete from yx_market_inbound ";

	if(!isset($params['wheres'])){
		$sql .= "where m_inbound_id = " . $params['m_inbound_id'];
	}else{
		$sql .= "where " . $params['wheres'];
	}

	if(execSQL($sql)){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
	}
	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketInboundRead($params){
	global $configs;
	global $db;

	$returns = array('success' => false, 'errorcode' => '401', 'data' => array(), 'page' => array());

	$sql = "select * from yx_market_inbound";

	/* 查询条件 */
	if(isset($params['m_inbound_id']) && $params['m_inbound_id']){
		$wheres = " m_inbound_id = " . $params['m_inbound_id'];
	}else{
		$wheres = " (m_inbound_id > 0 or m_inbound_id is null)";
	}

	if(isset($params['join_inbound_m_product_id'])){
		$wheres .= " and (join_inbound_m_product_id = " . $params["join_inbound_m_product_id"] . ")";
	}

	if(isset($params['join_inbound_m_customer_id'])){
		$wheres .= " and (join_inbound_m_customer_id = " . $params["join_inbound_m_customer_id"] . ")";
	}

	if(isset($params['join_inbound_m_site_id'])){
		$wheres .= " and (join_inbound_m_site_id = " . $params["join_inbound_m_site_id"] . ")";
	}

	if(isset($params['join_inbound_m_member_id'])){
		$wheres .= " and (join_inbound_m_member_id = " . $params["join_inbound_m_member_id"] . ")";
	}

	if(isset($params['join_inbound_operate_m_member_id'])){
		$wheres .= " and (join_inbound_operate_m_member_id = " . $params["join_inbound_operate_m_member_id"] . ")";
	}

	if(isset($params['m_inbound_status'])){
		$wheres .= " and (m_inbound_status = '" . $params['m_inbound_status'] . "')";
	}

	if(isset($params['m_inbound_category'])){
		$wheres .= " and (m_inbound_category = '" . $params['m_inbound_category'] . "')";
	}

	if(isset($params['m_inbound_card_num'])){
		$wheres .= " and (m_inbound_card_num = '" . $params['m_inbound_card_num'] . "')";
	}

	if(isset($params['m_inbound_times'])){
		$wheres .= " and (m_inbound_times = '" . $params['m_inbound_times'] . "')";
	}

	if(isset($params['m_inbound_num'])){
		$wheres .= " and (m_inbound_num = " . $params["m_inbound_num"] . ")";
	}

	if(isset($params['m_inbound_added'])){
	}

	if(isset($params['m_inbound_order'])){
		$wheres .= " and (m_inbound_order = " . $params["m_inbound_order"] . ")";
	}

	if(isset($params['m_inbound_extend_json'])){
	}

	/* 外部其他条件 */
	if(isset($params['wheres']) && $params['wheres']){
		$wheres .= " and " . $params['wheres'];
	}

	/* 排序设置 */
	if(isset($params['orderby']) && $params['orderby']){
		$orders = " order by " . $params['orderby'];
	}else{
		$orders = " order by m_inbound_id desc";
	}

	/* 监测并获取规范数据 */
	$pagecount       = (isset($params['pagecount']) && $params['pagecount']) ? $params['pagecount'] : $configs['database']['config']['pagecount'];
	$returns['page'] = doPage($sql, $wheres);
	if($returns['page']['counts'] > 0){
		/* 当前页码 */
		$returns['page']['current'] = (isset($params['pageno'])) ? $params['pageno'] : 1;
		$limits = doPageSQL(array('pageno' => $params['pageno'], 'pagecount' => $pagecount, 'limit' => $params['limit']));

		if($returns['page']['counts'] > 0){
			/* 具备数据 */
			$dataSQL = "select * from (" . $sql . ") data where " . $wheres . $orders . $limits;
	
			$returns['data'] = doSQL($dataSQL);
	
			$returns['data'] = funcFormatResultData(array('data' => $returns['data'], 'datafield' => '', 'datatimes' => ''));
	
			$returns['success'] = true;
			$returns['errorcode'] = "200";
		}
	}
	//$returns['sql'] = "select * from (" . $sql . ") data where " . $wheres . $orders . $limits;

	$returns['msg'] = $configs['errors'][$returns['errorcode']];
	return $returns;
}

?>