dever 3 gadi atpakaļ
vecāks
revīzija
48a26b6807

+ 79 - 151
app/mshop/lib/Buy.php

@@ -8,6 +8,10 @@ use Dever;
 
 class Buy
 {
+    # 1是用户自己看,2是门店看
+    public $type = 1;
+    # 1是列表,2是详情
+    public $view = 1;
     # 获取配置
     public $config = array();
     # table
@@ -18,6 +22,16 @@ class Buy
         $this->config = Dever::db($this->table)->config;
     }
 
+    # 设置订单的类型
+    public function set($type, $view)
+    {
+        $this->type = $type;
+        $this->view = $view;
+
+        return $this;
+    }
+
+
     # 获取公共的where
     public function where($id)
     {
@@ -68,7 +82,7 @@ class Buy
             array('value' => 4, 'name' => '待收货确认'),
             array('value' => 5, 'name' => '已完成'),
             array('value' => 6, 'name' => '已完成(有退款)'),
-            array('value' => '7,8,9', 'name' => '已取消'),
+            array('value' => '7,8,11', 'name' => '已取消'),
         );
 
         return $result;
@@ -93,6 +107,16 @@ class Buy
         return $result;
     }
 
+    # 查看退款详情
+    public function getRefund($id, $order_id)
+    {
+        $data = $this->getView($id, $order_id, false);
+
+        $data['refund'] = Dever::db('shop/buy_order_refund')->select(array('order_id' => $info['id']));
+
+        return $data;
+    }
+
     # 获取订单详细信息
     public function getInfo($info, $view = false)
     {
@@ -117,15 +141,13 @@ class Buy
         $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
         $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
 
-        $info['tui'] = Dever::db('shop/buy_order_tui')->find(array('order_id' => $info['id'], 'order_goods_id' => -1));
-
         $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
+        $goods_process = Dever::db('shop/buy_order_refund')->config['process'];
         if ($view) {
             foreach ($info['goods'] as $k => $v) {
                 $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
                 $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
 
-                $info['goods'][$k]['tui'] = Dever::db('shop/buy_order_tui')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id']));
             }
         } else {
             foreach ($info['goods'] as $k => $v) {
@@ -133,11 +155,13 @@ class Buy
                 $info['goods'][$k]['name'] = $goods['name'];
                 $info['goods'][$k]['cover'] = $goods['cover'];
                 $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
-
-                $info['goods'][$k]['tui'] = Dever::db('shop/buy_order_tui')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id']));
             }
         }
 
+        if (!$info['refund_cash']) {
+            $info['refund_cash'] = 0;
+        }
+
         if ($info['fdate']) {
             $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
         }
@@ -147,6 +171,9 @@ class Buy
         if ($info['operdate']) {
             $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
         }
+        if ($info['shdate']) {
+            $info['shdate'] = date('Y-m-d H:i', $info['shdate']);
+        }
 
         return $info;
     }
@@ -325,76 +352,63 @@ class Buy
         }
     }
 
-    # 取消订单
-    public function cancel($id, $order_id)
+    # 确认收货
+    public function finish($id, $order_id)
     {
         $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 1) {
-            $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
+        if ($data['status'] <= 4) {
+            if ($data['refund_cash'] > 0) {
+                $status = 6;
+            } else {
+                $status = 5;
+            }
+            $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => $status, 'qsdate' => time()));
             if ($state) {
-                
+                # 增加库存
+                $where['order_id'] = $data['id'];
+                $where['status'] = 1;
+                $data = Dever::db('shop/buy_order_goods')->select($where);
+                if ($data) {
+                    Dever::load('shop/lib/goods')->oper(1, 1, $data);
+                }
             }
 
             return 'ok';
         } else {
-            Dever::alert('当前订单状态不允许取消');
+            Dever::alert('您没有权限');
         }
     }
 
-    # 同意退款
-    public function tui_yes_api()
+    # 取消订单
+    public function cancel($id, $order_id)
     {
-        $id = Dever::input('id');
-        $process = Dever::input('process', 3);
-        $info = Dever::db('shop/buy_order_tui')->find($id);
-
-        if ($info && $info['process'] == 1) {
-            $update['process'] = $process;
-            $update['where_id'] = $info['id'];
-            $state = Dever::db('shop/buy_order_tui')->update($update);
-
-            if ($state && $process == 2) {
-                $data = $this->getView($info['shop_id'], $info['order_id'], false);
-                if ($data) {
-                    if ($info['order_goods_id'] > 0) {
-                        $state = Dever::db('shop/buy_order_goods')->update(array('where_id' => $info['order_goods_id'], 'status' => 3));
-                        $this->pay_tui($state, $data, $info['cash']);
-
-                        # 检查这个订单下的商品是不是都退了
-                        $total = Dever::db('shop/buy_order_goods')->total(array('order_id' => $info['order_id'], 'shop_id' => $id, 'status' => 1));
-                        if ($total <= 0) {
-                            $state = Dever::db('shop/buy_order')->update(array('where_id' => $info['order_id'], 'status' => 8));
-                        }
-                    } else {
-                        # 全部退款
-                        $state = Dever::db('shop/buy_order')->update(array('where_id' => $info['order_id'], 'status' => 8));
-                        $this->pay_tui($state, $data, $info['cash']);
-                    }
-                }
-            }
+        $data = $this->getView($id, $order_id, false);
+        if ($data['status'] == 1) {
+            $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
+            return 'ok';
+        } else {
+            Dever::alert('当前订单状态不允许取消');
         }
-
-        return 'ok';
     }
 
     # 发货
-    public function fahuo_api()
+    public function send_api()
     {
         $id = Dever::input('id');
         $process = Dever::input('process', 3);
-        $info = Dever::db('shop/buy_order_tui')->find($id);
+        $info = Dever::db('shop/buy_order_refund')->find($id);
 
         if ($info && $info['process'] == 1) {
             $update['process'] = $process;
             $update['where_id'] = $info['id'];
-            $state = Dever::db('shop/buy_order_tui')->update($update);
+            $state = Dever::db('shop/buy_order_refund')->update($update);
 
             if ($state && $process == 2) {
                 $data = $this->getView($info['shop_id'], $info['order_id'], false);
                 if ($data) {
                     if ($info['order_goods_id'] > 0) {
                         $state = Dever::db('shop/buy_order_goods')->update(array('where_id' => $info['order_goods_id'], 'status' => 3));
-                        $this->pay_tui($state, $data, $info['cash']);
+                        $this->pay_refund($state, $data, $info['cash']);
 
                         # 检查这个订单下的商品是不是都退了
                         $total = Dever::db('shop/buy_order_goods')->total(array('order_id' => $info['order_id'], 'shop_id' => $id, 'status' => 1));
@@ -404,7 +418,7 @@ class Buy
                     } else {
                         # 全部退款
                         $state = Dever::db('shop/buy_order')->update(array('where_id' => $info['order_id'], 'status' => 8));
-                        $this->pay_tui($state, $data, $info['cash']);
+                        $this->pay_refund($state, $data, $info['cash']);
                     }
                 }
             }
@@ -413,107 +427,13 @@ class Buy
         return 'ok';
     }
 
-    # 申请退款
-    public function tui_action($shop_id, $order_id, $order_goods_id, $status, $price, $num = false, $desc = '', $pic = '')
-    {
-        $data['shop_id'] = $shop_id;
-        $data['order_id'] = $order_id;
-        $data['order_goods_id'] = $order_goods_id;
-        $info = Dever::db('shop/buy_order_tui')->find($data);
-
-        $data['status'] = $status;
-        $data['cash'] = $price;
-        if ($num) {
-            $data['num'] = $num;
-        }
-        $data['desc'] = $desc;
-        $data['pic'] = $pic;
-        
-        if ($info) {
-            $data['where_id'] = $info['id'];
-            $data['process'] = 1;
-            return Dever::db('shop/buy_order_tui')->update($data);
-        } else {
-            
-            return Dever::db('shop/buy_order_tui')->insert($data);
-        }
-    }
-
-    # 全部退款申请
-    public function tui($id, $order_id, $status = 1, $desc = '', $pic = '')
-    {
-        if ($status != 1 && $status != 2) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-
-        $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 2) {
-
-            $state = $this->tui_action($data['shop_id'], $data['id'], -1, $status, $data['price'], false, $desc, $pic);
-
-            return 'ok';
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-    }
-
-    # 获取部分退款的详情
-    public function tui_one_info($id, $order_id, $order_goods_id)
-    {
-        $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 4) {
-            $info = Dever::db('shop/buy_order_goods')->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'shop_id' => $id));
-            if ($info && $info['status'] <= 4) {
-                $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;
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-    }
-
-    # 部分退款申请
-    public function tui_one($id, $order_id, $order_goods_id, $num = false, $status = 6, $desc = '', $pic = '')
-    {
-        if ($status != 5 && $status != 6) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-
-        $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 4 || $data['status'] == 5) {
-            $info = Dever::db('shop/buy_order_goods')->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'shop_id' => $id));
-            if ($info && $info['status'] <= 4) {
-                $info['price'] = $info['price'];
-                if ($num > 0 && $info['num'] >= $num) {
-                    $price = round($info['price'] / $info['num'], 2);
-                    $info['price'] = round($price * $num, 2);
-                }
-
-                $state = $this->tui_action($data['shop_id'], $data['id'], $order_goods_id, $status, $info['price'], $num, $desc, $pic);
-            } else {
-                Dever::alert('当前订单状态不允许退货退款');
-            }
-            return 'ok';
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-    }
-
-    # 退款到原账户
-    public function pay_tui($state, $data, $price)
-    {
-
-    }
-
     # 查看订单状态
     public function orderStatus($id)
     {
         $config = Dever::db('shop/buy_order')->config;
 
         $info = Dever::db('shop/buy_order')->one($id);
-        $tk = Dever::db('shop/buy_order_tui')->find(array('order_id' => $info['id'], 'process' => 1));
+        $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
         $status = $config['status'][$info['status']];
 
         if ($tk) {
@@ -522,6 +442,14 @@ class Buy
         return $status;
     }
 
+    # 后台审核退款
+    public function refund_api()
+    {
+        $id = Dever::input('id');
+        $process = Dever::input('process');
+        return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
+    }
+
     # 展示订单详情
     public function show()
     {
@@ -533,12 +461,12 @@ class Buy
 
         $shop = Dever::db('shop/info')->find($info['shop_id']);
 
-        $tk_status = Dever::db('shop/buy_order_tui')->config['status'];
-        $tk_process = Dever::db('shop/buy_order_tui')->config['process'];
+        $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
+        $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
 
         $status = $config['status'][$info['status']];
 
-        $tk = Dever::db('shop/buy_order_tui')->find(array('order_id' => $info['id'], 'order_goods_id' => -1));
+        $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
         
         $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
 
@@ -554,7 +482,7 @@ class Buy
 
         </tr>';
 
-        $url = Dever::url('lib/buy.tui_yes', 'mshop');
+        $url = Dever::url('lib/buy.refund', 'mshop');
 
         if ($tk && $tk['process'] == 1) {
             $status = '申请' . $tk_status[$tk['status']];
@@ -566,12 +494,12 @@ class Buy
             }
             $html .= '<tr>
               <td>订单信息</td>
-              <td>'.$this->table(array('金额', '数量', '订单状态', '审核状态'), array(array($info['price'], $info['num'], $status, $process))).'</td>
+              <td>'.$this->table(array('金额', '退款金额', '数量', '订单状态', '审核状态'), array(array($info['price'], $info['refund_cash'], $info['num'], $status, $process))).'</td>
             </tr>';
         } else {
             $html .= '<tr>
               <td>订单信息</td>
-              <td>'.$this->table(array('金额', '数量', '订单状态'), array(array($info['price'], $info['num'], $status))).'</td>
+              <td>'.$this->table(array('金额', '退款金额', '数量', '订单状态'), array(array($info['price'], $info['refund_cash'], $info['num'], $status))).'</td>
             </tr>';
         }
         
@@ -590,7 +518,7 @@ class Buy
         foreach ($goods as $k => $v) {
             $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
             $status = $goods_status[$v['status']];
-            $tk = Dever::db('shop/buy_order_tui')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id']));
+            $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
             if ($tk && $tk['process'] == 1) {
                 $status = '申请' . $tk_status[$tk['status']];
                 //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';

+ 29 - 22
app/mshop/src/Buy.php

@@ -37,7 +37,7 @@ class Buy extends Core
     {
         $order_id = Dever::input('order_id');
 
-        return Dever::load('shop/lib/sell')->getView($this->shop_id, $order_id);
+        return Dever::load('shop/lib/buy')->getView($this->shop_id, $order_id);
     }
 
     # 确认订单页面
@@ -113,61 +113,68 @@ class Buy extends Core
     }
 
     # 取消订单
-    public function order_cancel()
+    public function order_cancel_commit()
     {
         $order_id = Dever::input('order_id');
 
         return Dever::load('mshop/lib/buy')->cancel($this->shop_id, $order_id);
     }
 
-    # 全部退款
-    public function order_tui()
+    # 获取订单退款记录
+    public function order_tui_log()
+    {
+        $order_id = Dever::input('order_id');
+
+        return Dever::load('shop/lib/refund')->set('buy')->getList($this->shop_id, $order_id);
+    }
+
+    # 货物未发出,仅退款
+    public function order_refund_commit()
+    {
+        $order_id = Dever::input('order_id');
+
+        return Dever::load('shop/lib/refund')->set('buy')->apply(1, $this->shop_id, $order_id, false, 3, 0, '', '', 2);
+    }
+
+    # 货物已收到 全部退款 报损 需要审核
+    public function order_tui_commit()
     {
         $order_id = Dever::input('order_id');
         $status = Dever::input('status');
         $desc = Dever::input('desc');
         $pic = Dever::input('pic');
 
-        return Dever::load('mshop/lib/buy')->tui($this->shop_id, $order_id, $status, $desc, $pic);
+        return Dever::load('shop/lib/refund')->set('buy')->apply(2, $this->shop_id, $order_id, false, $status, 0, $desc, $pic, 1);
     }
 
-    # 部分退款信息
+    # 部分退款信息 报损
     public function order_tui_one_info()
     {
         $order_id = Dever::input('order_id');
         $order_goods_id = Dever::input('order_goods_id');
 
-        return Dever::load('mshop/lib/buy')->tui_one_info($this->shop_id, $order_id, $order_goods_id);
+        return Dever::load('shop/lib/refund')->set('buy')->getInfo(2, $this->shop_id, $order_id, $order_goods_id);
     }
 
-    # 部分退款
-    public function order_tui_one()
+    # 部分退款 报损
+    public function order_tui_one_commit()
     {
         $order_id = Dever::input('order_id');
         $order_goods_id = Dever::input('order_goods_id');
-        $num = Dever::input('num');
+        $num = Dever::input('num', 0);
         $status = Dever::input('status');
         $desc = Dever::input('desc');
         $pic = Dever::input('pic');
 
-        return Dever::load('mshop/lib/buy')->tui_one($this->shop_id, $order_id, $order_goods_id, $num, $status, $desc, $pic);
+        return Dever::load('shop/lib/refund')->set('buy')->apply(2, $this->shop_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
     }
 
     # 收货确认
-    public function finish()
+    public function finish_commit()
     {
         $order_id = Dever::input('order_id');
 
-        $info = Dever::db('shop/buy_order')->find(array('shop_id' => $this->shop_id, 'id' => $order_id));
-
-        if ($info && $info['status'] == 4) {
-
-            $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 5, 'qsdate' => time()));
-
-            return 'ok';
-        } else {
-            Dever::alert('您没有权限');
-        }
+        return Dever::load('mshop/lib/buy')->finish($this->shop_id, $order_id);
     }
 
     # 测试审核拆单

+ 21 - 24
app/mshop/src/Data.php

@@ -96,7 +96,7 @@ class Data extends Core
     }
 
     # 核销取件码,完成
