| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%erp_inventory}}".
- *
- * @property integer $id
- * @property string $goods_no
- */
- class ErpInventory extends \yii\db\ActiveRecord
- {
- public $sync2Goods = 1;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%erp_inventory}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => 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()
- ];
- }
- }
- }
|