add product

This commit is contained in:
rocho 2015-09-24 22:14:09 +02:00
parent fcc18d79f6
commit a2b835225a
15 changed files with 229 additions and 80 deletions

View File

@ -9,6 +9,7 @@ use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Account;
use common\models\ProductCategory;
/**
* ProductController implements the CRUD actions for Product model.
@ -62,8 +63,10 @@ class ProductController extends Controller
public function actionCreate()
{
$model = new Product();
$model->stock = 0;
$model->status = Product::STATUS_ACTIVE;
$accounts = Account::readAccounts(null);
$categories = ProductCategory::read(null);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_product]);
@ -71,6 +74,8 @@ class ProductController extends Controller
return $this->render('create', [
'model' => $model,
'accounts' => $accounts,
'categories' => $categories ,
]);
}
}
@ -85,11 +90,16 @@ class ProductController extends Controller
{
$model = $this->findModel($id);
$accounts = Account::readAccounts($model->id_account);
$categories = ProductCategory::read($model->id_product_category);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_product]);
} else {
return $this->render('update', [
'model' => $model,
'accounts' => $accounts,
'categories' => $categories ,
]);
}
}

View File

@ -18,8 +18,8 @@ class ProductSearch extends Product
public function rules()
{
return [
[['id_product', 'id_product_type', 'id_account', 'purchase_price', 'sale_price', 'profit_margins', 'status'], 'integer'],
[['product_number', 'barcode', 'description', 'created_at', 'updated_at'], 'safe'],
[[ 'id_product_category', 'id_account', 'status'], 'integer'],
[['product_number', 'barcode' ], 'safe'],
];
}
@ -56,20 +56,13 @@ class ProductSearch extends Product
}
$query->andFilterWhere([
'id_product' => $this->id_product,
'id_product_type' => $this->id_product_type,
'id_product_category' => $this->id_product_category,
'id_account' => $this->id_account,
'purchase_price' => $this->purchase_price,
'sale_price' => $this->sale_price,
'profit_margins' => $this->profit_margins,
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'product_number', $this->product_number])
->andFilterWhere(['like', 'barcode', $this->barcode])
->andFilterWhere(['like', 'description', $this->description]);
->andFilterWhere(['like', 'barcode', $this->barcode]);
return $dataProvider;
}

View File

@ -11,14 +11,19 @@ use yii\helpers\ArrayHelper;
?>
<?php
$account_options = ArrayHelper::map($accounts, 'id_account', 'name');
$product_category_options = ArrayHelper::map($categories, 'id_product_category', 'name');
?>
<div class="product-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'id_account')->dropDownList($account_options) ?>
<?= $form->field($model, 'id_product_category')->dropDownList($product_category_options) ?>
<?= $form->field($model, 'product_number')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'barcode')->textInput(['maxlength' => true]) ?>
@ -31,10 +36,10 @@ $account_options = ArrayHelper::map($accounts, 'id_account', 'name');
<?= $form->field($model, 'status')->checkbox( ['value' => 10, 'label' => Yii::t('common/product', "Active") ]) ?>
<?= $form->field($model, 'description')->textarea(['maxlength' => true])->hint( Yii::t( 'common/prodcut', "Max 255 character")) ?>
<?= $form->field($model, 'description')->textarea(['maxlength' => true])->hint( Yii::t( 'common/product', "Max 255 character")) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('common/ticket_type', 'Create') : Yii::t('common/ticket_type', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?= Html::submitButton($model->isNewRecord ? Yii::t('common/product', 'Create') : Yii::t('common/product', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>

View File

@ -2,12 +2,30 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\Product;
use common\models\ProductCategory;
use yii\helpers\ArrayHelper;
use common\models\Account;
/* @var $this yii\web\View */
/* @var $model backend\models\ProductSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
function mkOptions($options){
$o = $options;
$o[''] = Yii::t('common/product','All' ) ;
return $o;
}
$statusOptions = mkOptions( Product::statuses() );
$productCategories = mkOptions( ArrayHelper::map( ProductCategory::read(null) ,'id_product_category','name') );
$accounts = mkOptions( ArrayHelper::map( Account::readAccounts(null) ,'id_account','name'));
?>
<div class="product-search">
<?php $form = ActiveForm::begin([
@ -15,33 +33,29 @@ use yii\widgets\ActiveForm;
'method' => 'get',
]); ?>
<?= $form->field($model, 'id_product') ?>
<?= $form->field($model, 'id_product_type') ?>
<?= $form->field($model, 'id_account') ?>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'id_product_category')->dropDownList($productCategories) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'id_account')->dropDownList($accounts) ?>
</div>
<div class="col-md-4">
<?php echo $form->field($model, 'status')->dropDownList($statusOptions) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'product_number') ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'barcode') ?>
</div>
<div class="col-md-4">
</div>
</div>
<?php // echo $form->field($model, 'purchase_price') ?>
<?php // echo $form->field($model, 'sale_price') ?>
<?php // echo $form->field($model, 'profit_margins') ?>
<?php // echo $form->field($model, 'status') ?>
<?php // echo $form->field($model, 'description') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('common/ticket_type', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('common/ticket_type', 'Reset'), ['class' => 'btn btn-default']) ?>
<?= Html::submitButton(Yii::t('common/product', 'Search'), ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>

View File

@ -7,8 +7,8 @@ use yii\helpers\Html;
/* @var $model common\models\Product */
/* @var $accounts common\models\Account[] */
$this->title = Yii::t('common/ticket_type', 'Create Product');
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Products'), 'url' => ['index']];
$this->title = Yii::t('common/product', 'Create Product');
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/product', 'Products'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-create">
@ -18,6 +18,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?= $this->render('_form', [
'model' => $model,
'accounts' => $accounts,
'categories' => $categories ,
]) ?>
</div>

