dever hace 3 años
padre
commit
edd676d884

+ 4 - 0
app/mshop/src/Core.php

@@ -24,6 +24,10 @@ class Core
             Dever::alert('请选择门店');
         }
 
+        if ($this->shop_id != $this->user['shop_id']) {
+            Dever::alert('请选择门店');
+        }
+
         $this->shop = Dever::db('shop/info')->find($this->shop_id);
 
         if (!$this->shop) {

+ 260 - 3
app/mshop/src/Data.php

@@ -43,7 +43,7 @@ class Data extends Core
 	# 销售订单管理
 	public function sell_order()
 	{
-		return Dever::load('shop/lib/buyorder')->set(2, 1)->getList($this->shop_id);
+		return Dever::load('shop/lib/sell')->set(2, 1)->getList($this->shop_id);
 	}
 
     # 查看订单详情
@@ -51,7 +51,7 @@ class Data extends Core
     {
         $order_id = Dever::input('order_id');
 
-        return Dever::load('shop/lib/buyorder')->set(2, 2)->getView($this->shop_id, $order_id);
+        return Dever::load('shop/lib/sell')->set(2, 2)->getView($this->shop_id, $order_id);
     }
 
     # 门店全部退款
@@ -59,8 +59,265 @@ class Data extends Core
     {
         $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/buyorder')->set(2, 2)->tui($this->shop_id, $order_id, $status, $desc);
+        return Dever::load('shop/lib/sell')->set(2, 2)->tui($this->shop_id, $order_id, $status, $desc);
     }
+
+    # 门店部分退款信息
+    public function order_tui_one_info()
+    {
+        $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);
+    }
+
+    # 门店部分退款
+    public function order_tui_one()
+    {
+        $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);
+    }
+
+    # 根据店铺获取商品列表
+    public function getGoods()
+    {
+        return Dever::load('shop/lib/goods')->getList();
+    }
+
+    # 获取店铺的优惠券
+    public function getCoupon()
+    {
+        $this->data = Dever::db('shop/coupon')->getAllPage(array('shop_id' => $this->shop_id));
+
+        if ($this->data) {
+            $time = time();
+            foreach ($this->data as $k => $v) {
+                $this->data[$k]['info'] = Dever::db('goods/coupon')->find($v['coupon_id']);
+                $this->data[$k]['shop'] = array();
+                $coupon = Dever::db('shop/coupon')->select(array('coupon_id' => $v['coupon_id'], 'city' => $v['city']));
+
+                foreach ($coupon as $k1 => $v1) {
+                    $shop = Dever::db('shop/info')->getOne(array('id' => $v1['shop_id'], 'city' => $v['city'], 'coupon_city' => 1));
+                    if ($shop) {
+                        $this->data[$k]['shop'][] = $shop;
+
+                        if ($k1 > 0) {
+                            $city = Dever::db('area/city')->find($v['city']);
+                            $this->data[$k]['shop_name'] = $city['name'] . '多店通用';
+                        } else {
+                            $this->data[$k]['shop_name'] = $shop['name'];
+                        }
+                    }
+                }
+
+                if (!$this->data[$k]['shop']) {
+                    $shop = Dever::db('shop/info')->getOne($v['shop_id']);
+                    $this->data[$k]['shop'][] = $shop;
+                    $this->data[$k]['shop_name'] = $shop['name'];
+                }
+            }
+        }
+
+        return $this->data;
+    }
+
+    # 获取购物车的数据
+    public function getCart()
+    {
+        $where['shop_id'] = $this->shop_id;
+
+        $this->data['cart'] = Dever::db('shop/shop_cart')->select($where);
+
+        if ($this->data['cart']) {
+            foreach ($this->data['cart'] as $k => $v) {
+                $this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
+
+                if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
+                    $this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
+                }
+            }
+        }
+
+        return $this->data['cart'];
+    }
+
+    # 加入到购物车
+    public function addCart()
+    {
+        $where['shop_id'] = $this->shop_id;
+        $where['goods_id'] = Dever::input('goods_id');
+        $where['sku_id'] = Dever::input('price_id');
+        if (!$where['goods_id']) {
+            Dever::alert('错误的商品');
+        }
+
+        $info = Dever::db('shop/shop_cart')->find($where);
+
+        $where['num'] = Dever::input('num');
+        if (!$info) {
+            # 验证库存
+            Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
+            $state = Dever::db('shop/shop_cart')->insert($where);
+        } else {
+            $where['num'] += $info['num'];
+            # 验证库存
+            Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
+            $where['where_id'] = $info['id'];
+            $state = Dever::db('shop/shop_cart')->update($where);
+        }
+
+        return $this->getCart();
+    }
+
+    # 清空购物车
+    public function dropCart()
+    {
+        $where['shop_id'] = $this->shop_id;
+
+        $state = Dever::db('shop/shop_cart')->delete($where);
+
+        return $this->alert($state);
+    }
+
+    # 更新购物车数量
+    public function upCart()
+    {
+        $where['shop_id'] = $this->shop_id;
+        $where['id'] = Dever::input('cart_id');
+
+        $status = Dever::input('status', 1);
+        $type = Dever::input('type', 1);
+        $num = Dever::input('num');
+
+        $info = Dever::db('shop/shop_cart')->find($where);
+
+        $state = false;
+
+        if ($info) {
+            if ($type == 1) {
+                $num = $info['num'] + $num;
+            } else {
+                $num = $info['num'] - $num;
+            }
+            $where = array();
+            $where['where_id'] = $info['id'];
+            if ($num <= 0) {
+                $state = Dever::db('shop/shop_cart')->delete($info['id']);
+            } else {
+                $where['status'] = $status;
+                $where['num'] = $num;
+                
+                # 验证库存
+                $total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);
+
+                if ($where['num'] > $total) {
+                    # 库存不足
+                    $where['num'] = $total;
+                    $state = Dever::db('shop/shop_cart')->update($where); 
+                    return array('cart' => 2, 'data' => $total);
+                }
+                $state = Dever::db('shop/shop_cart')->update($where); 
+            }
+        }
+
+        return array('cart' => 1, 'data' => $this->getCart());
+    }
+
+    # 确认订单页面
+    public function confirm()
+    {
+        $this->data['shop'] = $this->shop;
+        Dever::load('shop/lib/sell')->goods($this->data);
+        Dever::load('shop/lib/sell')->coupon($this->data);
+
+        return $this->data;
+    }
+
+    # 开始下单
+    public function pay()
+    {
+        $refer = Dever::input('refer');
+        $cart = Dever::input('cart', 2);
+        $address_id = Dever::input('address_id');
+        $invoice_id = Dever::input('invoice_id');
+        $pay_type = Dever::input('pay_type', 1);
+        $mobile = Dever::input('mobile');
+        $cash = Dever::input('cash');
+
+        if (!$mobile) {
+            Dever::alert('手机号不能为空');
+        }
+        
+        $info = Dever::input('info');
+
+        $this->data['shop'] = $this->shop;
+
+        Dever::load('shop/lib/sell')->goods($this->data);
+        Dever::load('shop/lib/sell')->coupon($this->data, 2);
+
+        if ($this->data['pay_method'] > 1) {
+            $cart = false;
+        }
+
+        if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
+            Dever::alert('已售空');
+        }
+
+        if ($this->data['method'] == 2 && !$address_id) {
+            Dever::alert('收货地址不能为空');
+        }
+
+        if (!isset($this->data['card'])) {
+            $this->data['card'] = false;
+        }
+
+        $this->user = array
+        (
+            'id' => -1,
+            'mobile' => $mobile,
+        );
+
+        if ($cash) {
+            $this->data['price'] -= $cash;
+        }
+        $pay = Dever::load('shop/lib/sell')->pay($this->data['method'], $this->data['pay_method'], $pay_type, $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $cash, $address_id, $invoice_id, $info, $this->data['card'], $this->data['coupon_id'], $this->data['user_coupon_id'], $this->data['coupon_cash'], $cart, 4, $refer);
+        return $pay;
+    }
+
+    # 再次付款
+    public function r_pay()
+    {
+        $refer = Dever::input('refer');
+        $order_id = Dever::input('order_id');
+
+        $pay = Dever::load('shop/lib/sell')->rpay($order_id, 4, $refer);
+
+        return $pay;
+    }
+
+
+    # 销量统计
 }

