shish 5 лет назад
Родитель
Сommit
ee15228f8b

+ 2 - 1
app-ghs/controllers/OrderItemController.php

@@ -27,7 +27,8 @@ class OrderItemController extends BaseController
         OrderClass::valid($info, $this->shopId);
 
         //添加修改花材
-		OrderItemClass::replace($info, $product);
+        $orderSn = $info['orderSn'] ?? '';
+        OrderItemClass::replaceItem($orderSn, $product);
         util::complete();
     }
 

+ 1 - 0
biz-ghs/admin/classes/AdminClass.php

@@ -85,6 +85,7 @@ class AdminClass extends BaseClass
                     'mobile' => $mobile,
                     'currentShopId' => $shopId,
                     'super' => 1,
+                    'apply' => 2,
                 ];
                 self::updateById($adminId, $adminData);
                 $shopData = [

+ 61 - 47
biz-ghs/order/classes/OrderClass.php

@@ -71,7 +71,7 @@ class OrderClass extends BaseClass
     const CLEAR_HAS_GATHERING = 2;
 
     //添加订单 ssh 2019.12.5
-    public static function addOrder($data, $shop,$customOrder=false)
+    public static function addOrder($data, $shop, $customOrder = false)
     {
         $sjId = $data['merchantId'] ?? 0;
         $shopId = $shop['id'] ?? 0;
@@ -83,13 +83,15 @@ class OrderClass extends BaseClass
 
         //花材列表
         $product = $data['product'];
-        OrderItemClass::replace(['orderSn' => $orderSn], $product, true);
+        $respond = OrderItemClass::replaceItem($orderSn, $product);
+
+        $data['bigNum'] = $respond['bigNum'] ?? 0;
+        $data['smallNum'] = $respond['smallNum'] ?? 0;
+        $data['actPrice'] = $respond['price'] ?? 0;
 
         //将花店每月的总订单数作为编号
         $riseNum = StatOrderCountClass::addOrder($month, $sjId);
         $data['sendNum'] = $riseNum;
-        $data['num'] = 1;
-        $data['smallNum'] = 1;
         $data['sendTime'] = isset($data['sendTime']) && !empty($data['sendTime']) ? $data['sendTime'] : '0000-00-00 00:00:00';
         //订单30分钟后过期
         $data['deadline'] = date("Y-m-d H:i:s", (time() + 1800));
@@ -100,10 +102,12 @@ class OrderClass extends BaseClass
         $weight = 0;
         foreach ($product as $key => $val) {
             $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
-            $weight = bcadd($w,$weight,2);
+            $weight = bcadd($w, $weight, 2);
         }
-
         $data['weight'] = $weight;
+
+        //价格和数据
+
         $return = self::add($data);
 
         //总订单和待付款订单增加
@@ -115,7 +119,7 @@ class OrderClass extends BaseClass
         $sjAsset['unPayOrder']++;
         MerchantAssetClass::updateByCondition(['merchantId' => $sjId], ['totalOrder' => $sjAsset['totalOrder'], 'unPayOrder' => $sjAsset['unPayOrder']]);
 
-        if($customOrder == false){
+        if ($customOrder == false) {
 
             //0正常订单 1欠款
             $debt = $data['debt'] ?? 0;
@@ -161,11 +165,13 @@ class OrderClass extends BaseClass
         if ($showButton == false) {
             return [];
         }
-        //列表页显示的按钮
         if ($isList) {
-            $item = ['type' => 'item', 'show' => 1, 'disable' => 1];
-            $send = ['type' => 'send', 'show' => 1, 'disable' => 0];
-            $selfGet = ['type' => 'selfGet', 'show' => 1, 'disable' => 1];
+
+            //列表页显示的按钮
+
+            $item = ['type' => 'item', 'show' => 0, 'disable' => 1, 'name' => '改花材'];
+            $send = ['type' => 'send', 'show' => 1, 'disable' => 0, 'name' => '发货'];
+            $selfGet = ['type' => 'selfGet', 'show' => 1, 'disable' => 1, 'name' => '自取'];
             if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
                 //待付款订单,改花材按钮可点击
                 $item['disable'] = 0;
@@ -177,49 +183,57 @@ class OrderClass extends BaseClass
                 $item['disable'] = 1;
                 $selfGet['disable'] = 0;
             }
+            //自取订单发货按钮置灰
+            if ($order['sendType'] == self::SEND_TYPE_NO) {
+                $send['disable'] = 1;
+            }
             return [$item, $send, $selfGet];
-        }
+        } else {
 
-        //待确认订单显示延期付和已付款
-        if ($order['status'] == OrderClass::ORDER_STATUS_UN_PAY) {
-            $debtPay = ['type' => 'debtPay', 'show' => 1, 'disable' => 0];
-            $hasPay = ['type' => 'hasPay', 'show' => 1, 'disable' => 0];
-            return [$debtPay, $hasPay];
-        }
+            //详情页显示的按钮
 
-        //待配送订单显示打印、发货和自取
-        if ($order['status'] == OrderClass::ORDER_STATUS_UN_SEND) {
-            //没有花材的订单按钮全部置灰色不可点
-            $print = ['type' => 'print', 'show' => 0, 'disable' => 1];
-            $item = ['type' => 'item', 'show' => 1, 'disable' => 1];
-            $selfGet = ['type' => 'selfGet', 'show' => 1, 'disable' => 1];
-            $send = ['type' => 'send', 'show' => 1, 'disable' => 1];
-            if ($order['hasItem'] == 2) {
+            //待确认订单显示延期付和已付款
+            if ($order['status'] == OrderClass::ORDER_STATUS_UN_PAY) {
+                $debtPay = ['type' => 'debtPay', 'show' => 1, 'disable' => 0];
+                $hasPay = ['type' => 'hasPay', 'show' => 1, 'disable' => 0];
+                return [$debtPay, $hasPay];
+            }
+
+            //待配送订单显示打印、发货和自取
+            if ($order['status'] == OrderClass::ORDER_STATUS_UN_SEND) {
+                //没有花材的订单按钮全部置灰色不可点
+                $print = ['type' => 'print', 'show' => 0, 'disable' => 1];
+                $item = ['type' => 'item', 'show' => 0, 'disable' => 1];
+                $selfGet = ['type' => 'selfGet', 'show' => 1, 'disable' => 1];
+                $send = ['type' => 'send', 'show' => 1, 'disable' => 1];
+                if ($order['hasItem'] == 2) {
+                    return [$print, $item, $selfGet, $send];
+                }
+                $print['disable'] = 0;
+                $selfGet['disable'] = 0;
+                $send['disable'] = 0;
+                $item['disable'] = 0;
                 return [$print, $item, $selfGet, $send];
             }
-            $print['disable'] = 0;
-            $selfGet['disable'] = 0;
-            $send['disable'] = 0;
-            $item['disable'] = 0;
-            return [$print, $item, $selfGet, $send];
-        }
-        //配送中显示按钮
-        if ($order['status'] == OrderClass::ORDER_STATUS_SENDING) {
-            $print = ['type' => 'print', 'show' => 0, 'disable' => 1];
-            $send = ['type' => 'send', 'show' => 1, 'disable' => 0];
-            return [$print, $send];
-        }
-        //完成
-        if ($order['status'] == OrderClass::ORDER_STATUS_COMPLETE) {
-            $print = ['type' => 'print', 'show' => 0, 'disable' => 1];
-            $send = ['type' => 'send', 'show' => 1, 'disable' => 0];
-            //自取订单不显示发货按钮
-            if ($order['sendType'] == self::SEND_TYPE_NO) {
-                $send['show'] = 0;
+            //配送中显示按钮
+            if ($order['status'] == OrderClass::ORDER_STATUS_SENDING) {
+                $print = ['type' => 'print', 'show' => 0, 'disable' => 1];
+                $send = ['type' => 'send', 'show' => 1, 'disable' => 0];
+                return [$print, $send];
+            }
+            //完成
+            if ($order['status'] == OrderClass::ORDER_STATUS_COMPLETE) {
+                $print = ['type' => 'print', 'show' => 0, 'disable' => 1];
+                $send = ['type' => 'send', 'show' => 1, 'disable' => 0];
+                //自取订单不显示发货按钮
+                if ($order['sendType'] == self::SEND_TYPE_NO) {
+                    $send['show'] = 0;
+                }
+                return [$print, $send];
             }
-            return [$print, $send];
+            return [];
         }
-        return [];
+
     }
 
     //显示进度 ssh 2021.3.1

+ 37 - 56
biz-ghs/order/classes/OrderItemClass.php

@@ -10,63 +10,44 @@ use bizGhs\base\classes\BaseClass;
 
 class OrderItemClass extends BaseClass
 {
-	
-	public static $baseFile = '\bizGhs\order\models\OrderItem';
-	
-	//获取订单的花材 ssh 2021.3.2
-	public static function getOrderItem($orderSn)
-	{
-		
-		$list = self::getAllByCondition(['orderSn' => $orderSn], null, '*');
-		return self::groupInfo($list);
-	}
-	
-	//组合信息 ssh 2021.3.2
-	public static function groupInfo($list)
-	{
-		if (empty($list)) {
-			return [];
-		}
-		foreach ($list as $key => $val) {
-			$cover = imgUtil::getPrefix() . '/hhb_small.png';
-			if (isset($val['cover']) && !empty($val['cover'])) {
-				$cover = imgUtil::getPrefix() . $val['cover'];
-			}
-			$list[$key]['cover'] = $cover;
-		}
-		return $list;
-	}
-	
-	//添加修改花材 ssh 2021.1.22
-	public static function replace($info, $product, $add = false)
-	{
-		//快速付款没有商品
-		if (empty($product)) {
-			return false;
-		}
-		if ($add) {
-			$orderSn = $info['orderSn'];
-		} else {
-			if (isset($info['sendStatus']) == false) {
-				util::fail('订单无配送状态');
-			}
-			//确认当前订单是否还可以修改花材
-			if ($info['sendStatus'] >= OrderClass::ORDER_STATUS_SENDING) {
-				util::fail('此订单已经不能修改花材');
-			}
-		}
 
-		//价格等信息
-		$product = ProductClass::computedPriceByItemInfo($product);
-		$orderSn = $info['orderSn'];
-		self::deleteByCondition(['orderSn' => $orderSn]);
-		foreach ($product as $key => $val) {
-			$product[$key]['orderSn'] = $orderSn;
-		}
-		self::batchAdd($product);
+    public static $baseFile = '\bizGhs\order\models\OrderItem';
 
-		//未发货前可以修改花材。未发货前未付款的总金额要变化,未发货前已付款的,只改变成本,成本超过总金额的,则不允许修改。  todo @lqh
-		//修改价格
-	}
+    //获取订单的花材 ssh 2021.3.2
+    public static function getOrderItem($orderSn)
+    {
+
+        $list = self::getAllByCondition(['orderSn' => $orderSn], null, '*');
+        return self::groupInfo($list);
+    }
+
+    //组合信息 ssh 2021.3.2
+    public static function groupInfo($list)
+    {
+        if (empty($list)) {
+            return [];
+        }
+        foreach ($list as $key => $val) {
+            $cover = imgUtil::getPrefix() . '/hhb_small.png';
+            if (isset($val['cover']) && !empty($val['cover'])) {
+                $cover = imgUtil::getPrefix() . $val['cover'];
+            }
+            $list[$key]['cover'] = $cover;
+        }
+        return $list;
+    }
+
+    //添加修改花材 ssh 2021.1.22
+    public static function replaceItem($orderSn, $product)
+    {
+        $respond = ProductClass::formatProductInfo($product);
+        $product = $respond['product'];
+        self::deleteByCondition(['orderSn' => $orderSn]);
+        foreach ($product as $key => $val) {
+            $product[$key]['orderSn'] = $orderSn;
+        }
+        self::batchAdd($product);
+        return $respond;
+    }
 
 }

+ 461 - 457
biz-ghs/product/classes/ProductClass.php

@@ -14,68 +14,68 @@ use yii\di\ServiceLocator;
 
 class ProductClass extends BaseClass
 {
-	use OrderTrait;
-	public static $baseFile = '\bizGhs\product\models\Product';
-	const PRICE_LABEL_AUTO = 1; //自动调价
-	const PRICE_LABEL_CHANGE = 2; //改价
-	const LOCK_STOCK = 'lock_stock';//修改库存锁
-	const LOCK_PRICE = 'lock_price'; //改价锁
-
-	//根据itemIds获取相应的花材信息
-	public static function getProductInfoByItemIds(array $itemIds, $shopId)
-	{
-		$where = ['itemId' => ['in', $itemIds], 'delStatus' => 0, 'shopId' => $shopId];
-		$data = self::getAllList('*', $where);
-		$data = self::groupItemBaseInfo($data);
-		return $data;
-	}
-
-	//根据花材ID,获取花材名称,图片
-	public static function groupItemBaseInfo($list)
-	{
-		//取得itemIds
-		$itemIds = array_column($list, 'itemId');
-		//获取相应的图片,名称,py
-		$where = ['id' => ['in', $itemIds]];
-		$data = xhItemService::getAllByCondition($where, null, '*');
-		if (!empty($data)) {
-			$data = array_column($data, NULL, 'id');
-			foreach ($list as $k => $v) {
-				if (isset($data[$v['itemId']]['cover']) && !empty($data[$v['itemId']]['cover'])) {
-					$cover = self::groupImg($data[$v['itemId']]['cover']);
-				} else {
-					//没有图片时取默认图片 ssh 2021.2.18
-					$cover = imgUtil::getPrefix() . '/hhb_small.png';
-				}
-				$unitNum = $data[$v['itemId']]['unitNum'] ?? 0;
-				$stockInfo = self::formatStock($v['stock'], $unitNum);
-				$list[$k]['cover'] = $cover;
-				$list[$k]['bigNum'] = $stockInfo['bigNum'];
-				$list[$k]['smallNum'] = $stockInfo['smallNum'];
-				$list[$k]['itemName'] = $data[$v['itemId']]['name'] ?? '';
-				$list[$k]['py'] = $data[$v['itemId']]['py'] ?? '';
-				$list[$k]['ratio'] = $unitNum ?? 0;
-				//换算大小单位对应价格
-				$list[$k]['bigPrice'] = $v['price']; //大单位价格就是product表存的价格
-				$smallPrice = 0;
-				if ($unitNum) {
-					$smallPrice = bcdiv($v['price'], $unitNum, 2);
-				}
-				$list[$k]['smallPrice'] = $smallPrice;
-				$list[$k]['bigUnit'] = $data[$v['itemId']]['bigUnit'];
-				$list[$k]['smallUnit'] = $data[$v['itemId']]['smallUnit'];
-			}
-		}
-		return $list;
-	}
-
-	//花材图片,名称,大小单位,小单位价格
+    use OrderTrait;
+    public static $baseFile = '\bizGhs\product\models\Product';
+    const PRICE_LABEL_AUTO = 1; //自动调价
+    const PRICE_LABEL_CHANGE = 2; //改价
+    const LOCK_STOCK = 'lock_stock';//修改库存锁
+    const LOCK_PRICE = 'lock_price'; //改价锁
+
+    //根据itemIds获取相应的花材信息
+    public static function getProductInfoByItemIds(array $itemIds, $shopId)
+    {
+        $where = ['itemId' => ['in', $itemIds], 'delStatus' => 0, 'shopId' => $shopId];
+        $data = self::getAllList('*', $where);
+        $data = self::groupItemBaseInfo($data);
+        return $data;
+    }
+
+    //根据花材ID,获取花材名称,图片
+    public static function groupItemBaseInfo($list)
+    {
+        //取得itemIds
+        $itemIds = array_column($list, 'itemId');
+        //获取相应的图片,名称,py
+        $where = ['id' => ['in', $itemIds]];
+        $data = xhItemService::getAllByCondition($where, null, '*');
+        if (!empty($data)) {
+            $data = array_column($data, NULL, 'id');
+            foreach ($list as $k => $v) {
+                if (isset($data[$v['itemId']]['cover']) && !empty($data[$v['itemId']]['cover'])) {
+                    $cover = self::groupImg($data[$v['itemId']]['cover']);
+                } else {
+                    //没有图片时取默认图片 ssh 2021.2.18
+                    $cover = imgUtil::getPrefix() . '/hhb_small.png';
+                }
+                $unitNum = $data[$v['itemId']]['unitNum'] ?? 0;
+                $stockInfo = self::formatStock($v['stock'], $unitNum);
+                $list[$k]['cover'] = $cover;
+                $list[$k]['bigNum'] = $stockInfo['bigNum'];
+                $list[$k]['smallNum'] = $stockInfo['smallNum'];
+                $list[$k]['itemName'] = $data[$v['itemId']]['name'] ?? '';
+                $list[$k]['py'] = $data[$v['itemId']]['py'] ?? '';
+                $list[$k]['ratio'] = $unitNum ?? 0;
+                //换算大小单位对应价格
+                $list[$k]['bigPrice'] = $v['price']; //大单位价格就是product表存的价格
+                $smallPrice = 0;
+                if ($unitNum) {
+                    $smallPrice = bcdiv($v['price'], $unitNum, 2);
+                }
+                $list[$k]['smallPrice'] = $smallPrice;
+                $list[$k]['bigUnit'] = $data[$v['itemId']]['bigUnit'];
+                $list[$k]['smallUnit'] = $data[$v['itemId']]['smallUnit'];
+            }
+        }
+        return $list;
+    }
+
+    //花材图片,名称,大小单位,小单位价格
     public static function groupProductInfo($list)
     {
         foreach ($list as $k => $v) {
-            if($v['cover']){
+            if ($v['cover']) {
                 $cover = self::groupImg($v['cover']);
-            }else{
+            } else {
                 $cover = imgUtil::getPrefix() . '/hhb_small.png';
             }
             $ratio = $v['ratio'];
@@ -98,419 +98,423 @@ class ProductClass extends BaseClass
     }
 
 
-	//根据花材ID,库存=》  计算出多少扎,多少支
-	public static function formatStockByItemId($itemId, $stock)
-	{
-		$unitNum = xhItemService::getItemUnitNum($itemId);
-		return self::formatStock($stock, $unitNum);
-	}
-
-	//花材的库存转换: 库存是小数,根据各个花材的unitNum, 计算出多少扎,多少支
-	public static function formatStock($stock, $unitNum)
-	{
-		$pn = true; //正数
-		if ($stock < 0) {
-			$pn = false;
-			$stock = abs($stock);
-		}
-		$bigNum = floor($stock); // 扎
-		$stockArr = explode('.', $stock);
-		$smallNum = 0; //支
-		if (count($stockArr) === 2) {
-			$smallNum = bcmul(($stock - $bigNum), $unitNum);
-		}
-		if (!$pn) {
-			$bigNum *= -1;
-			$smallNum *= -1;
-		}
-		return [
-			'bigNum' => $bigNum,
-			'smallNum' => $smallNum,
-		];
-	}
-
-	/**
-	 * 根据 花材ID,将花材大小数量 换算成小数
-	 * @param $bigNum 大单位 数量
-	 * @param $smallNum 小单位 数量
-	 * @param $itemId  花材ID
-	 * @return string
-	 */
-	public static function mergeItemNumByItemId($bigNum, $smallNum, $itemId)
-	{
-		$unitNum = xhItemService::getItemUnitNum($itemId);
-		return self::mergeItemNum($bigNum, $smallNum, $unitNum);
-	}
-
-	//花材扎、支 合并转换 小数
-
-	/**
-	 * @param $bigNum  大单位 数量
-	 * @param $smallNum  小单位 数量
-	 * @param $unitNum  花材比例
-	 * @return string
-	 */
-	public static function mergeItemNum($bigNum, $smallNum, $unitNum)
-	{
-		$stockDecimal = 0;
-		if ($unitNum > 0) {
-			$stockDecimal = bcdiv($smallNum, $unitNum, 2);
-		}
-		$itemNum = bcadd($bigNum, $stockDecimal, 2);
-		return $itemNum;
-	}
-
-
-	//获取常用的itemIds
-	public static function getOftenItemIds($shopId)
-	{
-		$data = self::getLimitList("itemId", ['shopId' => $shopId], 10,'actualSold desc');
-		if ($data) {
-			$data = array_column($data, 'itemId');
-		}
-		return $data;
-	}
-
-
-
-	//加库存,允许负库存,负数数量(盘点时)
-
-	/**
-	 * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
-	 * @param $itemNumBundle  花材数量(扎)
-	 * @param $itemNumPiece  花材数量(支)
-	 * @return bool
-	 */
-	public static function addStock($productId, $itemNumBundle, $itemNumPiece)
-	{
-		$productData = self::getById($productId);
-		if (!$productData) {
-			util::fail("花材不存在");
-		}
-		$itemId = $productData['itemId'];
-		$unitNum = xhItemService::getItemUnitNum($itemId);
-		//转成小数,加库存
-		$itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $unitNum);
-		$newStock = bcadd($productData['stock'], $itemNum, 2);
-		self::updateStockById($productId, $newStock);
-		return true;
-	}
-
-
-	//todo 库存修改 加锁
-
-	/**
-	 * 对商品ID 加库存
-	 * @param $productId  商品ID
-	 * @param $itemNum  数量
-	 * @return bool
-	 */
-	public static function addStockByItemNum($productId, $itemNum)
-	{
-		$productData = self::getById($productId, true);
-		if (!$productData) {
-			util::fail("花材不存在");
-		}
-		$newStock = bcadd($productData->stock, $productData['ratio'], 2);
-		self::updateStockById($productId, $newStock);
-		return true;
-	}
-
-	//减库存,允许负库存,负数数量(盘点时)
-	//todo 库存修改 加锁
-	/**
-	 * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
-	 * @param $itemNumBundle  花材数量(扎)
-	 * @param $itemNumPiece  花材数量(支)
-	 * @return bool
-	 */
-	public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece)
-	{
-		$productData = self::getById($productId, true);
-		if (!$productData) {
-			util::fail("花材不存在");
-		}
-		$ratio = $productData['ratio'];
-		//转成小数,更新库存
-		$itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $ratio);
-		$newStock = bcsub($productData->stock, $itemNum, 2);
-		self::updateStockById($productId, $newStock);
-		return true;
-	}
-
-
-	//根据数量减库存
-	//todo 库存修改 加锁
-	/**
-	 * @param $productId
-	 * @param $itemNum
-	 * @return bool
-	 */
-	public static function decreaseStockByItemNum($productId, $itemNum)
-	{
-		$productData = self::getById($productId);
-		if (!$productData) {
-			util::fail("花材不存在");
-		}
-		$newStock = bcsub($productData['stock'], $itemNum, 2);
-		self::updateStockById($productId, $newStock);
-		return true;
-	}
-
-
-
-	//计算价格
-
-	/**
-	 * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
-	 * @param $bigNum  花材数量(扎)
-	 * @param $smallNum  花材数量(支)
-	 * @return float
-	 */
-	public static function computedPrice($productId, $bigNum, $smallNum)
-	{
-		//获取itemId的价格
-		$productData = self::getById($productId);
-		if (!$productData) {
-			util::fail("花材不存在");
-		}
-		$price = $productData['price'];
-		$itemId = $productData['itemId'];
-		$unitNum = xhItemService::getItemUnitNum($itemId);
-		//换算出一支多少钱
-		$piecePrice = 0;
-		if ($unitNum > 0) {
-			//有支
-			$unitPrice = bcdiv($price, $unitNum, 2);
-			$piecePrice = bcmul($unitPrice, $smallNum, 2);
-		}
-		$bundlePrice = bcmul($price, $bigNum, 2);
-
-		$total = bcadd($bundlePrice, $piecePrice, 2);
-		return $total;
-	}
-
-	//根据门店ID, 平台花材ID, 获取门店的花材ID
-	public static function getGhsProductId($shopId, $itemId)
-	{
-		$data = self::getGhsProductDataByItemId($shopId, $itemId);
-		if ($data) {
-			return $data['id'];
-		}
-		return 0;
-	}
-
-	public static function getGhsProductDataByItemId($shopId, $itemId)
-	{
-		$where = [
-			'shopId' => $shopId,
-			'itemId' => $itemId,
-		];
-		$data = self::getByCondition($where);
-		return $data;
-	}
-
-	//根据多个itemIds shopId 获取对应的 productId  linqh 2021.1.24
-	public static function getGhsProductDataByItemIds($shopId, $itemIds)
-	{
-		$where = ['itemId' => ['in', $itemIds], 'shopId' => $shopId];
-		$data = self::getAllByCondition($where, null, "*");
-		return $data;
-	}
-
-	//补全itemId (针对入库时 当前门店下无对应的花材 itemId)  linqh 2021.1.24
-	public static function complementProduct($merchantId, $shopId, $itemInfo)
-	{
-		$itemIds = array_column($itemInfo, "itemId");
-		$itemData = array_column($itemInfo, null, "itemId");
-		$productData = self::getGhsProductDataByItemIds($shopId, $itemIds);
-
-		if (count($productData) !== count($itemIds)) {
-			$productDataItemIds = array_column($productData, 'itemId');
-			//取差集
-			$diffItemIds = array_diff($itemIds, $productDataItemIds);
-			if ($diffItemIds) {
-				//新增到product表,price = itemPrice (出库时的售价), stock=0
-				foreach ($diffItemIds as $v) {
-					$addData = [];
-					$addData['merchantId'] = $merchantId;
-					$addData['shopId'] = $shopId;
-					$addData['itemId'] = $v;
-					$addData['price'] = $itemData[$v]['itemPrice'] ?? 0;
-					self::add($addData);
-				}
-			}
-		}
-	}
-
-	//有效花材判断,包括id有效、同个门店等 ssh 2021.1.19
-	public static function valid($list, $shopId)
-	{
-		$ids = array_unique(array_filter(array_column($list, 'productId')));
-		$productList = self::getByIds($ids, null, 'id');
-		if (!empty($productList)) {
-			foreach ($productList as $product) {
-				if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
-					util::fail('只能选择同一家的商品下单');
-				}
-			}
-			if (count($productList) != count($ids)) {
-				util::fail('存在无效商品');
-			}
-		} else {
-			util::fail('商品不存在');
-		}
-	}
-
-	//通过ids 批量获取花材商品的信息
-	public static function getProductByIds($ids)
-	{
-		return self::getByIds($ids);
-	}
-
-	//修改花材商品的库存: 统一方法
-	//todo 库存修改 加锁
-	public static function updateStockById($id, $stock, $adminId = 0)
-	{
-		$key = self::LOCK_STOCK . '_' . $id;
-		$lock = util::lock($key);
-		if (!$lock) {
-			util::fail("系统繁忙中,请稍后再试!");
-		}
-		$data['stock'] = $stock;
-		if ($adminId) {
-			$data['adminId'] = $adminId;
-		}
-		$res = self::updateById($id, $data);
-		util::unlock($key);
-		return $res;
-	}
-
-	//订单退回库存
-	public static function backStockByOrderItemInfo($orderItemInfo)
-	{
-		foreach ($orderItemInfo as $v) {
-			ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
-		}
-	}
-
-
-	//库存告警的itemIds (库存数量少于告警数量)  linqh 2021.1.26
-	public static function getItemIdsWarning($merchantId, $shopId)
-	{
-		//获取当前门店下的 itemId=>stock
-		$productData = ProductClass::getAllList("itemId,stock", ['shopId' => $shopId]);
-		$productItemStock = [];
-		if ($productData) {
-			foreach ($productData as $v) {
-				$productItemStock[$v['itemId']] = $v['stock'];
-			}
-		}
-
-		//获取merchantId 下 对应的itemId=>stockWaring
-		$itemData = ItemClass::getAllList("itemId,stockWarning", ['merchantId' => $merchantId]);
-		$itemStockWaring = [];
-		if ($itemData) {
-			foreach ($itemData as $v) {
-				$itemStockWaring[$v['itemId']] = $v['stockWarning'];
-			}
-		}
-		//取库存数量少于告警数量 的itemId
-		$itemIds = [];
-		foreach ($productItemStock as $k => $v) {
-			$stockWaring = $itemStockWaring[$k] ?? 0;
-			if ($v <= $stockWaring) {
-				$itemIds[] = $k;
-			}
-		}
-		return $itemIds;
-	}
-
-	//获取当前门店下商品信息 linqh 2021.1.28
-	public static function getProductData($id, $shopId, $obj = false)
-	{
-		return ProductClass::getByCondition(['id' => $id, 'shopId' => $shopId], $obj);
-	}
-
-	//修改价格 linqh 2021.1.28
-	public static function changePrice($id, $shopId, $price, $priceLabel)
-	{
-		$res = ProductClass::updateByCondition(['id' => $id, 'shopId' => $shopId], ['price' => $price, 'priceLabel' => $priceLabel]);
-		return $res;
-	}
-
-	//下单时计算商品的价格  linqh 2021.1.28
-	//itemInfo : [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":5,"productId":4,"bigNum":1,"smallNum":5}]
-	//返回 [{"productId":3,"price":2.1}]
-	public static function computedPriceByItemInfo(array $itemInfo)
-	{
-		$data = [];
-		if (empty($itemInfo)) {
-			util::fail('请选择花材');
-		}
-		$itemInfo = self::mergeItemInfo($itemInfo);
-		$productIds = array_column($itemInfo, 'productId');
-		$productData = ProductClass::getProductByIds($productIds);
-		$productData = array_column($productData, NULL, 'id');
-		//$itemIds = array_column($productData, 'itemId');
-		//$itemData = xhItemService::getByIds($itemIds);
-//		if (empty($itemData)) {
-//			util::fail('请选择花材');
-//		}
-		//$itemData = array_column($itemData, NULL, 'id');
-		foreach ($itemInfo as $v) {
-			$productId = $v['productId'];
-			$itemId = $productData[$productId]['itemId'] ?? 0;
-			$unitNum = $productData[$productId]['ratio'] ?? 0;
-			//大小数量合并多少扎
-			$itemNum = self::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);
-			//售卖价格
-			$itemPrice = $productData[$productId]['price'] ?? 0;
-			//合计价格
-			$price = bcmul($itemNum, $itemPrice, 2);
-			$tmp = [];
-			$tmp['productId'] = $productId;
-			$tmp['price'] = $price;
-
-			$tmp['name'] = $productData[$productId]['name'] ?? '';
-			$tmp['smallUnit'] = $productData[$productId]['smallUnit'];
-			$tmp['bigUnit'] = $productData[$productId]['bigUnit'];
-			$tmp['cover'] = $productData[$productId]['cover'] ?? '';
-			$tmp['itemId'] = $itemId;
-			$tmp['rank'] = $productData[$productId]['rank'] ?? '';
-			$tmp['unitPrice'] = $itemPrice;
-			$tmp['num'] = $itemNum;
-			$tmp['bigNum'] = $v['bigNum'];
-			$tmp['smallNum'] = $v['smallNum'];
-
-			$data[] = $tmp;
-		}
-		return $data;
-	}
-
-	//  //修改某个花材的成本价(采购之后要用到) linqh 2021.04.06
+    //根据花材ID,库存=》  计算出多少扎,多少支
+    public static function formatStockByItemId($itemId, $stock)
+    {
+        $unitNum = xhItemService::getItemUnitNum($itemId);
+        return self::formatStock($stock, $unitNum);
+    }
+
+    //花材的库存转换: 库存是小数,根据各个花材的unitNum, 计算出多少扎,多少支
+    public static function formatStock($stock, $unitNum)
+    {
+        $pn = true; //正数
+        if ($stock < 0) {
+            $pn = false;
+            $stock = abs($stock);
+        }
+        $bigNum = floor($stock); // 扎
+        $stockArr = explode('.', $stock);
+        $smallNum = 0; //支
+        if (count($stockArr) === 2) {
+            $smallNum = bcmul(($stock - $bigNum), $unitNum);
+        }
+        if (!$pn) {
+            $bigNum *= -1;
+            $smallNum *= -1;
+        }
+        return [
+            'bigNum' => $bigNum,
+            'smallNum' => $smallNum,
+        ];
+    }
+
+    /**
+     * 根据 花材ID,将花材大小数量 换算成小数
+     * @param $bigNum 大单位 数量
+     * @param $smallNum 小单位 数量
+     * @param $itemId  花材ID
+     * @return string
+     */
+    public static function mergeItemNumByItemId($bigNum, $smallNum, $itemId)
+    {
+        $unitNum = xhItemService::getItemUnitNum($itemId);
+        return self::mergeItemNum($bigNum, $smallNum, $unitNum);
+    }
+
+    //花材扎、支 合并转换 小数
+
+    /**
+     * @param $bigNum  大单位 数量
+     * @param $smallNum  小单位 数量
+     * @param $unitNum  花材比例
+     * @return string
+     */
+    public static function mergeItemNum($bigNum, $smallNum, $unitNum)
+    {
+        $stockDecimal = 0;
+        if ($unitNum > 0) {
+            $stockDecimal = bcdiv($smallNum, $unitNum, 2);
+        }
+        $itemNum = bcadd($bigNum, $stockDecimal, 2);
+        return $itemNum;
+    }
+
+
+    //获取常用的itemIds
+    public static function getOftenItemIds($shopId)
+    {
+        $data = self::getLimitList("itemId", ['shopId' => $shopId], 10, 'actualSold desc');
+        if ($data) {
+            $data = array_column($data, 'itemId');
+        }
+        return $data;
+    }
+
+
+
+    //加库存,允许负库存,负数数量(盘点时)
+
+    /**
+     * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
+     * @param $itemNumBundle  花材数量(扎)
+     * @param $itemNumPiece  花材数量(支)
+     * @return bool
+     */
+    public static function addStock($productId, $itemNumBundle, $itemNumPiece)
+    {
+        $productData = self::getById($productId);
+        if (!$productData) {
+            util::fail("花材不存在");
+        }
+        $itemId = $productData['itemId'];
+        $unitNum = xhItemService::getItemUnitNum($itemId);
+        //转成小数,加库存
+        $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $unitNum);
+        $newStock = bcadd($productData['stock'], $itemNum, 2);
+        self::updateStockById($productId, $newStock);
+        return true;
+    }
+
+
+    //todo 库存修改 加锁
+
+    /**
+     * 对商品ID 加库存
+     * @param $productId  商品ID
+     * @param $itemNum  数量
+     * @return bool
+     */
+    public static function addStockByItemNum($productId, $itemNum)
+    {
+        $productData = self::getById($productId, true);
+        if (!$productData) {
+            util::fail("花材不存在");
+        }
+        $newStock = bcadd($productData->stock, $productData['ratio'], 2);
+        self::updateStockById($productId, $newStock);
+        return true;
+    }
+
+    //减库存,允许负库存,负数数量(盘点时)
+    //todo 库存修改 加锁
+    /**
+     * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
+     * @param $itemNumBundle  花材数量(扎)
+     * @param $itemNumPiece  花材数量(支)
+     * @return bool
+     */
+    public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece)
+    {
+        $productData = self::getById($productId, true);
+        if (!$productData) {
+            util::fail("花材不存在");
+        }
+        $ratio = $productData['ratio'];
+        //转成小数,更新库存
+        $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $ratio);
+        $newStock = bcsub($productData->stock, $itemNum, 2);
+        self::updateStockById($productId, $newStock);
+        return true;
+    }
+
+
+    //根据数量减库存
+    //todo 库存修改 加锁
+    /**
+     * @param $productId
+     * @param $itemNum
+     * @return bool
+     */
+    public static function decreaseStockByItemNum($productId, $itemNum)
+    {
+        $productData = self::getById($productId);
+        if (!$productData) {
+            util::fail("花材不存在");
+        }
+        $newStock = bcsub($productData['stock'], $itemNum, 2);
+        self::updateStockById($productId, $newStock);
+        return true;
+    }
+
+
+
+    //计算价格
+
+    /**
+     * @param $productId  门店下的花材ID(xhGhsItemInfo 主键ID)
+     * @param $bigNum  花材数量(扎)
+     * @param $smallNum  花材数量(支)
+     * @return float
+     */
+    public static function computedPrice($productId, $bigNum, $smallNum)
+    {
+        //获取itemId的价格
+        $productData = self::getById($productId);
+        if (!$productData) {
+            util::fail("花材不存在");
+        }
+        $price = $productData['price'];
+        $itemId = $productData['itemId'];
+        $unitNum = xhItemService::getItemUnitNum($itemId);
+        //换算出一支多少钱
+        $piecePrice = 0;
+        if ($unitNum > 0) {
+            //有支
+            $unitPrice = bcdiv($price, $unitNum, 2);
+            $piecePrice = bcmul($unitPrice, $smallNum, 2);
+        }
+        $bundlePrice = bcmul($price, $bigNum, 2);
+
+        $total = bcadd($bundlePrice, $piecePrice, 2);
+        return $total;
+    }
+
+    //根据门店ID, 平台花材ID, 获取门店的花材ID
+    public static function getGhsProductId($shopId, $itemId)
+    {
+        $data = self::getGhsProductDataByItemId($shopId, $itemId);
+        if ($data) {
+            return $data['id'];
+        }
+        return 0;
+    }
+
+    public static function getGhsProductDataByItemId($shopId, $itemId)
+    {
+        $where = [
+            'shopId' => $shopId,
+            'itemId' => $itemId,
+        ];
+        $data = self::getByCondition($where);
+        return $data;
+    }
+
+    //根据多个itemIds shopId 获取对应的 productId  linqh 2021.1.24
+    public static function getGhsProductDataByItemIds($shopId, $itemIds)
+    {
+        $where = ['itemId' => ['in', $itemIds], 'shopId' => $shopId];
+        $data = self::getAllByCondition($where, null, "*");
+        return $data;
+    }
+
+    //补全itemId (针对入库时 当前门店下无对应的花材 itemId)  linqh 2021.1.24
+    public static function complementProduct($merchantId, $shopId, $itemInfo)
+    {
+        $itemIds = array_column($itemInfo, "itemId");
+        $itemData = array_column($itemInfo, null, "itemId");
+        $productData = self::getGhsProductDataByItemIds($shopId, $itemIds);
+
+        if (count($productData) !== count($itemIds)) {
+            $productDataItemIds = array_column($productData, 'itemId');
+            //取差集
+            $diffItemIds = array_diff($itemIds, $productDataItemIds);
+            if ($diffItemIds) {
+                //新增到product表,price = itemPrice (出库时的售价), stock=0
+                foreach ($diffItemIds as $v) {
+                    $addData = [];
+                    $addData['merchantId'] = $merchantId;
+                    $addData['shopId'] = $shopId;
+                    $addData['itemId'] = $v;
+                    $addData['price'] = $itemData[$v]['itemPrice'] ?? 0;
+                    self::add($addData);
+                }
+            }
+        }
+    }
+
+    //有效花材判断,包括id有效、同个门店等 ssh 2021.1.19
+    public static function valid($list, $shopId)
+    {
+        $ids = array_unique(array_filter(array_column($list, 'productId')));
+        $productList = self::getByIds($ids, null, 'id');
+        if (!empty($productList)) {
+            foreach ($productList as $product) {
+                if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
+                    util::fail('只能选择同一家的商品下单');
+                }
+            }
+            if (count($productList) != count($ids)) {
+                util::fail('存在无效商品');
+            }
+        } else {
+            util::fail('商品不存在');
+        }
+    }
+
+    //通过ids 批量获取花材商品的信息
+    public static function getProductByIds($ids)
+    {
+        return self::getByIds($ids);
+    }
+
+    //修改花材商品的库存: 统一方法
+    //todo 库存修改 加锁
+    public static function updateStockById($id, $stock, $adminId = 0)
+    {
+        $key = self::LOCK_STOCK . '_' . $id;
+        $lock = util::lock($key);
+        if (!$lock) {
+            util::fail("系统繁忙中,请稍后再试!");
+        }
+        $data['stock'] = $stock;
+        if ($adminId) {
+            $data['adminId'] = $adminId;
+        }
+        $res = self::updateById($id, $data);
+        util::unlock($key);
+        return $res;
+    }
+
+    //订单退回库存
+    public static function backStockByOrderItemInfo($orderItemInfo)
+    {
+        foreach ($orderItemInfo as $v) {
+            ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
+        }
+    }
+
+
+    //库存告警的itemIds (库存数量少于告警数量)  linqh 2021.1.26
+    public static function getItemIdsWarning($merchantId, $shopId)
+    {
+        //获取当前门店下的 itemId=>stock
+        $productData = ProductClass::getAllList("itemId,stock", ['shopId' => $shopId]);
+        $productItemStock = [];
+        if ($productData) {
+            foreach ($productData as $v) {
+                $productItemStock[$v['itemId']] = $v['stock'];
+            }
+        }
+
+        //获取merchantId 下 对应的itemId=>stockWaring
+        $itemData = ItemClass::getAllList("itemId,stockWarning", ['merchantId' => $merchantId]);
+        $itemStockWaring = [];
+        if ($itemData) {
+            foreach ($itemData as $v) {
+                $itemStockWaring[$v['itemId']] = $v['stockWarning'];
+            }
+        }
+        //取库存数量少于告警数量 的itemId
+        $itemIds = [];
+        foreach ($productItemStock as $k => $v) {
+            $stockWaring = $itemStockWaring[$k] ?? 0;
+            if ($v <= $stockWaring) {
+                $itemIds[] = $k;
+            }
+        }
+        return $itemIds;
+    }
+
+    //获取当前门店下商品信息 linqh 2021.1.28
+    public static function getProductData($id, $shopId, $obj = false)
+    {
+        return ProductClass::getByCondition(['id' => $id, 'shopId' => $shopId], $obj);
+    }
+
+    //修改价格 linqh 2021.1.28
+    public static function changePrice($id, $shopId, $price, $priceLabel)
+    {
+        $res = ProductClass::updateByCondition(['id' => $id, 'shopId' => $shopId], ['price' => $price, 'priceLabel' => $priceLabel]);
+        return $res;
+    }
+
+    //下单时计算商品的价格  linqh 2021.1.28
+    //itemInfo : [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":5,"productId":4,"bigNum":1,"smallNum":5}]
+    //返回 [{"productId":3,"price":2.1}]
+    public static function formatProductInfo(array $itemInfo)
+    {
+        $data = [];
+        if (empty($itemInfo)) {
+            util::fail('请选择花材');
+        }
+        $itemInfo = self::mergeItemInfo($itemInfo);
+        $productIds = array_column($itemInfo, 'productId');
+        $productData = ProductClass::getProductByIds($productIds);
+        $productData = array_column($productData, NULL, 'id');
+
+        $totalPrice = 0;
+        $totalBigNum = 0;
+        $totalSmallNum = 0;
+
+        foreach ($itemInfo as $v) {
+            $productId = $v['productId'];
+            $itemId = $productData[$productId]['itemId'] ?? 0;
+            $unitNum = $productData[$productId]['ratio'] ?? 0;
+            //大小数量合并多少扎
+            $itemNum = self::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);
+            //售卖价格
+            $itemPrice = $productData[$productId]['price'] ?? 0;
+            //合计价格
+            $price = bcmul($itemNum, $itemPrice, 2);
+            $tmp = [];
+            $tmp['productId'] = $productId;
+            $tmp['price'] = $price;
+
+            $totalPrice = bcadd($totalPrice, $price, 2);
+
+            $tmp['name'] = $productData[$productId]['name'] ?? '';
+            $tmp['smallUnit'] = $productData[$productId]['smallUnit'];
+            $tmp['bigUnit'] = $productData[$productId]['bigUnit'];
+            $tmp['cover'] = $productData[$productId]['cover'] ?? '';
+            $tmp['itemId'] = $itemId;
+            $tmp['rank'] = $productData[$productId]['rank'] ?? '';
+            $tmp['unitPrice'] = $itemPrice;
+            $tmp['num'] = $itemNum;
+            $tmp['bigNum'] = $v['bigNum'];
+            $tmp['smallNum'] = $v['smallNum'];
+
+            $totalBigNum += $v['bigNum'];
+            $totalSmallNum += $v['smallNum'];
+
+            $data[] = $tmp;
+        }
+        return ['product' => $data, 'smallNum' => $totalSmallNum, 'bigNum' => $totalBigNum, 'price' => $totalPrice];
+    }
+
+    //  //修改某个花材的成本价(采购之后要用到) linqh 2021.04.06
     public static function changeCost($productId, $cost)
     {
         return self::updateById($productId, ['cost' => $cost]);
     }
 
     //更新某个花材的加价 2021.04.06
-    public static function changeAddPrice($productId,$addPrice)
+    public static function changeAddPrice($productId, $addPrice)
     {
         return self::updateById($productId, ['addPrice' => $addPrice]);
     }
 
     //根据productId 计算花材的重量
-    public static function getWeight($productId,$bigNum,$smallNum)
+    public static function getWeight($productId, $bigNum, $smallNum)
     {
         $productInfo = self::getById($productId);
-        if(empty($productInfo)){
+        if (empty($productInfo)) {
             return 0;
         }
         $ratio = $productInfo['ratio'];
         $weight = $productInfo['weight'];
-        $mergeNum = self::mergeItemNum($bigNum,$smallNum,$ratio);
-        return bcmul($mergeNum,$weight,2);
+        $mergeNum = self::mergeItemNum($bigNum, $smallNum, $ratio);
+        return bcmul($mergeNum, $weight, 2);
     }
 
     //修改product
@@ -608,9 +612,9 @@ class ProductClass extends BaseClass
     public static function autoPriceById($productId)
     {
         $product = self::getById($productId);
-        if($product && $product['priceLabel']==self::PRICE_LABEL_AUTO){
-            $price = bcadd($product['cost'],$product['addPrice'],2);
-            self::changePrice($productId,$product['shopId'],$price,self::PRICE_LABEL_AUTO);
+        if ($product && $product['priceLabel'] == self::PRICE_LABEL_AUTO) {
+            $price = bcadd($product['cost'], $product['addPrice'], 2);
+            self::changePrice($productId, $product['shopId'], $price, self::PRICE_LABEL_AUTO);
         }
     }
 }

+ 1 - 1
biz-hd/admin/classes/AdminClass.php

@@ -60,7 +60,7 @@ class AdminClass extends BaseClass
                     'payPassword' => password_hash($mobile, PASSWORD_BCRYPT),
                     'mobile' => $mobile,
                     'super' => 1,
-
+                    'apply' => 2,
                 ];
                 AdminClass::updateById($adminId, $adminData);
                 $shopData = [

+ 3 - 1
sql.sql

@@ -2123,4 +2123,6 @@ ADD INDEX `idx_shopId` (`shopId`) ;
 
 ======shish 2021-4-17
 ALTER TABLE xhStatIncomeMonth DROP INDEX `income`;
-ALTER TABLE xhStatIncome DROP INDEX `income`;
+ALTER TABLE xhStatIncome DROP INDEX `income`;
+ALTER TABLE xhAdmin ADD apply TINYINT NOT NULL DEFAULT 1 COMMENT '是否已申请开店 1没有 2有' AFTER `super`;
+ALTER TABLE xhAdmin MODIFY `apply` TINYINT NOT NULL DEFAULT 1 COMMENT '申请开店 1没有 2有申请,待审核 3有申请,已审核通过';