Skip to content

Commit

Permalink
Warehouses migration, Storing counts per warehouse, backend/product/e…
Browse files Browse the repository at this point in the history
…dit interface - #174
  • Loading branch information
bethrezen committed Feb 28, 2015
1 parent 5d0d837 commit 09209f8
Show file tree
Hide file tree
Showing 14 changed files with 1,125 additions and 20 deletions.
131 changes: 131 additions & 0 deletions application/backend/controllers/WarehouseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace app\backend\controllers;

use devgroup\TagDependencyHelper\ActiveRecordHelper;
use app\backend\actions\DeleteOne;
use app\backend\actions\MultipleDelete;
use app\backend\actions\UpdateEditable;
use app\models\Warehouse;
use app\models\WarehouseProduct;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\caching\TagDependency;

class WarehouseController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['product manage'],
],
],
],
];
}

/**
* @inheritdoc
*/
public function actions()
{
return [
'remove-all' => [
'class' => MultipleDelete::className(),
'modelName' => Warehouse::className(),
],
'delete' => [
'class' => DeleteOne::className(),
'modelName' => Warehouse::className(),
],
'update-editable' => [
'class' => UpdateEditable::className(),
'modelName' => Warehouse::className(),
'allowedAttributes' => [
],
],
];
}

public function actionIndex()
{
$searchModel = new Warehouse();
$dataProvider = $searchModel->search($_GET);

return $this->render(
'index',
[
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]
);
}

public function actionEdit($id = null)
{
$model = new Warehouse;
$model->loadDefaultValues();

if ($id !== null) {
$model = Warehouse::findOne($id);
}



$post = \Yii::$app->request->post();

if ($model->load($post) && $model->validate()) {

$save_result = $model->save();
if ($save_result) {
Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved'));
return $this->redirect(['/backend/warehouse/edit', 'id' => $model->id]);
} else {
\Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data'));
}


}

return $this->render(
'form',
[
'model' => $model,
]
);
}

public function actionUpdateRemains()
{
$post = Yii::$app->request->post('remain', null);
if (isset($post)) {

Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$remainId = current(array_keys($post));
$model = WarehouseProduct::findOne($remainId);
if ($model === null) {
throw new NotFoundHttpException;
}

$model->setAttributes(current($post));
TagDependency::invalidate(Yii::$app->cache, ActiveRecordHelper::getObjectTag(\app\models\Product::className(), $model->product_id));
return $model->save();


} else {
throw new HttpException(400);
}
}

}
30 changes: 16 additions & 14 deletions application/backend/views/product/product-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@
BackendWidget::end();
?>

<?php
BackendWidget::begin(
[
'title'=> Yii::t('app', 'Warehouse'),
'icon'=>'archive',
'footer'=>$this->blocks['submit']
]
); ?>

<?= $form->field($model, 'sku') ?>
<?= $form->field($model, 'unlimited_count')->widget(\kartik\switchinput\SwitchInput::className())?>
<?= \app\backend\widgets\WarehousesRemains::widget([
'model' => $model,
]) ?>
<?php BackendWidget::end(); ?>