+ 16 - 0
app/shop/database/coupon.php

@@ -138,6 +138,22 @@ return array
 
     'request' => array
     (
+        # 列表
+        'getAllPage' => array
+        (
+            # 匹配的正则或函数 选填项
+            'option' => array
+            (
+                'shop_id' => 'yes',
+                'status' => 1,
+                'state' => 1,
+            ),
+            'type' => 'all',
+            'page' => array(10, 'list'),
+            'order' => array('status' => 'asc','edate' => 'asc', 'id' => 'desc'),
+            'col' => '*',
+        ),
+
         'getData' => array
         (
             # 匹配的正则或函数 选填项

+ 49 - 2
app/shop/database/sell_order.php

@@ -15,6 +15,12 @@ $pay_method = array
     3 => '礼品卡',
 );
 
+$pay_type = array
+(
+    1 => '微信',
+    2 => '现金',
+);
+
 $status = array
 (
     1 => '待支付',//买家待支付
@@ -39,6 +45,7 @@ return array
     'order' => 100,
     'status' => $status,
     'pay_method' => $pay_method,
+    'pay_type' => $pay_type,
     'method' => $method,
     # 数据结构
     'struct' => array
@@ -147,9 +154,9 @@ return array
         'price'      => array
         (
             'type'      => 'varchar-50',
-            'name'      => '付金额',
+            'name'      => '付金额',
             'default'   => '',
-            'desc'      => '付金额',
+            'desc'      => '付金额',
             'match'     => 'option',
             'update'    => 'text',
             'list'        => true,
@@ -166,6 +173,16 @@ return array
             //'list'        => true,
         ),
 
+        'kou_cash'      => array
+        (
+            'type'      => 'varchar-50',
+            'name'      => '抹零金额',
+            'default'   => '',
+            'desc'      => '抹零金额',
+            'match'     => 'option',
+            'update'    => 'text',
+        ),
+
         'card_code_id'      => array
         (
             'type'      => 'int-11',
@@ -175,6 +192,15 @@ return array
             'match'     => 'option',
         ),
 
+        'coupon_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '门店的优惠券id',
+            'default'   => '',
+            'desc'      => '门店的优惠券id',
+            'match'     => 'option',
+        ),
+
         'user_coupon_id'      => array
         (
             'type'      => 'int-11',
@@ -184,6 +210,16 @@ return array
             'match'     => 'option',
         ),
 
+        'coupon_cash'      => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '优惠金额',
+            'default'   => '0',
+            'desc'      => '优惠金额',
+            'match'     => 'option',
+            'update'    => 'text',
+        ),
+
         'method'        => array
         (
             'type'      => 'int-11',
@@ -216,6 +252,17 @@ return array
             'list'      => true,
         ),
 
+        'pay_type'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '支付方式',
+            'default'   => '1',
+            'desc'      => '支付方式',
+            'match'     => 'is_numeric',
+            'update'    => 'radio',
+            'option'    => $pay_type,
+        ),
+
         'tk_cash'      => array
         (
             'type'      => 'varchar-300',

+ 30 - 2
app/shop/database/sell_order_goods.php

@@ -103,9 +103,9 @@ return array
         'price'      => array
         (
             'type'      => 'varchar-50',
-            'name'      => '价',
+            'name'      => '商品原价',
             'default'   => '',
-            'desc'      => '价',
+            'desc'      => '商品原价',
             'match'     => 'option',
             'update'    => 'text',
             'list'        => true,
@@ -142,6 +142,34 @@ return array
             'update'    => 'textarea',
         ),
 
+        'coupon_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '门店的优惠券id',
+            'default'   => '',
+            'desc'      => '门店的优惠券id',
+            'match'     => 'option',
+        ),
+
+        'user_coupon_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '用户的优惠券id',
+            'default'   => '',
+            'desc'      => '用户的优惠券id',
+            'match'     => 'option',
+        ),
+
+        'coupon_cash'      => array
+        (
+            'type'      => 'varchar-300',
+            'name'      => '优惠金额',
+            'default'   => '0',
+            'desc'      => '优惠金额',
+            'match'     => 'option',
+            'update'    => 'text',
+        ),
+
         'status'        => array
         (
             'type'      => 'int-11',

+ 117 - 0
app/shop/database/shop_cart.php

@@ -0,0 +1,117 @@
+<?php
+
+$status = array
+(
+    1 => '默认选中',
+    2 => '未选中',
+);
+
+return array
+(
+    # 表名
+    'name' => 'shop_cart',
+    # 显示给用户看的名称
+    'lang' => '门店购物车',
+    'menu' => false,
+    # 数据结构
+    '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',
+            'list'      => 'Dever::load("shop/info-find#name", {shop_id})',
+        ),
+
+        'goods_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '商品名称',
+            'default'   => '',
+            'desc'      => '商品名称',
+            'match'     => 'is_numeric',
+            'list'      => 'Dever::load("goods/info-find#name", {goods_id})',
+        ),
+
+        'sku_id'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => 'sku',
+            'default'   => '',
+            'desc'      => 'sku',
+            'match'     => 'is_numeric',
+        ),
+
+        'num'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '购买数量',
+            'default'   => '',
+            'desc'      => '购买数量',
+            'match'     => 'is_numeric',
+            'search'    => 'select',
+            'list'        => true,
+        ),
+
+        'status'        => array
+        (
+            'type'      => 'int-11',
+            'name'      => '状态',
+            'default'   => '1',
+            'desc'      => '状态',
+            'match'     => 'is_numeric',
+            //'update'  => 'select',
+            'option'    => $status,
+            '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
+    (
+        
+    ),
+);

