ErpInventoryProp.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.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 "{{%erp_inventory_prop}}".
  13. *
  14. * @property integer $id
  15. */
  16. class ErpInventoryProp extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%erp_inventory_prop}}';
  24. }
  25. public function behaviors()
  26. {
  27. return [
  28. [
  29. 'class' => TimestampBehavior::class,
  30. ]
  31. ];
  32. }
  33. public static function saveIn($eil, $purchasein) {
  34. $item = $purchasein;
  35. $model = ErpInventoryProp::findOne([
  36. 'inventory_id' => $item['inventory_id'],
  37. 'batch_no' => (string)$item['batch_no'],
  38. 'timeout' => (int)$item['timeout'],
  39. ]);
  40. if(!$model){
  41. $model = new ErpInventoryProp();
  42. $model->inventory_id = $item['inventory_id'];
  43. $model->batch_no = (string)$item['batch_no'];
  44. $model->timeout = (int)$item['timeout'];
  45. }
  46. $model->num += (int)$item['num'];
  47. $model->price = (float)$item['price'];
  48. $model->is_delete = 0;
  49. if (!$model->save()) {
  50. \Yii::error([__METHOD__, $model->attributes]);
  51. throw new \Exception('属性库存保存失败1!' . array_shift($model->getFirstErrors()));
  52. }
  53. self::_logSave($eil, $model, $item['num']);
  54. }
  55. public static function saveOut($eil, $purchaseout) {
  56. $item = $purchaseout;
  57. if(empty($item['eip_id'])){
  58. return;
  59. }
  60. $model = ErpInventoryProp::findOne($item['eip_id']);
  61. if(!$model){
  62. return;
  63. }
  64. $model->num -= (int)$item['num'];
  65. if ($model->num < 0) {
  66. \Yii::error([__METHOD__, $model->attributes]);
  67. throw new \Exception('属性库存数量异常!' . $model->num);
  68. }
  69. if (!$model->save()) {
  70. \Yii::error([__METHOD__, $model->attributes]);
  71. throw new \Exception('属性库存保存失败1!' . array_shift($model->getFirstErrors()));
  72. }
  73. self::_logSave($eil, $model, -$item['num']);
  74. }
  75. public static function saveOrderOut($eil) {
  76. $num = $eil->num;
  77. $list = ErpInventoryProp::find()->where(['inventory_id' => $eil['inventory_id']])->andWhere(['>', 'num', 0])->orderBy('timeout ASC')->all();
  78. $break = 0;
  79. foreach($list as $model){
  80. $num_prop = $num;
  81. $model->num += $num;
  82. if($model->num < 0){
  83. $num_prop = $num - $model->num;
  84. $num = $model->num;
  85. $model->num = 0;
  86. }else{
  87. $break = 1;
  88. }
  89. if (!$model->save()) {
  90. \Yii::error([__METHOD__, $model->attributes]);
  91. throw new \Exception('属性库存保存失败!' . array_shift($model->getFirstErrors()));
  92. }
  93. self::_logSave($eil, $model, $num_prop);
  94. if($break){
  95. break;
  96. }
  97. }
  98. }
  99. public static function saveOrderIn($eil) {
  100. $logOut = ErpInventoryLog::find()->where(['type' => 0, 'inventory_id' => $eil['inventory_id'], 'order_id' => $eil['order_id']])->andWhere(['<', 'num', 0])->limit(1)->one();
  101. if($logOut){
  102. $list = ErpInventoryPropLog::find()->where(['inventory_log_id' => $logOut['id']])->all();
  103. foreach($list as $model){
  104. $prop = ErpInventoryProp::findOne($model['inventory_prop_id']);
  105. $prop->num -= (int)$model->num;
  106. if ($prop->num < 0) {
  107. \Yii::error([__METHOD__, $prop->attributes]);
  108. throw new \Exception('属性库存数量异常!' . $prop->num);
  109. }
  110. if (!$prop->save()) {
  111. \Yii::error([__METHOD__, $prop->attributes]);
  112. throw new \Exception('属性库存保存失败1!' . array_shift($prop->getFirstErrors()));
  113. }
  114. self::_logSave($eil, $prop, -$model->num);
  115. }
  116. }else{
  117. //
  118. }
  119. }
  120. public static function _logSave($eil, $prop, $num){
  121. $model = new ErpInventoryPropLog();
  122. $model->inventory_log_id = $eil['id'];
  123. $model->inventory_prop_id = $prop['id'];
  124. $model->before = $prop['num'] - $num;
  125. $model->after = $prop['num'];
  126. $model->num = $num;
  127. if (!$model->save()) {
  128. \Yii::error([__METHOD__, $model->attributes]);
  129. throw new \Exception('属性日志保存失败!' . array_shift($model->getFirstErrors()));
  130. }
  131. }
  132. public static function logSave($eil){
  133. if($eil->type == ErpInventoryLog::LOG_TYPE_PURCHASEIN){
  134. $purchasein = ErpPurchasein::findOne(['purchase_order_id' => $eil->order_id, 'inventory_id' => $eil->inventory_id]);
  135. return self::saveIn($eil, $purchasein);
  136. }
  137. if($eil->type == ErpInventoryLog::LOG_TYPE_PURCHASEOUT){
  138. $purchaseout = ErpPurchaseout::findOne(['purchase_order_id' => $eil->order_id]);
  139. return self::saveOut($eil, $purchaseout);
  140. }
  141. if($eil->type == ErpInventoryLog::LOG_TYPE_ORDER){
  142. if($eil->num >= 0){
  143. return self::saveOrderIn($eil);
  144. }else{
  145. return self::saveOrderOut($eil);
  146. }
  147. }
  148. }
  149. public static function logFind($order, $goods_id, $attr){
  150. $sku_id = Goods::skuid1($goods_id, $attr);
  151. $eil = ErpInventoryLog::find()->where(['inventory_id' => ErpInventory::find()->select('id')->where(['goods_id' => $goods_id, 'sku_id' => $sku_id, 'store_id' => $order['store_id']]), 'order_id' => $order['id']])->one();
  152. if(!$eil){
  153. return [];
  154. }
  155. $list = ErpInventoryPropLog::find()->where(['inventory_log_id' => $eil->id])->asArray()->all();
  156. foreach($list as &$item){
  157. $item['eip'] = ErpInventoryProp::findOne($item['inventory_prop_id']);
  158. }
  159. return $list;
  160. }
  161. }