<?php
BackendWidget::begin(
[
Expand Down Expand Up @@ -292,21 +308,7 @@

<?php BackendWidget::end(); ?>

<?php
BackendWidget::begin(
[
'title'=> Yii::t('app', 'Warehouse'),
'icon'=>'archive',
'footer'=>$this->blocks['submit']
]
); ?>

<?= $form->field($model, 'sku') ?>
<?= $form->field($model, 'in_warehouse')?>
<?= $form->field($model, 'unlimited_count')->widget(\kartik\switchinput\SwitchInput::className())?>
<?= $form->field($model, 'reserved_count')?>

<?php BackendWidget::end(); ?>

<?php if ($model->parent_id == 0) : ?>

Expand Down
67 changes: 67 additions & 0 deletions application/backend/widgets/WarehousesRemains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace app\backend\widgets;

use devgroup\TagDependencyHelper\ActiveRecordHelper;
use Yii;
use app;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\caching\TagDependency;


/**
* Widget WarehousesRemains renders input block for specifying product remains on each of active warehouse
* @package app\backend\widgets
*/
class WarehousesRemains extends Widget
{
/**
* @var app\models\Product Product model
*/
public $model = null;

/**
* @inheritdoc
*/
public function run()
{
if ($this->model === null) {
throw new InvalidConfigException("Model should be set for WarehousesRemains widget");
}

$state = $this->model->getWarehousesState();

$activeWarehousesIds = app\models\Warehouse::activeWarehousesIds();
$remains = [];
foreach ($state as $remain) {
$remains[$remain->warehouse_id] = $remain;
if(($key = array_search($remain->warehouse_id, $activeWarehousesIds)) !== false) {
unset($activeWarehousesIds[$key]);
}
}

// if we have new warehouses that not represented in warehouses state
if (count($activeWarehousesIds) > 0) {
foreach ($activeWarehousesIds as $id) {
// create new record with default values
$remain = new app\models\WarehouseProduct;
$remain->warehouse_id = $id;
$remain->product_id = $this->model->id;
$remain->save();

// add to remains
$remains[$remain->warehouse_id] = $remain;
}
TagDependency::invalidate(Yii::$app->cache, ActiveRecordHelper::getObjectTag($this->model->className(), $this->model->id));
}

return $this->render(
'warehouses-remains',
[
'model' => $this->model,
'remains' => $remains,
]
);
}
}
93 changes: 93 additions & 0 deletions application/backend/widgets/views/warehouses-remains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use kartik\helpers\Html;
use yii\helpers\Url;
/** @var $remains app\models\WarehouseProduct[] */
/** @var $model app\models\Product */

?>

<table class="table table-condensed table-striped table-hover">
<thead>
<tr>
<th>
<?= Yii::t('app', 'Warehouse') ?>
</th>
<th>
<?= Yii::t('app', 'In warehouse') ?>
</th>
<th>
<?= Yii::t('app', 'Reserved count') ?>
</th>
<th>
<?= Yii::t('app', 'SKU') ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($remains as $warehouse_id => $remain): ?>
<tr>
<td>
<?= $remain->warehouse->name ?>
</td>
<td>
<?=
Html::textInput(
'remain[' . $remain->id . '][in_warehouse]',
$remain->in_warehouse,
[
'class' => 'warehouse-remain-input form-control',
'placeholder' => Yii::t('app', 'In warehouse'),
]
) ?>
</td>
<td>
<?=
Html::textInput(
'remain[' . $remain->id . '][reserved_count]',
$remain->reserved_count,
[
'class' => 'warehouse-remain-input form-control',
'placeholder' => Yii::t('app', 'Reserved count'),
]
) ?>
</td>
<td>
<?=
Html::textInput(
'remain[' . $remain->id . '][sku]',
$remain->sku,
[
'class' => 'warehouse-remain-input form-control',
'placeholder' => Yii::t('app', 'SKU'),
]
) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<script>
$(function(){
$('.warehouse-remain-input').change(function(){
var formData = {},
$this = $(this);

formData[$this.attr('name')] = $this.val();

$.ajax({
url: "<?= Url::toRoute(['/backend/warehouse/update-remains']) ?>",
data: formData,
method: 'POST',
success: function(data, textStatus, jqXHR) {
$this.parent().addClass('has-success');
},
error: function(jqXHR, textStatus, errorThrown) {
$this.parent().addClass('has-error');
}
});
return true;
})
})
</script>
2 changes: 2 additions & 0 deletions application/messages/ru/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@
'Is active' => 'Активен',
'Item <strong>{value}</strong> is not assigned:' => '',
'Items in warehouse' => 'На складе',
'In warehouse' => 'На складе',
'Reserved count' => 'Зарезерировано',
'Items inside item: ' => 'Элементы внутри элемента: ',
'Items reserved' => 'Зарезервировано',
'ISO-4217 code' => 'ISO-4217 код',
Expand Down
Loading

0 comments on commit 09209f8

Please sign in to comment.