add wate to inventory

This commit is contained in:
2017-04-27 20:53:35 +02:00
parent ec5968511b
commit 567592b73e
6 changed files with 164 additions and 37 deletions

View File

@@ -2,6 +2,7 @@
namespace backend\controllers;
use common\models\Log;
use Yii;
use common\models\Product;
use backend\models\ProductSearch;
@@ -117,8 +118,19 @@ class ProductController extends \backend\controllers\BackendController
$accounts = Account::readAccounts($model->id_account);
$categories = ProductCategory::read($model->id_product_category);
$stockOriginal = $model->stock;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$stockUpdated = $model->stock;
$message = "Update product. Stock: " . $stockOriginal . " -> " . $stockUpdated;
Log::log([
'type' =>Log::$TYPE_CRUD,
'message' => $message,
'id_product' => $model->id_product
]);
return $this->redirect(['view', 'id' => $model->id_product]);
} else {
return $this->render('update', [

View File

@@ -2,6 +2,7 @@
namespace backend\models;
use common\models\Waste;
use /** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
Yii;
use yii\base\Model;
@@ -47,6 +48,7 @@ class InventoryItemForm extends Model{
/** internal stuff*/
public $product_in;
public $product_waste;
public $product_out;
public $product_stock;
public $last_inventory_item;
@@ -94,19 +96,18 @@ class InventoryItemForm extends Model{
public function save(){
$this->loadLastInventory();
$this->loadLastInventoryItem();
$this->loadProductIn();
$this->loadProductSold();
$this->loadProductStock();
$this->loadProductWaste();
$item = new InventoryItem();
$item->count_in = $this->product_in;
$item->count_sold = $this->product_out;
$item->count_system = $this->product_stock;
$item->count_waste = $this->product_waste;
if ( isset($this->inventory )){
if ( $this->inventory->type == Inventory::$TYPE_DAILY){
@@ -125,7 +126,6 @@ class InventoryItemForm extends Model{
}else{
$item->count_prev = 0;
}
if ( isset(\Yii::$app->user )){
$item->id_user = \Yii::$app->user->id;
@@ -157,7 +157,7 @@ class InventoryItemForm extends Model{
$query->select(['coalesce(sum(procurement.count),0) as total_in']);
$query->from(Procurement::tableName());
$query->innerJoin(Product::tableName(),"product.id_product = procurement.id_product");
if ( isset($this->last_inventory_item ) ){
$query->andWhere([ '>', 'procurement.created_at' ,$this->last_inventory_item->created_at]);
}
@@ -170,10 +170,31 @@ class InventoryItemForm extends Model{
}else{
$query->andWhere(['product.id_inventory_group' => $this->inventoryGroup->id_inventory_group]);
}
$this->product_in = $query->scalar();
}
protected function loadProductWaste(){
$query = new Query();
$query->select(['coalesce(sum(waste.count),0) as total_waste']);
$query->from(Waste::tableName());
$query->innerJoin(Product::tableName(),"product.id_product = waste.id_product");
if ( isset($this->last_inventory_item ) ){
$query->andWhere([ '>', 'waste.created_at' ,$this->last_inventory_item->created_at]);
}
if ( $this->type == 'product') {
$query->andWhere(['product.id_product' => $this->product->id_product]);
}else{
$query->andWhere(['product.id_inventory_group' => $this->inventoryGroup->id_inventory_group]);
}
$this->product_waste = $query->scalar();
}
protected function loadProductSold(){