| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.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_prop}}".
- *
- * @property integer $id
- */
- class ErpInventoryProp extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%erp_inventory_prop}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- ]
- ];
- }
-
- public static function saveIn($eil, $purchasein) {
- $item = $purchasein;
- $model = ErpInventoryProp::findOne([
- 'inventory_id' => $item['inventory_id'],
- 'batch_no' => (string)$item['batch_no'],
- 'timeout' => (int)$item['timeout'],
- ]);
- if(!$model){
- $model = new ErpInventoryProp();
- $model->inventory_id = $item['inventory_id'];
- $model->batch_no = (string)$item['batch_no'];
- $model->timeout = (int)$item['timeout'];
- }
- $model->num += (int)$item['num'];
- $model->price = (float)$item['price'];
- $model->is_delete = 0;
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception('属性库存保存失败1!' . array_shift($model->getFirstErrors()));
- }
- self::_logSave($eil, $model, $item['num']);
- }
-
- public static function saveOut($eil, $purchaseout) {
- $item = $purchaseout;
- if(empty($item['eip_id'])){
- return;
- }
- $model = ErpInventoryProp::findOne($item['eip_id']);
- if(!$model){
- return;
- }
- $model->num -= (int)$item['num'];
- if ($model->num < 0) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception('属性库存数量异常!' . $model->num);
- }
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception('属性库存保存失败1!' . array_shift($model->getFirstErrors()));
- }
- self::_logSave($eil, $model, -$item['num']);
- }
-
- public static function saveOrderOut($eil) {
- $num = $eil->num;
- $list = ErpInventoryProp::find()->where(['inventory_id' => $eil['inventory_id']])->andWhere(['>', 'num', 0])->orderBy('timeout ASC')->all();
- $break = 0;
- foreach($list as $model){
- $num_prop = $num;
- $model->num += $num;
- if($model->num < 0){
- $num_prop = $num - $model->num;
- $num = $model->num;
- $model->num = 0;
- }else{
- $break = 1;
- }
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception('属性库存保存失败!' . array_shift($model->getFirstErrors()));
- }
-
- self::_logSave($eil, $model, $num_prop);
-
- if($break){
- break;
- }
- }
- }
-
- public static function saveOrderIn($eil) {
- $logOut = ErpInventoryLog::find()->where(['type' => 0, 'inventory_id' => $eil['inventory_id'], 'order_id' => $eil['order_id']])->andWhere(['<', 'num', 0])->limit(1)->one();
- if($logOut){
- $list = ErpInventoryPropLog::find()->where(['inventory_log_id' => $logOut['id']])->all();
- foreach($list as $model){
- $prop = ErpInventoryProp::findOne($model['inventory_prop_id']);
- $prop->num -= (int)$model->num;
- if ($prop->num < 0) {
- \Yii::error([__METHOD__, $prop->attributes]);
- throw new \Exception('属性库存数量异常!' . $prop->num);
- }
- if (!$prop->save()) {
- \Yii::error([__METHOD__, $prop->attributes]);
- throw new \Exception('属性库存保存失败1!' . array_shift($prop->getFirstErrors()));
- }
- self::_logSave($eil, $prop, -$model->num);
- }
- }else{
- //
- }
- }
- public static function _logSave($eil, $prop, $num){
- $model = new ErpInventoryPropLog();
- $model->inventory_log_id = $eil['id'];
- $model->inventory_prop_id = $prop['id'];
- $model->before = $prop['num'] - $num;
- $model->after = $prop['num'];
- $model->num = $num;
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception('属性日志保存失败!' . array_shift($model->getFirstErrors()));
- }
- }
- public static function logSave($eil){
- if($eil->type == ErpInventoryLog::LOG_TYPE_PURCHASEIN){
- $purchasein = ErpPurchasein::findOne(['purchase_order_id' => $eil->order_id, 'inventory_id' => $eil->inventory_id]);
- return self::saveIn($eil, $purchasein);
- }
- if($eil->type == ErpInventoryLog::LOG_TYPE_PURCHASEOUT){
- $purchaseout = ErpPurchaseout::findOne(['purchase_order_id' => $eil->order_id]);
- return self::saveOut($eil, $purchaseout);
- }
- if($eil->type == ErpInventoryLog::LOG_TYPE_ORDER){
- if($eil->num >= 0){
- return self::saveOrderIn($eil);
- }else{
- return self::saveOrderOut($eil);
- }
- }
- }
- public static function logFind($order, $goods_id, $attr){
- $sku_id = Goods::skuid1($goods_id, $attr);
- $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();
- if(!$eil){
- return [];
- }
- $list = ErpInventoryPropLog::find()->where(['inventory_log_id' => $eil->id])->asArray()->all();
- foreach($list as &$item){
- $item['eip'] = ErpInventoryProp::findOne($item['inventory_prop_id']);
- }
- return $list;
- }
- }
|