+ 12 - 0
app/shop/database/user_coupon.php

@@ -85,6 +85,18 @@ return array
             //'update'  => 'text',
         ),
 
+        'cash'      => array
+        (
+            'type'      => 'varchar-100',
+            'name'      => '抵扣金额',
+            'default'   => '0',
+            'desc'      => '请输入抵扣金额',
+            'match'     => 'is_string',
+            'update'    => 'text',
+            'search'    => 'fulltext',
+            //'list'      => true,
+        ),
+
         'status'        => array
         (
             'type'      => 'int-11',

+ 0 - 293
app/shop/lib/Buyorder.php

@@ -1,293 +0,0 @@
-<?php
-
-namespace Shop\Lib;
-
-use Dever;
-
-class Buyorder
-{
-	# 1是用户自己看,2是门店看
-	public $type = 1;
-	# 1是列表,2是详情
-	public $view = 1;
-	# 获取配置
-	public $config = array();
-	# table
-	public $table = 'shop/sell_order';
-
-	public function __construct()
-	{
-		$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)
-	{
-		$where = array();
-		if ($this->type == 1) {
-			$where['uid'] = $id;
-		} elseif ($this->type == 2) {
-			$where['shop_id'] = $id;
-		}
-		if (!$where) {
-			Dever::alert('参数错误');
-		}
-
-		return $where;
-	}
-
-	# 获取订单列表
-	public function getList($id)
-	{
-		$result = array();
-		$where = $this->where($id);
-		$mobile = Dever::input('mobile');
-		if ($mobile) {
-			$where['mobile'] = $mobile;
-		}
-		$status = Dever::input('status');
-		if ($status) {
-			$where['status'] = $status;
-		}
-
-		$method = Dever::input('method');
-		if ($method) {
-			$where['method'] = $method;
-		}
-
-		$pay_method = Dever::input('pay_method');
-		if ($pay_method) {
-			$where['pay_method'] = $pay_method;
-		}
-
-		$result['search_value'] = $where;
-		$result['search_value']['day'] = $day = Dever::input('day');
-		if ($day) {
-			$where['start'] = Dever::maketime($day . ' 00:00:00');
-			$where['end'] = Dever::maketime($day . ' 23:59:59');
-		}
-
-		$result['order'] = Dever::db($this->table)->getAll($where);
-
-		if ($result['order']) {
-			foreach ($result['order'] as $k => $v) {
-				$result['order'][$k] = $this->getInfo($v);
-			}
-		}
-
-		$result['search'] = array();
-		$result['search']['status'] = array
-		(
-			array('value' => 1, 'name' => '待支付'),
-			array('value' => 2, 'name' => '待处理'),
-			array('value' => 3, 'name' => '待自提'),
-			array('value' => 3, 'name' => '配送中'),
-			array('value' => '6,7,8', 'name' => '退款'),
-			array('value' => 4, 'name' => '已完成'),
-			array('value' => 5, 'name' => '已取消'),
-		);
-		$result['search']['method'] = array
-		(
-			array('value' => 1, 'name' => '自提'),
-			array('value' => 2, 'name' => '配送')
-		);
-		$result['search']['pay_method'] = array
-		(
-			array('value' => 1, 'name' => '平台结算'),
-			array('value' => 2, 'name' => '门店代下单')
-		);
-
-		return $result;
-	}
-
-	# 查看详情
-	public function getView($id, $order_id, $show = true)
-	{
-		$where = $this->where($id);
-		$where['id'] = $order_id;
-
-		$result = Dever::db($this->table)->find($where);
-
-		if (!$result) {
-			Dever::alert('订单不存在');
-		}
-
-		if ($show) {
-			$result = $this->getInfo($result);
-		}
-
-		return $result;
-	}
-
-	# 获取订单详细信息
-	public function getInfo($info)
-	{
-		if ($info['status'] == 1) {
-			# 15分钟内支付,900秒
-			$m = 9000;
-			# 支付倒计时
-			$info['time'] = time() - $info['cdate'];
-			if ($info['time'] >= $m) {
-				# 已过期,自动取消
-				$info['time'] = -1;
-				Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
-				$info['status'] = 11;
-			} else {
-				$info['time'] = $m - $info['time'];
-			}
-		}
-
-		$info['status_name'] = $this->config['status'][$info['status']];
-		$info['method_name'] = $this->config['method'][$info['method']];
-		$info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
-
-		if ($this->type == 1) {
-			if ($info['status'] == 2) {
-				$info['status_name'] = '支付成功';
-			}
-			if ($info['status'] == 3) {
-				if ($info['method'] == 1) {
-					$info['status_name'] = '已配货';
-				} else {
-					$info['status_name'] = '配送中';
-				}
-			}
-		} elseif ($this->type == 2) {
-			if ($info['status'] == 2) {
-				$info['status_name'] = '待处理';
-			}
-			if ($info['status'] == 3) {
-				if ($info['method'] == 1) {
-					$info['status_name'] = '待自提';
-				} else {
-					$info['status_name'] = '配送中';
-				}
-			}
-		}
-
-		if ($info['method'] == 2) {
-			$info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
-			if ($info['ps_info']) {
-				$info['ps_info']['service_name'] = '商家自送';
-				if ($info['ps_info']['service_id'] > 0) {
-					//暂时还没有
-				}
-			}
-		}
-
-		$info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
-		$info['user'] = Dever::load('passport/api')->info($info['uid']);
-		$info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
-		$info['cdate'] = date('Y-m-d H:i', $info['cdate']);
-
-		if ($this->view == 2) {
-			foreach ($info['goods'] as $k => $v) {
-				$info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
-			}
-			if ($info['address_id']) {
-				$info['address'] = Dever::db('passport/address')->find($info['address_id']);
-			}
-
-			if ($info['invoice_id']) {
-				$info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
-			}
-
-			if ($info['user_coupon_id']) {
-				$coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
-				if ($coupon) {
-					$info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
-				}
-			}
-		} else {
-			foreach ($info['goods'] as $k => $v) {
-				$goods = Dever::db('goods/info')->one($v['goods_id']);
-				$info['goods'][$k]['name'] = $goods['name'];
-				$info['goods'][$k]['cover'] = $goods['cover'];
-			}
-		}
-
-		return $info;
-	}
-
-	# 取消订单
-	public function cancel($id, $order_id)
-	{
-		$data = $this->getView($id, $order_id);
-		if ($data['status'] == 1) {
-			$state = Dever::db('shop/sell_order')->update(array('where_id' => $id, 'status' => 5));
-			if ($state) {
-
-			}
-
-			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);
-        if ($data['status'] == 2) {
-            $state = Dever::db('shop/sell_order')->update(array('where_id' => $id, 'status' => $status, 'tk_desc' => $desc, 'tk_cash' => $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);
-            }
-
-            # 退款到原支付账户 待处理
-
-            return 'ok';
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-	}
-
-	# 部分退款
-	public function tui_one($id, $order_id, $goods_id, $status = 6, $desc = '')
-	{
-        if ($status != 6 && $status != 7 && $status != 8) {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-
-        $data = $this->getView($id, $order_id);
-        if ($data['status'] == 2) {
-            $state = Dever::db('shop/sell_order')->update(array('where_id' => $id, 'status' => $status, 'tk_desc' => $desc, 'tk_cash' => $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);
-            }
-
-            # 退款到原支付账户 待处理
-
-            return 'ok';
-        } else {
-            Dever::alert('当前订单状态不允许退货退款');
-        }
-	}
-
-}

+ 1 - 0
app/shop/lib/Coupon.php

@@ -83,6 +83,7 @@ class Coupon
         $data['coupon_id'] = $shop_coupon['coupon_id'];
         $data['shop_coupon_id'] = $shop_coupon['id'];
         $data['edate'] = time() + ($shop_coupon['day'] * 86400);
+        //$data['cash'] = $cash;
         $id = Dever::db('shop/user_coupon')->insert($data);
 
         if ($id) {

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

@@ -0,0 +1,32 @@
+<?php
+
+namespace Shop\Lib;
+
+use Dever;
+
+class Goods
+{
+    # 根据店铺获取商品列表
+    public function getList()
+    {
+        $shop_id = Dever::input('shop_id');
+        $lng = Dever::input('lng');
+        $lat = Dever::input('lat');
+
+        $column = Dever::db('goods/column')->select();
+
+        $data = array();
+
+        # 这里要加一下缓存
+
+        $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
+        if ($column) {
+            foreach ($column as $k => $v) {
+                $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $v['id']);
+                $data['cate'][] = $v;
+            }
+        }
+
+        return $data;
+    }
+}

+ 586 - 14
app/shop/lib/Sell.php

@@ -8,8 +8,546 @@ use Dever;
 
 class Sell
 {
+    # 1是用户自己看,2是门店看
+    public $type = 1;
+    # 1是列表,2是详情
+    public $view = 1;
+    # 获取配置
+    public $config = array();
+    # table
+    public $table = 'shop/sell_order';
+
+    public function __construct()
+    {
+        $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)
+    {
+        $where = array();
+        if ($this->type == 1) {
+            $where['uid'] = $id;
+        } elseif ($this->type == 2) {
+            $where['shop_id'] = $id;
+        }
+        if (!$where) {
+            Dever::alert('参数错误');
+        }
+
+        return $where;
+    }
+
+    # 获取订单列表
+    public function getList($id)
+    {
+        $result = array();
+        $where = $this->where($id);
+        $mobile = Dever::input('mobile');
+        if ($mobile) {
+            $where['mobile'] = $mobile;
+        }
+        $status = Dever::input('status');
+        if ($status) {
+            $where['status'] = $status;
+        }
+
+        $method = Dever::input('method');
+        if ($method) {
+            $where['method'] = $method;
+        }
+
+        $pay_method = Dever::input('pay_method');
+        if ($pay_method) {
+            $where['pay_method'] = $pay_method;
+        }
+
+        $result['search_value'] = $where;
+        $result['search_value']['day'] = $day = Dever::input('day');
+        if ($day) {
+            $where['start'] = Dever::maketime($day . ' 00:00:00');
+            $where['end'] = Dever::maketime($day . ' 23:59:59');
+        }
+
+        $result['order'] = Dever::db($this->table)->getAll($where);
+
+        if ($result['order']) {
+            foreach ($result['order'] as $k => $v) {
+                $result['order'][$k] = $this->getInfo($v);
+            }
+        }
+
+        $result['search'] = array();
+        $result['search']['status'] = array
+        (
+            array('value' => 1, 'name' => '待支付'),
+            array('value' => 2, 'name' => '待处理'),
+            array('value' => 3, 'name' => '待自提'),
+            array('value' => 3, 'name' => '配送中'),
+            array('value' => '6,7,8', 'name' => '退款'),
+            array('value' => 4, 'name' => '已完成'),
+            array('value' => 5, 'name' => '已取消'),
+        );
+        $result['search']['method'] = array
+        (
+            array('value' => 1, 'name' => '自提'),
+            array('value' => 2, 'name' => '配送')
+        );
+        $result['search']['pay_method'] = array
+        (
+            array('value' => 1, 'name' => '平台结算'),
+            array('value' => 2, 'name' => '门店代下单')
+        );
+
+        return $result;
+    }
+
+    # 查看详情
+    public function getView($id, $order_id, $show = true)
+    {
+        $where = $this->where($id);
+        $where['id'] = $order_id;
+
+        $result = Dever::db($this->table)->find($where);
+
+        if (!$result) {
+            Dever::alert('订单不存在');
+        }
+
+        if ($show) {
+            $result = $this->getInfo($result);
+        }
+
+        return $result;
+    }
+
+    # 获取订单详细信息
+    public function getInfo($info)
+    {
+        if ($info['status'] == 1) {
+            # 15分钟内支付,900秒
+            $m = 9000;
+            # 支付倒计时
+            $info['time'] = time() - $info['cdate'];
+            if ($info['time'] >= $m) {
+                # 已过期,自动取消
+                $info['time'] = -1;
+                Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
+                $info['status'] = 11;
+            } else {
+                $info['time'] = $m - $info['time'];
+            }
+        }
+
+        $info['status_name'] = $this->config['status'][$info['status']];
+        $info['method_name'] = $this->config['method'][$info['method']];
+        $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
+
+        if ($this->type == 1) {
+            if ($info['status'] == 2) {
+                $info['status_name'] = '支付成功';
+            }
+            if ($info['status'] == 3) {
+                if ($info['method'] == 1) {
+                    $info['status_name'] = '已配货';
+                } else {
+                    $info['status_name'] = '配送中';
+                }
+            }
+        } elseif ($this->type == 2) {
+            if ($info['status'] == 2) {
+                $info['status_name'] = '待处理';
+            }
+            if ($info['status'] == 3) {
+                if ($info['method'] == 1) {
+                    $info['status_name'] = '待自提';
+                } else {
+                    $info['status_name'] = '配送中';
+                }
+            }
+        }
+
+        if ($info['method'] == 2) {
+            $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
+            if ($info['ps_info']) {
+                $info['ps_info']['service_name'] = '商家自送';
+                if ($info['ps_info']['service_id'] > 0) {
+                    //暂时还没有
+                }
+            }
+        }
+
+        $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
+        $info['user'] = Dever::load('passport/api')->info($info['uid']);
+        $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
+        $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
+
+        if ($this->view == 2) {
+            foreach ($info['goods'] as $k => $v) {
+                $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
+            }
+            if ($info['address_id']) {
+                $info['address'] = Dever::db('passport/address')->find($info['address_id']);
+            }
+
+            if ($info['invoice_id']) {
+                $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
+            }
+
+            if ($info['user_coupon_id']) {
+                $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
+                if ($coupon) {
+                    $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
+                }
+            }
+        } else {
+            foreach ($info['goods'] as $k => $v) {
+                $goods = Dever::db('goods/info')->one($v['goods_id']);
+                $info['goods'][$k]['name'] = $goods['name'];
+                $info['goods'][$k]['cover'] = $goods['cover'];
+            }
+        }
+
+        return $info;
+    }
+
+    # 取消订单
+    public function cancel($id, $order_id)
+    {
+        $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));
+            if ($state) {
+                if ($data['user_coupon_id']) {
+                    $this->noCoupon($data['user_coupon_id']);
+                }
+            }
+
+            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']);
+            }
+
+            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);
+
+            # 退款到原支付账户 待处理
+        }
+    }
+
+    # 获取优惠券
+    public function coupon(&$data, $type = 1)
+    {
+        $user_coupon_id = Dever::input('user_coupon_id');
+        $coupon_id = Dever::input('coupon_id');
+
+        $data['user_coupon_id'] = 0;
+        $data['coupon_id'] = 0;
+        $data['coupon_cash'] = 0;
+
+        if ($type == 1 && isset($data['uid']) && $data['uid'] > 0) {
+            if ($data['price'] <= 0) {
+                Dever::alert('付款价格错误');
+            }
+            # 查找符合要求的优惠券
+            $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
+
+            if ($coupon) {
+                foreach ($coupon as $k => $v) {
+                    if ($v['shop_id'] != $data['shop']['id']) {
+                        # 查找该券是否符合当前店铺
+                        if ($data['shop']['coupon_city'] == 2) {
+                            continue;
+                        }
+                    }
+                    $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
+                    if ($coupon_info && ($coupon_info['method'] == $data['method'] || $coupon_info['method'] == 3)) {
+                        $kou = false;
+                        if ($coupon_info['type'] == 1) {
+                            # 满减券
+                            if ($data['price'] >= $coupon_info['total_cash']) {
+                                $kou = true;
+                            }
+                        } else {
+                            $kou = true;
+                        }
+
+                        if ($kou) {
+                            $coupon_info['user_coupon_id'] = $v['id'];
+                            $coupon_info['uid'] = $v['uid'];
+                            $coupon_info['shop_id'] = $v['shop_id'];
+                            $coupon_info['edate'] = date('Y-m-d', $v['edate']);
+                            $data['coupon'][] = $coupon_info;
+
+                            if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
+                                $data['user_coupon_id'] = $v['id'];
+                                $data['coupon_id'] = $coupon_info['id'];
+                                $data['coupon_cash'] = $coupon_info['cash'];
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
+            if ($data['price'] <= 0) {
+                Dever::alert('付款价格错误');
+            }
+            $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
+            if (!$coupon) {
+                Dever::alert('优惠券不可用');
+            }
+            if (time() > $coupon['edate']) {
+                Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
+                Dever::alert('优惠券已过期');
+            }
+
+            if ($coupon['shop_id'] != $data['shop']['id']) {
+                if ($data['shop']['coupon_city'] == 2) {
+                    Dever::alert('优惠券不可用');
+                } else {
+                    $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
+                    if (!$coupon_info) {
+                        Dever::alert('优惠券不可用');
+                    }
+                }
+            }
+
+            $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
+            if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
+                Dever::alert('优惠券不可用');
+            }
+            if ($goods_coupon['method'] != $data['method'] && $goods_coupon['method'] != 3) {
+                Dever::alert('优惠券不可用');
+            }
+
+            $data['user_coupon_id'] = $user_coupon_id;
+            $data['coupon_id'] = $goods_coupon['id'];
+            $data['coupon_cash'] = $goods_coupon['cash'];
+        } elseif ($coupon_id && $type == 3) {
+            if ($data['price'] <= 0) {
+                Dever::alert('付款价格错误');
+            }
+            $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $coupon_id));
+            if (!$coupon_info) {
+                Dever::alert('优惠券不可用');
+            }
+
+            $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
+            if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
+                Dever::alert('优惠券不可用');
+            }
+            if ($goods_coupon['method'] != $data['method'] && $goods_coupon['method'] != 3) {
+                Dever::alert('优惠券不可用');
+            }
+
+            $data['user_coupon_id'] = -1;
+            $data['coupon_id'] = $goods_coupon['id'];
+            $data['coupon_cash'] = $goods_coupon['cash'];
+        }
+
+        if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
+            # 这里还要计算每个商品的优惠金额
+            if (isset($data['list']) && $data['list']) {
+                foreach ($data['list'] as $k => $v) {
+                    $data['list'][$k]['coupon_id'] = $data['coupon_id'];
+                    $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
+                    $data['list'][$k]['coupon_cash'] = round($v['price']/$data['price'], 2) * $data['coupon_cash'];
+                }
+            }
+            $data['price'] -= $data['coupon_cash'];
+            if ($data['price'] < 0) {
+                $data['price'] = 0;
+            }
+        }
+
+        return $data;
+    }
+
+    # 得到商品和总价
+    public function goods(&$data)
+    {
+        # 1自提,2配送
+        $data['method'] = Dever::input('method', 1);
+        $data['pay_method'] = Dever::input('pay_method');
+        $card = Dever::input('card');
+        $pwd = Dever::input('pwd');
+
+        if ($data['pay_method'] == 3 && $card && $pwd) {
+            $data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
+            if (!$data['card']) {
+                Dever::alert('卡号/密码错误');
+            }
+            if (time() > $data['card']['edate']) {
+                Dever::alert('礼品卡已失效');
+            }
+            $card_info = Dever::db('goods/card')->find($data['card']['card_id']);
+            if (!$card_info) {
+                Dever::alert('卡号/密码错误');
+            }
+            $goods = Dever::array_decode($card_info['goods']);
+
+            $goods_id = array();
+            $num = array();
+            $sku_id = array();
+            foreach ($goods as $k => $v) {
+                $goods_id[] = $v['goods_id'];
+                $num[] = $v['num'];
+                $sku_id[] = -1;
+            }
+        } else {
+            $goods_id = Dever::input('goods_id');
+            if (!$goods_id) {
+                Dever::alert('请传入商品');
+            }
+            $goods_id = explode(',', $goods_id);
+
+            $sku_id = Dever::input('price_id');
+            if ($sku_id) {
+                $sku_id = explode(',', $sku_id);
+            }
+            
+            $num = Dever::input('num');
+            if (!$num) {
+                Dever::alert('请传入商品数量');
+            }
+            $num = explode(',', $num);
+        }
+
+        $data['price'] = 0;
+        $data['num'] = 0;
+        $data['name'] = array();
+        $count = count($goods_id);
+        # 计算总价格
+        foreach ($goods_id as $k => $v) {
+            $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
+            $n = isset($num[$k]) ? $num[$k] : 1;
+            $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
+            $data['list'][$k]['num'] = $n;
+
+            # 2是库存不足
+            $data['list'][$k]['ku_state'] = 1;
+
+            # 验证是否有货
+            $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
+
+            if ($total <= 0) {
+                $data['list'][$k]['ku_state'] = 2;
+                $data['list'][$k]['buy_num'] = 0;
+            }
+
+            if ($data['list'][$k]['ku_state'] == 1) {
+                $data['list'][$k]['buy_num'] = $n;
+                $data['num'] += $n;
+                $data['price'] += $data['list'][$k]['price'] * $n;
+                if ($count > 1) {
+                    $data['name'][] = $data['list'][$k]['name'];
+                } else {
+                    $data['name'][] = $data['list'][$k]['name'];
+                }
+            }
+        }
+
+        return $data;
+    }
+
     # 发起支付
