| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\agent\front_erp;
- use app\models\Admin;
- use app\models\AgentFrontBind;
- use app\models\AgentFrontErpCloudGoods;
- use app\models\AgentFrontErpInventory;
- use app\models\AgentFrontErpInventoryLog;
- //use app\models\ErpSupplier;
- use app\models\Goods;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\OrderTransit;
- use app\models\Supplier;
- use app\models\User;
- use app\models\SaasUser;
- use app\models\Warehouse;
- use app\models\WarehouseZone;
- class InventoryForm extends Model
- {
- public $front_agent_admin_id;
- public $id;
- public $ids;
- public $goods_name;
- public $supplier_name;
- public $goods_id;
- public $supplier_id;
- public $warehouse_id;
- public $warehouse_zone_id;
- public $is_delete;
- public $warning_num;
- public $eiId;
- public $nickname;
- public $phone;
- public $order_no;
- public $type;
- public function rules()
- {
- return [
- [['id', 'front_agent_admin_id', 'is_delete'], 'integer'],
- [['ids', 'goods_name', 'supplier_name'], 'string'],
- [['is_delete', 'goods_id', 'supplier_id', 'warehouse_id', 'warehouse_zone_id', 'warning_num', 'eiId', 'nickname', 'phone', 'order_no', 'type'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->front_agent_admin_id)){
- $admin = get_admin();
- $this->front_agent_admin_id = 0;
- if ($admin->type === Admin::ADMIN_TYPE_FRONT_AGENT) {
- $this->front_agent_admin_id = $admin->id;
- }
- }
- }
-
- // public static function isOpen($store_id) {
- // $isOpen = Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value'];
- // return $isOpen;
- // }
-
- public static function initStoreGoods($front_agent_admin_id, $start_id = 0, $count = 10) {
- // if(!self::isOpen($store_id)){
- // return [
- // 'code' => 1,
- // 'msg' => '操作失败!功能未开启'
- // ];
- // }
- try {
- // if($start_id == 0 && cache_lock(__METHOD__ . $front_agent_admin_id, 86400 - time()%86400)){
- // return [
- // 'code' => 1,
- // 'msg' => '操作失败!提交太频繁,明天再试'
- // ];
- // }
- //获取仓库关联的供货商
- $supplier_arr = AgentFrontBind::getAgentFrontBindSupplierId($front_agent_admin_id);
- $supplier_str = implode(',', $supplier_arr);
- $goods_url = "/goods/getSupplierSendTypeGoods";
- $param = [
- 'send_type' => '5,6',
- 'supplier_id' => $supplier_str,
- 'fields' => 'id',
- 'page' => $start_id,
- 'limit' => $count
- ];
- //请求接口
- $domain = (new OptionSetting)->getCloudDomainName();
- $goodsIdList = cloud_post($domain . $goods_url, $param);
- $goodsIdList = json_decode($goodsIdList, true);
- if (intval($goodsIdList['code']) !== 0) {
- throw new \Exception('云仓商品获取失败');
- }
- if (empty($goodsIdList['data']['data'])) {
- throw new \Exception('云仓商品获取失败');
- }
- $goodsIdArr = array_column($goodsIdList['data']['data'], 'id');
- $errIds = [];
- foreach($goodsIdArr as $gid){
- // $start_id = $gid;
- $res = self::initGoods($front_agent_admin_id, $gid);
- if($res['code'] != 0){
- $errIds[] = $gid . $res['msg'];
- // return $res;
- }
- }
- $queue = 0;
- if(count($goodsIdArr) >= $count){
- $queue = queue_push(new \app\jobs\erp\AgentFrontErpInitStoreGoodsJob([
- 'front_agent_admin_id' => $front_agent_admin_id,
- 'id' => $start_id + 1,
- 'count' => 100,
- ]), 30, 1);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!' . ($queue ? '数据较多,后台继续处理中' : ''),
- 'errIds' => $errIds,
- ];
- } catch (\Exception $e) {
- }
- }
- public static function filterGoods($goods) {
- if($goods['jst_goods_id']){
- return false;
- }
- if($goods['cloud_goods_id']){
- return false;
- }
- if($goods['md_food_id']){
- return false;
- }
- return true;
- }
- public static function initGoods($front_agent_admin_id, $goods) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- // if(!($goods instanceof AgentFrontErpCloudGoods)){
- // $goods = AgentFrontErpCloudGoods::find()->where(['id' => $goods])->limit(1)->one();
- // }
- // if(!self::filterGoods($goods)){
- // throw new \Exception('非本地上传商品或无需进销存管理');
- // }
- //流程
- //获取仓库与云仓供货商绑定的商品列表 前端从获取的商品列表中进行导入到仓库
- //
- $goods_url = "/goods/getAttrParams";
- $param = [
- 'id' => $goods
- ];
- //请求接口
- $domain = (new OptionSetting)->getCloudDomainName();
- $goodsInfo = cloud_post($domain . $goods_url, $param);
- $goodsInfo = json_decode($goodsInfo, true);
- if (intval($goodsInfo['code']) !== 0) {
- throw new \Exception('云仓商品获取失败');
- }
- if (empty($goodsInfo['data'])) {
- throw new \Exception('云仓商品获取失败_');
- }
- if (!in_array($goodsInfo['data']['send_type'], [5, 6])) {
- throw new \Exception('非仓库配送产品,不能拉取');
- }
- $goods_supplier_id = $goodsInfo['data']['supplier_id'];
- if (!in_array($goods_supplier_id, AgentFrontBind::getAgentFrontBindSupplierId($front_agent_admin_id) ?? [])) {
- throw new \Exception('当前供货商商品未与仓库关联');
- }
- $goods = $goodsInfo['data'];
- $goods['attr'] = $goods['attrs'];
- Goods::skuAttr($goods);
- $agentFrontErpCloudGoods = AgentFrontErpCloudGoods::findOne(['cloud_goods_id' => $goods['id']]);
- if (!$agentFrontErpCloudGoods) {
- $agentFrontErpCloudGoods = new AgentFrontErpCloudGoods();
- $agentFrontErpCloudGoods->cloud_supplier_id = $goods_supplier_id;
- $agentFrontErpCloudGoods->cloud_goods_id = $goods['id'];
- }
- $agentFrontErpCloudGoods->goods_no = $goods['goods_no'];
- $agentFrontErpCloudGoods->goods_name = $goods['name'];
- $agentFrontErpCloudGoods->cover_pic = $goods['pic_url'];
- $agentFrontErpCloudGoods->attr = $goods['attr'];
- $agentFrontErpCloudGoods->status = $goods['status'];
- if (!$agentFrontErpCloudGoods->save()) {
- throw new \Exception('商品拉取失败');
- }
- $supplier = Supplier::findOne(['cloud_supplier_id' => $goods_supplier_id, 'is_delete' => 0]);
- $attr = json_decode($goods['attr'], true);
- foreach($attr as $item){
- if (isset($item['old_price'])) {
- $item['price'] = $item['old_price'];
- }
- if(!$item['attr_ids']){
- throw new \Exception('商品规格数据错误' . $goods['id'] . json_encode($attr));
- }
- $model = AgentFrontErpInventory::findOne(['goods_id' => $goods['id'], 'sku_id' => $item['cyy_skuId'], 'front_agent_admin_id' => $front_agent_admin_id]);
- if (empty($model)) {
- $model = new AgentFrontErpInventory();
- $model->front_agent_admin_id = $front_agent_admin_id;
- $model->goods_id = $goods['id'];
- $model->sku_id = $item['cyy_skuId'];
- $model->num = $item['num'];
- $model->attr_ids = $item['attr_ids'];
- $model->attr_info = json_encode($item, JSON_UNESCAPED_UNICODE);
- $model->goods_no = $item['no'];
- $model->supplier_id = $supplier->id ?? 0;
- $model->sync2Goods = 0;
- if (!$model->save()) {
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- }else{
- $attr_info = json_decode($model->attr_info, true);
- $model->goods_no = $item['no'];
- $model->attr_info = json_encode($item, JSON_UNESCAPED_UNICODE);
- $model->sync2Goods = 0;
- $model->supplier_id = $supplier->id ?? 0;
- if (!$model->save()) {
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public static function afterGoodsSave($goods, $insert, $changedAttributes) {
- // if(!self::isOpen($goods['store_id'])){
- // return;
- // }
- if($insert || $changedAttributes['attr']){
- self::initGoods($goods['store_id'], $goods);
- }
- }
- // public static function erp2Goods($store_id, $goods) {
- // try{
- //// if(!self::isOpen($store_id)){
- //// return;
- //// }
- // if (intval($goods->is_front_delivery)) {
- // $list = AgentFrontErpInventory::findAll(['goods_id' => $goods->cloud_goods_id, 'is_delete' => 0]);
- // foreach($list as $item){
- // AgentFrontErpInventory::num2Goods($item['id']);
- // }
- // $list && $goods->refresh();
- // }
- //
- // } catch (\Exception $ex) {
- // \Yii::error($ex->getMessage());
- // }
- // }
- public static function afterOrderDetailSave($od) {
- try {
- // $order = $od->order;
- // $goods = $od->goods;
- //// if(!self::isOpen($order['store_id'])){
- //// return;
- //// }
- //
- // $goods_info = json_decode($od->goods_info, true);
- //
- // $goods_no = $goods_info['goods_no'];
- // if (!empty($goods_info)) {
- // $od_attr = json_decode($od['attr'], true);
- // $od_attr_id = array_column($od_attr, 'attr_id');
- // sort($od_attr_id);
- // foreach ($goods_info['attr'] as $goods_attr_item) {
- // $goods_attr_id = array_column($goods_attr_item['attr_list'], 'attr_id');
- // sort($goods_attr_id);
- //
- // if (!array_diff($goods_attr_id, $od_attr_id)) {
- // $goods_no = $goods_attr_item['no'];
- // }
- // }
- // }
- //
- //
- //// $skuId = Goods::skuid1($od['goods_id'], json_decode($od['attr'], true));
- // $ei = AgentFrontErpInventory::findOne(['goods_id' => $goods->cloud_goods_id, 'goods_no' => $goods_no]);
- // if($ei){
- // AgentFrontErpInventory::logSave($ei, -$od['num'], AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $order->id, $order->order_no);
- // }
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- // public static function afterOrderSave($order, $insert, $changedAttributes) {
- // try {
- //// if(!self::isOpen($order['store_id'])){
- //// return;
- //// }
- // //恢复库存
- // if (!$insert && isset($changedAttributes['trade_status']) && $order->trade_status == Order::ORDER_FLOW_CANCEL) {
- // if(!cache_lock(__METHOD__ . $order->id, 60)){
- // $od = $order->detail;
- // foreach($od as $item){
- // $skuId = Goods::skuid1($item['goods_id'], json_decode($item['attr'], true));
- // $ei = AgentFrontErpInventory::findOne(['goods_id' => $item['goods_id'], 'sku_id' => $skuId]);
- // if($ei){
- // AgentFrontErpInventory::logSave($ei, $item['num'], AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $order->id, $order->order_no);
- // }
- // }
- // }
- // }
- // } catch (\Exception $e) {
- // \Yii::error($e);
- // return [
- // 'code' => 1,
- // 'msg' => $e->getMessage()
- // ];
- // }
- // }
- public function warningList ()
- {
- $this->warning_num = ERP::getWarningNum($this->front_agent_admin_id);
- if (empty($this->supplier_id)) {
- return [
- 'code' => 1,
- 'msg' => '请选择供货商'
- ];
- }
- $_GET['pageSize'] = 300;
- return $this->search();
- }
- public function search ()
- {
- try {
- $query = AgentFrontErpInventory::find()->alias('ei')->where(['ei.is_delete' => 0, 'ei.front_agent_admin_id' => $this->front_agent_admin_id]);
- $query->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id');
- $query->leftJoin(['s' => Supplier::tableName()], 's.id = ei.supplier_id');
- // $query->leftJoin(['wh' => Warehouse::tableName()], 'wh.id = g.warehouse_id');
- // $query->leftJoin(['whz' => WarehouseZone::tableName()], 'whz.id = g.warehouse_zone_id');
- if (!empty($this->warning_num)) {
- $query->andWhere(['<', 'ei.num', $this->warning_num]);
- }
- if (!empty($this->goods_name)) {
- $query->andWhere(['like', 'g.goods_name', $this->goods_name]);
- }
- if (!empty($this->supplier_name)) {
- $query->andWhere(['ei.supplier_id' => Supplier::find()->select('id')->where(['like', 'supplier_name', $this->supplier_name])]);
- }
- if (!empty($this->supplier_id)) {
- $query->andWhere(['ei.supplier_id' => $this->supplier_id]);
- }
- if (!empty($this->goods_id)) {
- $query->andWhere(['ei.goods_id' => $this->goods_id]);
- }
- $query->orderBy('ei.goods_id DESC');
- $query->select('ei.*, g.goods_name, g.cover_pic, s.supplier_name');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- // $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $attr_info = json_decode($item['attr_info'], true);
- $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
- $item['price'] = $attr_info['price'];
- if($attr_info['pic']){
- $item['cover_pic'] = $attr_info['pic'];
- }
- $admin = Admin::findOne($item['front_agent_admin_id']);
- $item['warehouse_name'] = $admin->name ?: '';
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- // 保存库存供货商信息 supplier_id为供货商表id 非云仓供货商id
- // public function saveSupplier ()
- // {
- // $t = \Yii::$app->db->beginTransaction();
- // try {
- // $front_agent_admin_id = $this->front_agent_admin_id;
- // $ids = explode(',', $this->id);
- //
- // $supplier = Supplier::findOne($this->supplier_id);
- // if (!$supplier) {
- // throw new \Exception('供货商信息不存在');
- // }
- //
- // foreach($ids as $id){
- // $model = AgentFrontErpInventory::findOne(['id' => $id, 'front_agent_admin_id' => $front_agent_admin_id]);
- // $model->supplier_id = $this->supplier_id;
- // if (!$model->save()) {
- // \Yii::error([__METHOD__, $model->attributes]);
- // throw new \Exception(array_shift($model->getFirstErrors()));
- // }
- // }
- // $t->commit();
- //
- //
- // $erpSupplier = Supplier::findOne($this->supplier_id);
- // $erp_supplier_name = $erpSupplier->name;
- //
- // return [
- // 'code' => 0,
- // 'msg' => '操作成功!',
- // 'data' => [
- // 'supplier_id' => $this->supplier_id,
- // 'erp_supplier_name' => $erp_supplier_name,
- // ]
- // ];
- // } catch (\Exception $e) {
- // \Yii::error($e);
- // $t->rollBack();
- // return [
- // 'code' => 1,
- // 'msg' => $e->getMessage()
- // ];
- // }
- // }
- public function saveKey ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $model = AgentFrontErpInventory::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($model)) {
- throw new \Exception('参数错误');
- }
- $key = $this->key;
- $model->$key = $this->val;
-
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function del ()
- {
- try {
- if ($this->ids) {
- $ids = explode(',', $this->ids);
- AgentFrontErpInventory::updateAll(['is_delete' => 1, 'deleted_at' => time()], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
-
-
-
-
-
- public function logList() {
-
- try {
- $query = AgentFrontErpInventoryLog::find()->alias('eil')
- ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = eil.inventory_id')
- ->where(['eil.is_delete' => 0, 'ei.front_agent_admin_id' => $this->front_agent_admin_id, 'ei.id' => $this->eiId]);
- $query->leftJoin(['ot' => OrderTransit::tableName()], 'ot.cloud_order_id = eil.order_id');
- $query->leftJoin(['o' => Order::tableName()], 'o.id = ot.order_id');
- $query->leftJoin(['u' => User::tableName()], 'o.user_id = u.id');
- $query->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile');
- if(isset($this->type) && $this->type > -1){
- $query->andWhere(['eil.type' => $this->type]);
- }
- if($this->nickname || $this->phone){
- $query->andWhere(['eil.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER]);
- $query->andWhere(['like', 'su.mobile', $this->phone]);
- $query->andWhere(['like', 'su.name', $this->nickname]);
- }
- if($this->order_no){
- $query->andWhere(['like', 'eil.order_no', $this->order_no]);
- }
- $query->orderBy('eil.id DESC');
- $query->select('eil.*, su.name, su.mobile, su.avatar, o.id store_order_id');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- if($item['type'] == AgentFrontErpInventoryLog::LOG_TYPE_PURCHASEIN){
- $item['name'] = '入库';
- }
- if($item['type'] == AgentFrontErpInventoryLog::LOG_TYPE_PURCHASEOUT){
- $item['name'] = '出库';
- }
- if($item['type'] == AgentFrontErpInventoryLog::LOG_TYPE_ORDER){
- $item['order_id'] = $item['store_order_id'];
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getSupplierSendTypeGoods() {
- $front_agent_admin_id = $this->front_agent_admin_id;
- //获取仓库关联的供货商
- $supplier_arr = AgentFrontBind::getAgentFrontBindSupplierId($front_agent_admin_id);
- $supplier_str = implode(',', $supplier_arr);
- $goods_url = "/goods/getSupplierSendTypeGoods";
- $param = [
- 'send_type' => '5,6',
- 'supplier_id' => $supplier_str,
- 'fields' => 'id, name, price, num, pic_url',
- 'page' => input_params('pageNo'),
- 'limit' => 10,
- 'search_type' => 1,
- 'goods_name' => $this->goods_name
- ];
- //请求接口
- $domain = (new OptionSetting)->getCloudDomainName();
- $goodsIdList = cloud_post($domain . $goods_url, $param);
- return json_decode($goodsIdList, true);
- }
-
- }
|