-    public function finish()
+    public function finish_commit()
     {
         $code = Dever::input('code');
 
@@ -104,9 +104,14 @@ class Data extends Core
 
         $info = Dever::db('shop/sell_order')->find(array('code' => $code, 'id' => $order_id));
 
-        if ($info && $info['shop_id'] == $this->shop_id && $info['status'] <= 3) {
+        if ($info && $info['shop_id'] == $this->shop_id && $info['status'] <= 4) {
 
-            $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 4, 'operdate' => time()));
+            if ($info['refund_cash'] > 0) {
+                $status = 6;
+            } else {
+                $status = 5;
+            }
+            $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => $status, 'operdate' => time()));
 
             # 给用户发消息
             if ($info['uid'] && $info['uid'] > 0) {
@@ -127,22 +132,22 @@ class Data extends Core
         }
     }
 
+    # 获取订单退款记录
+    public function order_tui_log()
+    {
+        $order_id = Dever::input('order_id');
+
+        return Dever::load('shop/lib/refund')->set('sell')->getList($this->shop_id, $order_id);
+    }
+
     # 门店全部退款
-    public function order_tui()
+    public function order_tui_commit()
     {
         $order_id = Dever::input('order_id');
         $status = Dever::input('status');
-        if ($status != 1 && $status != 2) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-        if ($status == 1) {
-            $status = 7;
-        } elseif ($status == 2) {
-            $status = 8;
-        }
         $desc = Dever::input('desc');
 
-        return Dever::load('shop/lib/sell')->set(2, 2)->tui($this->shop_id, $order_id, $status, $desc);
+        return Dever::load('shop/lib/refund')->set('sell')->apply(2, $this->shop_id, $order_id, false, $status, 0, $desc);
     }
 
     # 门店部分退款信息
@@ -151,27 +156,19 @@ class Data extends Core
         $order_id = Dever::input('order_id');
         $order_goods_id = Dever::input('order_goods_id');
 
-        return Dever::load('shop/lib/sell')->set(2, 2)->tui_one_info($this->shop_id, $order_id, $order_goods_id);
+        return Dever::load('shop/lib/refund')->set('sell')->getInfo(2, $this->shop_id, $order_id, $order_goods_id);
     }
 
     # 门店部分退款
-    public function order_tui_one()
+    public function order_tui_one_commit()
     {
         $order_id = Dever::input('order_id');
         $order_goods_id = Dever::input('order_goods_id');
         $num = Dever::input('num');
         $status = Dever::input('status');
         $desc = Dever::input('desc');
-        if ($status != 1 && $status != 2) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-        if ($status == 1) {
-            $status = 3;
-        } elseif ($status == 2) {
-            $status = 4;
-        }
 
-        return Dever::load('shop/lib/sell')->set(2, 2)->tui_one($this->shop_id, $order_id, $order_goods_id, $num, $status, $desc);
+        return Dever::load('shop/lib/refund')->set('sell')->apply(2, $this->shop_id, $order_id, $order_goods_id, $status, $num, $desc);
     }
 
     # 根据店铺获取商品列表

+ 1 - 1
app/mshop/src/My.php

@@ -229,7 +229,7 @@ class My extends Core
         $this->data['coupon'] = Dever::db('goods/coupon')->select();
 
         if ($this->data['coupon']) {
-            $this->data['day'] = Dever::db('goods/coupon')->config['time'];
+            $this->data['day'] = Dever::db('shop/coupon')->config['time'];
             foreach ($this->data['coupon'] as $k => $v) {
                 $this->data['coupon'][$k]['check'] = 1;
                 $check = Dever::db('shop/coupon')->find(array('shop_id' => $this->shop_id, 'coupon_id' => $v['id']));

+ 11 - 1
app/shop/database/buy_order.php

@@ -116,6 +116,16 @@ return array
             'list'        => true,
         ),
 
+        'refund_cash'      => array
+        (
+            'type'      => 'varchar-50',
+            'name'      => '退款合计金额',
+            'default'   => '0',
+            'desc'      => '退款合计金额',
+            'match'     => 'option',
+            'update'    => 'text',
+        ),
+
         'type'      => array
         (
             'type'      => 'int-11',
@@ -243,7 +253,7 @@ return array
             'desc'      => '',
         ),
 
-        'qsdate'     => array
+        'shdate'     => array
         (
             'type'      => 'int-11',
             'name'      => '确认收货时间',

+ 33 - 3
app/shop/database/buy_order_goods.php

@@ -3,8 +3,8 @@
 $status = array
 (
     1 => '正常',
-    2 => '缺货',
-    3 => '退款',
+    2 => '已申报',
+    3 => '退款',
 );
 
 return array
@@ -138,6 +138,36 @@ return array
 
     'request' => array
     (
-        
+        # 获取单条数据
+        'getIds' => array
+        (
+            # 匹配的正则或函数 选填项
+            'option' => array
+            (
+                'uid' => 'yes',
+                'shop_id' => 'yes',
+                'order_id' => 'yes',
+                'status' => 1,
+                'state' => 1,
+            ),
+            'type' => 'all',
+            'col' => '*|id',
+        ),
+
+        # 获取多条数据
+        'getDataByIds' => array
+        (
+            # 匹配的正则或函数 选填项
+            'option' => array
+            (
+                'ids' => array('yes-id', 'in'),
+                'uid' => 'yes',
+                'shop_id' => 'yes',
+                'order_id' => 'yes',
+                'state' => 1,
+            ),
+            'type' => 'all',
+            'col' => '*',
+        ),
     ),
 );

+ 24 - 4
app/shop/database/buy_order_tui.php → app/shop/database/buy_order_refund.php

@@ -1,22 +1,28 @@
 <?php
+$type = array
+(
+    1 => '整个订单',
+    2 => '单一商品',
+);
 
 $status = array
 (
     1 => '报损退款',
     2 => '无货退款',
+    3 => '仅退款',
 );
 
 $process = array
 (
-    1 => '申请退款中',
-    2 => '审核通过',
-    3 => '审核驳回',
+    1 => '申中',
+    2 => '已退款',
+    3 => '驳回',
 );
 
 return array
 (
     # 表名
-    'name' => 'buy_order_tui',
+    'name' => 'buy_order_refund',
     # 显示给用户看的名称
     'lang' => '采购退款申请',
     'menu' => false,
@@ -111,6 +117,20 @@ return array
             'place'     => '660*660',
         ),
 
+        'type'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '类型',
+            'default'   => '1',
+            'desc'      => '类型',
+            'match'     => 'is_numeric',
+            //'update'  => 'select',
+            'option'    => $type,
+            'search'    => 'select',
+            'list'      => true,
+            //'edit'      => true,
+        ),
+
         'status'        => array
         (
             'type'      => 'int-11',

+ 46 - 1
app/shop/database/goods.php

@@ -150,7 +150,7 @@ return array
     'request' => array
     (
         # 更新售出量
-        'updateSell' => array
+        'incSell' => array
         (
             'type' => 'update',
             'where' => array
@@ -164,6 +164,21 @@ return array
             ),
         ),
 
+        # 减少售出量
+        'decSell' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'shop_id' => 'yes',
+                'goods_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'sell_num' => array('yes', '-='),
+            ),
+        ),
+
         # 更新总库存
         'updateTotal' => array
         (
@@ -178,6 +193,36 @@ return array
             ),
         ),
 
