<?php namespace Phpcmf\Model\Order;class Order extends \Phpcmf\Model
{
private $row;
public function info($id) {
$order = $this->table_site('order')->get($id);
return $order;
}
public function checkout($order, $field, $item) {
$save = [
'pay_id' => 0, 'pay_time' => 0,
'deliver_name' => '', 'deliver_sn' => '', 'deliver_time' => 0,
'receipt_time' => 0,
'status' => 1, 'inputtime' => SYS_TIME,
];
$data = array_merge($save, $order, $field);
$rt = $this->table_site('order')->insert($data);
if (!$rt['code']) {
return $rt;
}
$data['id'] = $rt['code'];
foreach ($item as $t) {
$rt = $this->table_site('order_item')->insert([
'oid' => $data['id'],
'cid' => $t['id'],
'mid' => $data['mid'],
'uid' => (int)$data['uid'],
'sell_uid' => (int)$t['uid'],
'price' => $t['price'],
'quantity' => $t['quantity'],
'sn' => $t['sn'],
'sku_name' => dr_array2string($t['sku_name']),
'sku_value' => $t['sku_value'],
'title' => $t['title'],
'thumb' => $t['thumb'],
'url' => $t['url'],
'status' => 0,
'inputtime' => 0,
]);
if (!$rt['code']) {
return $rt;
}
}
return dr_return_data($data['id'], 'ok');
}
public function get_item($oid) {
return $this->table_site('order_item')->where('oid', $oid)->getAll();
}
public function pay($order_id, $pay_id) {
if (!$order_id) {
log_message('error', '订单付款回调失败:order_id值不存在('.FC_NOW_URL.')');
return;
}
$order = $this->info($order_id);
if (!$order) {
log_message('error', '订单付款回调失败:订单('.$order_id.')不存在('.FC_NOW_URL.')');
return;
}
$order['pay_id'] = $pay_id;
$order['pay_time'] = SYS_TIME;
$order['status'] = 2;
$rt = $this->table_site('order')->update($order_id, [
'pay_id' => $order['pay_id'],
'pay_time' => $order['pay_time'],
'status' => $order['status'],
]);
if (!$rt['code']) {
log_message('error', '订单('.$order_id.')付款回调失败:'.$rt['msg'].'('.FC_NOW_URL.')');
return;
}
$config = \Phpcmf\Service::M('app')->get_config('order');
if (isset($config['module'][$order['mid']]['quantity']) && $config['module'][$order['mid']]['quantity']) {
$goods = $this->table_site('order_item')->where('oid', $order_id)->getAll();
if (!$goods) {
log_message('error', '订单('.$order_id.')库存计算失败:没有找到订单对应的商品('.FC_NOW_URL.')');
} else {
foreach ($goods as $t) {
$data = $this->table_site($order['mid'])->get($t['cid']);
if ($data) {
$update = [];
if ($t['sku_value']) {
$data['price_sku'] = dr_string2array($data['price_sku']);
if (!isset($data['price_sku']['value'][$t['sku_value']]) || !$data['price_sku']['value'][$t['sku_value']]) {
log_message('error', '订单('.$order_id.')库存计算失败:商品('.$order['nid'].'-'.$t['cid'].')sku信息查询失败('.FC_NOW_URL.')');
} else {
$data['price_sku']['value'][$t['sku_value']]['quantity'] = max(0, $data['price_sku']['value'][$t['sku_value']]['quantity'] - $t['quantity']);
$update = [
'price_sku' => dr_array2string($data['price_sku']),
'price_quantity' => max($data['price_quantity'] - $t['quantity'], 0),
];
}
}
!$update && $update = [
'price_quantity' => max($data['price_quantity'] - $t['quantity'], 0),
];
$this->table_site($order['mid'])->update($t['cid'], $update);
} else {
log_message('error', '订单('.$order_id.')库存计算失败:没有找到('.$order['nid'].'-'.$t['cid'].')商品('.FC_NOW_URL.')');
}
}
}
}
\Phpcmf\Service::L('Notice')->send_notice('order_pay', $order);
$uid = $order['uid'] = $order['sell_uid'];
$username = $order['username'] = $order['sell_username'];
\Phpcmf\Service::L('Notice')->send_notice('order_pay2', $order);
$order['uid'] = $uid;
$order['username'] = $username;
$step = intval($config['module'][$order['mid']]['ptype']);
switch ($step) {
case 1:
$order['uid'] = $order['sell_uid'];
$order['username'] = $order['sell_username'];
\Phpcmf\Service::L('Notice')->send_notice('order_fahuo', $order);
break;
case 2:
$this->fahuo($order, [
'deliver_sn' => '自动发货',
'deliver_name' => '自动发货',
]);
break;
case 3:
$order['uid'] = $order['sell_uid'];
$order['username'] = $order['sell_username'];
\Phpcmf\Service::L('Notice')->send_notice('order_fahuo', $order);
break;
default:
$this->shouhuo($order, '付款后自动完成订单');
break;
}
return dr_return_data($order['id'], 'ok');
}
public function shouhuo($order, $note) {
$order['status'] = 9; $order['receipt_time'] = SYS_TIME;
$rt = $this->table_site('order')->update($order['id'], [
'status' => $order['status'],
'receipt_time' => $order['receipt_time'],
]);
if (!$rt['code']) {
return $rt;
}
$this->db->table(SITE_ID.'_order_item')->where('oid', $order['id'])->update([
'status' => 1,
'inputtime' => SYS_TIME,
]);
\Phpcmf\Service::M('pay')->add_money($order['sell_uid'], $order['money']);
\Phpcmf\Service::M('pay')->add_paylog([
'uid' => $order['sell_uid'],
'username' => $order['sell_username'],
'touid' => '0',
'tousername' => '',
'mid' => 'order',
'title' => dr_lang('订单['.$order['sn'].']交易成功'),
'value' => $order['money'],
'type' => 'system',
'status' => 1,
'result' => '订单编号:'.$order['sn'],
'paytime' => SYS_TIME,
'inputtime' => SYS_TIME,
]);
\Phpcmf\Service::L('Notice')->send_notice('order_shouhuo', $order);
$uid = $order['uid'] = $order['sell_uid'];
$username = $order['username'] = $order['sell_username'];
\Phpcmf\Service::L('Notice')->send_notice('order_shouhuo2', $order);
$order['uid'] = $uid;
$order['username'] = $username;
$this->add_log($order, $note);
return dr_return_data($order['id'], 'ok');
}
public function add_log($order, $log) {
$this->table_site('order_log')->insert([
'oid' => (int)$order['id'],
'log' => (string)$log,
'status' => (int)$order['status'],
'inputtime' => SYS_TIME
]);
}
public function log($id) {
return $this->table_site('order_log')->where('oid', $id)->getAll();
}
public function fahuo($order, $data) {
$is_tx = 0;
$update = [];
if ($order['status'] != 3) {
$update['deliver_time'] = SYS_TIME;
$order['status'] = $update['status'] = 3;
$is_tx = 1;
}
$update['deliver_name'] = $data['deliver_name'];
$update['deliver_sn'] = $data['deliver_sn'];
$update['name'] = $data['name'];
$update['phone'] = $data['phone'];
$update['address'] = $data['address'];
$rt = $this->table_site('order')->update($order['id'], $update);
if (!$rt['code']) {
return $rt;
}
$this->add_log($order, $data['note']);
if ($is_tx) {
\Phpcmf\Service::L('Notice')->send_notice('order_fahuo', $order);
$config = \Phpcmf\Service::M('app')->get_config('order');
if (intval($config['module'][$order['mid']]['ptype']) == 1) {
$this->wancheng($order, '发货后自动完成订单');
}
}
return dr_return_data($order['id'], 'ok');
}
public function close($order, $note) {
if ($order['status'] == 0) {
return dr_return_data(0, dr_lang('订单已经关闭')); }
$rt = $this->table_site('order')->update($order['id'], [
'status' => 0,
]);
if (!$rt['code']) {
return $rt;
}
$order['status'] = 0;
$this->add_log($order, $note);
\Phpcmf\Service::L('Notice')->send_notice('order_close', $order);
if ($order['money'] > 0 && $order['pay_id'] == 1) {
$rt = $this->query('update `'.$this->dbprefix('member').'` set `money`=`money`+'.$order['money'].',`freeze`=`spend`-'.$order['money'].' where id='.$order['uid']);
if ($rt['code']) {
\Phpcmf\Service::M('pay')->add_paylog([
'mid' => 'order',
'uid' => $order['uid'],
'username' => $order['username'],
'touid' => 0,
'tousername' => '',
'title' => '订单退款',
'value' => $order['money'],
'type' => 'finecms',
'url' => dr_member_url('order/home/show', ['id' => $order['id']]),
'status' => 1,
'result' => '订单号:'.$order['sn'],
'paytime' => SYS_TIME,
]);
}
}
return dr_return_data($order['id'], 'ok');
}
public function get_field($config, $mid) {
$rt = [];
$field = \Phpcmf\Service::L('cache')->get_file('order-field-'.SITE_ID);
if (!$field) {
return [];
}
foreach ($field as $t) {
if (isset($config['field'][SITE_ID][$t['id']]) && in_array($mid, $config['field'][SITE_ID][$t['id']])) {
$t['setting'] = dr_string2array($t['setting']);
$rt[$t['fieldname']] = $t;
}
}
return $rt;
}
public function member_is_buy($uid, $mid, $cid, $sku = '') {
if (!$uid) {
return 0;
} elseif (!$mid) {
return 0;
} elseif (!$cid) {
return 0;
}
$db = $this->table_site('order_item')
->where('uid', $uid)
->where('mid', $mid)
->where('cid', $cid)
->where('status', 1);
if ($sku) {
$db->where('sku_value', $sku);
}
return $db->counts();
}
public function get_buy_items($mid, $cid, $num = '') {
if (!$mid) {
return [];
} elseif (!$cid) {
return [];
}
$db = $this->table_site('order_item')
->where('mid', $mid)
->where('cid', $cid)
->where('status', 1)->order_by('inputtime desc');
return $db->getAll($num);
}
public function cache($siteid = SITE_ID) {
$field = \Phpcmf\Service::M()->db->table('field')->where('disabled', 0)->where('relatedname', 'order')->where('relatedid', $siteid)->orderBy('displayorder ASC,id ASC')->get()->getResultArray();
\Phpcmf\Service::L('cache')->set_file('order-field-'.$siteid, $field);
}
}