add inventory changes
This commit is contained in:
@@ -5,12 +5,16 @@ namespace common\models;
|
||||
use Yii;
|
||||
use common\components\UserAwareBehavior;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\db\Query;
|
||||
use backend\models\InventoryItemForm;
|
||||
use common\components\AccountAwareBehavior;
|
||||
|
||||
/**
|
||||
* This is the model class for table "inventory".
|
||||
*
|
||||
* @property integer $id_inventory
|
||||
* @property integer $id_user
|
||||
* @property integer $id_account
|
||||
* @property string $name
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
@@ -26,6 +30,7 @@ class Inventory extends \common\models\BaseFitnessActiveRecord
|
||||
{
|
||||
return 'inventory';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
@@ -33,7 +38,8 @@ class Inventory extends \common\models\BaseFitnessActiveRecord
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['name'], 'string']
|
||||
[['name'], 'string'],
|
||||
[['id_account'], 'integer'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -46,6 +52,7 @@ class Inventory extends \common\models\BaseFitnessActiveRecord
|
||||
'id_inventory' => Yii::t('common/inventory', 'Leltár azonosító'),
|
||||
'name' => Yii::t('common/inventory', 'Megnevezés'),
|
||||
'id_user' => Yii::t('common/inventory', 'Felhasználó'),
|
||||
'id_account' => Yii::t('common/inventory', 'Kassza'),
|
||||
'created_at' => Yii::t('common/inventory', 'Létrehozás'),
|
||||
'updated_at' => Yii::t('common/inventory', 'Módosítás'),
|
||||
];
|
||||
@@ -59,7 +66,47 @@ class Inventory extends \common\models\BaseFitnessActiveRecord
|
||||
return ArrayHelper::merge( [
|
||||
[
|
||||
'class' => UserAwareBehavior::className(),
|
||||
],
|
||||
[
|
||||
'class' => AccountAwareBehavior::className()
|
||||
|
||||
]
|
||||
], parent::behaviors());
|
||||
}
|
||||
|
||||
public function afterSave($insert, $changedAttributes){
|
||||
if ( $insert ){
|
||||
|
||||
$query = Product::find();
|
||||
if ( isset($this->id_account) && !is_numeric($this->id_account)){
|
||||
$query->andWhere(['id_account' => $this->id_account]);
|
||||
|
||||
}
|
||||
// $query->andWhere("product.id_inventory_group is null");
|
||||
$products = $query->all();
|
||||
|
||||
echo "Products found: " . count($products);
|
||||
|
||||
$inventoryGroups = InventoryGroup::find()->all();
|
||||
|
||||
|
||||
foreach ($products as $product){
|
||||
$form = new InventoryItemForm(
|
||||
[
|
||||
'inventory' => $this,
|
||||
'id_product' => $product->id_product,
|
||||
'product' => $product,
|
||||
'type' => 'product'
|
||||
]
|
||||
);
|
||||
$form->save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use common\components\UserAwareBehavior;
|
||||
use common\components\Helper;
|
||||
use common\components\ProductAwareBehavior;
|
||||
|
||||
/**
|
||||
* This is the model class for table "inventory_item".
|
||||
@@ -45,6 +46,8 @@ class InventoryItem extends BaseFitnessActiveRecord
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['count'],'integer'] ,
|
||||
[['count'],'required'] ,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -58,7 +61,7 @@ class InventoryItem extends BaseFitnessActiveRecord
|
||||
'id_inventory' => Yii::t('common/inventory_item', 'Id Inventory'),
|
||||
'count_in' => Yii::t('common/inventory_item', 'Count In'),
|
||||
'count_sold' => Yii::t('common/inventory_item', 'Count Sold'),
|
||||
'count' => Yii::t('common/inventory_item', 'Count'),
|
||||
'count' => Yii::t('common/inventory_item', 'Leltározott mennyiség'),
|
||||
'type' => Yii::t('common/inventory_item', 'Type'),
|
||||
'id_product' => Yii::t('common/inventory_item', 'Id Product'),
|
||||
'id_inventory_group' => Yii::t('common/inventory_item', 'Id Inventory Group'),
|
||||
@@ -72,6 +75,9 @@ class InventoryItem extends BaseFitnessActiveRecord
|
||||
return ArrayHelper::merge( [
|
||||
[
|
||||
'class' => UserAwareBehavior::className(),
|
||||
],
|
||||
[
|
||||
'class' => ProductAwareBehavior::className(),
|
||||
]
|
||||
], parent::behaviors());
|
||||
}
|
||||
@@ -133,4 +139,13 @@ class InventoryItem extends BaseFitnessActiveRecord
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function afterSave($insert, $changedAttributes){
|
||||
if ( !$insert ){
|
||||
$product = $this->product;
|
||||
$product->stock = $this->count;
|
||||
$product->save(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -264,4 +264,36 @@ class Product extends \common\models\BaseFitnessActiveRecord {
|
||||
$product->stock = $product->stock - $count;
|
||||
}
|
||||
|
||||
|
||||
public static function readProductsNotPartOfInventoryGroup( $id_account = null ){
|
||||
$query = Product::find();
|
||||
$query->andWhere("id_inventory_group is null");
|
||||
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
return $query->all();
|
||||
}
|
||||
|
||||
public static function readInventoryGroups(){
|
||||
$query = InventoryGroup::find();
|
||||
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
return $query->all();
|
||||
}
|
||||
|
||||
public static function buildProductAndInventoryGroupList($id_account = null){
|
||||
$productOptions = [];
|
||||
|
||||
$products = Product::readProductsNotPartOfInventoryGroup($id_account);
|
||||
$inventoryGroups = Product::readInventoryGroups();
|
||||
|
||||
foreach ($products as $product ){
|
||||
$productOptions[]= [ 'type' =>'product', 'id' => $product->id_product ,'name' => $product->name . " (Termék/" .$product->productCategoryName ."/" . $product->accountName .")" ];
|
||||
}
|
||||
|
||||
foreach ($inventoryGroups as $inventoryGroup ){
|
||||
$productOptions[] = ['type' =>'group', 'id' =>$inventoryGroup->id_inventory_group ,'name' =>$inventoryGroup->name ."/Termékcsoport" ];
|
||||
|
||||
}
|
||||
return $productOptions;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user