| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- <?php
- namespace bizGhs\order\classes;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\stat\classes\StatOutClass;
- use biz\stat\classes\StatStockInClass;
- use biz\stat\classes\StatStockOutClass;
- use bizGhs\item\classes\CostChangeClass;
- use bizGhs\item\classes\PriceChangeClass;
- use bizGhs\order\models\StockOutOrderItem;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\shop\classes\ShopAdminClass;
- use bizGhs\stock\classes\StockInDayClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizHd\stat\classes\StatIncomeClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- class StockInOrderClass extends BaseClass
- {
- use OrderTrait;
- const STATUS_WAIT = 1; //待入库
- const STATUS_COMPLETE = 2; //已入库
- const STATUS_CANCEL = 3; //取消入库
- const ACTION_CONFIRM = 'confirm'; //直接入库
- public static $baseFile = '\bizGhs\order\models\StockInOrder';
- public static $statusMap = [
- self::STATUS_WAIT => '待入库',
- self::STATUS_COMPLETE => '已入库',
- ];
- //2021.1.25 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, 'outShopId');
- $ids = array_filter(array_unique($ids));
- $shopInfo = ShopClass::getByIds($ids, null, 'id');
- foreach ($list as $key => $val) {
- $outShopId = $val['outShopId'] ?? 0;
- $list[$key]['outShopName'] = $shopInfo[$outShopId]['shopName'] ?? '';
- }
- $data['list'] = $list;
- $data['list'] = self::groupAdminList($list);
- $data['list'] = self::groupStatusName($data['list']);
- return $data;
- }
- //入库 (状态在待入库中 )lqh 2021.1.23
- public static function addOrder($outOrderData)
- {
- $data = [];
- $data['status'] = self::STATUS_WAIT;//待入库
- $orderSn = orderSn::getGhsStockInSn();
- $data['orderSn'] = $orderSn;
- $data['sjId'] = $outOrderData['sjId'];
- $data['outShopId'] = $outOrderData['outShopId'] ?? 0;
- $data['outMainId'] = $outOrderData['outMainId'] ?? 0;
- $data['inShopId'] = $outOrderData['inShopId'];
- $data['inMainId'] = $outOrderData['inMainId'] ?? 0;
- $data['outOrderSn'] = $outOrderData['orderSn'];
- $data['adminId'] = $outOrderData['adminId'];
- $data['bigNum'] = $outOrderData['bigNum'];
- $data['smallNum'] = $outOrderData['smallNum'];
- $data['price'] = $outOrderData['price'] ?? 0;
- $data['remark'] = $outOrderData['remark'];
- $ghsItemInfo = $outOrderData['itemInfo'];
- $shopId = $outOrderData['inShopId'] ?? 0;
- $mainId = $outOrderData['inMainId'] ?? 0;
- $adminId = $outOrderData['adminId'] ?? 0;
- $staffId = 0;
- $staffName = $outOrderData['staffName'] ?? '';
- if (!empty($adminId)) {
- $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
- $staffId = $staff->id ?? 0;
- $staffName = $staff->name ?? '';
- }
- $data['staffId'] = $staffId;
- $data['staffName'] = $staffName;
- //入库: 需要将itemId 转换成对应的productId,若没有对应的productId,则需要新增
- ProductClass::complementProduct($data['sjId'], $mainId, $shopId, $ghsItemInfo);
- $itemIds = array_column($ghsItemInfo, "itemId");
- $productData = ProductClass::getGhsProductDataByItemIds($mainId, $itemIds);
- $itemData = array_column($productData, null, 'itemId');
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //写入订单表
- $order = self::add($data);
- //写入详情表
- $batchData = [];
- foreach ($ghsItemInfo as $v) {
- $tmp = [];
- $itemId = $v['itemId'];
- $productId = $itemData[$itemId]['id'] ?? 0;
- if (!$productId) {
- util::fail("入库失败");
- }
- $tmp['productId'] = $productId;
- $tmp['orderSn'] = $orderSn;
- $tmp['itemId'] = $itemId;
- $tmp['itemStock'] = $itemData[$itemId]['stock'] ?? 0;//当时库存
- $tmp['itemNum'] = $v['itemNum'];
- $tmp['itemPrice'] = $v['itemPrice'] ?? 0;
- $tmp['price'] = $v['price'] ?? 0;
- $tmp['skPrice'] = $v['skPrice'] ?? 0;
- $tmp['hjPrice'] = $v['hjPrice'] ?? 0;
- $tmp['cost'] = $v['cost'] ?? 0;
- $ratio = $itemData[$itemId]['ratio'] ?? 0;
- $formatStock = ProductClass::formatStock($tmp['itemStock'], $ratio);
- $itemInfo = [
- 'itemName' => $v['itemName'] ?? '',
- 'itemCover' => $v['cover'] ?? '',
- 'bigNum' => $v['bigNum'],// 出库的大单位数量
- 'smallNum' => $v['smallNum'], //出库的小单位数量
- 'bigNumStock' => $formatStock['bigNum'],// 当时库存大单位数
- 'smallNumStock' => $formatStock['smallNum'],//当时库存 小单位数
- 'itemUnit' => $v['itemUnit'],
- 'ratio' => $v['ratio'],// 比例
- 'rank' => $v['rank'] ?? 'B',// 花材级别
- 'bigUnit' => $v['bigUnit'],
- 'smallUnit' => $v['smallUnit'],
- ];
- $tmp['itemInfo'] = json_encode($itemInfo);
- $tmp['shopId'] = $outOrderData['inShopId'] ?? 0;
- $tmp['mainId'] = $outOrderData['inMainId'] ?? 0;
- $batchData[] = $tmp;
- }
- StockInOrderItemClass::batchAddOrderItem($batchData);
- $transaction->commit();
- return $order;
- } catch (\Exception $exception) {
- $transaction->rollBack();
- util::fail("入库失败" . $exception->getMessage());
- }
- }
- //确定入库
- //修改状态 1=》2
- // 加库存
- // 加流水记录
- //增加门店收支记录 : 出库的门店加金额,入库的门店减金额(用成本价计算)
- public static function confirmOrder($orderSn, $shop, $adminId)
- {
- $shopId = $shop->id ?? 0;
- $mainId = $shop->mainId ?? 0;
- $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
- $staffId = $staff->id ?? 0;
- $staffName = $staff->name ?? '';
- $data = ['status' => self::STATUS_COMPLETE, 'adminId' => $adminId];
- $orderData = self::getOrderDetail($orderSn, $shopId);
- if ($orderData['status'] != self::STATUS_WAIT) {
- util::fail("订单已处理");
- }
- $orderId = $orderData['id'];
- self::updateById($orderId, $data);
- $outShopId = $orderData['outShopId'] ?? 0;
- $outShop = ShopClass::getById($outShopId, true);
- $event = '从别的门店调拨过来';
- if (!empty($outShop)) {
- $event = '从' . $outShop->shopName . '调拨过来';
- }
- $itemInfo = $orderData['itemInfo'] ?? [];
- if (empty($itemInfo)) {
- util::fail("订单异常");
- }
- $chainShopList = [];
- $default = $shop->default ?? 0;
- $sjId = $shop->sjId ?? 0;
- if ($default == 1) {
- $chainShopList = \bizGhs\merchant\classes\ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
- }
- $totalCost = $orderData['price'] ?? 0;
- foreach ($itemInfo as $k => $v) {
- $currentItemId = $v['productId'] ?? 0;
- $currentCost = $v['itemPrice'] ?? 0;
- $currentHdPrice = $v['price'] ?? 0;
- $currentSkPrice = $v['skPrice'] ?? 0;
- $currentHjPrice = $v['hjPrice'] ?? 0;
- //不管加盟还是直营店如果当前花材没有库存,则成本和价格都传导过来。
- $currentInfo = ProductClass::getById($currentItemId, true);
- if (isset($currentInfo->stock) && floatval($currentInfo->stock) == 0) {
- $upData = ['cost' => $currentCost];
- ProductClass::updateById($currentItemId, $upData);
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => $currentInfo->sjId ?? 0,
- 'mainId' => $currentInfo->mainId ?? 0,
- 'itemId' => $currentInfo->id ?? 0,
- 'ptItemId' => $currentInfo->itemId ?? 0,
- 'cost' => $currentCost,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'name' => $currentInfo->name ?? '',
- 'cover' => $currentInfo->cover ?? '',
- 'targetId' => $orderId,
- 'type' => 1,
- 'event' => $event,
- ];
- CostChangeClass::addData($changeData);
- if ($currentHdPrice > 0) {
- $ptItemId = $currentInfo->itemId ?? 0;
- $changeParams = ['staffId' => $staffId, 'staffName' => $staffName, 'event' => '调拨入库改价', 'targetId' => $orderId];
- ProductClass::changeSinglePrice($shop, $ptItemId, $currentHdPrice, $currentSkPrice, $currentHjPrice, $changeParams);
- //在首店修改需要同步到所有直营店
- if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
- foreach ($chainShopList as $chain) {
- if ($chain->id == $shop->id) {
- continue;
- }
- $chainMainId = $chain->mainId ?? 0;
- $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
- $staffId = $staff->id ?? 0;
- $staffName = $staff->name ?? '';
- $changeParams2 = ['staffId' => $staffId, 'staffName' => $staffName, 'event' => '调拨入库改价,同步给分店'];
- ProductClass::changeSinglePrice($chain, $ptItemId, $currentHdPrice, $currentSkPrice, $currentHjPrice, $changeParams2);
- }
- }
- }
- }
- $stockInfo = ProductClass::addStockByItemNum($currentItemId, $v['itemNum'], ['cost' => $currentCost]);
- $itemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
- $itemInfo[$k]['itemStock'] = $stockInfo['oldStock'];
- $itemInfo[$k]['newStock'] = $stockInfo['newStock'];
- $itemInfo[$k]['avCost'] = $stockInfo['avCost'] ?? 0;
- $itemInfo[$k]['totalCost'] = $stockInfo['totalCost'] ?? 0;
- $itemInfo[$k]['totalStock'] = $stockInfo['totalStock'] ?? 0;
- $itemInfo[$k]['changeCost'] = $stockInfo['changeCost'] ?? 0;
- $itemInfo[$k]['unitCost'] = $stockInfo['unitCost'] ?? 0;
- }
- //流水记录
- $orderData['shopId'] = $shopId;
- $orderData['itemInfo'] = $itemInfo;
- $orderData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- $orderData['relateName'] = $orderData['staffName'] ?? '';
- StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_IN_STOCK);
- $outOrderSn = $orderData['outOrderSn'];
- $outShopId = $orderData['outShopId'];
- $outOrderInfo = StockOutOrderClass::getOrderDetail($outOrderSn, $outShopId);
- $payWay = dict::getDict('payWay', 'unknown');
- if (empty($outOrderInfo)) {
- util::fail('没有找到出库单');
- }
- $outOrderInfoItem = $outOrderInfo['itemInfo'] ?? '';
- if (empty($outOrderInfoItem)) {
- util::fail('没有出库单信息');
- }
- $outEvent = '出库';
- $outCapitalType = dict::getDict('capitalType', 'ghsCheckOut', 'id');
- $shop = ShopClass::getLockById($outShopId);
- if (empty($shop)) {
- util::fail('没有找到出库门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息34');
- }
- $outCurrentTotalIncome = bcadd($main->totalIncome, $totalCost, 2);
- $currentTotalStockOut = bcadd($main->totalStockOut, $totalCost, 2);
- $main->totalIncome = $outCurrentTotalIncome;
- $main->totalStockOut = $currentTotalStockOut;
- $main->save();
- $capitalData = [
- 'capitalType' => $outCapitalType,
- 'io' => 1,
- 'totalIncome' => $outCurrentTotalIncome,
- 'payWay' => $payWay,
- 'amount' => $totalCost,
- 'sjId' => $outOrderInfo['sjId'] ?? 0,
- 'shopId' => $outShopId,
- 'event' => $outEvent,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //每天和每月收入统计,出库也算收入
- StatIncomeClass::updateOrInsert($main, $shop, $totalCost);
- //出库每日和每月统计
- StatStockOutClass::replace($main, $shop, $totalCost);
- //入库门店
- $inEvent = '入库';
- $inCapitalType = dict::getDict('capitalType', 'ghsCheckIn', 'id');
- $inShop = ShopClass::getLockById($shopId);
- if (empty($inShop)) {
- util::fail('没有找到入库门店');
- }
- $inMainId = $inShop->mainId ?? 0;
- $inMain = MainClass::getLockById($inMainId);
- if (empty($inMain)) {
- util::fail('没有main信息35');
- }
- $inCurrentTotalExpend = bcadd($inMain->totalExpend, $totalCost, 2);
- $inMain->totalExpend = $inCurrentTotalExpend;
- $currentTotalStockIn = bcadd($inMain->totalStockIn, $totalCost, 2);
- $inMain->totalStockIn = $currentTotalStockIn;
- $inMain->save();
- $capitalData = [
- 'capitalType' => $inCapitalType,
- 'io' => 0,
- 'totalExpend' => $inCurrentTotalExpend,
- 'payWay' => $payWay,
- 'amount' => $totalCost,
- 'sjId' => $orderData['sjId'] ?? 0,
- 'shopId' => $shopId,
- 'event' => $inEvent,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //当天和当月支出统计,调拨入库算支出
- StatOutClass::updateOrInsert($main, $shop, $totalCost);
- //入库每天和每月统计
- StatStockInClass::replace($inMain, $inShop, $totalCost);
- }
- //2021.1.23 lqh 订单详情
- public static function getOrderDetail($orderSn, $shopId)
- {
- $orderData = self::orderExist(['orderSn' => $orderSn, 'inShopId' => $shopId]);
- $orderInfo = StockInOrderItemClass::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']);
- 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']);
- $outShopId = $orderData['outShopId'] ?? 0;
- $outShop = ShopClass::getById($outShopId);
- $outShopName = $outShop['shopName'] ?? '';
- $orderData['outShopName'] = $outShopName;
- return $orderData;
- }
- //2021.1.25 lqh 判断定时是否存在
- public static function orderExist($where)
- {
- $orderData = self::getByCondition($where);
- if (!$orderData) {
- util::fail('订单不存在');
- }
- return $orderData;
- }
- // 组装状态码 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 groupOutShopName($list)
- {
- foreach ($list as $k => $val) {
- $list[$k]['shopName'] = "出库门店名称";
- }
- return $list;
- }
- //取消入库
- public static function cancelOrder($order, $adminId)
- {
- if ($order->status != self::STATUS_WAIT) {
- util::fail('订单不能取消');
- }
- $order->status = self::STATUS_CANCEL;
- $order->adminId = $adminId;
- $order->save();
- $outOrderSn = $order->outOrderSn ?? '';
- $outShopId = $order->outShopId ?? '';
- $outOrderData = StockOutOrderClass::getOrderDetail($outOrderSn, $outShopId);
- $outOrderData['shopId'] = $outShopId;
- //加库存
- $itemInfo = $outOrderData['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'];
- }
- $outOrderData['itemInfo'] = $itemInfo;
- $outOrderData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- StockRecordClass::addRecordByOrder($outOrderData, StockRecordClass::STOCK_RECORD_TYPE_IN_STOCK_CANCEL);
- //修改出库订单状态
- $whereOut = [
- 'orderSn' => $outOrderSn,
- 'outShopId' => $outShopId,
- ];
- StockOutOrderClass::updateByCondition($whereOut, ['status' => StockOutOrderClass::STATUS_CANCEL]);
- }
- }
|