+        # 增加总库存
+        'incTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'shop_id' => 'yes',
+                'goods_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '+='),
+            ),
+        ),
+
+        # 减少总库存
+        'decTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'shop_id' => 'yes',
+                'goods_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '-='),
+            ),
+        ),
+
         # 获取单条数据
         'getOne' => array
         (

+ 49 - 1
app/shop/database/goods_sku.php

@@ -171,7 +171,7 @@ $config = array
         ),
 
         # 更新售出量
-        'updateSell' => array
+        'incSell' => array
         (
             'type' => 'update',
             'where' => array
@@ -186,6 +186,22 @@ $config = array
             ),
         ),
 
+        # 减少售出量
+        'decSell' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'shop_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'sell_num' => array('yes', '-='),
+            ),
+        ),
+
         # 更新总库存
         'updateTotal' => array
         (
@@ -199,6 +215,38 @@ $config = array
                 'total_num' => array('yes', '+='),
             ),
         ),
+
+        # 增加总库存
+        'incTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'shop_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '+='),
+            ),
+        ),
+
+        # 减少总库存
+        'decTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'shop_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '-='),
+            ),
+        ),
     ),
 );
 

+ 20 - 30
app/shop/database/sell_order.php

@@ -23,18 +23,18 @@ $pay_type = array
 
 $status = array
 (
-    1 => '待支付',//买家待支付
-    2 => '待处理',//买家已支付,待商家处理
-    3 => '配送中',//商家已处理,配送中或者已配货
-    4 => '已完成',//买家已确认
-    5 => '已取消',//买家已取消
-    6 => '已退款',//买家退款,当status==2时,买家可以申请退款
-    7 => '仅退款',//商家退款
-    8 => '退货退款',//商家退款
-    11 => '已过期',//长时间未支付,自动过期
+    1 => '待支付',
+    2 => '待处理',
+    3 => '处理中',
+    4 => '待收货确认',
+    5 => '已完成',
+    6 => '已完成(有退款',
+    7 => '已取消',
+    8 => '已退款',
+    11 => '已过期',
 );
 
-//1待支付2支付成功3处理中4已完成5已取消6已退款7仅退款8退货退款11已过期
+//1待支付2支付成功3处理中4待确认5已完成6已完成(有退款)7已取消8已退款11已过期
 
 return array
 (
@@ -173,6 +173,16 @@ return array
             //'list'        => true,
         ),
 
+        'refund_cash'      => array
+        (
+            'type'      => 'varchar-50',
+            'name'      => '退款合计金额',
+            'default'   => '0',
+            'desc'      => '退款合计金额',
+            'match'     => 'option',
+            'update'    => 'text',
+        ),
+
         'kou_cash'      => array
         (
             'type'      => 'varchar-50',
@@ -263,26 +273,6 @@ return array
             'option'    => $pay_type,
         ),
 
-        'tk_cash'      => array
-        (
-            'type'      => 'varchar-300',
-            'name'      => '退款金额',
-            'default'   => '',
-            'desc'      => '退款金额',
-            'match'     => 'option',
-            'update'    => 'text',
-        ),
-
-        'tk_desc'      => array
-        (
-            'type'      => 'varchar-300',
-            'name'      => '退款描述',
-            'default'   => '',
-            'desc'      => '退款描述',
-            'match'     => 'option',
-            'update'    => 'textarea',
-        ),
-
         'info'      => array
         (
             'type'      => 'varchar-300',

+ 33 - 24
app/shop/database/sell_order_goods.php

@@ -3,9 +3,8 @@
 $status = array
 (
     1 => '正常',
-    2 => '缺货',
-    3 => '仅退款',
-    4 => '退款退货',
+    2 => '申请退款中',
+    3 => '已退款',
 );
 
 return array
@@ -112,26 +111,6 @@ return array
             'list'        => true,
         ),
 
-        'tk_cash'      => array
-        (
-            'type'      => 'varchar-300',
-            'name'      => '退款金额',
-            'default'   => '',
-            'desc'      => '退款金额',
-            'match'     => 'option',
-            'update'    => 'text',
-        ),
-
-        'tk_desc'      => array
-        (
-            'type'      => 'varchar-300',
-            'name'      => '退款描述',
-            'default'   => '',
-            'desc'      => '退款描述',
-            'match'     => 'option',
-            'update'    => 'textarea',
-        ),
-
         'coupon_id'      => array
         (
             'type'      => 'int-11',
@@ -205,6 +184,36 @@ return array
 
     'request' => array
     (
-        
+        # 获取单条数据
+        'getIds' => array
+        (
+            # 匹配的正则或函数 选填项
+            'option' => array
+            (
+                'uid' => 'yes',
+                'shop_id' => 'yes',
+                'order_id' => 'yes',
+                'status' => 1,
+                'state' => 1,
+            ),
+            'type' => 'all',
+            'col' => '*|id',
+        ),
+
+        # 获取多条数据
+        'getDataByIds' => array
+        (
+            # 匹配的正则或函数 选填项
+            'option' => array
+            (
+                'ids' => array('yes-id', 'in'),
+                'uid' => 'yes',
+                'shop_id' => 'yes',
+                'order_id' => 'yes',
+                'state' => 1,
+            ),
+            'type' => 'all',
+            'col' => '*',
+        ),
     ),
 );

+ 191 - 0
app/shop/database/sell_order_refund.php

