-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warehouses migration, Storing counts per warehouse, backend/product/e…
…dit interface - #174
- Loading branch information
Showing
14 changed files
with
1,125 additions
and
20 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
application/backend/controllers/WarehouseController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.