|
|
@@ -1,7 +1,13 @@
|
|
|
<?php
|
|
|
|
|
|
namespace bizHd\part\classes;
|
|
|
+
|
|
|
use bizHd\base\classes\BaseClass;
|
|
|
+use bizHd\shop\classes\MainClass;
|
|
|
+use common\components\dict;
|
|
|
+use common\components\orderSn;
|
|
|
+use bizGhs\product\classes\ProductClass;
|
|
|
+use bizGhs\stock\classes\StockRecordClass;
|
|
|
use Yii;
|
|
|
|
|
|
class PartClass extends BaseClass
|
|
|
@@ -9,4 +15,145 @@ class PartClass extends BaseClass
|
|
|
|
|
|
public static $baseFile = '\bizHd\part\models\Part';
|
|
|
|
|
|
+ const STATUS_COMPLETE = 1; //已完成
|
|
|
+
|
|
|
+ //列表
|
|
|
+ public static function getPartList($where)
|
|
|
+ {
|
|
|
+ $data = self::getList('*', $where, 'addTime DESC');
|
|
|
+ $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
|
|
|
+ if (empty($list)) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ $data['list'] = $list;
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function addOrder($post)
|
|
|
+ {
|
|
|
+ $mainId = $post['mainId'] ?? 0;
|
|
|
+ $data = [];
|
|
|
+ $data['status'] = self::STATUS_COMPLETE;
|
|
|
+ $orderSn = orderSn::getPartSn();
|
|
|
+ $data['orderSn'] = $orderSn;
|
|
|
+ $data['sjId'] = $post['sjId'];
|
|
|
+ $data['shopId'] = $post['shopId'];
|
|
|
+ $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
|
|
|
+ $data['shopAdminName'] = $post['shopAdminName'] ?? '';
|
|
|
+
|
|
|
+ //花材列表
|
|
|
+ $productInfoList = $post['product'];
|
|
|
+ $productInfoList = self::mergeItemInfo($productInfoList);
|
|
|
+ $totalNum = 0;
|
|
|
+ $totalBig = 0;
|
|
|
+ $totalSmall = 0;
|
|
|
+ if (!empty($productInfoList)) {
|
|
|
+ foreach ($productInfoList as $current) {
|
|
|
+ $bigNum = $current['bigNum'] ?? 0;
|
|
|
+ $totalNum = bcadd($totalNum, $bigNum, 2);
|
|
|
+ $smallNum = $current['smallNum'] ?? 0;
|
|
|
+ $ratio = isset($current['ratio']) && $current['ratio'] > 0 ? $current['ratio'] : 1;
|
|
|
+ $div = bcdiv($smallNum, $ratio, 2);
|
|
|
+ $totalNum = bcadd($totalNum, $div, 2);
|
|
|
+ $totalBig = bcadd($totalBig, $bigNum);
|
|
|
+ $totalSmall = bcadd($totalSmall, $smallNum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data['bigNum'] = $totalBig;
|
|
|
+ $data['smallNum'] = $totalSmall;
|
|
|
+ $data['num'] = $totalNum;
|
|
|
+ $data['remark'] = $post['remark'] ?? '';
|
|
|
+ $data['mainId'] = $mainId;
|
|
|
+ //写入订单表
|
|
|
+ $order = self::add($data, true);
|
|
|
+ foreach ($productInfoList as $k => $v) {
|
|
|
+ $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true);
|
|
|
+ $productInfoList[$k]['oldStock'] = $stockInfo['oldStock'];
|
|
|
+ $productInfoList[$k]['newStock'] = $stockInfo['newStock'];
|
|
|
+ }
|
|
|
+ //写入详情表
|
|
|
+ $batchData = self::groupOrderItem($productInfoList, $orderSn);
|
|
|
+ $stockItem = [];
|
|
|
+ foreach ($batchData as $currentItem) {
|
|
|
+ $currentItem['mainId'] = $mainId;
|
|
|
+ PartItemClass::add($currentItem);
|
|
|
+ $stockItem[] = [
|
|
|
+ 'productId' => $currentItem['productId'],
|
|
|
+ 'itemId' => $currentItem['itemId'],
|
|
|
+ 'itemNum' => $currentItem['itemNum'],
|
|
|
+ 'oldStock' => $currentItem['newStock'],
|
|
|
+ 'newStock' => $currentItem['newStock'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $stockData = [];
|
|
|
+ $stockData['shopId'] = $data['shopId'];
|
|
|
+ $stockData['relateName'] = $data['shopAdminName'] ?? '';
|
|
|
+ $stockData['ptStyle'] = $post['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
|
|
|
+ $stockData['orderSn'] = $orderSn;
|
|
|
+ $stockData['sjId'] = $data['sjId'] ?? 0;
|
|
|
+ $stockData['itemInfo'] = $stockItem;
|
|
|
+ StockRecordClass::addRecordByOrder($stockData, StockRecordClass::STOCK_RECORD_TYPE_PART);
|
|
|
+ $main = MainClass::getLockById($mainId);
|
|
|
+ if (empty($main)) {
|
|
|
+ util::fail('没有main信息');
|
|
|
+ }
|
|
|
+ $totalCost = 0;
|
|
|
+ $totalNum = 0;
|
|
|
+ foreach ($batchData as $v) {
|
|
|
+ $num = $v['itemNum'] ?? 0;
|
|
|
+ $cost = $v['itemCost'] ?? 0;
|
|
|
+ $totalCost = bcadd($totalCost, bcmul($num, $cost, 2), 2);
|
|
|
+ $totalNum = bcadd($totalNum, $num);
|
|
|
+ }
|
|
|
+ $order->cost = $totalCost;
|
|
|
+ $order->save();
|
|
|
+ StatPartClass::replace($main, $totalCost, $totalNum);
|
|
|
+ return $order;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 组装 orderItem lqh 2021.1.25
|
|
|
+ public static function groupOrderItem($productInfoList, $orderSn)
|
|
|
+ {
|
|
|
+ $productIds = array_column($productInfoList, 'productId');
|
|
|
+ $productData = ProductClass::getProductByIds($productIds);
|
|
|
+ $productData = array_column($productData, NULL, 'id');
|
|
|
+ //写入详情表
|
|
|
+ $batchData = [];
|
|
|
+ foreach ($productInfoList as $v) {
|
|
|
+ $tmp = [];
|
|
|
+ $productId = $v['productId'];
|
|
|
+ $itemId = $productData[$productId]['itemId'];
|
|
|
+ $tmp['orderSn'] = $orderSn;
|
|
|
+ $tmp['itemId'] = $itemId;
|
|
|
+ $tmp['productId'] = $v['productId'];
|
|
|
+ $unitNum = $productData[$productId]['ratio'] ?? 0;
|
|
|
+ $tmp['itemStock'] = $v['oldStock'];//当时库存
|
|
|
+ $tmp['newStock'] = $v['newStock'];//当时库存
|
|
|
+ $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
|
|
|
+ $tmp['itemPrice'] = $productData[$productId]['price'] ?? 0; //当时售卖的价格
|
|
|
+ $tmp['itemCost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
|
|
|
+ $tmp['cover'] = $productData[$productId]['cover'] ?? '';
|
|
|
+ $tmp['name'] = $productData[$productId]['name'] ?? '';
|
|
|
+ $batchData[] = $tmp;
|
|
|
+ }
|
|
|
+ return $batchData;
|
|
|
+ }
|
|
|
+
|
|
|
+ //itemInfo 相同product合并处理 [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":4,"productId":3,"bigNum":1,"smallNum":5}]
|
|
|
+ public static function mergeItemInfo($itemInfo)
|
|
|
+ {
|
|
|
+ $newItemInfo = [];
|
|
|
+ foreach ($itemInfo as $v) {
|
|
|
+ $productId = $v['productId'];
|
|
|
+ if (isset($newItemInfo[$productId])) {
|
|
|
+ $newItemInfo[$productId]['bigNum'] = bcadd($v['bigNum'], $newItemInfo[$productId]['bigNum'], 2);
|
|
|
+ $newItemInfo[$productId]['smallNum'] = bcadd($v['smallNum'], $newItemInfo[$productId]['smallNum'], 2);
|
|
|
+ } else {
|
|
|
+ $newItemInfo[$productId] = $v;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $newItemInfo = array_merge($newItemInfo);
|
|
|
+ return $newItemInfo;
|
|
|
+ }
|
|
|
+
|
|
|
}
|