123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- namespace Shop\Lib;
- use Dever;
- class Refund
- {
-
- public function set($type)
- {
- $this->type = $type;
- $this->order_table = 'shop/' . $type . '_order';
- $this->goods_table = 'shop/' . $type . '_order_goods';
- $this->refund_table = 'shop/' . $type . '_order_refund';
- if ($type == 'buy') {
- $this->lib = 'mshop/lib/' . $type;
- } else {
- $this->lib = 'shop/lib/' . $type;
- }
- return $this;
- }
-
- public function getList($type, $id, $order_id)
- {
- $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
- $where['order_id'] = $order_id;
- $data = Dever::db($this->refund_table)->select_page($where);
- if ($data) {
- $goods_status = Dever::db($this->goods_table)->config['status'];
- $refund_status = Dever::db($this->refund_table)->config['status'];
- $refund_process = Dever::db($this->refund_table)->config['process'];
- foreach ($data as $k => $v) {
- $data[$k]['cdate'] = date('Y-m-d H:i', $v['cdate']);
- $data[$k]['goods'] = Dever::db($this->goods_table)->getDataByIds(array('ids' => $v['order_goods_id']));
- $data[$k]['status_name'] = $refund_status[$v['status']];
- $data[$k]['process_name'] = $refund_process[$v['process']];
- if ($data[$k]['goods']) {
- foreach ($data[$k]['goods'] as $k1 => $v1) {
- $data[$k]['goods'][$k1]['info'] = Dever::load('goods/lib/info')->getPayInfo($v1['goods_id'], $v1['sku_id']);
- $data[$k]['goods'][$k1]['status_name'] = $goods_status[$v1['status']];
- }
- }
- }
- }
- return $data;
- }
-
- public function up($order_id, $order_goods_id, $status, $price, $num = false, $desc = '', $pic = '', $process = 1)
- {
- $data['order_id'] = $order_id;
- if (!$order_goods_id) {
- $where['order_id'] = $order_id;
- $goods = Dever::db($this->goods_table)->getIds($where);
- if ($goods) {
- $order_goods_id = implode(',', array_keys($goods));
- }
- $data['type'] = 1;
- } else {
- $data['type'] = 2;
- }
- $data['order_goods_id'] = $order_goods_id;
- $info = Dever::db($this->refund_table)->find($data);
- $data['status'] = $status;
- $data['cash'] = $price;
- if ($num) {
- $data['num'] = $num;
- }
- $data['desc'] = $desc;
- $data['pic'] = $pic;
- $data['process'] = $process;
- if ($info) {
- $data['where_id'] = $info['id'];
- $state = Dever::db($this->refund_table)->update($data);
- if ($state) {
- return $data;
- }
- } else {
- $state = Dever::db($this->refund_table)->insert($data);
- if ($state) {
- $data['id'] = $state;
- return $data;
- }
- }
- return false;
- }
-
- public function apply($type, $id, $order_id, $order_goods_id, $status = 1, $num = 0, $desc = '', $pic = '', $process = 2)
- {
- if ($status != 1 && $status != 2 && $status != 3) {
- Dever::alert('当前订单状态不允许退货退款');
- }
- $dec = $num;
- $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
- $auth = false;
- if ($this->type == 'sell' && $type == 1) {
- if ($data['status'] == 2) {
- $auth = true;
- }
- } else {
- $config = array(2,3,4,5,6);
- if (in_array($data['status'], $config)) {
- $auth = true;
- }
- }
- if ($auth) {
- if ($order_goods_id) {
- $info = Dever::db($this->goods_table)->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'status' => 1));
- if ($info) {
- Dever::db($this->goods_table)->update(array('where_id' => $info['id'], 'status' => 2));
- if (isset($info['coupon_cash']) && $info['coupon_cash']) {
- $cash = $info['price'] - $info['coupon_cash'];
- } else {
- $cash = $info['price'];
- }
- if ($num > 0) {
- $dec = $num;
- } else {
- $dec = $info['num'];
- }
-
- if ($num > 0 && $info['num'] >= $num) {
- $dec = $info['num'];
- $price = round($cash / $info['num'], 2);
- $cash = round($price * $num, 2);
- }
- } else {
- Dever::alert('您没有权限操作');
- }
- } else {
- $cash = $data['price'] - $data['refund_cash'];
- }
- if ($cash > 0) {
- $log = $this->up($data['id'], $order_goods_id, $status, $cash, $dec, $desc, $pic, $process);
- if ($this->type == 'buy' && ($data['status'] == 5 || $data['status'] == 6)) {
-
- $oper_data = Dever::db($this->goods_table)->getDataByIds(array('ids' => $log['order_goods_id']));
- Dever::load('shop/lib/goods')->oper($data, 2, 1, $oper_data);
- }
- $update = array();
- $update['where_id'] = $data['id'];
- $update['refund_cash'] = $data['refund_cash'] + $cash;
- $update['refund_status'] = 2;
- if ($dec > 0) {
- $update['num'] = $data['num'] - $dec;
- }
-
- Dever::db($this->order_table)->update($update);
- if ($id && $process == 2) {
- $this->action($log, $process, $data);
- }
- } else {
- Dever::alert('您没有可以退款的金额');
- }
- } else {
- Dever::alert('您没有权限操作');
- }
- return 'ok';
- }
-
- public function action($info, $process, $order = false)
- {
- if (!is_array($info)) {
- $info = Dever::db($this->refund_table)->find($info);
- }
- if (!$info) {
- Dever::alert('您没有权限操作');
- }
- if (!$order) {
- $order = Dever::db($this->order_table)->find($info['order_id']);
- }
- if (!$order) {
- Dever::alert('您没有权限操作');
- }
- $state = Dever::db($this->refund_table)->update(array('where_id' => $info['id'], 'process' => $process));
- if (!$state) {
- Dever::alert('操作失败');
- }
- $update = array();
- if ($process == 3) {
- if ($info['num'] && $info['num'] > 0) {
- $update['num'] = $order['num'] + $info['num'];
- }
- $update['refund_cash'] = $order['refund_cash'] - $info['cash'];
- $update['where_id'] = $order['id'];
- if ($update['refund_cash'] <= 0) {
- $update['refund_status'] = 1;
- }
-
- $state = Dever::db($this->order_table)->update($update);
- if ($info['type'] == 2) {
- Dever::db($this->goods_table)->update(array('where_id' => $info['order_goods_id'], 'status' => 1));
- }
- } else {
- if ($info['type'] == 2) {
- $state = Dever::db($this->goods_table)->update(array('where_id' => $info['order_goods_id'], 'status' => 3));
- if ($state) {
- $this->notice($state, $order, $info['cash']);
-
- $total = Dever::db($this->goods_table)->total(array('order_id' => $order['id'], 'status' => 1));
- if ($order['status'] == 5) {
- $update['status'] = 6;
- } elseif ($total <= 0) {
- $update['operdate'] = time();
- $update['status'] = 8;
- }
- if ($update) {
- $update['where_id'] = $order['id'];
- $state = Dever::db($this->order_table)->update($update);
- }
- }
- } else {
- $update['where_id'] = $order['id'];
- $update['operdate'] = time();
- $update['status'] = 8;
- $state = Dever::db($this->order_table)->update($update);
- if ($state) {
- $this->notice($state, $order, $info['cash']);
- if (isset($order['user_coupon_id']) && $order['user_coupon_id']) {
-
- Dever::db('shop/user_coupon')->update(array('where_id' => $order['user_coupon_id'], 'status' => 1));
- }
- }
- }
- }
- $data = Dever::db($this->goods_table)->getDataByIds(array('ids' => $info['order_goods_id']));
- if ($this->type == 'sell' && $process == 2) {
-
- Dever::load('shop/lib/goods')->oper($order, 2, 2, $data);
- } elseif ($this->type == 'buy' && $process == 3 && ($order['status'] == 5 || $order['status'] == 6)) {
-
- Dever::load('shop/lib/goods')->oper($order, 1, 1, $data);
- }
- return 'ok';
- }
-
- public function getInfo($type, $id, $order_id, $order_goods_id)
- {
- $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
- $info = Dever::db($this->goods_table)->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'status' => 1));
- if ($info) {
- if (isset($info['coupon_cash']) && $info['coupon_cash']) {
- $info['tui_price'] = $info['price'] - $info['coupon_cash'];
- } else {
- $info['tui_price'] = $info['price'];
- }
- $info['tui_one_price'] = round($info['tui_price'] / $info['num'], 2);
- $info['goods'] = Dever::db('goods/info')->find($info['goods_id']);
- }
- return $info;
- }
-
- public function notice($state, $data, $refund_cash)
- {
- if ($state && isset($data['uid']) && $data['uid'] > 0) {
- $shop = Dever::db('shop/info')->one($data['shop_id']);
- $msg_param['type'] = 1;
- $msg_param['id'] = $data['id'];
- $msg_param['name'] = $shop['name'];
- $msg_param = Dever::json_encode($msg_param);
- $msg = '您有一笔订单已退款,退款将在3个工作日内返回原支付账户';
- Dever::load('message/lib/data')->push(-1, $data['uid'], '退款成功通知', $msg, 2, 1, false, $msg_param);
-
- }
- }
- }
|