store_id)){ $this->store_id = get_store_id(); } } public static function isOpen($store_id) { $isOpen = Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value']; return $isOpen; } public static function initStoreGoods($store_id, $start_id = 0, $count = 10) { if(!self::isOpen($store_id)){ return [ 'code' => 1, 'msg' => '操作失败!功能未开启' ]; } if($start_id == 0 && cache_lock(__METHOD__ . $store_id, 86400 - time()%86400)){ return [ 'code' => 1, 'msg' => '操作失败!提交太频繁,明天再试' ]; } $query = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'mch_id' => 0, 'product_type' => 0]); $query->andWhere(['md_food_id' => 0]); $query->andWhere(['cloud_goods_id' => 0]); $query->andWhere(['jst_goods_id' => '']); $query->andWhere(['>=', 'id', $start_id]); $goodsList = $query->select('id')->limit($count)->asArray()->column(); $errIds = []; foreach($goodsList as $gid){ $start_id = $gid; $res = self::initGoods($store_id, $gid); if($res['code'] != 0){ $errIds[] = $gid . $res['msg']; // return $res; } } $queue = 0; if(count($goodsList) >= $count){ $queue = queue_push(new \app\jobs\erp\ErpInitStoreGoodsJob([ 'store_id' => $store_id, 'id' => $start_id + 1, 'count' => 100, ]), 30, 1); } return [ 'code' => 0, 'msg' => '操作成功!' . ($queue ? '数据较多,后台继续处理中' : ''), 'errIds' => $errIds, ]; } public static function filterGoods($goods) { if($goods['jst_goods_id']){ return false; } if($goods['cloud_goods_id'] && !intval($goods['is_wholesale'])){ return false; } if($goods['md_food_id']){ return false; } if($goods['mch_id']){ return false; } if($goods['product_type']){ return false; } return true; } public static function initGoods($store_id, $goods) { $t = \Yii::$app->db->beginTransaction(); try { if(!($goods instanceof Goods)){ $goods = Goods::find()->where(['id' => $goods, 'store_id' => $store_id])->limit(1)->one(); } if(!self::filterGoods($goods)){ throw new \Exception('非本地上传商品或无需进销存管理'); } Goods::skuAttr($goods); $attr = json_decode($goods['attr'], true); if(empty($attr)){ throw new \Exception('attr是空的'); } foreach($attr as $item){ if(!$item['attr_ids']){ throw new \Exception('商品规格数据错误' . $goods['id'] . json_encode($attr)); } $model = ErpInventory::findOne(['goods_id' => $goods['id'], 'sku_id' => $item['cyy_skuId'], 'store_id' => $goods['store_id']]); if (empty($model)) { $num = 0; $erp_init_goods_set_num = Option::get(OptionSetting::ERP_INIT_GOODS_SET_NUM, $store_id)['value']; if($erp_init_goods_set_num){ $num = $item['num']; } $model = new ErpInventory(); $model->store_id = $goods['store_id']; $model->store_id = $goods['store_id']; $model->goods_id = $goods['id']; $model->sku_id = $item['cyy_skuId']; $model->num = $num; $model->attr_ids = $item['attr_ids']; $model->attr_info = json_encode($item); $model->goods_no = $item['no']; $model->sync2Goods = 0; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception(array_shift($model->getFirstErrors())); } }else{ $model->is_delete = 0; $up = 0; $attr_info = json_decode($model->attr_info, true); if($model->goods_no != $item['no']){ $up++; $model->goods_no = $item['no']; } \Yii::error([__METHOD__, $attr_info, $item]); if($attr_info['pic'] != $item['pic'] || $attr_info['price'] != $item['price']){ $up++; $model->attr_info = json_encode($item); } $model->sync2Goods = 0; if ($up && !$model->save()) { \Yii::error([__METHOD__, $model->attributes]); 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 || array_key_exists('attr', $changedAttributes)){ self::initGoods($goods['store_id'], $goods); } } public static function erp2Goods($store_id, $goods) { try{ if(!self::isOpen($store_id)){ return; } $list = ErpInventory::findAll(['store_id' => $store_id, 'goods_id' => $goods['id'], 'is_delete' => 0]); foreach($list as $item){ ErpInventory::num2Goods($item['id']); } $list && $goods->refresh(); } catch (\Exception $ex) { \Yii::error($ex->getMessage()); } } public static function afterOrderDetailSave($od) { try { $order = $od->order; if(!self::isOpen($order['store_id'])){ return; } $skuId = Goods::skuid1($od['goods_id'], json_decode($od['attr'], true)); $ei = ErpInventory::findOne(['is_delete' => 0, 'goods_id' => $od['goods_id'], 'sku_id' => $skuId]); if($ei){ ErpInventory::logSave($ei, -$od['num'], ErpInventoryLog::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 = ErpInventory::findOne(['is_delete' => 0, 'goods_id' => $item['goods_id'], 'sku_id' => $skuId]); if($ei){ ErpInventory::logSave($ei, $item['num'], ErpInventoryLog::LOG_TYPE_ORDER, $order->id, $order->order_no); } } } } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public static function getAllByGid($goods_id = 0){ return ErpInventory::find()->where(['goods_id' => $goods_id, 'is_delete' => 0])->asArray()->all(); } public function warningList ($query = []) { $this->warning_num = ERP::getWarningNum($this->store_id); // if (empty($this->supplier_id)) { // return [ // 'code' => 1, // 'msg' => '请选择供货商' // ]; // } // $_GET['pageSize'] = 300; return $this->search(0, $query); } public function search($eip_list = 0, $params = []) { try { $query = ErpInventory::find()->alias('ei')->where(['ei.is_delete' => 0, 'ei.store_id' => $this->store_id]); $query->leftJoin(['g' => Goods::tableName()], 'g.id = ei.goods_id')->andWhere(['g.is_delete' => 0]); $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.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($params['prop'])) { if($params['prop'] == 1){ $query->andWhere(['ei.id' => ErpInventoryProp::find()->select('inventory_id')->where(['is_delete' => 0])]); } if($params['prop'] == 2){ $query->andWhere(['not in', 'ei.id', ErpInventoryProp::find()->select('inventory_id')->where(['is_delete' => 0])]); } } if (!empty($this->goods_name)) { $query->andWhere(['like', 'g.name', trim($this->goods_name)]); } if (!empty($this->supplier_name)) { $query->andWhere(['ei.supplier_id' => ErpSupplier::find()->select('id')->where(['like', '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]); } if (!empty($params['goods_no'])) { $query->andWhere(['ei.goods_no' => $params['goods_no']]); } if (!empty($this->cat_id)) { $query->andWhere(['ei.goods_id' => GoodsCat::find()->select('goods_id')->where(['cat_id' => Cat::getCatId($this->cat_id)])]); } if (!empty($params['warehouse_id'])) { $query->andWhere(['g.warehouse_id' => $params['warehouse_id']]); } if (!empty($params['warehouse_zone_id'])) { $query->andWhere(['g.warehouse_zone_id' => $params['warehouse_zone_id']]); } $query->select('ei.*, g.name goods_name, g.cover_pic, g.warehouse_id, g.warehouse_zone_id, es.name supplier_name,wh.name as wh_name,whz.name as whz_name'); if($eip_list){ $query->leftJoin(['eip' => ErpInventoryProp::tableName()], 'eip.inventory_id = ei.id AND eip.is_delete = 0'); $query->leftJoin(['eipl' => ErpInventoryPropLog::find()->alias('eipl')->leftJoin(['eil' => ErpInventoryLog::tableName()], 'eipl.inventory_log_id=eil.id')->where('eil.type=1')->select('MAX(eipl.id) max_eipl_id,eipl.inventory_prop_id')->groupBy('eipl.inventory_prop_id')], 'eipl.inventory_prop_id = eip.id'); $query->addSelect('eip.id eip_id'); $query->indexBy(function($row){ return implode('_', [$row['id'], $row['eip_id']]); }); $query->addSelect('eipl.max_eipl_id'); $query->addOrderBy('eipl.max_eipl_id DESC'); $query_in = ErpPurchaseinOrder::find()->alias('epo')->leftJoin(['epi' => ErpPurchasein::tableName()], 'epo.id=epi.purchase_order_id')->select('epi.inventory_id'); if (!empty($params['in_start_time'])) { $query_in->andWhere(['>=' , 'epo.created_at', strtotime($params['in_start_time'])]); } if (!empty($params['in_end_time'])) { $query_in->andWhere(['<=' , 'epo.created_at', strtotime($params['in_end_time']) + 86400]); } if (!empty($params['timeout_start_time'])) { $query_in->andWhere(['>=' , 'eip.timeout', strtotime($params['timeout_start_time'])]); } if (!empty($params['timeout_end_time'])) { $query_in->andWhere(['<=' , 'eip.timeout', strtotime($params['timeout_end_time']) + 86400]); } if($query_in->where){ $query->andWhere(['ei.id' => $query_in]); } } $query->addOrderBy('ei.goods_id DESC'); $pagination = pagination_make($query); $pagination['list'] = array_values($pagination['list']); foreach ($pagination['list'] as &$item) { $item['warehouse_name'] = $item['wh_name'].'-'.$item['whz_name']; // $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']; } $eip = null; if($item['eip_id']){ $eip = ErpInventoryProp::findOne($item['eip_id'])->toArray(); $item['num_total'] = $item['num']; $item['num'] = $eip['num']; } $item['num_total_goods'] = (int)ErpInventory::find()->where(['is_delete' => 0, 'goods_id' => $item['goods_id']])->sum('num'); $item['eip'] = $eip; $erpPurchasein = ErpPurchasein::find()->where(['goods_id' => $item['goods_id']])->orderBy('created_at DESC')->one(); if ($erpPurchasein){ $item['purchase_in_price'] = $erpPurchasein['price']; $item['num'] = $erpPurchasein['num']; $item['supplier_id'] = $erpPurchasein['supplier_id']; $item['timeout'] = $erpPurchasein['timeout'] ? date('Y-m-d', $erpPurchasein['timeout']) : ''; $item['batch_no'] = $erpPurchasein['batch_no']; } } 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 saveSupplier () { $t = \Yii::$app->db->beginTransaction(); try { $ids = explode(',', $this->id); foreach($ids as $id){ $up = 0; $model = ErpInventory::findOne(['is_delete' => 0, 'id' => $id, 'store_id' => $this->store_id]); if($this->supplier_id && $model->supplier_id != $this->supplier_id){ $up++; $model->supplier_id = $this->supplier_id; } if($this->purchase_in_price && $model->purchase_in_price != $this->purchase_in_price){ $up++; $model->purchase_in_price = $this->purchase_in_price; } if ($up && !$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception(array_shift($model->getFirstErrors())); } if($this->warehouse_id){ $goods = Goods::findOne($model->goods_id); $goods->warehouse_id = $this->warehouse_id; $goods->warehouse_zone_id = $this->warehouse_zone_id; if (!$goods->save()) { \Yii::error([__METHOD__, $goods->attributes]); throw new \Exception(array_shift($goods->getFirstErrors())); } } } $t->commit(); $warehouseZone = WarehouseZone::findOne($this->warehouse_zone_id); $warehouse_zone_name = $warehouseZone->name; $warehouse = Warehouse::findOne($this->warehouse_id); $warehouse_name = $warehouse->name; $erpSupplier = ErpSupplier::findOne($this->supplier_id); $erp_supplier_name = $erpSupplier->name; return [ 'code' => 0, 'msg' => '操作成功!', 'data' => [ 'supplier_id' => $this->supplier_id, 'warehouse_id' => $this->warehouse_id, 'warehouse_zone_id' => $this->warehouse_zone_id, 'warehouse_zone_name' => $warehouse_zone_name, 'warehouse_name' => $warehouse_name, '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 = ErpInventory::findOne(['is_delete' => 0, '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 ($params = []) { $t = \Yii::$app->db->beginTransaction(); try { $eip_id = $params['eip_id'] ?: 0; $delSave = (new PurchaseoutForm())->delSave($eip_id); if ($delSave['code']) { \Yii::error([__METHOD__, $eip_id, $delSave]); throw new \Exception($delSave['msg']); } if ($eip_id) { $up = ErpInventoryProp::updateAll(['is_delete' => 1], ['id' => $eip_id]); if (!$up) { \Yii::error([__METHOD__, $eip_id, $up]); throw new \Exception('eip删除错误'); } } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!', ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function logList() { try { $query = ErpInventoryLog::find()->alias('eil') ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = eil.inventory_id') ->where(['eil.is_delete' => 0, 'ei.store_id' => $this->store_id]); $query->leftJoin(['o' => Order::tableName()], 'o.id = eil.order_id'); $query->leftJoin(['u' => User::tableName()], 'o.user_id = u.id'); $query->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile'); $query->leftJoin(['g' => Goods::tableName()], 'g.id = ei.goods_id'); if($this->eiId){ $query->andWhere(['ei.id' => $this->eiId]); } if($this->eipId){ $query->andWhere(['eil.id' => ErpInventoryPropLog::find()->select('inventory_log_id')->where(['inventory_prop_id' => $this->eipId])]); } if(isset($this->type) && $this->type > -1){ $query->andWhere(['eil.type' => $this->type]); } if (!empty($this->goods_name)) { $query->andWhere(['like', 'g.name', trim($this->goods_name)]); } if($this->nickname || $this->phone){ $query->andWhere(['eil.type' => ErpInventoryLog::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]); } if (!empty($this->supplier_id)) { $query->andWhere(['ei.supplier_id' => $this->supplier_id]); } $query->orderBy('eil.id DESC'); $query->select('eil.*, su.name, su.mobile, su.avatar, g.name goods_name, g.cover_pic'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['ei'] = ErpInventory::findOne($item['inventory_id']); $attr_info = json_decode($item['ei']['attr_info'], true); $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name')); if($attr_info['pic']){ $item['cover_pic'] = $attr_info['pic']; } if($item['type'] == ErpInventoryLog::LOG_TYPE_PURCHASEIN){ $item['epo'] = ErpPurchaseinOrder::findOne($item['order_id']); $item['ep'] = ErpPurchasein::findOne(['purchase_order_id' => $item['order_id'], 'inventory_id' => $item['inventory_id']]); } $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); if($item['type'] == ErpInventoryLog::LOG_TYPE_PURCHASEIN){ $item['name'] = '入库'; } if($item['type'] == ErpInventoryLog::LOG_TYPE_PURCHASEOUT){ $item['name'] = '出库'; } if($this->eipId){ $propLog = ErpInventoryPropLog::find()->where(['inventory_prop_id' => $this->eipId, 'inventory_log_id' => $item['id']])->one(); $item['propLog'] = $propLog; $item['num'] = $propLog['num']; $item['before'] = $propLog['before']; $item['after'] = $propLog['after']; } } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }