AgentFrontErpInventory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * This is the model class for table "{{%agent_front_erp_inventory}}".
  13. *
  14. * @property integer $id
  15. * @property string $goods_no
  16. */
  17. class AgentFrontErpInventory extends \yii\db\ActiveRecord
  18. {
  19. public $sync2Goods = 1;
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%agent_front_erp_inventory}}';
  26. }
  27. public function behaviors()
  28. {
  29. return [
  30. [
  31. 'class' => TimestampBehavior::class,
  32. ]
  33. ];
  34. }
  35. public function afterSave($insert, $changedAttributes)
  36. {
  37. parent::afterSave($insert, $changedAttributes);
  38. // if(!$insert && $changedAttributes['num'] && $this->sync2Goods){
  39. // queue_push(new \app\jobs\ErpJob([
  40. // 'in_action' => 'executeAgentFrontErpInventory2Goods',
  41. // 'id' => $this->id,
  42. // ]), 30);
  43. // }
  44. //TODO 入库出库时 此处调用云仓供货商同步商品数量的方案 进行同步数量
  45. }
  46. // public static function num2Goods($id)
  47. // {
  48. // $ei = AgentFrontErpInventory::findOne($id);
  49. //
  50. // if ($ei) {
  51. // $goods_arr = Goods::find()->where(['cloud_goods_id' => $ei->goods_id])->select('id, attr')->asArray()->all();
  52. // $attr_id = [];
  53. // foreach ($goods_arr as $goods_) {
  54. // $attr_list = json_decode($goods_['attr'], true);
  55. // foreach ($attr_list as $attr) {
  56. // if ($attr['no'] === $ei->goods_no) {
  57. // $attr_id = array_column($attr['attr_list'], 'attr_id');
  58. // }
  59. // }
  60. // $numAdd = Goods::numAddStatic($goods_['id'], $attr_id, $ei->num, 1);
  61. // if(!$numAdd){
  62. // \Yii::error([__METHOD__, 'erp同步库存到商品失败!', $ei->attributes]);
  63. // }
  64. // }
  65. // }
  66. //
  67. //
  68. // }
  69. public static function logSave ($ei, $num, $type = AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $order_id = 0, $order_no = '')
  70. {
  71. $t = \Yii::$app->db->beginTransaction();
  72. try {
  73. $model = new AgentFrontErpInventoryLog();
  74. $model->inventory_id = $ei['id'];
  75. $model->attr_ids = $ei['attr_ids'];
  76. $model->type = $type;
  77. $model->order_id = $order_id;
  78. $model->order_no = $order_no;
  79. $model->before = $ei['num'];
  80. $model->after = $ei['num'] + $num;
  81. $model->num = $num;
  82. if (!$model->save()) {
  83. \Yii::error([__METHOD__, $model->attributes]);
  84. throw new \Exception('日志保存失败!' . array_shift($model->getFirstErrors()));
  85. }
  86. $ei->num = $model->after;
  87. if($type == ErpInventoryLog::LOG_TYPE_PURCHASEOUT && $ei->num < 0){
  88. \Yii::error([__METHOD__, $ei->attributes]);
  89. throw new \Exception('库存保存失败!ID:'. $ei->id .'出库数量大于库存');
  90. }
  91. if (!$ei->save()) {
  92. \Yii::error([__METHOD__, $ei->attributes]);
  93. throw new \Exception('库存保存失败!' . array_shift($ei->getFirstErrors()));
  94. }
  95. $ei_attr_info = json_decode($ei->attr_info, true);
  96. if (!empty($ei_attr_info)) {
  97. $updateAttrList = [
  98. [
  99. 'attr_id_list' => array_column($ei_attr_info['attr_list'], 'attr_id'),
  100. 'num' => 0
  101. ]
  102. ];
  103. $form = new \app\modules\admin\models\SupplierForm();
  104. $res = $form->setGoodsAttrNum($ei->supplier_id, $ei->goods_id, $updateAttrList);
  105. }
  106. $t->commit();
  107. return [
  108. 'code' => 0,
  109. 'msg' => '操作成功!'
  110. ];
  111. } catch (\Exception $e) {
  112. $t->rollBack();
  113. \Yii::error($e);
  114. return [
  115. 'code' => 1,
  116. 'msg' => $e->getMessage()
  117. ];
  118. }
  119. }
  120. }