| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace ghs\controllers;
- use bizGhs\merchant\classes\ShopClass;
- use bizGhs\order\classes\StockOutOrderClass;
- use bizGhs\product\classes\ProductClass;
- use common\components\util;
- use Yii;
- //出库 lqh 2021.1.23
- class StockOutController extends BaseController
- {
- //全部出库 ssh 20220906
- public function actionAllOut()
- {
- $get = Yii::$app->request->get();
- $inShopId = $get['shopId'] ?? 0;
- $inShop = ShopClass::getById($inShopId, true);
- if (empty($inShop)) {
- util::fail('入库门店没有找到');
- }
- $inMainId = $inShop->mainId ?? 0;
- $shop = $this->shop;
- if ($shop->default == 1) {
- util::fail('首店库存不能全部出库');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $shopId = $this->shopId;
- $mainId = $this->mainId;
- $sjId = $shop->sjId ?? 0;
- $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
- if (empty($list)) {
- util::fail('没有花材');
- }
- $itemInfo = [];
- foreach ($list as $item) {
- if ($item->delStatus == 1) {
- continue;
- }
- $stock = $item->stock ?? 0;
- $ratio = $item->ratio ?? 0;
- if ($stock <= 0) {
- continue;
- }
- $bigNum = intval($stock);
- $small = bcsub($stock, $bigNum, 2);
- $smallNum = bcmul($small, $ratio);
- $currentId = $item->id;
- $itemInfo[] = ['productId' => $currentId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
- }
- if (empty($itemInfo)) {
- util::fail('没有库存');
- }
- $data = [
- 'remark' => '全部出库',
- 'itemInfo' => $itemInfo,
- 'inShopId' => $inShopId,
- 'inMainId' => $inMainId,
- 'outMainId' => $mainId,
- 'outShopId' => $shopId,
- 'sjId' => $sjId,
- 'action' => StockOutOrderClass::ACTION_CONFIRM,
- ];
- StockOutOrderClass::addOrder($data);
- $transaction->commit();
- util::complete();
- } catch (\Exception $exception) {
- $transaction->rollBack();
- util::fail('出库失败');
- }
- }
- //出库列表 lqh 2021.1.23
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $orderStatus = isset($get['status']) ? $get['status'] : '';
- $where = [];
- $where['outShopId'] = $this->shopId;
- if (!empty($orderStatus)) {
- $where['status'] = $orderStatus;
- }
- $list = StockOutOrderClass::getOrderList($where);
- util::success($list);
- }
- //新增出库 lqh 2021.1.23
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $post['sjId'] = $this->sjId;
- $post['outShopId'] = $this->shopId;
- $post['outMainId'] = $this->mainId;
- $post['adminId'] = $this->adminId;
- $inShopId = $post['inShopId'] ?? 0;
- $now = date('Y-m-d H:i:s');
- $date = $post['date'] ?? $now;
- $post['outTime'] = $date;
- $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
- $post['action'] = $action;
- $post['inShopId'] = $inShopId;
- $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
- if ($check) {
- util::fail('还有待出库的订单');
- }
- $ghsItemInfo = $post['itemInfo'] ?? '';
- if (empty($ghsItemInfo)) {
- util::fail('请选择花材');
- }
- $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
- if (!is_array($ghsItemInfo)) {
- util::fail('花材格式不正确');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //入库门店有效性判断
- if ($this->shopId == $inShopId) {
- util::fail('不能出库给当前门店');
- }
- $inShop = ShopClass::getById($inShopId);
- if (empty($inShop)) {
- util::fail('没有找到门店');
- }
- ShopClass::valid($inShop, $this->sjId);
- $post['inMainId'] = $inShop['mainId'] ?? 0;
- ProductClass::valid($ghsItemInfo, $this->mainId);
- //判断花材里的信息
- foreach ($ghsItemInfo as $v) {
- $bigNum = $v['bigNum'] ?? 0;
- $smallNum = $v['smallNum'] ?? 0;
- if ($bigNum <= 0 && $smallNum <= 0) {
- util::fail('花材数量不能为0');
- }
- }
- $post['itemInfo'] = $ghsItemInfo;
- $order = StockOutOrderClass::addOrder($post);
- if ($order) {
- $transaction->commit();
- util::success($order);
- }
- util::fail('出库失败');
- } catch (\Exception $exception) {
- $transaction->rollBack();
- util::fail('出库失败');
- }
- }
- //出库详情 lqh 2021.1.23
- public function actionDetail()
- {
- $orderSn = Yii::$app->request->get('orderSn', '');
- $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
- util::success($orderData);
- }
- //确认出库 lqh 2021.1.23
- public function actionConfirmOrder()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'];
- StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
- util::complete("出库成功");
- }
- //取消出库 lqh 2021.1.23
- public function actionCancelOrder()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'];
- StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
- util::complete("取消成功");
- }
- }
|