217 lines
6.2 KiB
PHP
217 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace common\models;
|
|
|
|
use Yii;
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
/**
|
|
* This is the model class for table "product".
|
|
*
|
|
* @property integer $id_product
|
|
* @property string $name length 64
|
|
* @property integer $id_product_category
|
|
* @property integer $id_account
|
|
* @property string $product_number
|
|
* @property string $barcode
|
|
* @property integer $purchase_price
|
|
* @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
|
|
*/
|
|
class Product extends \common\models\BaseFitnessActiveRecord {
|
|
|
|
const STATUS_DELETED = 0;
|
|
const STATUS_ACTIVE = 10;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
return 'product';
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['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],
|
|
[['name'], 'string', 'max' => 128],
|
|
[['description'], 'string', 'max' => 255],
|
|
[['product_number'], 'unique' ],
|
|
[['barcode'], 'unique' ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'id_product' => Yii::t('common/product', 'Id Product'),
|
|
'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'),
|
|
'purchase_price' => Yii::t('common/product', 'Purchase Price'),
|
|
'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'),
|
|
];
|
|
}
|
|
|
|
|
|
public function getAccount() {
|
|
return $this->hasOne ( Account::className (), [
|
|
'id_account' => 'id_account'
|
|
] );
|
|
}
|
|
public function getAccountName() {
|
|
return $this->account->name;
|
|
}
|
|
|
|
public function getProductCategory() {
|
|
return $this->hasOne ( ProductCategory::className (), [
|
|
'id_product_category' => 'id_product_category'
|
|
] );
|
|
}
|
|
public function getProductCategoryName() {
|
|
return $this->productCategory->name;
|
|
}
|
|
|
|
/**
|
|
* @formatter:on
|
|
*/
|
|
static function statuses() {
|
|
return [
|
|
self::STATUS_ACTIVE => Yii::t ( 'common/product', 'Active' ),
|
|
self::STATUS_DELETED => Yii::t ( 'common/product', 'Inactive' )
|
|
];
|
|
}
|
|
public function getStatusHuman() {
|
|
$result = null;
|
|
$s = self::statuses ( );
|
|
if (array_key_exists ( $this->status, $s )) {
|
|
$result = $s [$this->status];
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* $param int $forceIncludeAccount id warehouse, that should be included in list, even if it is inactive
|
|
* */
|
|
public static function read($forceIncludeObjectWithId = null){
|
|
$warehouses = null;
|
|
|
|
if ( $forceIncludeObjectWithId == null){
|
|
$warehouses = Product::find()->andWhere(['status' => Product::STATUS_ACTIVE])->orderBy(['product.name' => SORT_ASC])->all();
|
|
}else{
|
|
$warehouses = Product::find()->andWhere( ['or', ['status' => Product::STATUS_ACTIVE], ['id_product' => $forceIncludeObjectWithId ] ])->orderBy(['product.name' => SORT_ASC])->all();
|
|
}
|
|
|
|
return $warehouses;
|
|
}
|
|
|
|
/**
|
|
* $param int $forceIncludeAccount id warehouse, that should be included in list, even if it is inactive
|
|
* */
|
|
public static function readForDefaultAccount($forceIncludeObjectWithId = null){
|
|
$result = null;
|
|
|
|
$account = Account::readDefault();
|
|
|
|
if ( $forceIncludeObjectWithId == null){
|
|
$query = Product::find()->andWhere(['status' => Product::STATUS_ACTIVE])->orderBy(['product.name' => SORT_ASC]);
|
|
if ( $account )
|
|
$query->andWhere(["product.id_account" => $account]);
|
|
|
|
$result = $query->all();
|
|
}else{
|
|
$query = Product::find()->andWhere( ['or', ['status' => Product::STATUS_ACTIVE], ['id_product' => $forceIncludeObjectWithId ] ])->orderBy(['product.name' => SORT_ASC]);
|
|
if ( $account )
|
|
$query->andWhere(["product.id_account" => $account]);
|
|
|
|
$result = $query->all();
|
|
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function findProduct($query, $account = null){
|
|
$result = [];
|
|
$product = null;
|
|
|
|
$products = Product::find()
|
|
->andWhere(
|
|
['or',
|
|
['product_number' => $query ],
|
|
['barcode' => $query ],
|
|
]
|
|
)->andWhere(['status' =>Product::STATUS_ACTIVE])
|
|
->andFilterWhere(['product.id_account' => $account])
|
|
->all();
|
|
|
|
|
|
if ( count($products) == 1 ){
|
|
$product = $products[0];
|
|
}
|
|
return $product;
|
|
}
|
|
|
|
public static function modelToArray($product,$default = null){
|
|
|
|
if ( $product == null ){
|
|
return $default;
|
|
}
|
|
|
|
return ArrayHelper::toArray($product, [
|
|
'common\models\Product' => [
|
|
'id_product',
|
|
'name',
|
|
'product_number',
|
|
'barcode',
|
|
'sale_price',
|
|
'stock',
|
|
'id_account',
|
|
'category' => function ($product) {
|
|
return $product->productCategoryName;
|
|
},
|
|
],
|
|
]);
|
|
}
|
|
|
|
public static function modelToMapIdName($product,$default = null){
|
|
|
|
if ( $product == null ){
|
|
return $default;
|
|
}
|
|
|
|
return ArrayHelper::toArray($product, [
|
|
'common\models\Product' => [
|
|
'id_product',
|
|
'name',
|
|
],
|
|
]);
|
|
}
|
|
|
|
public static function sellProduct($product,$count){
|
|
$product->stock = $product->stock - $count;
|
|
}
|
|
|
|
}
|