| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?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 "{{%agent_front_erp_inventory}}".
- *
- * @property integer $id
- * @property string $goods_no
- */
- class AgentFrontErpInventory extends \yii\db\ActiveRecord
- {
- public $sync2Goods = 1;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%agent_front_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\ErpJob([
- // 'in_action' => 'executeAgentFrontErpInventory2Goods',
- // 'id' => $this->id,
- // ]), 30);
- // }
- //TODO 入库出库时 此处调用云仓供货商同步商品数量的方案 进行同步数量
- }
- // public static function num2Goods($id)
- // {
- // $ei = AgentFrontErpInventory::findOne($id);
- //
- // if ($ei) {
- // $goods_arr = Goods::find()->where(['cloud_goods_id' => $ei->goods_id])->select('id, attr')->asArray()->all();
- // $attr_id = [];
- // foreach ($goods_arr as $goods_) {
- // $attr_list = json_decode($goods_['attr'], true);
- // foreach ($attr_list as $attr) {
- // if ($attr['no'] === $ei->goods_no) {
- // $attr_id = array_column($attr['attr_list'], 'attr_id');
- // }
- // }
- // $numAdd = Goods::numAddStatic($goods_['id'], $attr_id, $ei->num, 1);
- // if(!$numAdd){
- // \Yii::error([__METHOD__, 'erp同步库存到商品失败!', $ei->attributes]);
- // }
- // }
- // }
- //
- //
- // }
- public static function logSave ($ei, $num, $type = AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $order_id = 0, $order_no = '')
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $model = new AgentFrontErpInventoryLog();
- $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()));
- }
- $ei_attr_info = json_decode($ei->attr_info, true);
- if (!empty($ei_attr_info)) {
- $updateAttrList = [
- [
- 'attr_id_list' => array_column($ei_attr_info['attr_list'], 'attr_id'),
- 'num' => 0
- ]
- ];
- $form = new \app\modules\admin\models\SupplierForm();
- $res = $form->setGoodsAttrNum($ei->supplier_id, $ei->goods_id, $updateAttrList);
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|