-	public function action($method, $pay_method, $user, $shop, $name, $num, $goods, $price, $address_id, $invoice_id, $info, $card, $user_coupon_id, $cart = false, $refer = '')
+	public function pay($method, $pay_method, $pay_type, $user, $shop, $name, $num, $goods, $price, $cash, $address_id, $invoice_id, $info, $card, $coupon_id, $user_coupon_id, $coupon_cash, $cart = false, $system_source = 5, $refer = '')
     {
         if (!$user) {
             Dever::alert('错误的用户信息');
@@ -26,6 +564,7 @@ class Sell
         $order_data['num'] = $num;
         $order_data['info'] = $info;
         $order_data['price'] = $price;
+        $order_data['kou_cash'] = $cash;
 
         $order_data['area'] = $shop['area'];
         $order_data['province'] = $shop['province'];
@@ -35,6 +574,7 @@ class Sell
 
         $order_data['method'] = $method;
         $order_data['pay_method'] = $pay_method;
+        $order_data['pay_type'] = $pay_type;
 
         if ($card) {
         	$order_data['card_code_id'] = $card['id'];
@@ -42,6 +582,12 @@ class Sell
         if ($user_coupon_id) {
             $order_data['user_coupon_id'] = $user_coupon_id;
         }
+        if ($coupon_id) {
+            $order_data['coupon_id'] = $coupon_id;
+        }
+        if ($coupon_cash) {
+            $order_data['coupon_cash'] = $coupon_cash;
+        }
 
         $order_data['order_num'] = $this->getOrderId();
         $id = Dever::db('shop/sell_order')->insert($order_data);
@@ -60,11 +606,23 @@ class Sell
                 $data['price'] = $v['price'];
                 $data['num'] = $v['buy_num'];
 
+                if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
+                    $data['user_coupon_id'] = $v['user_coupon_id'];
+                }
+                if (isset($v['coupon_id']) && $v['coupon_id']) {
+                    $data['coupon_id'] = $v['coupon_id'];
+                }
+                if (isset($v['coupon_cash']) && $v['coupon_cash']) {
+                    $data['coupon_cash'] = $v['coupon_cash'];
+                } else {
+                    $data['coupon_cash'] = 0;
+                }
+
                 Dever::db('shop/sell_order_goods')->insert($data);
             }
         }
 
