diff --git a/backend/controllers/InventoryItemController.php b/backend/controllers/InventoryItemController.php index c50cbee..aa92a72 100644 --- a/backend/controllers/InventoryItemController.php +++ b/backend/controllers/InventoryItemController.php @@ -81,7 +81,11 @@ class InventoryItemController extends Controller ['item_count_sold', 'Eladott mennyiség (db)'], ['item_count_in', 'Beszerzett mennyiség (db)'], ['item_count', 'Leltározott mennyiség (db)'], - ['item_difference', 'Különbség (db)'], + ['item_purchase_price_net', 'Nettó beszerezési ár (Ft)'], + ['item_net_stock_money', 'Nettó készlet érték (Ft)'], + ['item_stock_missing_count', 'Különbség (db)'], + ['item_price_brutto', 'Bruttó eladási ár (Ft)'], + ['item_stock_missing_money', 'Leltár hiány (Ft)'], ['item_count_system', 'Rendszer szerinti mennyiség (db)'], ]; $cols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']; @@ -121,11 +125,13 @@ class InventoryItemController extends Controller // 'name' => 'Verdana' - foreach (range('A', 'I') as $columnID) { + foreach (range('A', 'N') as $columnID) { $sheet->getColumnDimension($columnID)->setAutoSize(true); $sheet->getStyle($columnID . '1')->applyFromArray($styleArray); } + $sheet->freezePane( "N2" ); + // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="leltar.xls"'); @@ -191,32 +197,18 @@ class InventoryItemController extends Controller { $model = $this->findModel($id); $inventory = Inventory::findOne($model->id_inventory); - if (!$inventory->isOpen()) { throw new NotAcceptableHttpException("A leltár elem nem található"); } - if ($model->load(Yii::$app->request->post()) && $model->validate()) { - - $model->recalculateTotalPriceBrutto(); - $model->save(false); - \Yii::$app->session->setFlash('success', "Mennyiség elmentve: " . $model->productName . ' - ' .$model->count . ' db'); - -// $prev = Url::previous("inventory-item-index"); - - - - $next = InventoryItem::findNextItemAlphabetical($model->id_inventory_item); if ( isset($_POST['_next']) && isset($next)){ return $this->redirect(['inventory-item/update', 'id' => $next]); }else{ return $this->redirect(['index', 'id' => $inventory->id_inventory]); } - - } else { return $this->render('update', [ 'model' => $model, @@ -226,11 +218,11 @@ class InventoryItemController extends Controller } /** + * @param null $idInventory the id of the inventory * @param integer $id the inventory item id * @return string * @throws Exception * @throws NotAcceptableHttpException - * @throws NotFoundHttpException */ public function actionUpdateItem($idInventory = null,$id = null) { @@ -269,7 +261,6 @@ class InventoryItemController extends Controller if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $model->recalculateTotalPriceBrutto(); if ( $model->save(false) ){ \Yii::$app->session->setFlash('success', "Mennyiség elmentve: " . $model->productName . ' - ' .$model->count . ' db'); diff --git a/backend/models/InventoryItemForm.php b/backend/models/InventoryItemForm.php index e0a9c25..ab34f93 100644 --- a/backend/models/InventoryItemForm.php +++ b/backend/models/InventoryItemForm.php @@ -123,16 +123,14 @@ class InventoryItemForm extends Model{ $item->price_brutto = $this->product->sale_price; $item->type = InventoryItem::$TYPE_PRODUCT; $item->id_product = $this->product->id_product; + $item->tax = $this->product->tax; + $item->purchase_price_net = $this->product->purchase_net_price; }else{ $item->id_inventory_group = $this->inventoryGroup->id_inventory_group; $item->type = InventoryItem::$TYPE_INVENTORY_GROUP; } $item->id_inventory = $this->inventory->id_inventory; - - - $item->recalculateTotalPriceBrutto(); - - + if ( !$item->save(false) ){ throw new \Exception("Nem sikerült a leltár végrehajtása"); } diff --git a/backend/models/InventoryItemSearch.php b/backend/models/InventoryItemSearch.php index f9f52f5..54bc7d9 100644 --- a/backend/models/InventoryItemSearch.php +++ b/backend/models/InventoryItemSearch.php @@ -2,7 +2,6 @@ namespace backend\models; -use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\InventoryItem; @@ -72,8 +71,10 @@ class InventoryItemSearch extends InventoryItem 'inventory_prev.id_inventory as inventory_prev_id_inventory', 'inventory_prev.name as inventory_prev_name', 'inventory_item.price_brutto as item_price_brutto', - 'inventory_item.total_price_brutto as item_total_price_brutto', - new Expression('(coalesce(inventory_item.count,0) - ( coalesce(inventory_item.count_prev,0) + coalesce(inventory_item.count_in,0) - coalesce(inventory_item.count_sold,0) )) as item_difference'), + 'inventory_item.purchase_price_net as item_purchase_price_net', + 'inventory_item.net_stock_money as item_net_stock_money', + 'inventory_item.stock_missing_count as item_stock_missing_count', + 'inventory_item.stock_missing_money as item_stock_missing_money' ]); $query->from(InventoryItem::tableName()); @@ -91,11 +92,7 @@ class InventoryItemSearch extends InventoryItem $this->load($params); - if (!$this->validate()) { - // uncomment the following line if you do not want to return any records when validation fails - // $query->where('0=1'); - return $dataProvider; - } + $dataProvider = new ActiveDataProvider( ['query' => $query , @@ -117,12 +114,20 @@ class InventoryItemSearch extends InventoryItem ['inventory.name', 'inventory_name'], ['inventory_prev_name', 'inventory_prev_name'], ['item_price_brutto', 'item_price_brutto'], - ['item_total_price_brutto', 'item_total_price_brutto'], + ['item_purchase_price_net', 'item_purchase_price_net'], + ['item_stock_missing_count', 'item_stock_missing_count'], + ['item_stock_missing_money', 'item_stock_missing_money'], ]) , ] ] ); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + $query->where('0=1'); + return $dataProvider; + } if ( !empty($this->item_type) && is_numeric($this->item_id) ){ diff --git a/backend/views/inventory-item/index.php b/backend/views/inventory-item/index.php index 6139d9a..52378c2 100644 --- a/backend/views/inventory-item/index.php +++ b/backend/views/inventory-item/index.php @@ -115,18 +115,24 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );'); [ 'attribute' => 'inventory_prev_name', 'label' => 'Utolsó leltár', - 'contentOptions' => ['class' => 'col-b'] + 'contentOptions' => ['class' => ''] ], [ 'attribute' => 'item_count_system', 'label' => 'Rendszer szerinti mennyiség (db)', 'contentOptions' => ['class' => 'numeric'] + ], + [ + 'attribute' => 'item_purchase_price_net', + 'label' => 'Nettó beszerezési ár', + 'contentOptions' => ['class' => 'numeric'] + ], [ 'attribute' => 'item_count', 'label' => 'Leltározott mennyiség (db)', - 'contentOptions' => ['class' => 'numeric col-b'] + 'contentOptions' => ['class' => 'numeric '] ], [ @@ -138,7 +144,7 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );'); [ 'attribute' => 'item_count_in', 'label' => 'Beszerzett mennyiség (db)', - 'contentOptions' => ['class' => 'numeric col-b' ] + 'contentOptions' => ['class' => 'numeric ' ] ], [ @@ -148,9 +154,9 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );'); ], [ - 'attribute' => 'item_difference', + 'attribute' => 'item_stock_missing_count', 'label' => 'Különbség (db)', - 'contentOptions' => ['class' => 'numeric col-b'] + 'contentOptions' => ['class' => 'numeric '] ], [ @@ -159,13 +165,12 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );'); 'contentOptions' => ['class' => 'numeric'] ], [ - 'attribute' => 'item_total_price_brutto', - 'label' => 'Összeg (Ft)', - 'contentOptions' => ['class' => 'numeric col-b'] - ], - - + 'attribute' => 'item_stock_missing_money', + 'label' => 'Leltár hiány (Ft)', + 'contentOptions' => ['class' => 'numeric '] + ] ]; + if ($model->isOpen()) { $columns[] = ['class' => 'yii\grid\ActionColumn', 'template' => '{update}', @@ -187,6 +192,28 @@ $this->registerJs('inventoryItemIndex.init( ' . json_encode($options) . ' );'); ]; } + $i = 0; + foreach ($columns as $col){ + $value = ""; + if ( !isset($col["contentOptions"])){ + $col["contentOptions"] = []; + } + + if ( isset($col["contentOptions"]["class"])){ + $value = $col["contentOptions"]["class"]; + } + + if ( $i % 2 == 1){ + $columns[$i]["contentOptions"]["class"] = $value . " col-b"; + }else{ + $columns[$i]["contentOptions"]["class"] = $value . " col-a"; + } + + + + $i++; + } + echo GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => $columns diff --git a/backend/views/product/_form.php b/backend/views/product/_form.php index bb477ae..51567d8 100644 --- a/backend/views/product/_form.php +++ b/backend/views/product/_form.php @@ -40,6 +40,8 @@ $inventory_groups = ['' => ''] + ArrayHelper::map($inventory_groups, "id_invent field($model, 'profit_margins')->textInput() ?> + field($model, 'tax')->textInput() ?> + field($model, 'status')->checkbox( ['value' => 10, 'label' => Yii::t('common/product', "Active") ]) ?> field($model, 'description')->textarea(['maxlength' => true])->hint( Yii::t( 'common/product', "Max 255 character")) ?> diff --git a/backend/views/product/index.php b/backend/views/product/index.php index 6a6023e..9b5b8b2 100644 --- a/backend/views/product/index.php +++ b/backend/views/product/index.php @@ -37,7 +37,8 @@ $this->params['breadcrumbs'][] = $this->title; 'product_number', 'barcode', 'sale_price', - [ + 'tax', + [ 'attribute' => 'status', 'value' => 'statusHuman', diff --git a/backend/views/product/view.php b/backend/views/product/view.php index 30002c5..a9a142c 100644 --- a/backend/views/product/view.php +++ b/backend/views/product/view.php @@ -29,6 +29,7 @@ $this->params['breadcrumbs'][] = $this->title; 'purchase_price', 'sale_price', 'profit_margins', + 'tax', 'statusHuman', 'stock', [ diff --git a/common/models/InventoryItem.php b/common/models/InventoryItem.php index e1c496b..aea0365 100644 --- a/common/models/InventoryItem.php +++ b/common/models/InventoryItem.php @@ -26,11 +26,16 @@ use common\components\ProductAwareBehavior; * @property integer $id_user * @property integer $id_inventory_item_prev * @property integer $price_brutto - * @property integer $total_price_brutto * @property string $created_at * @property string $updated_at * @property mixed productName * @property mixed product + * @property integer tax + * @property integer purchase_price_net + * @property integer net_stock_money + * @property integer stock_missing_count + * @property integer stock_missing_money + * @property \common\models\InventoryGroup inventoryGroup */ class InventoryItem extends BaseFitnessActiveRecord { @@ -133,6 +138,7 @@ class InventoryItem extends BaseFitnessActiveRecord return $result; } + public function getInventoryGroupName(){ $result = ""; @@ -158,32 +164,6 @@ class InventoryItem extends BaseFitnessActiveRecord return $name; } - - - public function getDifference(){ - $diff = $this->count - ( $this->count_prev + $this->count_in - $this->count_sold ); - return $diff; - - } - - - public function recalculateTotalPriceBrutto(){ - $diff = $this->difference; - if (!isset($diff) || !is_numeric($diff)){ - $diff = 0; - } - - $price = $this->price_brutto; - if (!isset($price) || !is_numeric($price)){ - $price = 0; - } - - $this->total_price_brutto = $price * $diff; - } - - - - public static function findNextItemAlphabetical($id_inventory_item){ @@ -227,6 +207,65 @@ class InventoryItem extends BaseFitnessActiveRecord return null; } + /** + * Recalculate the next values: + * + netto készlet érték + leltárhiány db + leltárhiány összeg + * + * @param boolean $insert whether this method called while inserting a record. + * If false, it means the method is called while updating a record. + * @return boolean whether the insertion or updating should continue. + * If false, the insertion or updating will be cancelled. + */ + public function beforeSave($insert) + { + $result = parent::beforeSave($insert); + if ( $result ){ + if ( !isset($this->id_inventory_group) ){ + $count = 0; + $purchase_price_net = 0; + $count_prev = 0; + $count_in = 0; + $count_sold = 0; + + $price = 0; + if (isset($price) && is_numeric($price)){ + $price = $this->price_brutto; + } + + if ( isset($this->count)){ + $count = $this->count; + } + + if ( isset($this->purchase_price_net)){ + $purchase_price_net = $this->purchase_price_net; + } + + if ( isset($this->count_prev) ){ + $count_prev = $this->count_prev; + } + + if ( isset($this->count_in)){ + $count_in = $this->count_in; + } + + if ( isset($this->count_sold)){ + $count_sold = $this->count_sold; + } + + //netto készlet érték + $this->net_stock_money = $count * $purchase_price_net; + //leltárhiány db + $this->stock_missing_count = $this->count - ($count_prev + $count_in - $count_sold) ; + //leltárhiány összeg + $this->stock_missing_money = $price * $this->stock_missing_count; + } + } + return $result; + } + public function afterSave($insert, $changedAttributes){ if ( !$insert ){ diff --git a/common/models/Product.php b/common/models/Product.php index 281f7c3..74c2b84 100644 --- a/common/models/Product.php +++ b/common/models/Product.php @@ -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; + } + + } diff --git a/common/models/Transfer.php b/common/models/Transfer.php index 6e8f619..86c5351 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -40,6 +40,7 @@ use yii\web\NotAcceptableHttpException; * @property integer $id_customer * @property int|string paid_by * @property int payment_method + * @property \common\models\Ticket ticket */ class Transfer extends \common\models\BaseFitnessActiveRecord { const TYPE_PRODUCT = 10; @@ -597,7 +598,6 @@ class Transfer extends \common\models\BaseFitnessActiveRecord { public function beforeDelete() { parent::beforeDelete (); if ($this->type == Transfer::TYPE_TICKET) { - /** @noinspection PhpUndefinedFieldInspection */ $ticket = $this->ticket; if ($ticket != null) { $ticket->delete (); diff --git a/console/controllers/InventoryConsoleController.php b/console/controllers/InventoryConsoleController.php index 6329852..3981fdb 100644 --- a/console/controllers/InventoryConsoleController.php +++ b/console/controllers/InventoryConsoleController.php @@ -54,7 +54,6 @@ class InventoryConsoleController extends Controller{ $item->count_sold = $products; - $item->recalculateTotalPriceBrutto(); echo $item->price_brutto . " - ". $item->productName ."\n"; diff --git a/console/migrations/m170103_172147_alter__product__add__column__tax.php b/console/migrations/m170103_172147_alter__product__add__column__tax.php new file mode 100644 index 0000000..0bcb70a --- /dev/null +++ b/console/migrations/m170103_172147_alter__product__add__column__tax.php @@ -0,0 +1,30 @@ +addColumn("product", "tax", "int default 27"); + } + + public function down() + { + echo "m170103_172147_alter__product__add__column__tax cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m170103_175608_alter__product__add__column__purchase_net_price.php b/console/migrations/m170103_175608_alter__product__add__column__purchase_net_price.php new file mode 100644 index 0000000..c49a683 --- /dev/null +++ b/console/migrations/m170103_175608_alter__product__add__column__purchase_net_price.php @@ -0,0 +1,31 @@ +addColumn("product", "purchase_net_price", "int default 0"); + $this->execute("update product set purchase_net_price = purchase_price - FLOOR( purchase_price * tax / 100 )"); + } + + public function down() + { + echo "m170103_175608_alter__product__add__column__purchase_net_price cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m170104_054306_alter__table__product__set_products__tax__18.php b/console/migrations/m170104_054306_alter__table__product__set_products__tax__18.php new file mode 100644 index 0000000..3a8dc75 --- /dev/null +++ b/console/migrations/m170104_054306_alter__table__product__set_products__tax__18.php @@ -0,0 +1,32 @@ +execute("update product set tax = 18 where name = 'Zabcsók';"); + $this->execute("update product set tax = 18 where name = 'Mabaker zabszelet';"); + $this->execute("update product set purchase_net_price = purchase_price - FLOOR( purchase_price * tax / 100 )"); + } + + public function down() + { + echo "m170104_054306_alter__table__product__set_products__tax__18 cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/console/migrations/m170104_063931_alter__table__inventory__item__add__tax__net_stock_valu__sotck_missing.php b/console/migrations/m170104_063931_alter__table__inventory__item__add__tax__net_stock_valu__sotck_missing.php new file mode 100644 index 0000000..2760be1 --- /dev/null +++ b/console/migrations/m170104_063931_alter__table__inventory__item__add__tax__net_stock_valu__sotck_missing.php @@ -0,0 +1,50 @@ +addColumn("inventory_item", "tax", "int default 0"); + $this->addColumn("inventory_item", "net_stock_money", "int default 0"); + $this->addColumn("inventory_item", "purchase_price_net", "int default 0"); + $this->addColumn("inventory_item", "stock_missing_money", "int default 0"); + $this->addColumn("inventory_item", "stock_missing_count", "int default 0"); + + + $sql = "UPDATE inventory_item AS i + INNER JOIN product AS p ON p.id_product = i.id_product + SET + i.tax = p.tax, + i.purchase_price_net = p.purchase_net_price, + i.net_stock_money = i.count * p.purchase_net_price, + i.stock_missing_count = (coalesce(i.count,0) - ( coalesce(i.count_prev,0) + coalesce(i.count_in,0) - coalesce(i.count_sold,0) )), + i.stock_missing_money = i.price_brutto * (coalesce(i.count,0) - ( coalesce(i.count_prev,0) + coalesce(i.count_in,0) - coalesce(i.count_sold,0) )) + WHERE i.id_inventory_group is null "; + + + $this->execute($sql); + + + } + + public function down() + { + echo "m170104_063931_alter__table__inventory__item__add__tax__net_stock_valu__sotck_missing cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/frontend/controllers/TicketController.php b/frontend/controllers/TicketController.php index 00a7c3e..1ee2830 100644 --- a/frontend/controllers/TicketController.php +++ b/frontend/controllers/TicketController.php @@ -161,7 +161,9 @@ class TicketController extends FrontendController public function actionDelete($id) { $ticket = $this->findModel($id); - $transfer = Transfer::find()->andWhere(['transfer.type' => Transfer::TYPE_TICKET]) + + /** @var \common\models\Transfer $transfer */ + $transfer = Transfer::find()->andWhere(['transfer.type' => Transfer::TYPE_TICKET]) ->andWhere(['transfer.id_object' => $ticket->id_ticket]) ->one(); $connection = \Yii::$app->db;