| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- namespace bizGhs\order\classes;
- use biz\shop\classes\ShopClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\StockRecordClass;
- use common\components\dict;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\order\traits\OrderTrait;
- use biz\shop\classes\ShopExtClass;
- use common\components\printUtil;
- class StockOutOrderClass extends BaseClass
- {
- use OrderTrait;
- const STATUS_WAIT = 1; //待出库
- const STATUS_COMPLETE = 2; //已出库
- const STATUS_CANCEL = 3; //取消出库
- const STATUS_BACK = 4; //退回,取消入库 (暂不用)
- const ACTION_WAIT = 'wait';
- const ACTION_CONFIRM = 'confirm'; //直接入库
- public static $statusMap = [
- self::STATUS_WAIT => '待出库',
- self::STATUS_COMPLETE => '已出库',
- self::STATUS_CANCEL => '取消出库',
- ];
- public static $baseFile = '\bizGhs\order\models\StockOutOrder';
- //2021.1.23 lqh 新增出库订单
- public static function addOrder($data)
- {
- $action = $data['action'];
- $data['status'] = self::STATUS_WAIT; //待入库
- $orderSn = orderSn::getGhsStockOutSn();
- $data['orderSn'] = $orderSn;
- $ghsItemInfo = $data['itemInfo']; // [{bigNum:0,smallNum:0,productId:1}]
- $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
- $sumNum = self::sumNumByItemInfo($ghsItemInfo);
- $data['bigNum'] = $sumNum['bigNum'];// 总的大单位数量 总共多少扎
- $data['smallNum'] = $sumNum['smallNum']; //总的小单位数量 总共多少支
- $outShopId = $data['outShopId'] ?? 0;
- $outMainId = $data['outMainId'] ?? 0;
- $staffName = $data['staffName'] ?? '';
- $ids = array_column($ghsItemInfo, 'productId');
- $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,avCost', 'id');
- $order = self::add($data, true);
- $map = [];
- //先减库存,若取消出库则加回来
- foreach ($ghsItemInfo as $k => $v) {
- $productId = $v['productId'] ?? 0;
- $product = $productList[$productId] ?? [];
- $avCost = $product['avCost'] ?? 0;
- $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true, false, ['cost' => $avCost]);
- $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
- $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
- $newAvCost = $stockInfo['avCost'] ?? 0;
- $totalCost = $stockInfo['totalCost'] ?? 0;
- $totalStock = $stockInfo['totalStock'] ?? 0;
- $changeCost = $stockInfo['changeCost'] ?? 0;
- $unitCost = $stockInfo['unitCost'] ?? 0;
- $map[$productId] = ['avCost' => $newAvCost, 'totalCost' => $totalCost, 'totalStock' => $totalStock, 'changeCost' => $changeCost,'unitCost'=>$unitCost];
- }
- //写入详情表
- $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
- $totalCost = 0;
- foreach ($batchData as $outKey => $outVal) {
- $batchData[$outKey]['mainId'] = $outMainId;
- $batchData[$outKey]['shopId'] = $outShopId;
- $unitCost = $outVal['itemPrice'] ?? 0;
- $currentNum = $outVal['itemNum'] ?? 0;
- $currentCost = bcmul($unitCost, $currentNum, 2);
- $totalCost = bcadd($totalCost, $currentCost, 2);
- }
- StockOutOrderItemClass::batchAddOrderItem($batchData);
- //记录总成本 ssh 20221113
- $order->price = $totalCost;
- $order->save();
- $orderData = self::getOrderDetail($orderSn, $data['outShopId']);
- $orderData['shopId'] = $outShopId;
- $orderData['mainId'] = $outMainId;
- $orderData['staffName'] = $staffName;
- $orderData['relateName'] = $staffName;
- $itemInfo = $orderData['itemInfo'] ?? [];
- if (empty($itemInfo)) {
- util::fail("订单异常");
- }
- //把平均成本相关参数加进入,存到库存变动明细里去 ssh
- foreach ($itemInfo as $cKey => $cVal) {
- $productId = $cVal['productId'] ?? 0;
- $mapItem = $map[$productId] ?? [];
- $avCost = $mapItem['avCost'] ?? 0;
- $totalCost = $mapItem['totalCost'] ?? 0;
- $totalStock = $mapItem['totalStock'] ?? 0;
- $changeCost = $mapItem['changeCost'] ?? 0;
- $unitCost = $mapItem['unitCost']??0;
- $itemInfo[$cKey]['avCost'] = $avCost;
- $itemInfo[$cKey]['totalCost'] = $totalCost;
- $itemInfo[$cKey]['totalStock'] = $totalStock;
- $itemInfo[$cKey]['changeCost'] = $changeCost;
- $itemInfo[$cKey]['unitCost'] = $unitCost;
- }
- $orderData['itemInfo'] = $itemInfo;
- $orderData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_OUT_STOCK);
- if ($action === self::ACTION_CONFIRM) {
- $adminId = $data['adminId'] ?? 0;
- //直接出库,并且在对方门店生成待入库单
- $inOrder = self::confirmOrder($orderSn, $data['outShopId'], $adminId);
- $confirmIn = $data['confirmIn'] ?? 0;
- //直接确认入库
- if ($confirmIn == 1) {
- $inOrderSn = $inOrder['orderSn'] ?? '';
- $inShopId = $inOrder['inShopId'] ?? 0;
- $inShop = ShopClass::getById($inShopId, true);
- if (empty($inShop)) {
- util::fail('没有找到入库门店');
- }
- StockInOrderClass::confirmOrder($inOrderSn, $inShop, $adminId);
- }
- }
- return $order;
- }
- //2021.1.23 lqh 确认出库 :
- // 一、从状态 1 修改为 2
- // 二、解除解冻的库存
- // 三、增加流水记录
- // 四、写入一条数据到入库门店的入库订单表(生成入库订单号)
- // 五、将入库订单号保存当前记录中
- public static function confirmOrder($orderSn, $shopId, $adminId)
- {
- $outTime = date("Y-m-d H:i:s");
- $data = [
- 'status' => self::STATUS_COMPLETE,
- 'adminId' => $adminId,
- 'outTime' => $outTime,
- ];
- $orderData = self::getOrderDetail($orderSn, $shopId);
- if ($orderData['status'] != self::STATUS_WAIT) {
- util::fail("订单已处理");
- }
- $orderId = $orderData['id'] ?? 0;
- //修改状态
- self::updateById($orderId, $data);
- $itemInfo = $orderData['itemInfo'] ?? [];
- if (empty($itemInfo)) {
- util::fail("订单异常");
- }
- $orderData['shopId'] = $shopId;
- //写入一条数据到入库店的入库订单表,生成入库订单号
- $inOrderData = StockInOrderClass::addOrder($orderData);
- $inOrderSn = $inOrderData['orderSn'];
- //更新入库订单号
- self::updateById($orderId, ['inOrderSn' => $inOrderSn]);
- //完成,提交事务
- return $inOrderData;
- }
- //2021.1.23. lqh 取消出库 :
- //一、 从状态1 修改为 3
- //二、 加回库存
- public static function cancelOrder($orderSn, $shopId, $adminId)
- {
- $where = [
- 'orderSn' => $orderSn,
- 'status' => self::STATUS_WAIT,
- 'outShopId' => $shopId,
- ];
- self::orderExist($where);
- $data = [
- 'status' => self::STATUS_CANCEL,
- 'adminId' => $adminId,
- ];
- $orderData = self::getOrderDetail($orderSn, $shopId);
- $orderData['shopId'] = $shopId;
- //加库存
- $itemInfo = $orderData['itemInfo'] ?? [];
- if (empty($itemInfo)) {
- util::fail("订单异常");
- }
- foreach ($itemInfo as $k => $v) {
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
- $itemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
- $itemInfo[$k]['itemStock'] = $stockInfo['oldStock'];
- $itemInfo[$k]['newStock'] = $stockInfo['newStock'];
- }
- $orderData['itemInfo'] = $itemInfo;
- StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_IN_STOCK_CANCEL);
- self::updateByCondition($where, $data);
- }
- //2021.1.23 lqh 订单列表
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- $ids = array_column($list, 'inShopId');
- $ids = array_filter(array_unique($ids));
- $shopInfo = ShopClass::getByIds($ids, null, 'id');
- foreach ($list as $key => $val) {
- $inShopId = $val['inShopId'] ?? 0;
- $list[$key]['inShopName'] = $shopInfo[$inShopId]['shopName'] ?? '';
- }
- $data['list'] = $list;
- $data['list'] = self::groupAdminList($list);
- $data['list'] = self::groupStatusName($data['list']);
- return $data;
- }
- //2021.1.23 lqh 订单详情
- public static function getOrderDetail($orderSn, $shopId)
- {
- $orderData = self::orderExist(['orderSn' => $orderSn, 'outShopId' => $shopId]);
- $orderInfo = StockOutOrderItemClass::getOrderItemDetail($orderSn);
- $itemData = [];
- if ($orderInfo) {
- foreach ($orderInfo as $v) {
- $info = json_decode($v['itemInfo'], true);
- $tmp = $info;
- $tmp['itemCover'] = self::groupImg($info['itemCover']);
- unset($v['itemInfo']);
- //兼容 旧数据,没有 rank (花材等级 默认给B)
- if (!isset($tmp['rank'])) {
- $tmp['rank'] = 'B';
- }
- $tmp = array_merge($tmp, $v);
- $itemData[] = $tmp;
- }
- }
- $orderData = self::groupAdmin($orderData);
- $orderData['itemInfo'] = $itemData;
- $orderData['statusName'] = self::getStatusName($orderData['status']);
- $inShopId = $orderData['inShopId'];
- $inShop = ShopClass::getById($inShopId);
- $orderData['shopName'] = $inShop['shopName'] ?? '';
- return $orderData;
- }
- //2021.1.23 lqh 判断定时是否存在
- public static function orderExist($where)
- {
- $orderData = self::getByCondition($where);
- if (!$orderData) {
- util::fail('订单不存在或不能取消');
- }
- return $orderData;
- }
- // 组装 orderItem lqh 2021.1.25
- public static function groupOrderItem($ghsItemInfo, $orderSn)
- {
- $productData = self::getProductMapData($ghsItemInfo);
- //写入详情表
- $batchData = [];
- foreach ($ghsItemInfo as $v) {
- $tmp = [];
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $rank = $productData[$productId]['rank'] ?? 'B';// 花材级别
- $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]['avCost'] ?? 0; //平均成本
- $tmp['price'] = $productData[$productId]['price'] ?? 0; //当时批发价
- $tmp['skPrice'] = $productData[$productId]['skPrice'] ?? 0; //当时零售价
- $tmp['hjPrice'] = $productData[$productId]['hjPrice'] ?? 0; //当时会员价
- $tmp['cost'] = $productData[$productId]['avCost'] ?? 0; //平均成本
- $stockFormat = ProductClass::formatStock($tmp['itemStock'], $unitNum); // 库存总单位数量转大小单位的数量
- $itemInfo = [
- 'itemName' => $productData[$productId]['name'] ?? '',//花材名称
- 'itemCover' => $productData[$productId]['cover'] ?? '',//花材图片
- 'bigNum' => $v['bigNum'] ?? 0,//数量(大单位)
- 'smallNum' => $v['smallNum'] ?? 0,//数量(小单位)
- 'bigNumStock' => $stockFormat['bigNum'],// 当时库存大单位数
- 'smallNumStock' => $stockFormat['smallNum'],//当时库存 小单位数
- 'itemUnit' => $unitNum ? 2 : 1,//单位数量 2表示有两个单位(e.g 扎和支) 1表是只有一个单位(e.g 扎)
- 'ratio' => $unitNum,// 比例
- 'rank' => $rank,// 花材级别
- 'bigUnit' => $productData[$productId]['bigUnit'],//大单位名称 扎
- 'smallUnit' => $productData[$productId]['smallUnit'],//小单位名称 支
- ];
- $tmp['itemInfo'] = json_encode($itemInfo);
- $batchData[] = $tmp;
- }
- return $batchData;
- }
- // 组装状态码 lqh 2021.1.30
- public static function groupStatusName($list)
- {
- foreach ($list as $k => $val) {
- $status = $val['status'] ?? 0;
- $list[$k]['statusName'] = self::getStatusName($status);
- }
- return $list;
- }
- // 获取状态码名称
- public static function getStatusName($status)
- {
- return self::$statusMap[$status] ?? '';
- }
- public static function printOrder($out, $shop)
- {
- $orderSn = $out->orderSn;
- $returnList = StockOutOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (empty($returnList)) {
- return false;
- }
- $arr = array_chunk($returnList, 50);
- $totalPage = count($arr);
- foreach ($arr as $key => $itemList) {
- $sjName = $shop->merchantName ?? '';
- $shopName = $shop->shopName ?? '';
- $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- $content = '';
- $content .= "<CB>{$currentName}</CB><BR>";
- $content .= "<CB>出库单</CB><BR><BR>";
- $content .= '<CB>' . $orderSn . '</CB><BR><BR>';
- $content .= '<CB>' . date("m-d H:i", strtotime($out->outTime)) . '</CB><BR>';
- if ($totalPage > 1) {
- $currentPage = bcadd($key, 1);
- $content .= "<CB>第{$currentPage}页,共{$totalPage}页</CB><BR>";
- }
- $content .= '<BR>';
- $content .= '商品 数量 <BR>';
- $content .= '--------------------------------<BR>';
- if (isset($itemList) && !empty($itemList)) {
- foreach ($itemList as $current) {
- $json = $current['itemInfo'] ?? '';
- $info = json_decode($json, true);
- $name = $info['itemName'] ?? '';
- $num = $info['bigNum'] ?? 0;
- $unitName = $info['bigUnit'] ?? '';
- $show = $name . ' ' . $num . $unitName;
- $content .= '<B>' . $show . '</B><BR>';
- $content .= '--------------------------------<BR>';
- }
- }
- $content .= '<BR>';
- $content .= '<BR>';
- $content .= '<BR>';
- $content .= '<B>签名:</B><BR>';
- $content .= '<BR>';
- $content .= '<BR>';
- $content .= '<BR>';
- $shopId = $shop->id;
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
- $printSn = $ext['printSn'] ?? '';
- if (!empty($printSn)) {
- $p = new printUtil($printSn);
- $p->printMsg($content, $shop);
- }
- }
- $out->printNum = bcadd($out->printNum, 1);
- $out->save();
- }
- }
|