-        if ($card || $price <= 0) {
+        if ($card || $price <= 0 || $pay_type == 2) {
 
         	$param['pay_product_id'] = $id;
         	$param['pay_order_id'] = $order_data['order_num'];
@@ -73,12 +631,12 @@ class Sell
         	$param['pay_msg'] = '';
         	$this->success($param);
 
-            if ($cart == 1) {
+            if ($cart) {
                 # 来自购物车,要清空一下
-                $this->dropCart($uid, $goods, $shop['id']);
+                $this->dropCart($cart, $uid, $goods, $shop['id']);
             }
             # 使用优惠券
-            $this->useCoupon($user_coupon_id);
+            $this->useCoupon($coupon_id, $user_coupon_id);
 
             if ($card) {
                 # 更新礼品卡状态
@@ -97,7 +655,7 @@ class Sell
         (
             'project_id' => 1,
             'channel_id' => 1,
-            'system_source' => 5,
+            'system_source' => $system_source,
             'uid' => $uid,
             'name' => $order_data['name'],
             'cash' => $price,
@@ -112,34 +670,48 @@ class Sell
         }
 
         $result = Dever::load('pay/api.pay', $param);
-        if ($cart == 1) {
+        if ($cart) {
             # 来自购物车,要清空一下
-            $this->dropCart($uid, $goods, $shop['id']);
+            $this->dropCart($cart, $uid, $goods, $shop['id']);
         }
         # 使用优惠券
-        $this->useCoupon($user_coupon_id);
+        $this->useCoupon($coupon_id, $user_coupon_id);
         return $result;
     }
 
-    public function dropCart($uid, $goods, $shop_id)
+    public function dropCart($cart, $uid, $goods, $shop_id)
     {
         $where['uid'] = $uid;
         $where['shop_id'] = $shop_id;
         foreach ($goods as $k => $v) {
             $where['goods_id'] = $v['id'];
-            Dever::db('shop/cart')->delete($where);
+            if ($cart == 1) {
+                Dever::db('shop/cart')->delete($where);
+            } else {
+                unset($where['uid']);
+                Dever::db('shop/shop_cart')->delete($where);
+            }
         }
     }
 
-    public function useCoupon($user_coupon_id)
+    public function useCoupon($coupon_id, $user_coupon_id)
     {
         if ($user_coupon_id) {
             Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
         }
+
+        # 这里可以记录一下使用日志
+    }
+
+    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 raction($id, $refer = '')
+    public function rpay($id, $system_source = 5, $refer = '')
     {
     	$order = Dever::db('shop/sell_order')->find($id);
 
@@ -155,7 +727,7 @@ class Sell
         (
             'project_id' => 1,
             'channel_id' => 1,
-            'system_source' => 5,
+            'system_source' => $system_source,
             'uid' => $order['uid'],
             'name' => $order['name'],
             'cash' => $order['price'],

+ 12 - 193
app/shop/src/Buy.php

@@ -189,213 +189,32 @@ class Buy extends Core
 	public function confirm()
 	{
 		$this->data['user'] = $this->user;
-		$this->goods();
-		$this->coupon($this->data['price'], $this->data['method']);
+		$this->data['shop'] = $this->shop;
+		Dever::load('shop/lib/sell')->goods($this->data);
+		Dever::load('shop/lib/sell')->coupon($this->data);
 
 		return $this->data;
 	}
 
-	# 优惠券
-	public function coupon($price, $method, $type = 1)
-	{
-		$user_coupon_id = Dever::input('user_coupon_id');
-
-		$this->data['user_coupon_id'] = 0;
-		$this->data['coupon_id'] = 0;
-		$this->data['coupon_cash'] = 0;
-
-		if ($type == 1) {
-			if ($price <= 0) {
-				Dever::alert('付款价格错误');
-			}
-			# 查找符合要求的优惠券
-			$coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $this->uid, 'city' => $this->shop['city'], 'status' => 1, 'edate' => time()));
-
-			if ($coupon) {
-				foreach ($coupon as $k => $v) {
-					if ($v['shop_id'] != $this->shop_id) {
-						# 查找该券是否符合当前店铺
-						if ($this->shop['coupon_city'] == 2) {
-							continue;
-						}
-					}
-					$coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
-					if ($coupon_info && ($coupon_info['method'] == $method || $coupon_info['method'] == 3)) {
-						$kou = false;
-						if ($coupon_info['type'] == 1) {
-							# 满减券
-							if ($price >= $coupon_info['total_cash']) {
-								$kou = true;
-							}
-						} else {
-							$kou = true;
-						}
-
-						if ($kou) {
-							$coupon_info['user_coupon_id'] = $v['id'];
-							$coupon_info['uid'] = $v['uid'];
-							$coupon_info['shop_id'] = $v['shop_id'];
-							$coupon_info['edate'] = date('Y-m-d', $v['edate']);
-							$this->data['coupon'][] = $coupon_info;
-
-							if (!$user_coupon_id && $this->data['coupon_cash'] <= $coupon_info['cash']) {
-								$this->data['user_coupon_id'] = $v['id'];
-								$this->data['coupon_id'] = $coupon_info['id'];
-								$this->data['coupon_cash'] = $coupon_info['cash'];
-							}
-						}
-					}
-				}
-			}
-		}
-
-		if ($user_coupon_id) {
-			if ($price <= 0) {
-				Dever::alert('付款价格错误');
-			}
-			$coupon = Dever::db('shop/user_coupon')->find(array('uid' => $this->uid, 'id' => $user_coupon_id, 'status' => 1));
-			if (!$coupon) {
-				Dever::alert('优惠券不可用');
-			}
-			if (time() > $coupon['edate']) {
-				Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
-				Dever::alert('优惠券已过期');
-			}
-
-			if ($coupon['shop_id'] != $this->shop_id) {
-				if ($this->shop['coupon_city'] == 2) {
-					Dever::alert('优惠券不可用');
-				} else {
-					$coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
-					if (!$coupon_info) {
-						Dever::alert('优惠券不可用');
-					}
-				}
-			}
-
-			$goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
-			if ($goods_coupon['type'] == 2 && $this->data['price'] < $goods_coupon['total_cash']) {
-				Dever::alert('优惠券不可用');
-			}
-			if ($goods_coupon['method'] != $method && $goods_coupon['method'] != 3) {
-				Dever::alert('优惠券不可用');
-			}
-
-			$this->data['user_coupon_id'] = $user_coupon_id;
-			$this->data['coupon_id'] = $goods_coupon['id'];
-			$this->data['coupon_cash'] = $goods_coupon['cash'];
-		}
-
-		if (isset($this->data['coupon_cash']) && $this->data['coupon_cash'] > 0) {
-			$this->data['price'] -= $this->data['coupon_cash'];
-			if ($this->data['price'] < 0) {
-				$this->data['price'] = 0;
-			}
-		}
-	}
-
-	# 得到商品和总价
-	private function goods()
-	{
-		# 1自提,2配送
-		$this->data['method'] = Dever::input('method', 1);
-		$this->data['pay_method'] = Dever::input('pay_method');
-		$card = Dever::input('card');
-		$pwd = Dever::input('pwd');
-
-		if ($this->data['pay_method'] == 3 && $card && $pwd) {
-			$this->data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
-			if (!$this->data['card']) {
-				Dever::alert('卡号/密码错误');
-			}
-			if (time() > $this->data['card']['edate']) {
-				Dever::alert('礼品卡已失效');
-			}
-			$card_info = Dever::db('goods/card')->find($this->data['card']['card_id']);
-			if (!$card_info) {
-				Dever::alert('卡号/密码错误');
-			}
-			$goods = Dever::array_decode($card_info['goods']);
-
-			$goods_id = array();
-			$num = array();
-			$sku_id = array();
-			foreach ($goods as $k => $v) {
-				$goods_id[] = $v['goods_id'];
-				$num[] = $v['num'];
-				$sku_id[] = -1;
-			}
-		} else {
-			$goods_id = Dever::input('goods_id');
-			if (!$goods_id) {
-				Dever::alert('请传入商品');
-			}
-			$goods_id = explode(',', $goods_id);
-
-			$sku_id = Dever::input('price_id');
-			if ($sku_id) {
-				$sku_id = explode(',', $sku_id);
-			}
-			
-			$num = Dever::input('num');
-			$num = Dever::input('num');
-			if (!$num) {
-				Dever::alert('请传入商品数量');
-			}
-			$num = explode(',', $num);
-		}
-
-		$this->data['price'] = 0;
-		$this->data['num'] = 0;
-		$this->data['name'] = array();
-		$count = count($goods_id);
-		# 计算总价格
-		foreach ($goods_id as $k => $v) {
-			$s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
-			$n = isset($num[$k]) ? $num[$k] : 1;
-			$this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
-			$this->data['list'][$k]['num'] = $n;
-
-			# 2是库存不足
-			$this->data['list'][$k]['ku_state'] = 1;
-
-			# 验证是否有货
-			$total = Dever::load('shop/lib/info')->checkTotal($n, $v, $this->shop_id, $s);
-
-			if ($total <= 0) {
-				$this->data['list'][$k]['ku_state'] = 2;
-				$this->data['list'][$k]['buy_num'] = 0;
-			}
-
-			if ($this->data['list'][$k]['ku_state'] == 1) {
-				$this->data['list'][$k]['buy_num'] = $n;
-				$this->data['num'] += $n;
-				$this->data['price'] += $this->data['list'][$k]['price'] * $n;
-				if ($count > 1) {
-					$this->data['name'][] = $this->data['list'][$k]['name'];
-				} else {
-					$this->data['name'][] = $this->data['list'][$k]['name'];
-				}
-			}
-		}
-	}
-
 	# 开始下单
 	public function pay()
 	{
 		$refer = Dever::input('refer');
 		$cart = Dever::input('cart', 1);
-		$parent_uid = Dever::input('parent_uid');
 		$address_id = Dever::input('address_id');
 		$invoice_id = Dever::input('invoice_id');
 		
 		$info = Dever::input('info');
 
-		$this->goods();
-		$this->coupon($this->data['price'], $this->data['method'], 2);
+		$this->data['user'] = $this->user;
+		$this->data['shop'] = $this->shop;
+
+		Dever::load('shop/lib/sell')->goods($this->data);
+		Dever::load('shop/lib/sell')->coupon($this->data, 2);
+
 
 		if ($this->data['pay_method'] > 1) {
-			$cart = 2;
+			$cart = false;
 		}
 
 		if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
@@ -410,7 +229,7 @@ class Buy extends Core
 			$this->data['card'] = false;
 		}
 
-		$pay = Dever::load('shop/lib/sell')->action($this->data['method'], $this->data['pay_method'], $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $address_id, $invoice_id, $info, $this->data['card'], $this->data['user_coupon_id'], $cart, $refer);
+		$pay = Dever::load('shop/lib/sell')->pay($this->data['method'], $this->data['pay_method'], 1, $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], 0, $address_id, $invoice_id, $info, $this->data['card'], $this->data['coupon_id'], $this->data['user_coupon_id'], $this->data['coupon_cash'], $cart, 5, $refer);
 		return $pay;
 	}
 
@@ -420,7 +239,7 @@ class Buy extends Core
 		$refer = Dever::input('refer');
 		$order_id = Dever::input('order_id');
 
-		$pay = Dever::load('shop/lib/sell')->raction($order_id, $refer);
+		$pay = Dever::load('shop/lib/sell')->rpay($order_id, 5, $refer);
 
 		return $pay;
 	}

+ 1 - 19
app/shop/src/Main.php

@@ -84,25 +84,7 @@ class Main extends Core
     # 根据店铺获取商品列表
     public function getGoods()
     {
-        $shop_id = Dever::input('shop_id');
-        $lng = Dever::input('lng');
-        $lat = Dever::input('lat');
-
-        $column = Dever::db('goods/column')->select();
-
-        $data = array();
-
-        # 这里要加一下缓存
-
-        $data['shop'] = Dever::load('shop/lib/info')->getOne($shop_id, $lng, $lat);
-        if ($column) {
-            foreach ($column as $k => $v) {
-                $v['data'] = Dever::load('shop/lib/info')->getGoods($shop_id, $v['id']);
-                $data['cate'][] = $v;
-            }
-        }
-
-        return $data;
+        return Dever::load('shop/lib/goods')->getList();
     }
 
     # 获取文章详情

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

@@ -124,7 +124,7 @@ class My extends Core
 	# 我的订单列表
 	public function order()
 	{
-		return Dever::load('shop/lib/buyorder')->set(1, 1)->getList($this->uid);
+		return Dever::load('shop/lib/sell')->set(1, 1)->getList($this->uid);
 	}
 
 	# 查看订单详情
@@ -132,7 +132,7 @@ class My extends Core
 	{
 		$id = Dever::input('order_id');
 
-		return Dever::load('shop/lib/buyorder')->set(1, 2)->getView($this->uid, $id);
+		return Dever::load('shop/lib/sell')->set(1, 2)->getView($this->uid, $id);
 	}
 
 	# 取消订单
@@ -140,7 +140,7 @@ class My extends Core
 	{
 		$order_id = Dever::input('order_id');
 
-        return Dever::load('shop/lib/buyorder')->set(1, 2)->cancel($this->uid, $order_id);
+        return Dever::load('shop/lib/sell')->set(1, 2)->cancel($this->uid, $order_id);
 	}
 
 	# 用户退款
@@ -149,7 +149,7 @@ class My extends Core
 		$order_id = Dever::input('order_id');
         $status = Dever::input('status');
 
-        return Dever::load('shop/lib/buyorder')->set(1, 2)->tui($this->uid, $order_id, $status);
+        return Dever::load('shop/lib/sell')->set(1, 2)->tui($this->uid, $order_id, $status);
 	}
 
 	# 我的消息列表