dever 3 gadi atpakaļ
vecāks
revīzija
1951a72881

+ 11 - 0
app/factory/database/goods.php

@@ -95,6 +95,17 @@ return array
             'list'      => 'Dever::load("goods/lib/sku.table", "{goods_id}", "factory", "{factory_id}", false, false)',
         ),
 
+        'sell_num'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '销量',
+            'default'   => '0',
+            'desc'      => '请填写销量',
+            'match'     => 'option',
+            'search'    => 'order',
+            //'list'      => true,
+        ),
+
         'state'     => array
         (
             'type'      => 'tinyint-1',

+ 26 - 0
app/factory/database/goods_sku.php

@@ -61,6 +61,17 @@ $config = array
             'update'    => 'text',
         ),
 
+        'sell_num'      => array
+        (
+            'type'      => 'int-11',
+            'name'      => '销量',
+            'default'   => '0',
+            'desc'      => '请填写销量',
+            'match'     => 'option',
+            'search'    => 'order',
+            //'list'      => true,
+        ),
+
         'reorder'       => array
         (
             'type'      => 'int-11',
@@ -131,6 +142,21 @@ $config = array
             'col' => '*|sku_id',
         ),
 
+        # 获取单条数据
+        'getOne' => array
+        (
+            # 匹配的正则或函数 选填项
+            'option' => array
+            (
+                'factory_id' => 'yes',
+                'sku_id' => 'yes',
+                'goods_id' => 'yes',
+                'state' => 1,
+            ),
+            'type' => 'one',
+            'col' => '*',
+        ),
+
         'getMinOne' => array
         (
             # 匹配的正则或函数 选填项

+ 0 - 118
app/factory/database/order.php

@@ -86,28 +86,6 @@ return array
             'match'     => 'is_numeric',
         ),
 
-        'name'      => array
-        (
-            'type'      => 'varchar-80',
-            'name'      => '订单名称',
-            'default'   => '',
-            'desc'      => '订单名称',
-            'match'     => 'is_string',
-            'update'    => 'text',
-        ),
-
-        'order_num'      => array
-        (
-            'type'      => 'varchar-100',
-            'name'      => '订单编号',
-            'default'   => '',
-            'desc'      => '订单编号',
-            'match'     => 'is_string',
-            'update'    => 'text',
-            'search'    => 'fulltext',
-            'list'      => true,
-        ),
-
         'price'      => array
         (
             'type'      => 'varchar-50',
@@ -130,38 +108,6 @@ return array
             'list'        => true,
         ),
 
-        'pay_id'      => array
-        (
-            'type'      => 'varchar-100',
-            'name'      => '支付订单ID',
-            'default'   => '',
-            'desc'      => '付款订单id',
-            'match'     => 'is_string',
-            //'update'    => 'text',
-            //'search'    => 'fulltext',
-            //'list'        => true,
-        ),
-
-        'pay_status'        => array
-        (
-            'type'      => 'tinyint-1',
-            'name'      => '支付状态',
-            'default'   => '1',
-            'desc'      => '请选择支付状态',
-            'match'     => 'is_numeric',
-            'option'    => $pay_status,
-        ),
-
-        'pay_price'      => array
-        (
-            'type'      => 'varchar-50',
-            'name'      => '付款金额',
-            'default'   => '',
-            'desc'      => '付款金额',
-            'match'     => 'option',
-            'update'    => 'text',
-        ),
-
         'info'      => array
         (
             'type'      => 'varchar-300',
@@ -172,70 +118,6 @@ return array
             'update'    => 'textarea',
         ),
 