View File

@ -7,23 +7,26 @@ use yii\grid\GridView;
/* @var $searchModel backend\models\ProductSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('common/ticket_type', 'Products');
$this->title = Yii::t('common/product', 'Products');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('common/ticket_type', 'Create Product'), ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('common/product', 'Create Product'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'attribute' => 'id_product_type',
'attribute' => 'name',
],
[
'attribute' => 'id_product_category',
'value' => 'productCategoryName',
],
[
@ -32,15 +35,16 @@ $this->params['breadcrumbs'][] = $this->title;
],
'product_number',
'barcode',
'purchase_price',
'sale_price',
'profit_margins',
'status',
'description',
'created_at:datetime',
'updated_at:datetime',
[
'attribute' => 'status',
'value' => 'statusHuman',
['class' => 'yii\grid\ActionColumn'],
],
['class' => 'yii\grid\ActionColumn',
'template' => '{view} {update}'
],
],
]); ?>

View File

@ -5,12 +5,10 @@ use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Product */
$this->title = Yii::t('common/ticket_type', 'Update {modelClass}: ', [
'modelClass' => 'Product',
]) . ' ' . $model->id_product;
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Products'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id_product, 'url' => ['view', 'id' => $model->id_product]];
$this->params['breadcrumbs'][] = Yii::t('common/ticket_type', 'Update');
$this->title = Yii::t('common/product', 'Update product:' ) . ' ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/product', 'Products'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id_product]];
$this->params['breadcrumbs'][] = Yii::t('common/product', 'Update');
?>
<div class="product-update">
@ -18,6 +16,8 @@ $this->params['breadcrumbs'][] = Yii::t('common/ticket_type', 'Update');
<?= $this->render('_form', [
'model' => $model,
'accounts' => $accounts,
'categories' => $categories ,
]) ?>
</div>

View File

@ -6,8 +6,8 @@ use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\models\Product */
$this->title = $model->id_product;
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/ticket_type', 'Products'), 'url' => ['index']];
$this->title = $model->name ;
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/product', 'Products'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="product-view">
@ -15,31 +15,27 @@ $this->params['breadcrumbs'][] = $this->title;
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('common/ticket_type', 'Update'), ['update', 'id' => $model->id_product], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('common/ticket_type', 'Delete'), ['delete', 'id' => $model->id_product], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('common/ticket_type', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
<?= Html::a(Yii::t('common/product', 'Update'), ['update', 'id' => $model->id_product], ['class' => 'btn btn-primary']) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id_product',
'id_product_type',
'id_account',
'productCategoryName',
'accountName',
'product_number',
'barcode',
'purchase_price',
'sale_price',
'profit_margins',
'status',
'description',
'created_at',
'updated_at',
'statusHuman',
[
'attribute' => 'description',
'value' => nl2br($model->description),
'format' => 'raw'
],
'created_at:datetime',
'updated_at:datetime',
],
]) ?>

View File

