Add inventory changes ( tax, net purchase price
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use common\components\Helper;
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use common\components\Helper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "product".
|
||||
@@ -18,19 +18,22 @@ use common\components\Helper;
|
||||
* @property integer $purchase_price
|
||||
* @property integer $sale_price
|
||||
* @property integer $profit_margins
|
||||
* @property integer $tax
|
||||
* @property integer $purchase_net_price
|
||||
* @property integer $status
|
||||
* @property integer $stock
|
||||
* @property string $description
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
*/
|
||||
class Product extends \common\models\BaseFitnessActiveRecord {
|
||||
class Product extends \common\models\BaseFitnessActiveRecord
|
||||
{
|
||||
|
||||
const STATUS_DELETED = 0;
|
||||
const STATUS_ACTIVE = 10;
|
||||
|
||||
public static $BUNTETES = "buntetes3000";
|
||||
|
||||
const STATUS_DELETED = 0;
|
||||
const STATUS_ACTIVE = 10;
|
||||
|
||||
public static $BUNTETES = "buntetes3000";
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@@ -46,26 +49,29 @@ class Product extends \common\models\BaseFitnessActiveRecord {
|
||||
{
|
||||
return [
|
||||
[['id_product_category', 'id_account', 'name'], 'required'],
|
||||
[['id_inventory_group', 'id_product_category', 'id_account', 'purchase_price', 'sale_price', 'profit_margins', 'status'], 'integer'],
|
||||
[['id_inventory_group', 'id_product_category', 'id_account', 'purchase_price', 'sale_price', 'profit_margins', 'status', 'tax'], 'integer'],
|
||||
[['product_number', 'barcode'], 'string', 'max' => 20],
|
||||
[['product_number', 'barcode'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
|
||||
[['product_number', 'barcode'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
|
||||
[['name'], 'string', 'max' => 128],
|
||||
[['description'], 'string', 'max' => 255],
|
||||
// [['product_number'], 'unique' ],
|
||||
// [['barcode'], 'unique' ],
|
||||
// a1 and a2 need to be unique together, only a1 will receive error message
|
||||
// a1 and a2 need to be unique together, only a1 will receive error message
|
||||
// ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']]
|
||||
[['barcode','product_number'], 'filter', 'filter' => function($value){return Helper::fixAsciiChars($value);}],
|
||||
['barcode', 'unique', 'targetAttribute' => ['id_account', 'barcode']],
|
||||
['product_number', 'unique', 'targetAttribute' => ['id_account', 'product_number']],
|
||||
|
||||
[['barcode', 'product_number'], 'filter', 'filter' => function ($value) {
|
||||
return Helper::fixAsciiChars($value);
|
||||
}],
|
||||
['barcode', 'unique', 'targetAttribute' => ['id_account', 'barcode']],
|
||||
['product_number', 'unique', 'targetAttribute' => ['id_account', 'product_number']],
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
public function validateAscii($attribute,$params){
|
||||
if ( !$this->hasErrors($this->$attribute)){
|
||||
$this->attribute = Helper::fixAsciiChars($this->attributes);
|
||||
}
|
||||
|
||||
public function validateAscii($attribute, $params)
|
||||
{
|
||||
if (!$this->hasErrors($this->$attribute)) {
|
||||
$this->attribute = Helper::fixAsciiChars($this->attributes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,216 +90,262 @@ class Product extends \common\models\BaseFitnessActiveRecord {
|
||||
'profit_margins' => Yii::t('common/product', 'Profit Margins'),
|
||||
'status' => Yii::t('common/product', 'Status'),
|
||||
'name' => Yii::t('common/product', 'Name'),
|
||||
'tax' => Yii::t('common/product', 'Adó(%)'),
|
||||
'stock' => Yii::t('common/product', 'Raktáron'),
|
||||
'description' => Yii::t('common/product', 'Description'),
|
||||
'created_at' => Yii::t('common/product', 'Created At'),
|
||||
'updated_at' => Yii::t('common/product', 'Updated At'),
|
||||
'id_inventory_group' => Yii::t('common/product', 'Leltár csoport'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function getAccount() {
|
||||
return $this->hasOne ( Account::className (), [
|
||||
'id_account' => 'id_account'
|
||||
] );
|
||||
|
||||
|
||||
public function getAccount()
|
||||
{
|
||||
return $this->hasOne(Account::className(), [
|
||||
'id_account' => 'id_account'
|
||||
]);
|
||||
}
|
||||
public function getAccountName() {
|
||||
return $this->account->name;
|
||||
|
||||
public function getAccountName()
|
||||
{
|
||||
return $this->account->name;
|
||||
}
|
||||
|
||||
public function getInventoryGroup() {
|
||||
return $this->hasOne ( InventoryGroup::className (), [
|
||||
'id_inventory_group' => 'id_inventory_group'
|
||||
] );
|
||||
|
||||
public function getInventoryGroup()
|
||||
{
|
||||
return $this->hasOne(InventoryGroup::className(), [
|
||||
'id_inventory_group' => 'id_inventory_group'
|
||||
]);
|
||||
}
|
||||
public function getInventoryGroupName() {
|
||||
$result = "";
|
||||
$inventoryGroup = $this->inventoryGroup;
|
||||
if ( isset($inventoryGroup)){
|
||||
$result = $inventoryGroup->name;
|
||||
}
|
||||
return $result;
|
||||
|
||||
public function getInventoryGroupName()
|
||||
{
|
||||
$result = "";
|
||||
$inventoryGroup = $this->inventoryGroup;
|
||||
if (isset($inventoryGroup)) {
|
||||
$result = $inventoryGroup->name;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getProductCategory() {
|
||||
return $this->hasOne ( ProductCategory::className (), [
|
||||
'id_product_category' => 'id_product_category'
|
||||
] );
|
||||
|
||||
public function getProductCategory()
|
||||
{
|
||||
return $this->hasOne(ProductCategory::className(), [
|
||||
'id_product_category' => 'id_product_category'
|
||||
]);
|
||||
}
|
||||
public function getProductCategoryName() {
|
||||
return $this->productCategory->name;
|
||||
|
||||
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' )
|
||||
];
|
||||
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 ( Helper::isProductVisibilityAccount() && $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 ( Helper::isProductVisibilityAccount() && $account )
|
||||
$query->andWhere(["product.id_account" => $account]);
|
||||
|
||||
$result = $query->all();
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function findProduct($query, $account = null){
|
||||
$result = [];
|
||||
$product = null;
|
||||
|
||||
$query = Product::find()
|
||||
->andWhere(
|
||||
['or',
|
||||
['product_number' => $query ],
|
||||
['barcode' => $query ],
|
||||
]
|
||||
)->andWhere(['status' =>Product::STATUS_ACTIVE]);
|
||||
|
||||
if ( Helper::isProductVisibilityAccount() && $account ){
|
||||
$query->andFilterWhere(['product.id_account' => $account]);
|
||||
}
|
||||
|
||||
|
||||
$products = $query->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 modelToMapIdNameLong($product,$default = null){
|
||||
|
||||
if ( $product == null ){
|
||||
return $default;
|
||||
}
|
||||
|
||||
return ArrayHelper::toArray($product, [
|
||||
'common\models\Product' => [
|
||||
'id_product',
|
||||
'name' => function ($product) {
|
||||
return $product->name . " - " .$product->productCategoryName . " (" . $product->accountName . ")";
|
||||
},
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function sellProduct($product,$count){
|
||||
$product->stock = $product->stock - $count;
|
||||
}
|
||||
|
||||
|
||||
public static function readProductsNotPartOfInventoryGroup( $id_account = null ){
|
||||
$query = Product::find();
|
||||
$query->andWhere("id_inventory_group is null");
|
||||
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
return $query->all();
|
||||
}
|
||||
|
||||
public static function readInventoryGroups(){
|
||||
$query = InventoryGroup::find();
|
||||
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
return $query->all();
|
||||
}
|
||||
|
||||
public static function buildProductAndInventoryGroupList($id_account = null){
|
||||
$productOptions = [];
|
||||
|
||||
$products = Product::readProductsNotPartOfInventoryGroup($id_account);
|
||||
$inventoryGroups = Product::readInventoryGroups();
|
||||
|
||||
foreach ($products as $product ){
|
||||
$productOptions[]= [ 'type' =>'product', 'id' => $product->id_product ,'name' => $product->name . " (Termék/" .$product->productCategoryName ."/" . $product->accountName .")" ];
|
||||
}
|
||||
|
||||
foreach ($inventoryGroups as $inventoryGroup ){
|
||||
$productOptions[] = ['type' =>'group', 'id' =>$inventoryGroup->id_inventory_group ,'name' =>$inventoryGroup->name ."/Termékcsoport" ];
|
||||
|
||||
}
|
||||
return $productOptions;
|
||||
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 (Helper::isProductVisibilityAccount() && $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 (Helper::isProductVisibilityAccount() && $account)
|
||||
$query->andWhere(["product.id_account" => $account]);
|
||||
|
||||
$result = $query->all();
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function findProduct($query, $account = null)
|
||||
{
|
||||
$product = null;
|
||||
|
||||
$query = Product::find()
|
||||
->andWhere(
|
||||
['or',
|
||||
['product_number' => $query],
|
||||
['barcode' => $query],
|
||||
]
|
||||
)->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
|
||||
if (Helper::isProductVisibilityAccount() && $account) {
|
||||
$query->andFilterWhere(['product.id_account' => $account]);
|
||||
}
|
||||
|
||||
|
||||
$products = $query->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 modelToMapIdNameLong($product, $default = null)
|
||||
{
|
||||
|
||||
if ($product == null) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return ArrayHelper::toArray($product, [
|
||||
'common\models\Product' => [
|
||||
'id_product',
|
||||
'name' => function ($product) {
|
||||
return $product->name . " - " . $product->productCategoryName . " (" . $product->accountName . ")";
|
||||
},
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function sellProduct($product, $count)
|
||||
{
|
||||
$product->stock = $product->stock - $count;
|
||||
}
|
||||
|
||||
|
||||
public static function readProductsNotPartOfInventoryGroup($id_account = null)
|
||||
{
|
||||
$query = Product::find();
|
||||
$query->andWhere("id_inventory_group is null");
|
||||
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
return $query->all();
|
||||
}
|
||||
|
||||
public static function readInventoryGroups()
|
||||
{
|
||||
$query = InventoryGroup::find();
|
||||
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
|
||||
return $query->all();
|
||||
}
|
||||
|
||||
public static function buildProductAndInventoryGroupList($id_account = null)
|
||||
{
|
||||
$productOptions = [];
|
||||
|
||||
$products = Product::readProductsNotPartOfInventoryGroup($id_account);
|
||||
$inventoryGroups = Product::readInventoryGroups();
|
||||
|
||||
foreach ($products as $product) {
|
||||
$productOptions[] = ['type' => 'product', 'id' => $product->id_product, 'name' => $product->name . " (Termék/" . $product->productCategoryName . "/" . $product->accountName . ")"];
|
||||
}
|
||||
|
||||
foreach ($inventoryGroups as $inventoryGroup) {
|
||||
$productOptions[] = ['type' => 'group', 'id' => $inventoryGroup->id_inventory_group, 'name' => $inventoryGroup->name . "/Termékcsoport"];
|
||||
|
||||
}
|
||||
return $productOptions;
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
$result = parent::beforeSave($insert);
|
||||
if ($result) {
|
||||
|
||||
//automatically set net price
|
||||
//net price = purchase_price -(purchase_price * tax /100)
|
||||
$purchase_price_brutto = 0;
|
||||
$tax = 0;
|
||||
if (isset($this->purchase_price)) {
|
||||
$purchase_price_brutto = $this->purchase_price;
|
||||
}
|
||||
|
||||
if (isset($this->tax)) {
|
||||
$tax = $this->tax;
|
||||
}
|
||||
|
||||
$this->purchase_net_price = $purchase_price_brutto - ($purchase_price_brutto * $tax / 100);
|
||||
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user