add inventory item changes

This commit is contained in:
Roland Schneider 2016-09-07 07:42:43 +02:00
parent 59c8099530
commit 179b47c454
8 changed files with 181 additions and 30 deletions

View File

@ -202,14 +202,26 @@ class InventoryItemController extends Controller
$model->save(false);
$prev = Url::previous("inventory-item-index");
\Yii::$app->session->setFlash('success', "Mennyiség elmentve: " . $model->productName . ' - ' .$model->count . ' db');
if (isset($prev)) {
return $this->redirect($prev);
} else {
// $prev = Url::previous("inventory-item-index");
// if (isset($prev)) {
// return $this->redirect($prev);
// } else {
// return $this->redirect(['index', 'id' => $inventory->id_inventory]);
// }
$next = InventoryItem::findNextItemAlphabetical($model->id_inventory_item);
if ( isset($next)){
return $this->redirect(['inventory-item/update', 'id' => $next]);
}else{
return $this->redirect(['index', 'id' => $inventory->id_inventory]);
}
} else {
return $this->render('update', [
'model' => $model,

View File

@ -2,18 +2,15 @@
namespace backend\models;
use Yii;
use /** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
Yii;
use yii\base\Model;
use common\models\Card;
use common\models\Customer;
use common\models\Ticket;
use common\models\Account;
use yii\web\UploadedFile;
use common\models\Inventory;
use yii\base\Exception;
use common\models\Product;
use common\models\InventoryGroup;
use common\models\InventoryItem;
use yii\db\Expression;
use yii\db\Query;
use common\models\Procurement;
use common\models\Transfer;
@ -52,6 +49,8 @@ class InventoryItemForm extends Model{
public $last_inventory_item;
public $inventory_item;
public $next_item_index;
public function rules(){
return [
[[ 'name', 'count'], 'required'],
@ -63,14 +62,16 @@ class InventoryItemForm extends Model{
];
}
public function validateType($attribute,$params){
public function validateType(/** @noinspection PhpUnusedParameterInspection */
$attribute, $params){
if ( array_search($this->type, ['product','group']) === false ){
\Yii::error("Típus nem megfelelő: " . $this->type);
$this->addError("name","Nincs termék vagy termék csoport kiválasztva");
}
}
public function validateId($attribute,$params){
public function validateId(/** @noinspection PhpUnusedParameterInspection */
$attribute, $params){
if ( !$this->hasErrors('type')){
if ( $this->type == 'group'){
$this->inventoryGroup = InventoryGroup::findOne($this->id_product);
@ -171,6 +172,10 @@ class InventoryItemForm extends Model{
$query->andWhere(['in', 'transfer.status' ,[Transfer::STATUS_PAID ,Transfer::STATUS_NOT_PAID ] ]);
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
if ( isset($this->last_inventory_item ) ){
$query->andWhere([ '>', 'transfer.created_at' ,$this->last_inventory_item->created_at]);
}
if ( $this->type == 'product') {
$query->andWhere(['product.id_product' => $this->product->id_product]);
}else{
@ -275,12 +280,10 @@ class InventoryItemForm extends Model{
foreach ($this->products as $product ){
$this->productOptions[]= [ 'type' =>'product', 'id' => $product->id_product ,'name' => $product->name . " (Termék/" .$product->productCategoryName ."/" . $product->accountName .")" ];
}
foreach ($this->inventoryGroups as $inventoryGroup ){
$this->productOptions[] = ['type' =>'group', 'id' =>$inventoryGroup->id_inventory_group ,'name' =>$inventoryGroup->name ."/Termékcsoport" ];
}
}
@ -294,4 +297,6 @@ class InventoryItemForm extends Model{
}
}

View File

@ -19,7 +19,7 @@ $options = [];
$options['products'] = $model->productOptions;
$options['url_product_find'] = Url::toRoute(['product/find']);
$this->registerJs('inventoryItemCreate.init( '. json_encode($options) .' );');
$this-> registerJs('inventoryItemCreate.init( '. json_encode($options) .' );');
?>
<div class="inventory-item-create">

View File

@ -24,6 +24,7 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );');
?>
<!--suppress CssUnusedSymbol -->
<style>
.table th {
white-space: normal;
@ -65,7 +66,7 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );');
}
?>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<div>
Magyarázat:
<ul>
<li>
@ -87,7 +88,7 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );');
Különbség: = "Leltározott mennyiség - ( "Előző leltár" + "Beszerzett mennyiség" - "Eladott mennyiség" )
</li>
</ul>
</p>
</div>
<p>
<?php echo Html::a("XLS letöltése", Url::current(['InventoryItemSearch[output]' => 'xls']), ['class' => 'btn btn-primary']) ?>
</p>
@ -165,7 +166,7 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );');
'urlCreator' => function ($action, $model) {
$result = "";
if ($action == 'update') {
$result = Url::toRoute(['update-item', 'id' => $model['item_id_inventory_item']]);
$result = Url::toRoute(['update', 'id' => $model['item_id_inventory_item']]);
}
return $result;
}

View File

@ -7,10 +7,11 @@ use yii\widgets\DetailView;
/* @var $model common\models\InventoryItem */
$this->title = Yii::t('common/inventory-item', 'Leltár termék');
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/inventory-item', 'Inventory Items'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => 'Leltár termékek', 'url' => ['index', 'id' => $model->id_inventory]];
$this->params['breadcrumbs'][] = ['label' => $model->id_inventory_item, 'url' => ['view', 'id' => $model->id_inventory_item]];
$this->params['breadcrumbs'][] = Yii::t('common/inventory-item', 'Update');
?>
<div class="inventory-item-update">
<h1><?= Html::encode($this->title) ?></h1>

View File

@ -20,6 +20,8 @@ use common\components\Helper;
* @property string $name
* @property string $created_at
* @property string $updated_at
* @property mixed userName
* @property mixed accountName
*/
class Inventory extends \common\models\BaseFitnessActiveRecord
{

View File

@ -2,10 +2,12 @@
namespace common\models;
use Yii;
use /** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
Yii;
use yii\db\Expression;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use common\components\UserAwareBehavior;
use common\components\Helper;
use common\components\ProductAwareBehavior;
/**
@ -28,6 +30,7 @@ use common\components\ProductAwareBehavior;
* @property string $created_at
* @property string $updated_at
* @property mixed productName
* @property mixed product
*/
class InventoryItem extends BaseFitnessActiveRecord
{
@ -36,7 +39,8 @@ class InventoryItem extends BaseFitnessActiveRecord
public static $TYPE_INVENTORY_GROUP = 20;
public static $UPDATE_COUNT = "
public static /** @noinspection SqlResolve */
$UPDATE_COUNT = "
UPDATE product as p1
inner JOIN ( select inventory_item.id_product as id_product , inventory_item.count as count
from inventory_item
@ -114,6 +118,10 @@ class InventoryItem extends BaseFitnessActiveRecord
return $this->hasOne( Product::className() , [ 'id_inventory_group' => 'id_inventory_group' ]);
}
public function getInventoryItemPrev(){
return $this->hasOne( InventoryItem::className(),['id_inventory_item' => 'id_inventory_item_prev'] );
}
public function getProductName(){
$result = "";
@ -137,8 +145,10 @@ class InventoryItem extends BaseFitnessActiveRecord
return $result;
}
/**
* @return string
*/
public function getName(){
$name = "";
if ( isset($this->id_product )){
$name = $this->getProductName();
@ -171,6 +181,53 @@ class InventoryItem extends BaseFitnessActiveRecord
$this->total_price_brutto = $price * $diff;
}
public static function findNextItemAlphabetical($id_inventory_item){
$inventoryItem = InventoryItem::findOne($id_inventory_item);
$id_inventory = $inventoryItem->id_inventory;
$currentItemId = $id_inventory_item;
$query = new Query();
$query->select([
'inventory_item.id_inventory_item as inventory_item_id_inventory',
new Expression(
"case when inventory_item.type = " .InventoryItem::$TYPE_PRODUCT . " then product.name else inventory_group.name end as name"
)
]);
$query->from('inventory_item');
$query->leftJoin("product", 'product.id_product = inventory_item.id_product and inventory_item.type =' .InventoryItem::$TYPE_PRODUCT);
$query->leftJoin("inventory_group", 'inventory_group.id_inventory_group = inventory_item.id_product and inventory_item.type =' .InventoryItem::$TYPE_INVENTORY_GROUP);
$query->andWhere(['id_inventory' => $id_inventory]);
$query->orderBy("name");
$result = $query->all();
$currentItemIndex = null;
$size = count($result);
for ( $i = 0; $i < $size; $i++ ){
$item = $result[$i];
$id = $item['inventory_item_id_inventory'];
if ( $id == $currentItemId ){
$currentItemIndex = $i;
break;
}
}
if ( isset( $currentItemIndex ) ){
$next = ($currentItemIndex +1 );
if ($size > $next){
return $result[$next]['inventory_item_id_inventory'];
}
}
return null;
}
public function afterSave($insert, $changedAttributes){
if ( !$insert ){
// if ( $this->type == 'product'){

View File

@ -0,0 +1,73 @@
<?php
namespace console\controllers;
use common\models\Inventory;
use common\models\InventoryItem;
use common\models\Product;
use common\models\Transfer;
use yii\console\Controller;
use yii\db\Expression;
use yii\db\Query;
class InventoryConsoleController extends Controller{
/**
* @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.type', [Transfer::STATUS_NOT_PAID, Transfer::STATUS_PAID]]);
$query->andWhere(['product.id_product' => $prev->id_product]);
$query->andWhere([ '>','transfer.created_at',$start ]);
$products = $query->scalar();
$item->count_sold = $products;
$item->recalculateTotalPriceBrutto();
echo $item->price_brutto . " - ". $item->productName ."\n";
$item->save();
}
}
}