最近的訂單比較雜,有些想過節(jié)后或者一個(gè)月后再確認(rèn)訂單,于是 寫了一個(gè)插件:訂單定期自動(dòng)確認(rèn)。領(lǐng)導(dǎo)說:ERP抓不到未被確定的訂單。這是什么鳥的ERP系統(tǒng)呀。
CREATE TABLE `order_auto_confirm` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`order_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`order_sn` VARCHAR(20) NOT NULL,
`execute_time` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`order_status` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0未確定,1已經(jīng)確定',
`addtime` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`update_time` INT(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE INDEX `order_id` (`order_id`),
INDEX `execute_time` (`execute_time`)
)
COMMENT='訂單定期自動(dòng)確定'
COLLATE='utf8_general_ci'
ENGINE=MyISAM;
/admin/order.php加入以下代碼:
elseif($_REQUEST['act'] == 'order_cron')
{
$act1 = empty($_POST['act1']) ? 0 : $_POST['act1'];
if(empty($act1) || !in_array($act1, array('add', 'cancel'))) make_json_response('', -1, '未知請求act1'); $order_id = intval($_POST['order_id']);
$order = order_info($order_id);
if(empty($order)) make_json_response('', -2, '沒有此訂單ID'); if($order['order_status']) make_json_response('', -3, '此訂單已經(jīng)確認(rèn),不用自動(dòng)確認(rèn)'); if($order['pay_status']) make_json_response('', -4, '此訂單支付狀態(tài)已經(jīng)變動(dòng),無法添加任務(wù)'); if($act1 == 'add'){
$order_cron_time = empty($_POST['order_cron_time']) ? 0 : $_POST['order_cron_time']; if(empty($order_cron_time)) make_json_response('', -10, '請求的時(shí)間錯(cuò)誤'); $sql = 'select order_id from '.$ecs->table('order_auto_confirm').' where order_id='.$order_id;
$rs = $db->getRow($sql);
if($rs['order_id'] == $order_id){
make_json_response('', -30, '此訂單任務(wù)已經(jīng)存在,不能重復(fù)添加');
}
$execute_time = local_strtotime($order_cron_time);
$sql = "insert into ".$ecs->table('order_auto_confirm')."(order_id, order_sn, execute_time, order_status, addtime) values(".$order_id.",'".$order['order_sn']."',".$execute_time.", 0, ".local_gettime().")";
$result = $db->query($sql);
if($result){
make_json_response('', 0, '');
}
make_json_response('', -9, '添加任務(wù)計(jì)劃失敗');
}elseif($act1 == 'cancel'){
$sql = 'delete from '.$ecs->table('order_auto_confirm').' where order_id='.$order_id.' and order_status=0 ';
$db->query($sql);
make_json_response('', 0, '');
}
}
在elseif($_REQUEST['act'] == 'info')里加入:
//取自動(dòng)確定訂單信息
$sql = 'select order_status, execute_time, addtime, update_time from '.$ecs->table('order_auto_confirm').' where order_id='.$order['order_id'];
$cron= $db->getRow($sql);
if(!empty($cron)){
if($cron['order_status'] == 1)
$cron['update_time'] = sprintf($_LANG['order_auto_croned'], local_date('Y-m-d H:i:s', $cron['update_time']));
else
$cron['execute_time']= sprintf($_LANG['order_auto_cron'], local_date('Y-m-d H:i:s', $cron['execute_time']));
}
$smarty->assign('cron', $cron);
/includes/modules/cron/order_auto_confirm.php
<?php
if (!defined('IN_ECS'))
{
die('Hacking attempt');
}
require_once(ROOT_PATH . 'includes/lib_order.php');
$cron_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/cron/order_auto_confirm.php';
if (file_exists($cron_lang))
{
global $_LANG; include_once($cron_lang);
}/* 模塊的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
$i = isset($modules) ? count($modules) : 0; /* 代碼 */
$modules[$i]['code'] = basename(__FILE__, '.php'); /* 描述對應(yīng)的語言項(xiàng) */
$modules[$i]['desc'] = 'order_auto_confirm_desc'; /* 作者 */
$modules[$i]['author'] = 'wjzhhr'; /* 網(wǎng)址 */
$modules[$i]['website'] = 'http://www.wodeqingchun.com'; /* 版本號 */
$modules[$i]['version'] = '1.0.0'; /* 配置信息 */
$modules[$i]['config'] = array(
array('name' => 'order_auto_confirm_count', 'type' => 'select', 'value' => '10'),
); return;
}
$time = gmtime();
//$time = local_gettime();
$limit = empty($cron['order_auto_confirm_count']) ? 5 : $cron['order_auto_confirm_count'];
$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('order_auto_confirm') . " WHERE execute_time <= ".$time." and order_status=0 LIMIT $limit";
$autodb= $db->getAll($sql);
$i = 0;
foreach ($autodb as $key => $val)
{
$order_id = $val['order_id'];
$order_sn = $val['order_sn'];
/* 標(biāo)記訂單為已確認(rèn) */
$update_status = update_order($order_id, array('order_status' => OS_CONFIRMED, 'confirm_time' => gmtime()));
update_order_amount($order_id); /* 記錄log */
$action_note = "計(jì)劃任務(wù):定期自動(dòng)確定訂單,訂單號:".$order_sn.",執(zhí)行狀態(tài):".($update_status ? '成功' : '失敗');
order_action($order_sn, OS_CONFIRMED, SS_UNSHIPPED, PS_UNPAYED, $action_note, 'system_cron'); /* 如果原來狀態(tài)不是“未確認(rèn)”,且使用庫存,且下訂單時(shí)減庫存,則減少庫存 */
if ($val['order_status'] != OS_UNCONFIRMED && $_CFG['use_storage'] == '1' && $_CFG['stock_dec_time'] == SDT_PLACE)
{
change_order_goods_storage($order_id, true, SDT_PLACE);
} if($update_status)
{
$i += 1;
$sql = "update " . $GLOBALS['ecs']->table('order_auto_confirm') . " set order_status=1, update_time=".$time." where order_id=".$order_id;
$db->query($sql);
}
}$string = '此次共更新:'.$i.'條數(shù)據(jù)';
echo $string;file_put_contents('./a.txt', $time . '----' . date('Y-m-d H:i:s').$string."/r/n", FILE_APPEND);
/**
* 更新訂單總金額
* @param int $order_id 訂單id
* @return bool
//zuimoban.com
*/
function update_order_amount($order_id)
{
include_once(ROOT_PATH . 'includes/lib_order.php');
//更新訂單總金額
$sql = "UPDATE " . $GLOBALS['ecs']->table('order_info') .
" SET order_amount = " . order_due_field() .
" WHERE order_id = '$order_id' LIMIT 1"; return $GLOBALS['db']->query($sql);
}
?>
/languages/zh_cn/cron/order_auto_confirm.php
<?php
global $_LANG;$_LANG['order_auto_confirm'] = '訂單定期自動(dòng)確定';
$_LANG['order_auto_confirm_desc'] = '定期自動(dòng)確定訂單';
$_LANG['order_auto_confirm_count'] = '每次處理記錄個(gè)數(shù)';
$_LANG['order_auto_confirm_count_range']['5'] = '5';
$_LANG['order_auto_confirm_count_range']['10']= '10';
$_LANG['order_auto_confirm_count_range']['15']= '15';
$_LANG['order_auto_confirm_count_range']['20']= '20';
?>
/languages/zh_cn/admin/order.php里加入:
//
$_LANG['order_auto_croned'] = '此訂單于 %s 已被確認(rèn)';
$_LANG['order_auto_cron'] = '此訂單于 %s 進(jìn)行定時(shí)確認(rèn)';
$_LANG['order_auto'] = '<font color=red>將此訂單加入自動(dòng)定時(shí)確認(rèn)</font>';
$_LANG['order_auto_time'] = '自動(dòng)確認(rèn)時(shí)間:';
/admin/themes/order_info.htm
在:{$lang.base_info}后面加入:
{if $order.status == 0 && $order.pay_status == 0 }
<script type="text/javascript" src="../js/calendar.php?lang={$cfg_lang}"></script>
<link href="../js/calendar/calendar.css" rel="stylesheet" type="text/css" />
<div id="order_auto_cron" style="display: inline-block; width: 300px;">
{if !$cron}
<a href="javascript:;" id="ccd" onclick="document.getElementById('select_time').style.display=''; this.style.display='none';">{$lang.order_auto}</a>
<span id="select_time" style="display: none;">{$lang.order_auto_time}
<input type="text" class="button" id="order_cron_time" value="" onclick="return showCalendar('order_cron_time', '%Y-%m-%d %H:%M:%S', '24', false, 'order_cron_time');" name="order_cron_time">
<input type="button" value="保存" id="ccd_save" class="button" onclick="order_cron({$order.order_id}, 'add');">
<a href="javascript:;" onclick="document.getElementById('select_time').style.display='none'; document.getElementById('ccd').style.display='';">{$lang.op_cancel}</a></span>
{elseif $cron.order_status == 0}
{$cron.execute_time} <a href="javascript:;" onclick="if(confirm('確定要?jiǎng)h除定時(shí)執(zhí)行任務(wù)嗎?')){order_cron({$order.order_id}, 'cancel');}else{return false;}">{$lang.op_cancel}</a>
{else $cron.order_status == 1}
{$cron.update_time}
{/if}
</div>
{/if}
在此頁面的JS里面加入:
function order_cron(order_id, act)
{
var order_cron_time = 0;
if(act == 'add'){
order_cron_time = document.getElementById('order_cron_time').value;
if(!order_cron_time){
alert('無法獲取時(shí)間');
return false;
}
}
Ajax.call('order.php?act=order_cron', 'order_id=' + order_id + '&act1=' + act + '&order_cron_time=' + order_cron_time, order_cron_response, 'POST', 'JSON');
}
function order_cron_response(res)
{
if (res.error == 0)
{
alert('保存成功');
}
else
{
alert(res.message);
}
return false;
}
還有/themes/default/footer.dwt里是否含有:{insert name='query_info'}這一句,比較重要,前人把這句去掉了,害最模板到處找原因。
共涉及5個(gè)文件,兩個(gè)新添加的
新聞熱點(diǎn)
疑難解答
圖片精選