TimestampBehavior::class, ] ]; } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if(!$insert && $changedAttributes['num'] && $this->sync2Goods){ queue_push(new \app\jobs\erp\ErpInventory2GoodsJob([ 'id' => $this->id, ]), 30, 1); } } public static function num2Goods($id) { $ei = ErpInventory::findOne($id); $numAdd = Goods::numAddStatic($ei->goods_id, explode(',', $ei->attr_ids), $ei->num, 1); if(!$numAdd){ \Yii::error([__METHOD__, 'erp同步库存到商品失败!', $ei->attributes]); } } public static function logSave ($ei, $num, $type = ErpInventoryLog::LOG_TYPE_ORDER, $order_id = 0, $order_no = '') { $t = \Yii::$app->db->beginTransaction(); try { $model = new ErpInventoryLog(); $model->inventory_id = $ei['id']; $model->attr_ids = $ei['attr_ids']; $model->type = $type; $model->order_id = $order_id; $model->order_no = $order_no; $model->before = $ei['num']; $model->after = $ei['num'] + $num; $model->num = $num; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('日志保存失败!' . array_shift($model->getFirstErrors())); } $ei->num = $model->after; if($type == ErpInventoryLog::LOG_TYPE_PURCHASEOUT && $ei->num < 0){ \Yii::error([__METHOD__, $ei->attributes]); throw new \Exception('库存保存失败!ID:'. $ei->id .'出库数量大于库存'); } if (!$ei->save()) { \Yii::error([__METHOD__, $ei->attributes]); throw new \Exception('库存保存失败!' . array_shift($ei->getFirstErrors())); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { $t->rollBack(); \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }