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

@@ -1,22 +1,25 @@
<?php
namespace console\controllers;
use backend\components\InventoryManager;
use common\models\Inventory;
use common\models\InventoryItem;
use common\models\Transfer;
use common\models\Product;
use common\models\Waste;
use yii\console\Controller;
use yii\db\Expression;
use yii\db\Query;
class InventoryConsoleController extends Controller{
class InventoryConsoleController extends Controller
{
/**
* @param $idInventory
*/
public function actionFixSold($idInventory){
public function actionFixWaste($idInventory)
{
$inventory = Inventory::findOne($idInventory);
@@ -25,38 +28,94 @@ class InventoryConsoleController extends Controller{
$items = InventoryItem::find()->andWhere(['id_inventory' => $inventory->id_inventory])->all();
/** @var /common/models/InventoryItem $item */
foreach ($items as $item ){
foreach ($items as $item) {
/** @var /common/models/InventoryItem $prev */
$prev = $item->inventoryItemPrev;
if ( !isset($prev)){
continue;
if (!isset($prev)) {
$start = null;
$end = null;
} else {
$start = $prev->created_at;
$end = $item->created_at;
}
$start = $prev->created_at;
$end = $item->created_at;
$query = new Query();
$query->select([
new Expression(" coalesce( sum( transfer.count ),0)")
]);
$query->from('transfer');
$query->innerJoin('sale','sale.id_sale = transfer.id_object');
$query->innerJoin('product','sale.id_product = product.id_product');
$query->select(['coalesce(sum(waste.count),0) as total_waste']);
$query->from(Waste::tableName());
$query->innerJoin(Product::tableName(), "product.id_product = waste.id_product");
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
$query->andWhere(['in', 'transfer.status', [Transfer::STATUS_NOT_PAID, Transfer::STATUS_PAID]]);
$query->andWhere(['product.id_product' => $prev->id_product]);
if (isset($prev)) {
$query->andWhere(['>', 'waste.created_at', $prev->created_at]);
}
$query->andWhere([ '>','transfer.created_at',$start ]);
$query->andWhere([ '<','transfer.created_at',$end ]);
$query->andWhere(['<', 'waste.created_at', $inventory->created_at]);
$products = $query->scalar();
$item->count_sold = $products;
if ($item->type == 10) {
$query->andWhere(['product.id_product' => $item->id_product]);
} else {
$query->andWhere(['product.id_inventory_group' => $item->id_inventory_group]);
}
echo $item->price_brutto . " - ". $item->productName ."\n";
$product_waste = $query->scalar();
$item->count_waste = $product_waste;
echo $item->price_brutto . " - " . $item->productName . " - " . $product_waste . "\n";
$item->save();
}
}
/**
* @param $idInventory
*/
public
function actionFixSold($idInventory)
{
$inventory = Inventory::findOne($idInventory);
/** @var /common/models/InventoryItem[] $items */
$items = InventoryItem::find()->andWhere(['id_inventory' => $inventory->id_inventory])->all();
/** @var /common/models/InventoryItem $item */
foreach ($items as $item) {
// $prev = $item->inventoryItemPrev;
//
// if ( !isset($prev)){
// continue;
// }
// $start = $prev->created_at;
// $end = $item->created_at;
//
//
// $query = new Query();
// $query->select([
// new Expression(" coalesce( sum( transfer.count ),0)")
// ]);
// $query->from('transfer');
// $query->innerJoin('sale','sale.id_sale = transfer.id_object');
// $query->innerJoin('product','sale.id_product = product.id_product');
//
// $query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
// $query->andWhere(['in', 'transfer.status', [Transfer::STATUS_NOT_PAID, Transfer::STATUS_PAID]]);
// $query->andWhere(['product.id_product' => $prev->id_product]);
//
// $query->andWhere([ '>','transfer.created_at',$start ]);
// $query->andWhere([ '<','transfer.created_at',$end ]);
//
// $products = $query->scalar();
//
// $item->count_sold = $products;
//
echo $item->price_brutto . " - " . $item->productName . "\n";
$item->save();
@@ -69,14 +128,12 @@ class InventoryConsoleController extends Controller{
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
public function actionCreate()
{
$inventoryManager = new InventoryManager();
$inventoryManager->generateDaily();
}
}

View File

@@ -0,0 +1,30 @@
<?php
use yii\db\Schema;
use yii\db\Migration;
class m170404_060114_alter_table_inventory_item_add_colum_count_waste extends Migration
{
public function up()
{
$this->addColumn("inventory_item", "count_waste", "int default 0");
}
public function down()
{
echo "m170404_060114_alter_table_inventory_item_add_colum_count_waste cannot be reverted.\n";
return false;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}