Bladeren bron

复制健康E家改好的商城

lizhen_gitee 4 maanden geleden
bovenliggende
commit
99981005ec

+ 2 - 2
application/admin/controller/unishop/Category.php

@@ -100,12 +100,12 @@ class Category extends Backend
             if ($params) {
                 $params = $this->preExcludeFields($params);
 
-                if ($params['pid'] != $row['pid']) {
+                /*if ($params['pid'] != $row['pid']) {
                     $childrenIds = Tree::instance()->init(collection(\app\admin\model\unishop\Category::select())->toArray())->getChildrenIds($row['id']);
                     if (in_array($params['pid'], $childrenIds)) {
                         $this->error(__('Can not change the parent to child'));
                     }
-                }
+                }*/
 
                 try {
                     //是否采用模型验证

+ 290 - 40
application/admin/controller/unishop/Order.php

@@ -10,6 +10,7 @@ use think\Exception;
 use think\exception\PDOException;
 use think\exception\ValidateException;
 use think\Hook;
+use addons\epay\library\Service;
 
 /**
  * 订单管理
@@ -324,12 +325,13 @@ class Order extends Backend
             $result = false;
             Db::startTrans();
             try {
-                $express_number = $this->request->post('express_number');
-                $express_company = $this->request->post('express_company');
-                $have_delivered = $express_number ? time() : 0;
+                $express_number = $this->request->post('express_number','');
+                $express_company = $this->request->post('express_company','');
+//                $have_delivered = $express_number ? time() : 0;
+                $have_delivered = time();
                 $res1 = $row->allowField(true)->save(['have_delivered' => $have_delivered]);
                 $res2 = $row->extend->allowField(true)->save(['express_number' => $express_number, 'express_company' => $express_company]);
-                if ($res1 && $res2) {
+                if ($res1 !== false && $res2 !== false) {
                     $result = true;
                 } else {
                     throw new Exception(__('No rows were updated'));
@@ -354,8 +356,9 @@ class Order extends Backend
         }
         $address = json_decode($row->extend->address_json,true);
         if ($address) {
-            $area = (new Area)->whereIn('id',[$address['province_id'],$address['city_id'],$address['area_id']])->column('name', 'id');
-            $row['addressText'] = $area[$address['province_id']].$area[$address['city_id']].$area[$address['area_id']].' '.$address['address'];
+//            $area = (new Area)->whereIn('id',[$address['province_id'],$address['city_id'],$address['area_id']])->column('name', 'id');
+//            $row['addressText'] = $area[$address['province_id']].$area[$address['city_id']].$area[$address['area_id']].' '.$address['address'];
+            $row['addressText'] = $address['province_name'].$address['city_name'].$address['area_name'].$address['address'];
             $row['address'] = $address;
         }
 
@@ -382,13 +385,21 @@ class Order extends Backend
         if ($this->request->isPost()) {
             $this->success();
         }
-        $row = $this->model->get($ids, ['product','evaluate']);
+        $row = $this->model->get($ids, ['product','evaluate','extend']);
         $this->view->assign('product', $row->product);
         $evaluate = [];
         foreach ($row->evaluate as $key => $item) {
             $evaluate[$item['product_id']] = $item;
         }
 
+        //地址
+        $row['addressText'] = '';
+        $address = json_decode($row->extend->address_json,true);
+        if ($address) {
+            $row['addressText'] = $address['province_name'].$address['city_name'].$address['area_name'].$address['address'];
+            $row['address'] = $address;
+        }
+
         $this->view->assign('order', $row);
         $this->view->assign('evaluate', $evaluate);
         return $this->view->fetch();
@@ -404,45 +415,71 @@ class Order extends Backend
             $this->error(__('This order is not returned'));
         }
         if ($this->request->isPost()) {
-            $params = $this->request->post("row/a");
-            if ($params) {
-                $params = $this->preExcludeFields($params);
-                $result = false;
-                Db::startTrans();
-                try {
 
-                    // 退款
-                    if($params['refund_action'] == 1) {
-                        $params['had_refund'] = time();
-                        Hook::add('order_refund', 'addons\\unishop\\behavior\\Order');
-                    }
+            Db::startTrans();
+            try {
 
-                    $updatetime = $this->request->post('updatetime');
-                    // 乐观锁
-                    $result = $this->model->allowField(true)->save($params, ['id' => $ids, 'updatetime' => $updatetime]);
-                    if (!$result) {
-                        throw new Exception(__('Data had been update before saved, close windows and do it again'));
-                    }
+                $updatetime = $this->request->post('updatetime');
+                $refund_amount = $this->request->post('refund_amount');
+                $refund_status = $this->request->post('refund_status');
 
-                    Db::commit();
-                } catch (ValidateException $e) {
-                    Db::rollback();
-                    $this->error($e->getMessage());
-                } catch (PDOException $e) {
-                    Db::rollback();
-                    $this->error($e->getMessage());
-                } catch (Exception $e) {
-                    Db::rollback();
-                    $this->error($e->getMessage());
+                if ($refund_amount > $row['total_price']) {
+                    $this->error('退款金额不能大于实际支付金额');
                 }
-                if ($result !== false) {
-                    Hook::listen('order_refund', $row);
-                    $this->success();
-                } else {
-                    $this->error(__('No rows were updated'));
+
+                // 退款
+                $params = [
+                    'refund_status' => $refund_status,
+                    'updatetime' => time(),
+                    'refund_amount' => $refund_amount,
+                ];
+                if($refund_status == 3) {
+                    $params['had_refund'] = time();
+                }
+                // 乐观锁
+                $result = Db::name('unishop_order')->where(['id' => $ids, 'updatetime' => $updatetime])->update($params);
+                if (!$result) {
+                    throw new Exception(__('Data had been update before saved, close windows and do it again'));
+                }
+
+                //修改退款金额
+                $rs2 = Db::name('unishop_order_refund')->where('order_id',$ids)->update(['amount'=>$refund_amount]);
+                if ($rs2 === false) {
+                    throw new Exception('操作失败');
+                }
+
+                //同意并执行退款
+                if($refund_status == 3 && $refund_amount > 0){
+                    $order = Db::name('unishop_order')->where('id',$ids)->find();
+
+                    if($order['pay_type'] == 2){
+                        $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城售后订单退款'.$order['out_trade_no'],'unishop_order',$ids);
+                        if($wallet_rs['status'] === false){
+                            throw new Exception($wallet_rs['msg']);
+                        }
+                    }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
+                        $refund_result = $this->old_refund($order,$refund_amount);
+                        if($refund_result !== true){
+                            throw new Exception($refund_result);
+                        }
+                    }
+
                 }
+
+                Db::commit();
+            } catch (ValidateException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (PDOException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
             }
-            $this->error(__('Parameter %s can not be empty', ''));
+
+            $this->success();
+
         }
 
         $products = $row->product;
@@ -476,6 +513,219 @@ class Order extends Backend
     }
 
     /**
+     * 已支付未发货退款
+     */
+    public function cancelrefund($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if ($row['status'] != \app\admin\model\unishop\Order::STATUS_CANCEL || $row['have_paid'] == 0 || $row['have_delivered'] != 0) {
+            $this->error('订单状态错误');
+        }
+        if ($this->request->isPost()) {
+
+            Db::startTrans();
+            try {
+
+                $updatetime = $this->request->post('updatetime');
+                $refund_amount = $this->request->post('refund_amount');
+                $refund_status = $this->request->post('refund_status');
+
+                if ($refund_amount > $row['total_price']) {
+                    $this->error('退款金额不能大于实际支付金额');
+                }
+
+                // 退款
+                $params = [
+                    'refund_status' => $refund_status,
+                    'updatetime' => time(),
+                    'refund_amount' => $refund_amount,
+                ];
+                if($refund_status == 3) {
+                    $params['had_refund'] = time();
+                }
+                // 乐观锁
+                $result = Db::name('unishop_order')->where(['id' => $ids, 'updatetime' => $updatetime])->update($params);
+                if (!$result) {
+                    throw new Exception(__('Data had been update before saved, close windows and do it again'));
+                }
+
+                //修改退款金额
+                /*$rs2 = Db::name('unishop_order_refund')->where('order_id',$ids)->update(['amount'=>$refund_amount]);
+                if ($rs2 === false) {
+                    throw new Exception('操作失败');
+                }*/
+
+                //同意并执行退款
+                if($refund_status == 3 && $refund_amount > 0){
+                    $order = Db::name('unishop_order')->where('id',$ids)->find();
+
+                    if($order['pay_type'] == 2){
+                        $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城取消订单退款'.$order['out_trade_no'],'unishop_order',$ids);
+                        if($wallet_rs['status'] === false){
+                            throw new Exception($wallet_rs['msg']);
+                        }
+                    }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
+                        $refund_result = $this->old_refund($order,$refund_amount);
+                        if($refund_result !== true){
+                            throw new Exception($refund_result);
+                        }
+                    }
+
+                }
+
+                Db::commit();
+            } catch (ValidateException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (PDOException $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            } catch (Exception $e) {
+                Db::rollback();
+                $this->error($e->getMessage());
+            }
+
+            $this->success();
+
+        }
+
+        $this->view->assign('row', $row);
+        return $this->view->fetch();
+    }
+
+    // 退款
+    public function old_refund($order, $refund_price)
+    {
+
+        $table = 'unishop_order';
+        $remark = '订单退款';
+
+        if($order['pay_type'] == 3){
+            $order['pay_type'] = 'wechat';
+        }
+        if($order['pay_type'] == 4){
+            $order['pay_type'] = 'alipay';
+        }
+
+        // 生成退款单
+        $refund_data = [
+            'order_id' => $order['id'],
+            'out_refund_no'=> createUniqueNo('R'),
+            'pay_fee'  => $order['total_price'],
+            'refund_price' => $refund_price,
+            'pay_type' => $order['pay_type'],
+            'status' => 0,
+            'createtime' => time(),
+            'updatetime' => time(),
+            'table' => $table,
+            'table_id' => $order['id'],
+        ];
+        $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
+        if(!$refund_log_id){
+            return '退款失败';
+        }
+
+        if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
+            // 微信|支付宝退款
+
+            // 退款数据
+            $order_data = [
+                'out_trade_no' => $order['pay_out_trade_no']
+            ];
+
+            if ($order['pay_type'] == 'wechat') {
+                $total_fee = $order['total_price'] * 100;
+                $refund_fee = $refund_price * 100;
+
+                $order_data = array_merge($order_data, [
+                    'out_refund_no' => $refund_data['out_refund_no'],
+                    'total_fee' => $total_fee,
+                    'refund_fee' => $refund_fee,
+                    'refund_desc' => $remark,
+                ]);
+            } else {
+                $order_data = array_merge($order_data, [
+                    'out_request_no' => $refund_data['out_refund_no'],
+                    'refund_amount' => $refund_price,
+                ]);
+            }
+
+            //
+            if ($order['pay_type'] == 'wechat') {
+                $wxpay = new \app\common\library\Wxpay;
+                $result = $wxpay->WxPayRefund($order_data);
+                // 微信通知回调 pay->notifyr
+                if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
+                    Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
+                    return true;
+                } else {
+                    return $result['return_msg'];
+                }
+            } else {
+
+                $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
+
+                if($result['code'] == '10000'){
+                    Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
+                    return true;
+                }else{
+                    return $result['msg'];
+                }
+
+
+
+                /* return 'alipay wrong way';
+                 $alipay = new \app\common\library\AliPay;
+                 $result = $alipay->AliPayRefund($order_data);
+                 // 支付宝通知回调 pay->notifyx
+                 return $result;*/
+
+                /*if ($result['code'] == "10000") {
+                    return true;
+                } else {
+                    throw new \Exception($result['msg']);
+                }*/
+            }
+
+            // {        // 微信返回结果
+            //     "return_code":"SUCCESS",
+            //     "return_msg":"OK",
+            //     "appid":"wx39cd0799d4567dd0",
+            //     "mch_id":"1481069012",
+            //     "nonce_str":"huW9eIAb5BDPn0Ma",
+            //     "sign":"250316740B263FE53F5DFF50AF5A8FA1",
+            //     "result_code":"SUCCESS",
+            //     "transaction_id":"4200000497202004072822298902",
+            //     "out_trade_no":"202010300857029180027000",
+            //     "out_refund_no":"1586241595",
+            //     "refund_id":"50300603862020040700031444448",
+            //     "refund_channel":[],
+            //     "refund_fee":"1",
+            //     "coupon_refund_fee":"0",
+            //     "total_fee":"1",
+            //     "cash_fee":"1",
+            //     "coupon_refund_count":"0",
+            //     "cash_refund_fee":"1
+            // }
+
+            // {        // 支付宝返回结果
+            //     "code": "10000",
+            //     "msg": "Success",
+            //     "buyer_logon_id": "157***@163.com",
+            //     "buyer_user_id": "2088902485164146",
+            //     "fund_change": "Y",
+            //     "gmt_refund_pay": "2020-08-15 16:11:45",
+            //     "out_trade_no": "202002460317545607015300",
+            //     "refund_fee": "0.01",
+            //     "send_back_fee": "0.00",
+            //     "trade_no": "2020081522001464141438570535"
+            // }
+        }
+
+        return true;
+    }
+
+    /**
      * 回收站
      */
     public function recyclebin()

+ 2 - 2
application/admin/controller/unishop/Product.php

@@ -70,7 +70,7 @@ class Product extends Backend
                             throw new Exception('规格不能为空');
                         }
                     }
-                    $params['server'] = implode(',',$params['server']);
+//                    $params['server'] = implode(',',$params['server']);
 
                     $result = $this->model->allowField(true)->save($params);
                     Db::commit();
@@ -176,7 +176,7 @@ class Product extends Backend
                         }
                     }
 
-                    $params['server'] = implode(',',$params['server']);
+//                    $params['server'] = implode(',',$params['server']);
                     $result = $row->allowField(true)->save($params);
                     Db::commit();
                 } catch (ValidateException $e) {