123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php namespace Place_order\Lib\Source;
- use Dever;
- use Place;
- class Order
- {
- # 获取订单信息
- public function getInfo($info, $view = false)
- {
- if ($info['status'] == 1) {
- $m = 3600;
- # 支付倒计时
- $info['time'] = time() - $info['cdate'];
- if ($info['time'] >= $m) {
- # 已过期,自动取消
- $info['time'] = -1;
- $this->cancel($info);
- $info['status'] = 9;
- } else {
- $info['time'] = $m - $info['time'];
- }
- }
- $info['scash'] = $info['cash'];
- $info['cash'] = $info['cash'];
- $info['detail'] = $this->getDetail($info['id']);
- $info['info'] = '共' . intval($info['num']) . '件商品';
- if ($view) {
- $this->getView($info);
- }
- return $info;
- }
- # 获取订单里每个商品
- public function getDetail($id)
- {
- $result = Dever::db('source_detail', 'place_order')->select(['order_id' => $id], ['col' => 'id,name,pic,sku_name,sku_id,cash,num']);
- if ($result) {
- foreach ($result as &$v) {
- $v['cash'] = Dever::load('price', 'place')->getText($v['cash'], 3);
- }
- }
- return $result;
- }
- # 获取订单详情
- public function getView($info)
- {
- # 收货地址
- if ($info['address_id']) {
- $info['address'] = Dever::load('address', 'place_user')->getInfo($info['address_id'], $info['uid']);
- }
- if (in_[$info['status'], [3,4,5]]) {
- $info = Dever::load('source/express', 'place_order')->getInfo($info, $info['address_id']);
- $info['status_name'] = Dever::db('source', 'place_order')->value('status', $info['status']);
- }
- $info['status_list'] = [
- 1 => [
- 'selected' => false,
- 'name' => '待付款',
- 'time' => $info['cdate_str'],
- ],
- 2 => [
- 'selected' => false,
- 'name' => '待发货',
- 'time' => $info['pdate_str'] ?? '',
- ],
- 3 => [
- 'selected' => false,
- 'name' => '待签收',
- 'time' => $info['ddate_str'] ?? '',
- ],
- 4 => [
- 'selected' => false,
- 'name' => '待收货',
- 'time' => $info['qdate_str'] ?? '',
- ],
- 5 => [
- 'selected' => false,
- 'name' => '已完成',
- 'time' => $info['fdate_str'] ?? '',
- ],
- ];
- foreach ($info['status_list'] as $k => &$status_list) {
- if ($info['status'] >= $k) {
- $status_list['selected'] = true;
- }
- }
- if ($info['status'] == 1) {
- $info['status_msg'] = '下单成功,待支付';
- } elseif ($info['status'] == 2) {
- $info['status_msg'] = '支付成功,待发货';
- } elseif ($info['status'] == 3) {
- $info['status_msg'] = '配送中,等待签收';
- } elseif ($info['status'] == 4) {
- $info['status_msg'] = '已签收,等待确认';
- } elseif ($info['status'] == 5 || $info['status'] == 6) {
- $info['status_msg'] = '订单已完成';
- } elseif ($info['status'] > 6) {
- $info['status_msg'] = '订单已取消';
- }
-
- $info['refund'] = false;
- $info['refund_list'] = Dever::db('source_refund', 'place_order')->select(['order_id' => $info['id']]);
- if ($info['refund_list']) {
- foreach ($info['refund_list'] as $k => $v) {
- if ($v['status'] < 4) {
- $info['refund'] = $v;
- }
- }
- }
- if ($info['refund']) {
- if ($info['refund']['status'] > 2) {
- $info['refund'] = Dever::load('source/express', 'place_order')->getInfo($info['refund'], $info['address_id'], 2);
- }
- $info['refund']['cdate_str'] = date('Y-m-d H:i:s', $info['refund']['cdate']);
- if (isset($info['refund']['sdate']) && $info['refund']['sdate']) {
- $info['refund']['sdate_str'] = date('Y-m-d H:i:s', $info['refund']['sdate']);
- }
- if (isset($info['refund']['ddate']) && $info['refund']['ddate']) {
- $info['refund']['ddate_str'] = date('Y-m-d H:i:s', $info['refund']['ddate']);
- }
- if (isset($info['refund']['fdate']) && $info['refund']['fdate']) {
- $info['refund']['fdate_str'] = date('Y-m-d H:i:s', $info['refund']['fdate']);
- }
- if ($info['refund']['type'] == 1) {
- $info['refund_status_list'] = [
- 1 => [
- 'selected' => false,
- 'name' => '退货退款',
- 'time' => $info['refund']['cdate_str'],
- ],
- 2 => [
- 'selected' => false,
- 'name' => '已审核',
- 'time' => $info['refund']['sdate_str'] ?? '',
- ],
- 3 => [
- 'selected' => false,
- 'name' => '买家已发货',
- 'time' => $info['refund']['ddate_str'] ?? '',
- ],
- 4 => [
- 'selected' => false,
- 'name' => '已完成',
- 'time' => $info['refund']['fdate_str'] ?? '',
- ],
- ];
- }
- if ($info['refund']['type'] == 2) {
- $info['refund_status_list'] = [
- 1 => [
- 'selected' => false,
- 'name' => '仅退款',
- 'time' => $info['cdate_str'],
- ],
- 2 => [
- 'selected' => false,
- 'name' => '已审核',
- 'time' => $info['sdate_str'] ?? '',
- ],
- 4 => [
- 'selected' => false,
- 'name' => '已完成',
- 'time' => $info['fdate_str'] ?? '',
- ],
- ];
- }
- foreach ($info['refund_status_list'] as $k => $v) {
- if ($info['refund']['status'] >= $k) {
- $info['refund_status_list'][$k]['selected'] = true;
- }
- }
- }
- return $info;
- }
- # 取消订单
- public function cancel($order, $status = 9)
- {
- if ($order['status'] == 1) {
- $state = Dever::db('source', 'place_order')->update($order['id'], ['status' => $status, 'fdate' => time()]);
- if ($state) {
- # 恢复库存
- $this->resell($order['id']);
- }
- return 'ok';
- }
- }
- # 支付成功
- public function success($order, $status)
- {
- if ($status == 1) {
- $update['status'] = 2;
- $msg = '支付成功';
-
- } else {
- $update['status'] = 7;
- $msg = '支付失败';
- }
- $update['pdate'] = time();
- $state = Dever::db('source', 'place_order')->update($order['id'], $update);
- Dever::load('source/log', 'place_order')->up(1, $order['uid'], $data['order_id'], $msg);
- if ($state && $update['status'] == 7) {
- # 恢复库存
- $this->resell($order['id']);
- }
- return $state;
- }
- # 确认收货
- public function finish_commit(){}
- public function finish($type = 1, $order, $yes = false)
- {
- if ($yes || ($order['status'] >= 3 && $order['status'] <= 6)) {
- if ($type == 1) {
- $uid = $order['uid'];
- } else {
- $uid = '';
- }
- # 检查有没有退款
- $refund = Dever::db('source_refund', 'place_order')->find(['order_id' => $order['id'], 'refund_status' => 1]);
- $status = 5;
- if ($refund) {
- $status = 6;
- }
- Dever::db('source', 'place_order')->update($order['id'], ['status' => $status, 'fdate' => time()]);
- Dever::load('source/log', 'place_order')->up($type, $uid, $order['id'], '确认收货,订单完成');
- # 增加次数
- if ($status) {
- $detail = Dever::db('source_detail', 'place_order')->select(['order_id' => $order['id']]);
- if ($detail) {
- foreach ($detail as $k => $v) {
- $refund_detail = Dever::db('source_refund_detail', 'place_order')->select(['detail_id' => $v['id'], 'refund_status' => 1]);
- $unum = $v['unum'];
- if ($refund_detail) {
- foreach ($refund_detail as $v1) {
- $unum -= $v1['num'];
- }
- }
- if ($unum > 0) {
- Dever::load('source/user', 'place_order')->up($order['uid'], $v['source_id'], $v['sku_id'], 1, $unum);
- }
- }
- }
- # 获取当前用户上级 增加佣金 获取实付款
- //Dever::load('log', 'place_score')->add(Place::$uid, '推广', '购买');
- }
- }
- }
- # 恢复库存
- public function resell($id)
- {
- $result = Dever::db('source_detail', 'place_order')->select(['order_id' => $id]);
- if ($result) {
- foreach ($result as $v) {
- $state = Dever::load('stock', 'place_source')->sell($v['info_id'], $v['sku_id'], $v['num']*-1);
- }
- }
- }
- }
|