error = '请选择商品属性'; return false; } $saveData = []; $goods = HappinessStoreGoods::findOne(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]); if (!$goods) { $this->error = '商品不存在'; return false; } //平台商品还存在不 $happiness_goods = HappinessGoods::findOne(['mall_id' => $this->mall_id, 'goods_id' => $goods['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => StatusEnum::ENABLED]); if (!$happiness_goods) { $this->error = '选品库商品不存在或者已下架'; return false; } $attr_ids = array_column($attr, 'attr_id'); $mall_attr = GoodsAttr::find()->where(['id' => $attr_ids, 'status' => StatusEnum::ENABLED])->asArray()->indexBy('id')->all(); $total_price = 0; $total_num = 0; foreach ($attr as $key => $value) { if ($value['move_num'] <= 0) { continue; } if (!isset($mall_attr[$value['attr_id']])) { $this->error = '商品属性在平台商品库已经删除,无法调货'; return false; } $goods_price = bcmul($mall_attr[$value['attr_id']]['price'], $value['move_num'], 2); $saveData[] = [ 'goods_id' => $goods_id, 'attr_id' => $value['attr_id'], 'num' => $value['move_num'], 'mall_id' => $this->mall_id, 'total_price' => $goods_price, ]; $total_price = bcadd($total_price, $goods_price, 2); $total_num += $value['move_num']; } if ($total_num <= 0) { $this->error = '请填写调货数量'; return false; } $transaction = Yii::$app->db->beginTransaction(); try { $moveItem = HappinessMoveItem::setData([ 'goods_id' => $goods_id, 'total_price' => $total_price, 'total_num' => $total_num, 'mall_id' => $this->mall_id, 'label_id' => $happiness_goods['label_id'], 'store_id' => $this->store_id, 'order_status' => HappinessEnum::MOVE_NO_AUDIT, ]); if (!$moveItem) { throw new Exception(HappinessMoveItem::getStaticError()); } foreach ($saveData as $key => $value) { $value['move_id'] = $moveItem->id; $res = HappinessMoveGoods::setData($value); if (!$res) { throw new Exception(HappinessMoveGoods::getStaticError()); } } $transaction->commit(); return true; } catch (Exception $e) { $transaction->rollBack(); $this->error = $e->getMessage(); return false; } } //同意调货 public function auditMove($params) { $id = $params['id']; $give_store_id = $params['store_id']; $remark = $params['remark']; $status=$params['audit_status']; if($status==0) { //拒绝调货 $res = HappinessMoveItem::setData([ 'id' => $id, 'order_status' => HappinessEnum::MOVE_AUDIT_FAILED, 'remark' => $remark, 'agent_user'=>$this->user_id ]); if (!$res) { $this->error=HappinessMoveItem::getStaticError(); return false; } return true; } if (empty($id)) { $this->error = '请选择调货单'; return false; } if (empty($give_store_id)) { $this->error = '请选择调货小店'; return false; } $moveItem = HappinessMoveItem::find()->with(['goods'])->where(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->one(); if (!$moveItem) { $this->error = '调货单不存在'; return false; } if($moveItem['store_id'] == $give_store_id) { $this->error = '调货小店不能是自己'; return false; } //判断目标小店是否有库存 $attr_ids = array_column($moveItem['goods'], 'attr_id'); $goods_attr = HappinessStoreGoodsAttr::find()->where(['mall_id' => $this->mall_id, 'store_id' => $give_store_id, 'status' => StatusEnum::ENABLED, 'attr_id' => $attr_ids, 'goods_id' => $moveItem['goods_id']])->indexBy('attr_id')->asArray() ->all(); if (empty($goods_attr)) { $this->error = '调货小店商品库存不足'; return false; } $get_goods_attr = HappinessStoreGoodsAttr::find()->where(['mall_id' => $this->mall_id, 'store_id' => $moveItem['store_id'], 'status' => StatusEnum::ENABLED, 'attr_id' => $attr_ids, 'goods_id' => $moveItem['goods_id']])->indexBy('attr_id')->asArray() ->all(); if (empty($get_goods_attr)) { $this->error = '申请小店商品规格不存在'; return false; } $getGoods = HappinessStoreGoods::find()->where(['goods_id' => $moveItem['goods_id'], 'mall_id' => $this->mall_id, 'store_id' => $moveItem['store_id'], 'status' => StatusEnum::ENABLED])->asArray()->one(); if (empty($getGoods)) { $this->error = '申请小店商品不存在'; return false; } $giveStoreAttr = []; $giveGoodsTotalNum = 0; $getStoreAttr = []; $getGoodsTotalNum = 0; foreach ($moveItem['goods'] as $key => $value) { if (!isset($goods_attr[$value['attr_id']])) { $this->error = '调货小店商品库存不足,规格:' . $value['attr_id']; return false; } if ($value['num'] > $goods_attr[$value['attr_id']]['stock']) { $this->error = '调货小店商品库存不足,规格:' . $value['attr_id']; return false; } if (!isset($get_goods_attr[$value['attr_id']])) { $this->error = '申请小店商品规格不存在,规格:' . $value['attr_id']; return false; } $giveStoreAttr[] = [ 'id' => $goods_attr[$value['attr_id']]['id'], 'stock' => -$value['num'], 'mall_id' => $this->mall_id, 'store_id' => $give_store_id, ]; $getStoreAttr[] = [ 'id' => $get_goods_attr[$value['attr_id']]['id'], 'stock' => $value['num'], 'mall_id' => $this->mall_id, 'store_id' => $moveItem['store_id'], ]; $giveGoodsTotalNum += $value['num']; $getGoodsTotalNum += $value['num']; } $giveGoods = HappinessStoreGoods::find()->where(['goods_id' => $moveItem['goods_id'], 'mall_id' => $this->mall_id, 'store_id' => $give_store_id, 'status' => StatusEnum::ENABLED])->asArray()->one(); if (!$giveGoods) { $this->error = '调货商品不存在'; return false; } $giveGoodsSaveData = [ 'id' => $giveGoods['id'], 'stock' => -$giveGoodsTotalNum, 'mall_id' => $this->mall_id, 'store_id' => $give_store_id, 'goods_id' => $moveItem['goods_id'], ]; $getGoodsSaveData = [ 'id' => $getGoods['id'], 'stock' => $getGoodsTotalNum, 'mall_id' => $this->mall_id, 'store_id' => $moveItem['store_id'], 'goods_id' => $moveItem['goods_id'], ]; $transaction = Yii::$app->db->beginTransaction(); try { $res = HappinessMoveItem::setData([ 'id' => $id, 'order_status' => HappinessEnum::MOVE_AUDIT_SUCCESS, 'give_store_id' => $give_store_id, 'remark' => $remark, 'agent_user' => $this->user_id ]); if (!$res) { throw new Exception(HappinessMoveItem::getStaticError()); } //减少库存 $res = HappinessStoreGoods::setData($giveGoodsSaveData); if (!$res) { throw new Exception(HappinessStoreGoods::getStaticError()); } foreach ($giveStoreAttr as $key => $value) { $res = HappinessStoreGoodsAttr::setData($value); if (!$res) { throw new Exception(HappinessStoreGoodsAttr::getStaticError()); } } //增加库存 $res = HappinessStoreGoods::setData($getGoodsSaveData); if (!$res) { throw new Exception(HappinessStoreGoods::getStaticError()); } foreach ($getStoreAttr as $key => $value) { $res = HappinessStoreGoodsAttr::setData($value); if (!$res) { throw new Exception(HappinessStoreGoodsAttr::getStaticError()); } } //减少提货券和增加提货券 $wallet=HappinessVoucherWallet::find()->where(['mall_id' => $this->mall_id, 'store_id' => [$give_store_id, $moveItem['store_id']], 'status' => StatusEnum::ENABLED, 'level' => $moveItem['label_id']])->asArray()->indexBy('store_id') ->all(); if(!isset($wallet[$moveItem['store_id']]) || $wallet[$moveItem['store_id']]['balance']<=$moveItem['total_price']) { $this->error = '申请小店提货券余额不足'; return false; } $wallet_log[] = [ 'mall_id' => $this->mall_id, 'store_id' =>$moveItem['store_id'], 'level' => $moveItem['label_id'], 'change_type' => CapitalLog::CHANGE_TYPE_SUB, 'change_num' => -$moveItem['total_price'], 'desc' => '小店调货扣除提货券', 'source_table' => HappinessVoucherWallet::tableName(), 'source_table_id' => $moveItem['store_id'], 'from_type' => HappinessVoucherEnum::FROM_STORE_MOVE_SUB, 'wallet_type' => HappinessVoucherEnum::WALLET_VALUE, 'status' => 1, 'order_no' => '' ]; $wallet_log[] = [ 'mall_id' => $this->mall_id, 'store_id' =>$give_store_id, 'level' => $moveItem['label_id'], 'change_type' => CapitalLog::CHANGE_TYPE_ADD, 'change_num' => $moveItem['total_price'], 'desc' => '小店调货增加提货券', 'source_table' => HappinessVoucherWallet::tableName(), 'source_table_id' => $give_store_id, 'from_type' => HappinessVoucherEnum::FROM_STORE_MOVE_ADD, 'wallet_type' => HappinessVoucherEnum::WALLET_VALUE, 'status' => 1, 'order_no' => '' ]; foreach ($wallet_log as $item) { if(abs($item['change_num'])>0) { $res=WalletLogic::handle($item); if (!$res) { throw new \Exception(WalletLogic::getStaticError()); } } } $transaction->commit(); return true; } catch (Exception $e) { $transaction->rollBack(); $this->error = $e->getMessage(); return false; } } }