@@ -0,0 +1,191 @@
+<?php
+
+$type = array
+(
+    1 => '整个订单',
+    2 => '单一商品',
+);
+
+$status = array
+(
+    1 => '仅退款',
+    2 => '退货退款',
+);
+
+$process = array
+(
+    1 => '申请退款中',
+    2 => '审核通过',
+    3 => '审核驳回',
+);
+
+return array
+(
+    # 表名
+    'name' => 'sell_order_refund',
+    # 显示给用户看的名称
+    'lang' => '订单退款申请',
+    'menu' => false,
+    'status' => $status,
+    'process' => $process,
+    # 数据结构
+    'struct' => array
+    (
+    
+        'id'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => 'ID',
+            'default'   => '',
+            'desc'      => '',
+            'match'     => 'is_numeric',
+            'search'    => 'order',
+            'update'    => 'hidden',
+            //'list'        => true,
+        ),
+
+        'shop_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '所属门店',
+            'default'   => '',
+            'desc'      => '所属门店',
+            'match'     => 'is_numeric',
+            'update'    => 'hidden',
+            'value'     => Dever::input('search_option_shop_id'),
+            'list'      => 'Dever::load("shop/info-find#name", {shop_id})',
+        ),
+
+        'order_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '订单表id',
+            'default'   => '',
+            'desc'      => '订单表id',
+            'match'     => 'is_numeric',
+        ),
+
+        'order_goods_id'      => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '订单商品表id',
+            'default'   => '',
+            'desc'      => '订单商品表id',
+            'match'     => 'option',
+        ),
+
+        'cash'      => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '退款金额',
+            'default'   => '',
+            'desc'      => '退款金额',
+            'match'     => 'option',
+            'update'    => 'text',
+        ),
+
+        'num'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '退款数量',
+            'default'   => '',
+            'desc'      => '退款数量',
+            'match'     => 'is_numeric',
+        ),
+
+        'desc'      => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '退款描述',
+            'default'   => '',
+            'desc'      => '退款描述',
+            'match'     => 'option',
+            'update'    => 'textarea',
+        ),
+
+        'pic'      => array
+        (
+            'type'      => 'varchar-8000',
+            'name'      => '退款图片',
+            'default'   => '',
+            'desc'      => '退款图片',
+            'match'     => 'option',
+            'update'    => 'images',
+            'key'       => '1',
+            'place'     => '660*660',
+        ),
+
+        'type'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '类型',
+            'default'   => '1',
+            'desc'      => '类型',
+            'match'     => 'is_numeric',
+            //'update'  => 'select',
+            'option'    => $type,
+            'search'    => 'select',
+            'list'      => true,
+            //'edit'      => true,
+        ),
+
+        'status'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '状态',
+            'default'   => '1',
+            'desc'      => '状态',
+            'match'     => 'is_numeric',
+            //'update'  => 'select',
+            'option'    => $status,
+            'search'    => 'select',
+            'list'      => true,
+            //'edit'      => true,
+        ),
+
+        'process'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '审核进度',
+            'default'   => '1',
+            'desc'      => '审核进度',
+            'match'     => 'is_numeric',
+            //'update'  => 'select',
+            'option'    => $process,
+            'search'    => 'select',
+            'list'      => true,
+            'edit'      => true,
+        ),
+
+        'state'     => array
+        (
+            'type'      => 'tinyint-1',
+            'name'      => '状态',
+            'default'   => '1',
+            'desc'      => '请选择状态',
+            'match'     => 'is_numeric',
+        ),
+        
+        'cdate'     => array
+        (
+            'type'      => 'int-11',
+            'name'      => '创建时间',
+            'match'     => array('is_numeric', time()),
+            'desc'      => '',
+            # 只有insert时才生效
+            //'insert'    => true,
+            'search'    => 'date',
+            'list'      => 'date("Y-m-d H:i:s", {cdate})',
+        ),
+    ),
+
+    'manage' => array
+    (
+        
+    ),
+
+    'request' => array
+    (
+        
+    ),
+);

+ 45 - 0
app/shop/lib/Goods.php

@@ -29,4 +29,49 @@ class Goods
 
         return $data;
     }
+
+    # 处理库存操作 1是增加,2是减少 1是总库存,2是销量
+    public function oper($type, $col, $data)
+    {
+        if (!$data) {
+            return;
+        }
+        if ($type == 1) {
+            $method = 'inc';
+            $otherMethod = 'dec';
+        } else {
+            $method = 'dec';
+            $otherMethod = 'inc';
+        }
+
+        if ($col == 1) {
+            $method .= 'Total';
+            $otherMethod .= 'Total';
+            $num = 'total_num';
+        } else {
+            $method .= 'Sell';
+            $otherMethod .= 'Sell';
+            $num = 'sell_num';
+        }
+
+        foreach ($data as $k => $v) {
+            if (!$v['sku_id']) {
+                $v['sku_id'] = -1;
+            }
+
+            $up = array();
+            $up['where_shop_id'] = $v['shop_id'];
+            $up['where_goods_id'] = $v['goods_id'];
+            $up[$num] = $v['num'];
+            $state = Dever::db('shop/goods')->$method($up);
+            if ($state) {
+                $upSku = $up;
+                $upSku['where_sku_id'] = $v['sku_id'];
+                $state = Dever::db('shop/goods_sku')->$method($upSku);
+                if (!$state) {
+                    Dever::db('shop/goods')->$otherMethod($up);
+                }
+            }
+        }
+    }
 }

+ 274 - 0
app/shop/lib/Refund.php

@@ -0,0 +1,274 @@
+<?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($uid, $order_id)
+    {
+        $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($shop_id, $order_id, $order_goods_id, $status, $price, $num = false, $desc = '', $pic = '', $process = 1)
+    {
+        $data['shop_id'] = $shop_id;
+        $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('当前订单状态不允许退货退款');
+        }
+
+        $data = Dever::load($this->lib)->set($type, 1)->getView($id, $order_id, false);
+        $auth = false;
+        if ($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 && $info['num'] >= $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['shop_id'], $data['id'], $order_goods_id, $status, $cash, $num, $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(2, 1, $oper_data);
+                }
+
+                $update = array();
+                $update['where_id'] = $data['id'];
+                $update['refund_cash'] = $data['refund_cash'] + $cash;
+                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) {
+            $update['refund_cash'] = $order['refund_cash'] - $info['cash'];
+            $update['where_id'] = $order['id'];
+            $state = Dever::db($this->order_table)->update($update);
+        } 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 ($total <= 0) {
+                        if ($order['status'] == 5) {
+                            $update['status'] = 6;
+                        } else {
+                            $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(2, 2, $data);
+        } elseif ($this->type == 'buy' && $process == 3 && ($order['status'] == 5 || $order['status'] == 6)) {
+            # 如果是采购单,退款后要恢复库存
+            Dever::load('shop/lib/goods')->oper(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;//消息类型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, 1, 1, false, $msg_param);
+
+            # 退款到原支付账户 待处理
+        }
+    }
+}