@ -0,0 +1,43 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'Active' => 'Aktív',
'All' => 'Mind',
'Barcode' => 'Vonalkód',
'Create' => 'Mentés',
'Create Product' => 'Új termék',
'Created At' => 'Létrehozás ideje',
'Description' => 'Leírás',
'Id Account' => 'Kassza',
'Id Product' => 'Termék',
'Id Product Category' => 'Termék kategória',
'Inactive' => 'Inaktív',
'Max 255 character' => 'Max 255 karakter',
'Name' => 'Név',
'Product Number' => 'Termék szám',
'Products' => 'Termékek',
'Profit Margins' => 'Haszonkulcs',
'Purchase Price' => 'Beszerzési ár',
'Sale Price' => 'Eladási ár',
'Search' => 'Keresés',
'Status' => 'Státusz',
'Update' => 'Módosítás',
'Update product:' => 'Termék módosítása:',
'Updated At' => 'Módosítás ideje',
];

View File

@ -8,7 +8,8 @@ use Yii;
* This is the model class for table "product".
*
* @property integer $id_product
* @property integer $id_product_type
* @property string $name length 64
* @property integer $id_product_category
* @property integer $id_account
* @property string $product_number
* @property string $barcode
@ -16,6 +17,7 @@ use Yii;
* @property integer $sale_price
* @property integer $profit_margins
* @property integer $status
* @property integer $stock
* @property string $description
* @property string $created_at
* @property string $updated_at
@ -39,10 +41,13 @@ class Product extends \common\models\BaseFitnessActiveRecord {
public function rules()
{
return [
[['id_product_type', 'id_account'], 'required'],
[['id_product_type', 'id_account', 'purchase_price', 'sale_price', 'profit_margins', 'status'], 'integer'],
[['id_product_category', 'id_account', 'name'], 'required'],
[['id_product_category', 'id_account', 'purchase_price', 'sale_price', 'profit_margins', 'status'], 'integer'],
[['product_number', 'barcode'], 'string', 'max' => 20],
[['description'], 'string', 'max' => 255]
[['name'], 'string', 'max' => 128],
[['description'], 'string', 'max' => 255],
[['product_number'], 'unique' ],
[['barcode'], 'unique' ],
];
}
@ -53,7 +58,7 @@ class Product extends \common\models\BaseFitnessActiveRecord {
{
return [
'id_product' => Yii::t('common/product', 'Id Product'),
'id_product_type' => Yii::t('common/product', 'Id Product Type'),
'id_product_category' => Yii::t('common/product', 'Id Product Category'),
'id_account' => Yii::t('common/product', 'Id Account'),
'product_number' => Yii::t('common/product', 'Product Number'),
'barcode' => Yii::t('common/product', 'Barcode'),
@ -61,6 +66,7 @@ class Product extends \common\models\BaseFitnessActiveRecord {
'sale_price' => Yii::t('common/product', 'Sale Price'),
'profit_margins' => Yii::t('common/product', 'Profit Margins'),
'status' => Yii::t('common/product', 'Status'),
'name' => Yii::t('common/product', 'Name'),
'description' => Yii::t('common/product', 'Description'),
'created_at' => Yii::t('common/product', 'Created At'),
'updated_at' => Yii::t('common/product', 'Updated At'),

View File

@ -69,4 +69,20 @@ class ProductCategory extends \common\models\BaseFitnessActiveRecord
return $result;
}
/**
* $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive
* */
public static function read($forceIncludeObjectWithId = null){
$categories = null;
if ( $forceIncludeObjectWithId == null){
$categories = ProductCategory::find()->andWhere(['status' => ProductCategory::STATUS_ACTIVE])->all();
}else{
$categories = ProductCategory::find()->andWhere( ['or', ['status' => ProductCategory::STATUS_ACTIVE], ['id_product_category' => $forceIncludeObjectWithId ] ])->all();
}
return $categories;
}
}

View File

@ -17,8 +17,7 @@
"php": ">=5.4.0",
"yiisoft/yii2": ">=2.0.6",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "*",
"npm-asset/grunt": "^0.4.5"
"yiisoft/yii2-swiftmailer": "*"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "5fe15fcdcc6c43b39968ffd29104ac62",
"hash": "8580bd82955b1fbb80d47024e184056e",
"packages": [
{
"name": "bower-asset/bootstrap",

View File

@ -0,0 +1,32 @@
<?php
use yii\db\Schema;
use yii\db\Migration;
use common\models\Product;
class m150924_162425_alter__table__product_fix_columns extends Migration
{
public function up()
{
$this->renameColumn('product','id_product_type','id_product_category');
$this->addColumn('product','stock', 'int(11)' );
}
public function down()
{
echo "m150924_162425_alter__table__product_fix_columns cannot be reverted.\n";
return false;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}

View File

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