-        'note'     => array
-        (
-            'type'      => 'tinyint-1',
-            'name'      => '是否发送状态提醒-1未发送,2已发送',
-            'default'   => '1',
-            'desc'      => '请选择状态',
-            'match'     => 'is_numeric',
-        ),
-
-        'notice'        => array
-        (
-            'type'      => 'int-11',
-            'name'      => '模板消息提醒次数',
-            'default'   => '0',
-            'desc'      => '模板消息提醒次数',
-            'match'     => 'is_numeric',
-        ),
-
-        'tk_pic'     => array
-        (
-            'type'      => 'varchar-150',
-            'name'      => '退款截图',
-            'default'   => '',
-            'desc'      => '退款截图',
-            'match'     => 'is_string',
-            'update'    => 'image',
-            'key'       => 1
-        ),
-
-        'tk_time'       => array
-        (
-            'type'      => 'int-11',
-            'name'      => '退款时间',
-            'default'   => '',
-            'desc'      => '退款时间',
-            'match'     => 'option',
-            //'list'        => true,
-            //'update'    => 'date',
-            'callback'  => 'maketime',
-            'show'      => 'status=5',
-        ),
-
-        'tk_admin'      => array
-        (
-            'type'      => 'int-11',
-            'name'      => '退款审核人',
-            'default'   => '1',
-            'desc'      => '退款审核人',
-            'match'     => 'option',
-            //'list'        => true,
-            'show'      => 'status=5',
-        ),
-
-        'tk_desc'       => array
-        (
-            'type'      => 'varchar-300',
-            'name'      => '退款备注',
-            'default'   => '',
-            'desc'      => '退款备注',
-            'match'     => 'option',
-            'update'    => 'textarea',
-            //'show'        => 'status=5',
-        ),
-
         'state'     => array
         (
             'type'      => 'tinyint-1',

+ 79 - 0
app/factory/lib/Order.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace Factory\Lib;
+
+use Dever;
+
+class Order
+{
+	# 单个商品下单
+	public function pay($shop_order_id, $shop_id, $factory_id, $price, $num, $goods_id, $sku_id)
+	{
+		# 先更新销量
+		$state = $this->updateSell($factory_id, $goods_id, $sku_id, $num);
+
+		if (!$state) {
+			return false;
+		}
+
+		$where['shop_order_id'] = $shop_order_id;
+		$where['shop_id'] = $shop_id;
+		$where['factory_id'] = $factory_id;
+		$info = Dever::db('factory/order')->find($where);
+
+		if (!$info) {
+			$data = $where;
+			$data['price'] = $price;
+			$data['num'] = $num;
+			$id = $state = Dever::db('factory/order')->insert($where);
+		} else {
+			$id = $info['id'];
+
+			$data['where_id'] = $id;
+			$data['price'] = $info['price'] + $price;
+			$data['num'] = $info['num'] + $num;
+			$state = Dever::db('factory/order')->update($data);
+		}
+
+		if ($state && $id > 0) {
+			$goods_info = Dever::db('factory/order_goods')->find(array('order_id' => $id));
+			if (!$goods_info) {
+				$goods['shop_order_id'] = $shop_order_id;
+				$goods['shop_id'] = $shop_id;
+				$goods['factory_id'] = $factory_id;
+				$goods['price'] = $price;
+				$goods['num'] = $num;
+				$goods['goods_id'] = $goods_id;
+				$goods['sku_id'] = $sku_id;
+				Dever::db('factory/order_goods')->insert($goods);
+			}
+
+			return true;
+		}
+
+		return false;
+	}
+
+	# 增加销量
+    public function updateSell($factory_id, $goods_id, $sku_id, $num)
+    {
+        $up = array();
+        $up['where_factory_id'] = $factory_id;
+        $up['where_goods_id'] = $goods_id;
+        $up['sell_num'] = $order['num'];
+        $state = Dever::db('factory/goods')->updateSell($up);
+
+        if ($state) {
+        	if ($sku_id > 0) {
+	            
+	        } else {
+	            $sku_id = -1;
+	        }
+	        $upSku = $up;
+	        $upSku['where_sku_id'] = $v['sku_id'];
+	        $state = Dever::db('factory/goods_sku')->updateSell($upSku);
+        }
+
+        return $state;
+    }
+}

+ 115 - 15
app/mshop/lib/Buy.php

@@ -16,6 +16,7 @@ class Buy
         }
         
         $order_data['shop_id'] = $shop['id'];
+        $order_data['uid'] = -1;
         $order_data['mobile'] = $shop['mobile'];
         $order_data['name'] = $name;
         $order_data['num'] = $num;
@@ -148,10 +149,7 @@ class Buy
         if ($order && $order['pay_status'] == 1) {
 
             if ($status == 2) {
-                # 发消息
-                if (Dever::project('message')) {
-                    Dever::load('message/lib/data')->push(-1, $order['shop_id'], '订购成功通知', '购买成功', 1);
-                }
+                
             }
 
             $update['pay_status'] = $status;
@@ -280,25 +278,127 @@ class Buy
         $shop = Dever::db('shop/info')->find($info['shop_id']);
 
         # 获取门店分配的仓库和工厂
+        $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
+        $store = array();
+        $distance = array();
+
+        # 按照距离排序
+        foreach ($shop_store as $k => $v) {
+            $info = Dever::db('store/info')->find($v['store_id']);
+            if ($info && $info['status'] == 1) {
+                $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
+                $store[] = $info;
+            }
+        }
 
-        $store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
-        $store_id = array();
-        foreach ($store as $k => $v) {
-            $store_id[] = $v['store_id'];
+        array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
+
+
+        $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
+        $factory = array();
+        $distance = array();
+
+        # 按照距离排序
+        foreach ($shop_factory as $k => $v) {
+            $info = Dever::db('factory/info')->find($v['factory_id']);
+            if ($info && $info['status'] == 1) {
+                $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
+                $factory[] = $info;
+            }
         }
 
-        $store_id = implode(',', $store_id);
+        array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
+
+        print_r($store);
+        print_r($factory);die;
+
+        if ($store) {
+            foreach ($goods as $k => $v) {
+                # 开始找仓库
+                $find = $this->store($order_id, $shop, $store, $v);
+                if (!$find[1]) {
+                    # 仓库没有,找厂家
+                    if ($factory) {
+                        $find = $this->factory($order_id, $shop, $factory, $v);
+                    }
+                    
+                    if (!$find[1]) {
+                        # 下单失败 缺货
+                        Dever::db('shop/buy_order_goods')->update(array('where_id' => $v['id'], 'status' => 4));
+                        # 要退款吧
+                    }
+                }
+
+                if ($find[1]) {
+                    Dever::db('shop/buy_order_goods')->update(array('where_id' => $v['id'], 'status' => 2, 'type' => $find[0], 'type_id' => implode(',', $find[1])));
+                }
+            }
 
-        $where = ' id in('.$store_id.') and status = 1 ';
+            Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'status' => 3));
+        }
+    }
 
-        $sql = 'select *,round((st_distance(point(lng, lat), point('.$shop['lng'].', '.$shop['lat'].'))*111195)/1000, 2) as distance from {table} where '.$where.' order by distance asc';
+    private function store($order_id, $shop, $store, $goods, $state = 2)
+    {
+        $find = array();
+        $id = array();
+        foreach ($store as $k => $v) {
+            $where['store_id'] = $v['id'];
+            $where['goods_id'] = $goods['goods_id'];
+            $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
+            $sku = Dever::db('store/goods_sku')->getOne($where);
+
+            if ($sku) {
+                if ($goods['num'] <= $sku['total']) {
+                    # 如果库存符合,直接下单即可,生成仓库订单
+                    $state = Dever::load('store/lib/order')->pay($order_id, $shop['id'], $where['store_id'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
+                    if ($state) {
+                        $id[] = $where['store_id'];
+                        break;
+                    }
+                } elseif ($state == 1 && $sku['total'] > 0) {
+                    # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
+                    $goods['num'] = $goods['num'] - $sku['total'];
+                    # 生成仓库订单
+                    $state = Dever::load('store/lib/order')->pay($order_id, $shop['id'], $where['store_id'], $goods['price'], $sku['total'], $where['goods_id'], $where['sku_id']);
+                    if (!$state) {
+                        $goods['num'] = $goods['num'] + $sku['total'];
+                    } else {
+                        $id[] = $where['store_id'];
+                    }
+                    continue;
+
+                } else {
+                    # 直接切换到下一个仓库处理
+                    continue;
+                }
+            }
+        }
 
-        $store = Dever::db('factory/info')->$method($sql, array(), $page);
+        return array(1, $id);
+    }
 
-        # 得到所有的仓库,先验证库存
-        foreach ($goods as $k => $v) {
-            
+    private function factory($order_id, $shop, $factory, $goods)
+    {
+        $find = array();
+        $id = array();
+        foreach ($factory as $k => $v) {
+            $where['factory_id'] = $v['id'];
+            $where['goods_id'] = $goods['goods_id'];
+            $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
+            $sku = Dever::db('factory/goods_sku')->getOne($where);
+
+            if ($sku) {
+                $state = Dever::load('factory/lib/order')->pay($order_id, $shop['id'], $where['factory_id'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
+                if ($state) {
+                    $id[] = $where['factory_id'];
+                    break;
+                }
+            } else {
+                continue;
+            }
         }
 
+        return array(2, $id);
     }
 }

+ 8 - 6
app/shop/database/buy_order_goods.php

@@ -2,10 +2,12 @@
 
 $status = array
 (
-    1 => '正常',
-    2 => '缺货',
-    3 => '申请退款',
-    4 => '已退款',
+    1 => '待下单',
+    2 => '已下单',
+    3 => '已完成',
+    4 => '缺货',
+    5 => '门店退款',
+    6 => '上游退款',
 );
 
 $type = array
@@ -88,11 +90,11 @@ return array
 
         'type_id'      => array
         (
-            'type'      => 'int-11',
+            'type'      => 'varchar-200',
             'name'      => '来源id',
             'default'   => '',
             'desc'      => '来源id',
-            'match'     => 'is_numeric',
+            'match'     => 'is_string',
         ),
 
         'attr'      => array

+ 5 - 0
app/shop/src/Buy.php

@@ -344,6 +344,7 @@ class Buy extends Core
             $s = isset($sku_id[$k]) ? $sku_id[$k] : 0;
             $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;
@@ -384,6 +385,10 @@ class Buy extends Core
         $this->goods();
         $this->coupon($this->data['price'], $this->data['method'], 2);
 
+        if ($this->data['pay_method'] > 1) {
+            $cart = 2;
+        }
+
         if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
             Dever::alert('已售空');
         }

+ 5 - 123
app/store/database/order.php

@@ -67,34 +67,12 @@ return array
             'match'     => 'is_numeric',
         ),
 
-        'name'      => array
-        (
-            'type'      => 'varchar-80',
-            'name'      => '订单名称',
-            'default'   => '',
-            'desc'      => '订单名称',
-            'match'     => 'is_string',
-            'update'    => 'text',
-        ),
-
-        'order_num'      => array
-        (
-            'type'      => 'varchar-100',
-            'name'      => '订单编号',
-            'default'   => '',
-            'desc'      => '订单编号',
-            'match'     => 'is_string',
-            'update'    => 'text',
-            'search'    => 'fulltext',
-            'list'      => true,
-        ),
-
         'price'      => array
         (
             'type'      => 'varchar-50',
-            'name'      => '购买价格',
+            'name'      => '总价格',
             'default'   => '',
-            'desc'      => '购买价格',
+            'desc'      => '总价格',
             'match'     => 'option',
             'update'    => 'text',
             'list'        => true,
@@ -103,46 +81,14 @@ return array
         'num'        => array
         (
             'type'      => 'int-11',
-            'name'      => '购买数量',
+            'name'      => '数量',
             'default'   => '',
-            'desc'      => '购买数量',
+            'desc'      => '数量',
             'match'     => 'is_numeric',
             'search'    => 'select',
             'list'        => true,
         ),
 
-        'pay_id'      => array
-        (
-            'type'      => 'varchar-100',
-            'name'      => '支付订单ID',
-            'default'   => '',
-            'desc'      => '付款订单id',
-            'match'     => 'is_string',
-            //'update'    => 'text',
-            //'search'    => 'fulltext',
-            //'list'        => true,
-        ),
-
-        'pay_status'        => array
-        (
-            'type'      => 'tinyint-1',
-            'name'      => '支付状态',
-            'default'   => '1',
-            'desc'      => '请选择支付状态',
-            'match'     => 'is_numeric',
-            'option'    => $pay_status,
-        ),
-
-        'pay_price'      => array
-        (
-            'type'      => 'varchar-50',
-            'name'      => '付款金额',
-            'default'   => '',
-            'desc'      => '付款金额',
-            'match'     => 'option',
-            'update'    => 'text',
-        ),
-
         'info'      => array
         (
             'type'      => 'varchar-300',
@@ -152,71 +98,7 @@ return array
             'match'     => 'option',
             'update'    => 'textarea',
         ),
-
-        'note'     => array
-        (
-            'type'      => 'tinyint-1',
-            'name'      => '是否发送状态提醒-1未发送,2已发送',
-            'default'   => '1',
-            'desc'      => '请选择状态',
-            'match'     => 'is_numeric',
-        ),
-
-        'notice'        => array
-        (
-            'type'      => 'int-11',
-            'name'      => '模板消息提醒次数',
-            'default'   => '0',
-            'desc'      => '模板消息提醒次数',
-            'match'     => 'is_numeric',
-        ),
-
-        'tk_pic'     => array
-        (
-            'type'      => 'varchar-150',
-            'name'      => '退款截图',
-            'default'   => '',
-            'desc'      => '退款截图',
-            'match'     => 'is_string',
-            'update'    => 'image',
-            'key'       => 1
-        ),
-
-        'tk_time'       => array
-        (
-            'type'      => 'int-11',
-            'name'      => '退款时间',
-            'default'   => '',
-            'desc'      => '退款时间',
-            'match'     => 'option',
-            //'list'        => true,
-            //'update'    => 'date',
-            'callback'  => 'maketime',
-            'show'      => 'status=5',
-        ),
-
-        'tk_admin'      => array
-        (
-            'type'      => 'int-11',
-            'name'      => '退款审核人',
-            'default'   => '1',
-            'desc'      => '退款审核人',
-            'match'     => 'option',
-            //'list'        => true,
-            'show'      => 'status=5',
-        ),
-
-        'tk_desc'       => array
-        (
-            'type'      => 'varchar-300',
-            'name'      => '退款备注',
-            'default'   => '',
-            'desc'      => '退款备注',
-            'match'     => 'option',
-            'update'    => 'textarea',
-            //'show'        => 'status=5',
-        ),
-
+        
         'state'     => array
         (
             'type'      => 'tinyint-1',

+ 79 - 0
app/store/lib/Order.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace Store\Lib;
+
+use Dever;
+
+class Order
+{
+	# 单个商品下单
+	public function pay($shop_order_id, $shop_id, $store_id, $price, $num, $goods_id, $sku_id)
+	{
+		# 先更新库存
+		$state = $this->updateSell($store_id, $goods_id, $sku_id, $num);
+
+		if (!$state) {
+			return false;
+		}
+
+		$where['shop_order_id'] = $shop_order_id;
+		$where['shop_id'] = $shop_id;
+		$where['store_id'] = $store_id;
+		$info = Dever::db('store/order')->find($where);
+
+		if (!$info) {
+			$data = $where;
+			$data['price'] = $price;
+			$data['num'] = $num;
+			$id = $state = Dever::db('store/order')->insert($where);
+		} else {
+			$id = $info['id'];
+
+			$data['where_id'] = $id;
+			$data['price'] = $info['price'] + $price;
+			$data['num'] = $info['num'] + $num;
+			$state = Dever::db('store/order')->update($data);
+		}
+
+		if ($state && $id > 0) {
+			$goods_info = Dever::db('store/order_goods')->find(array('order_id' => $id));
+			if (!$goods_info) {
+				$goods['shop_order_id'] = $shop_order_id;
+				$goods['shop_id'] = $shop_id;
+				$goods['store_id'] = $store_id;
+				$goods['price'] = $price;
+				$goods['num'] = $num;
+				$goods['goods_id'] = $goods_id;
+				$goods['sku_id'] = $sku_id;
+				Dever::db('store/order_goods')->insert($goods);
+			}
+
+			return true;
+		}
+
+		return false;
+	}
+
+	# 减少库存,增加销量
+    public function updateSell($store_id, $goods_id, $sku_id, $num)
+    {
+        $up = array();
+        $up['where_store_id'] = $store_id;
+        $up['where_goods_id'] = $goods_id;
+        $up['sell_num'] = $order['num'];
+        $state = Dever::db('store/goods')->updateSell($up);
+
+        if ($state) {
+        	if ($sku_id > 0) {
+	            
+	        } else {
+	            $sku_id = -1;
+	        }
+	        $upSku = $up;
+	        $upSku['where_sku_id'] = $v['sku_id'];
+	        $state = Dever::db('store/goods_sku')->updateSell($upSku);
+        }
+
+        return $state;
+    }
+}