+ 22 - 141
app/shop/lib/Sell.php

@@ -100,9 +100,9 @@ class Sell
             $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
             $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
         }
-        $result['search']['status'][] = array('value' => '6,7,8', 'name' => '退款');
-        $result['search']['status'][] = array('value' => 4, 'name' => '已完成');
-        $result['search']['status'][] = array('value' => 5, 'name' => '已取消');
+        $result['search']['status'][] = array('value' => 8, 'name' => '退款');
+        $result['search']['status'][] = array('value' => '5,6', 'name' => '已完成');
+        $result['search']['status'][] = array('value' => 7, 'name' => '已取消');
 
 
         $result['search']['method'] = array
@@ -161,6 +161,13 @@ class Sell
         $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
         $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];
 
+        if (!$info['coupon_cash']) {
+            $info['coupon_cash'] = 0;
+        }
+        if (!$info['refund_cash']) {
+            $info['refund_cash'] = 0;
+        }
+
         if ($this->type == 1) {
             if ($info['status'] == 2) {
                 $info['status_name'] = '支付成功';
@@ -218,12 +225,6 @@ class Sell
             $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
         }
 
-        if ($info['status'] == 6 || $info['status'] == 7 || $info['status'] == 8) {
-            
-        } else {
-            $info['tk_cash'] = 0;
-        }
-
         $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
         if ($info['address_id']) {
             $info['address'] = Dever::db('passport/address')->find($info['address_id']);
@@ -232,11 +233,6 @@ class Sell
 
             foreach ($info['goods'] as $k => $v) {
                 $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
-
-                if ($v['status'] > 2 && $v['tk_cash'] > 0) {
-                    $info['tk_cash'] += $v['tk_cash'];
-                }
-
                 $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
             }
 
@@ -255,11 +251,6 @@ class Sell
                 $goods = Dever::db('goods/info')->one($v['goods_id']);
                 $info['goods'][$k]['name'] = $goods['name'];
                 $info['goods'][$k]['cover'] = $goods['cover'];
-
-                if ($v['status'] > 2 && $v['tk_cash'] > 0) {
-                    $info['tk_cash'] += $v['tk_cash'];
-                }
-
                 $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
             }
         }
@@ -272,107 +263,18 @@ class Sell
     {
         $data = $this->getView($id, $order_id, false);
         if ($data['status'] == 1) {
-            $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 5, 'qxdate' => time()));
+            $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
             if ($state) {
                 if ($data['user_coupon_id']) {
-                    $this->noCoupon($data['user_coupon_id']);
+                    Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
                 }
-            }
 
-            return 'ok';
-        } else {
-            Dever::alert('当前订单状态不允许取消');
-        }
-    }
-
-    # 全部退款
-    public function tui($id, $order_id, $status = 6, $desc = '')
-    {
-        if ($status != 6 && $status != 7 && $status != 8) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-
-        $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 2) {
-            $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => $status, 'tk_desc' => $desc, 'tk_cash' => $data['price']));
-
-            $this->pay_tui($state, $data, $data['price']);
-
-            if ($state && $data['user_coupon_id']) {
-                $this->noCoupon($data['user_coupon_id']);
+                $this->updateSell($data, 2);
             }
 
             return 'ok';
         } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-    }
-
-    # 获取部分退款的详情
-    public function tui_one_info($id, $order_id, $order_goods_id)
-    {
-        $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 2 || $data['status'] == 3 || $data['status'] == 4) {
-            $info = Dever::db('shop/sell_order_goods')->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'shop_id' => $id));
-            if ($info && $info['status'] <= 2) {
-                $info['tui_price'] = $info['price'] - $info['coupon_cash'];
-                $info['tui_one_price'] = round($info['tui_price'] / $info['num'], 2);
-                $info['goods'] = Dever::db('goods/info')->find($info['goods_id']);
-            }
-            return $info;
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-    }
-
-    # 部分退款
-    public function tui_one($id, $order_id, $order_goods_id, $num = false, $status = 6, $desc = '')
-    {
-        if ($status != 3 && $status != 4) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-
-        $data = $this->getView($id, $order_id, false);
-        if ($data['status'] == 2 || $data['status'] == 3 || $data['status'] == 4) {
-            $info = Dever::db('shop/sell_order_goods')->find(array('id' => $order_goods_id, 'order_id' => $data['id'], 'shop_id' => $id));
-            if ($info && $info['status'] <= 2) {
-                $info['price'] = $info['price'] - $info['coupon_cash'];
-                if ($num > 0 && $info['num'] >= $num) {
-                    $price = round($info['price'] / $info['num'], 2);
-                    $info['price'] = round($price * $num, 2);
-                }
-                $state = Dever::db('shop/sell_order_goods')->update(array('where_id' => $info['id'], 'status' => $status, 'tk_desc' => $desc, 'tk_cash' => $info['price']));
-
-                if ($state) {
-                    $this->pay_tui($state, $data, $info['price']);
-
-                    # 检查这个订单下的商品是不是都退了
-                    $total = Dever::db('shop/sell_order_goods')->total(array('order_id' => $data['id'], 'shop_id' => $id, 'status' => 1));
-                    if ($total <= 0) {
-                        $status += 4;
-                        $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => $status, 'tk_desc' => $desc, 'tk_cash' => $info['price']));
-                    }
-                }
-            }
-            return 'ok';
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-    }
-
-    # 退款通知
-    public function pay_tui($state, $data, $price)
-    {
-        if ($state) {
-            $shop = Dever::db('shop/info')->one($data['shop_id']);
-            $msg_param['type'] = 1;//消息类型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, 1, 1, false, $msg_param);
-
-            # 退款到原支付账户 待处理
+            Dever::alert('当前订单状态不允许取消');
         }
     }
 
@@ -671,6 +573,10 @@ class Sell
             }
         }
 
+        # 减少库存 增加销量
+        $order_data['id'] = $id;
+        $this->updateSell($order_data);
+
         if ($card || $price <= 0 || $pay_type == 2) {
 
         	$param['pay_product_id'] = $id;
@@ -762,13 +668,6 @@ class Sell
         # 这里可以记录一下使用日志
     }
 
-    public function noCoupon($user_coupon_id)
-    {
-        if ($user_coupon_id) {
-            Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 1));
-        }
-    }
-
     # 再次发起支付
     public function rpay($id, $system_source = 5, $refer = '')
     {
@@ -837,9 +736,6 @@ class Sell
         if ($order && $order['status'] == 1) {
 
             if ($status == 2) {
-                # 减少库存 增加销量
-                $this->updateSell($order);
-
                 # 生成自提码或者取件码
                 $update['code'] = $this->getCode();
                 if ($order['method'] == 1) {
@@ -903,28 +799,13 @@ class Sell
     }
 
     # 减少库存,增加销量
-    public function updateSell($order)
+    public function updateSell($order, $type = 1)
     {
         $where['order_id'] = $order['id'];
+        $where['status'] = 1;
         $data = Dever::db('shop/sell_order_goods')->select($where);
-
         if ($data) {
-            foreach ($data as $k => $v) {
-                $up = array();
-                $up['where_shop_id'] = $v['shop_id'];
-                $up['where_goods_id'] = $v['goods_id'];
-                $up['sell_num'] = $v['num'];
-                Dever::db('shop/goods')->updateSell($up);
-
-                if ($v['sku_id'] > 0) {
-                    
-                } else {
-                    $v['sku_id'] = -1;
-                }
-                $upSku = $up;
-                $upSku['where_sku_id'] = $v['sku_id'];
-                Dever::db('shop/goods_sku')->updateSell($upSku);
-            }
+            Dever::load('shop/lib/goods')->oper($type, 2, $data);
         }
     }
 
@@ -991,7 +872,7 @@ class Sell
 
         $html .= '<tr>
           <td>订单信息</td>
-          <td>'.$this->table(array('订单状态', '金额', '数量','支付方式', '配送方式'), array(array($config['status'][$info['status']], $info['price'], $info['num'],$config['pay_method'][$info['pay_method']], $config['method'][$info['method']]))).'</td>
+          <td>'.$this->table(array('金额', '数量','支付方式', '配送方式', '订单状态'), array(array($info['price'], $info['num'],$config['pay_method'][$info['pay_method']], $config['method'][$info['method']], $config['status'][$info['status']]))).'</td>
         </tr>';
 
         if ($info['address_id'] > 0) {

+ 11 - 4
app/shop/src/My.php

@@ -140,7 +140,7 @@ class My extends Core
 	}
 
 	# 取消订单
-	public function order_cancel()
+	public function order_cancel_commit()
 	{
 		$order_id = Dever::input('order_id');
 
@@ -148,12 +148,19 @@ class My extends Core
 	}
 
 	# 用户退款
-	public function order_tui()
+	public function order_tui_commit()
 	{
 		$order_id = Dever::input('order_id');
-        $status = 6;
 
-        return Dever::load('shop/lib/sell')->set(1, 2)->tui($this->uid, $order_id, $status);
+        return Dever::load('shop/lib/refund')->set('sell')->apply(1, $this->uid, $order_id, false, 1);
+	}
+
+	# 获取订单退款记录
+	public function order_tui_log()
+	{
+		$order_id = Dever::input('order_id');
+
+        return Dever::load('shop/lib/refund')->set('sell')->getList($this->uid, $order_id);
 	}
 
 	# 我的消息列表

+ 48 - 2
app/store/database/goods.php

@@ -149,12 +149,13 @@ return array
     'request' => array
     (
         # 更新售出量
-        'updateSell' => array
+        'incSell' => array
         (
             'type' => 'update',
             'where' => array
             (
-                'id' => 'yes',
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
             ),
             'set' => array
             (
@@ -162,6 +163,21 @@ return array
             ),
         ),
 
+        # 减少售出量
+        'decSell' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'sell_num' => array('yes', '-='),
+            ),
+        ),
+
         # 更新总库存
         'updateTotal' => array
         (
@@ -176,6 +192,36 @@ return array
             ),
         ),
 
+        # 增加总库存
+        'incTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '+='),
+            ),
+        ),
+
+        # 减少总库存
+        'decTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '-='),
+            ),
+        ),
+
         # 获取单条数据
         'getOne' => array
         (

+ 52 - 2
app/store/database/goods_sku.php

@@ -171,12 +171,14 @@ $config = array
         ),
 
         # 更新售出量
-        'updateSell' => array
+        'incSell' => array
         (
             'type' => 'update',
             'where' => array
             (
-                'id' => 'yes',
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
             ),
             'set' => array
             (
@@ -184,6 +186,22 @@ $config = array
             ),
         ),
 
+        # 减少售出量
+        'decSell' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'sell_num' => array('yes', '-='),
+            ),
+        ),
+
         # 更新总库存
         'updateTotal' => array
         (
@@ -197,6 +215,38 @@ $config = array
                 'total_num' => array('yes', '+='),
             ),
         ),
+
+        # 增加总库存
+        'incTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '+='),
+            ),
+        ),
+
+        # 减少总库存
+        'decTotal' => array
+        (
+            'type' => 'update',
+            'where' => array
+            (
+                'store_id' => 'yes',
+                'goods_id' => 'yes',
+                'sku_id' => 'yes',
+            ),
+            'set' => array
+            (
+                'total_num' => array('yes', '-='),
+            ),
+        ),
     ),
 );