From f2e30779f0e3b8adc350216b1c11eaa6d4ec0155 Mon Sep 17 00:00:00 2001 From: rocho Date: Tue, 20 Oct 2015 17:23:52 +0200 Subject: [PATCH] add product sell discount js changes --- common/models/Discount.php | 18 +++++- common/models/Transfer.php | 10 ++-- frontend/components/FrontendMenuStructure.php | 31 +++++++++- frontend/models/MoneyMovementSearch.php | 31 +++++----- frontend/views/layouts/main.php | 15 ----- frontend/views/money-movement/_search.php | 38 ++++++------ frontend/views/money-movement/index.php | 2 +- frontend/views/product/sale.php | 3 + frontend/views/transfer/_search.php | 48 ++++++--------- frontend/web/js/product.sell.js | 59 ++++++++++++++++++- 10 files changed, 165 insertions(+), 90 deletions(-) diff --git a/common/models/Discount.php b/common/models/Discount.php index 6e65bbf..6aad798 100644 --- a/common/models/Discount.php +++ b/common/models/Discount.php @@ -4,6 +4,7 @@ namespace common\models; use Yii; use yii\behaviors\TimestampBehavior; +use yii\helpers\ArrayHelper; /** * This is the model class for table "discount". @@ -113,9 +114,24 @@ class Discount extends \yii\db\ActiveRecord * */ public static function applyDiscount($money,$discount){ $result = $money; - $valueOfDiscount = $money * $discount->value * 100; + $valueOfDiscount = $money * $discount->value / 100; $result = $money - $valueOfDiscount; return $result; } + + public static function modelsToArray($discounts,$default = []){ + + if ( $discounts == null ){ + return $default; + } + + return ArrayHelper::toArray($discounts, [ + 'common\models\Discount' => [ + 'id_discount', + 'value', + ], + ]); + } + } diff --git a/common/models/Transfer.php b/common/models/Transfer.php index b37da55..47ca74b 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -148,8 +148,10 @@ class Transfer extends \common\models\BaseFitnessActiveRecord $result = ""; if ( $this->type == Transfer::TYPE_TICKET ){ $result = $this->ticketName; - }else{ + }else if ( $this->type == Transfer::TYPE_PRODUCT ){ $result = $this->productName; + }else if ( $this->type == Transfer::TYPE_MONEY_MOVEMENT_OUT ){ + $result = "Pénz kivétel - " . $this->moneyMovement->name; } return $result; } @@ -201,10 +203,6 @@ class Transfer extends \common\models\BaseFitnessActiveRecord }else if ( $this->type == Transfer::TYPE_MONEY_MOVEMENT_OUT ){ $result = Yii::t('common/transfer','Money movement'); } - -// $result .= " xx"; -// $result .= $this->type; - return $result; } @@ -281,7 +279,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord $transfer->type = Transfer::TYPE_MONEY_MOVEMENT_OUT; $transfer->status = Transfer::STATUS_PAID; $transfer->direction = Transfer::DIRECTION_OUT; - $transfer->count = 0; + $transfer->count = null; $transfer->id_object = $moneyMovement->id_money_movement; $transfer->money = $moneyMovement->money; diff --git a/frontend/components/FrontendMenuStructure.php b/frontend/components/FrontendMenuStructure.php index e076884..505d3d5 100644 --- a/frontend/components/FrontendMenuStructure.php +++ b/frontend/components/FrontendMenuStructure.php @@ -4,15 +4,41 @@ namespace frontend\components; use Yii; use common\models\Order; use yii\helpers\Html; +use common\models\MoneyMovement; +use yii\db\Query; +use common\models\AccountState; class FrontendMenuStructure{ public $menuItems; + public $start; + public $tomorrow; - public function FrontendMenuStructure(){ + public function __construct(){ $this->menuItems = []; +// print('abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
'); + + $this->start = Yii::$app->formatter->asDatetime( strtotime('today UTC') ); + $this->tomorrow = Yii::$app->formatter->asDatetime( strtotime('tomorrow UTC') ); + + Yii::info("Start date is : ". $this->start); + + + + if ( $this->isLogged() ){ + $lastAccountState = AccountState::find()->andWhere(['id_user' => Yii::$app->user->id])->orderBy(['account_state.created_at' => SORT_DESC])->limit(1)->one(); + if ( isset($lastAccountState) ){ + $this->start = strtotime( $lastAccountState->created_at . ' UTC' ); + } + } + +// print_r($this->start); +// print_r($this->tomorrow); + } + + protected function can($authItem){ $result = false; if (\Yii::$app->user->can($authItem)) { @@ -30,7 +56,8 @@ class FrontendMenuStructure{ protected function addRecepcio(){ if ( $this->isLogged() ){ $this->menuItems[] = ['label' => 'Recepcio', 'url' => ['/customer/reception'] ]; - $this->menuItems[] = ['label' => 'Pénzmozgások', 'url' => ['/money-movement/index'] ]; +// , 'MoneyMovementSearch[start]' => $this->start, 'MoneyMovementSearch[end]' => $this->tomorrow + $this->menuItems[] = ['label' => 'Pénzmozgások', 'url' => [ '/money-movement/index', 'MoneyMovementSearch[start]' => $this->start, 'MoneyMovementSearch[end]' => $this->tomorrow ] ]; $this->menuItems[] = ['label' => 'Kassza', 'items' => [ ['label' => Yii::t('frontend/account-state', 'Account states'), 'url' => ['/account-state/index'] ], diff --git a/frontend/models/MoneyMovementSearch.php b/frontend/models/MoneyMovementSearch.php index 2f9109f..e51e175 100644 --- a/frontend/models/MoneyMovementSearch.php +++ b/frontend/models/MoneyMovementSearch.php @@ -12,14 +12,22 @@ use common\models\MoneyMovement; */ class MoneyMovementSearch extends MoneyMovement { + + + public $start; + public $end; + + public $timestampStart; + public $timestampEnd; + /** * @inheritdoc */ public function rules() - { + { return [ - [['id_money_movement', 'id_account', 'id_user', 'type', 'money'], 'integer'], - [['name', 'comment', 'created_at', 'updated_at'], 'safe'], + [[ 'start', ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ], + [[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ], ]; } @@ -51,22 +59,13 @@ class MoneyMovementSearch extends MoneyMovement if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails - // $query->where('0=1'); + $query->where('0=1'); return $dataProvider; } - $query->andFilterWhere([ - 'id_money_movement' => $this->id_money_movement, - 'id_account' => $this->id_account, - 'id_user' => $this->id_user, - 'type' => $this->type, - 'money' => $this->money, - 'created_at' => $this->created_at, - 'updated_at' => $this->updated_at, - ]); - - $query->andFilterWhere(['like', 'name', $this->name]) - ->andFilterWhere(['like', 'comment', $this->comment]); + + $query->andFilterWhere([ '>=', 'money_movement.created_at', $this->timestampStart ] ); + $query->andFilterWhere([ '<', 'money_movement.created_at', $this->timestampEnd ] ); return $dataProvider; } diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index fd2f627..8725ef0 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -48,21 +48,6 @@ AppAsset::register($this); 'class' => 'navbar-inverse navbar-fixed-top', ], ]); - $menuItems = [ - ['label' => 'Home', 'url' => ['/site/index']], - ['label' => 'About', 'url' => ['/site/about']], - ['label' => 'Contact', 'url' => ['/site/contact']], - ]; - if (Yii::$app->user->isGuest) { - $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']]; - $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; - } else { - $menuItems[] = [ - 'label' => 'Logout (' . Yii::$app->user->identity->username . ')', - 'url' => ['/site/logout'], - 'linkOptions' => ['data-method' => 'post'] - ]; - } echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $items, diff --git a/frontend/views/money-movement/_search.php b/frontend/views/money-movement/_search.php index 79e99aa..f5b2c84 100644 --- a/frontend/views/money-movement/_search.php +++ b/frontend/views/money-movement/_search.php @@ -2,6 +2,8 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use kartik\widgets\DatePicker; +use kartik\widgets\DateTimePicker; /* @var $this yii\web\View */ /* @var $model frontend\models\MoneyMovementSearch */ @@ -15,27 +17,27 @@ use yii\widgets\ActiveForm; 'method' => 'get', ]); ?> - field($model, 'id_money_movement') ?> - - field($model, 'id_account') ?> - - field($model, 'id_user') ?> - - field($model, 'name') ?> - - field($model, 'type') ?> - - field($model, 'money') ?> - - field($model, 'comment') ?> - - field($model, 'created_at') ?> - - field($model, 'updated_at') ?> +
+
+ field($model, 'start')->widget(DateTimePicker::classname(), [ + 'pluginOptions' => [ + 'autoclose'=>true, + 'format' => 'yyyy.mm.dd hh:ii' + ] + ]) ?> +
+
+ field($model, 'end') ->widget(DateTimePicker::classname(), [ + 'pluginOptions' => [ + 'autoclose'=>true, + 'format' => 'yyyy.mm.dd hh:ii' + ] + ]) ?> +
+
'btn btn-primary']) ?> - 'btn btn-default']) ?>
diff --git a/frontend/views/money-movement/index.php b/frontend/views/money-movement/index.php index 4e5cc7f..3063fe2 100644 --- a/frontend/views/money-movement/index.php +++ b/frontend/views/money-movement/index.php @@ -13,7 +13,7 @@ $this->params['breadcrumbs'][] = $this->title;

title) ?>

- render('_search', ['model' => $searchModel]); ?> + render('_search', ['model' => $searchModel]); ?>

'btn btn-success']) ?> diff --git a/frontend/views/product/sale.php b/frontend/views/product/sale.php index 497226a..602350e 100644 --- a/frontend/views/product/sale.php +++ b/frontend/views/product/sale.php @@ -8,6 +8,7 @@ use frontend\assets\ProductSellAsset; use yii\helpers\Url; use yii\bootstrap\ActiveForm; use yii\helpers\ArrayHelper; +use common\models\Discount; /* @var $this yii\web\View */ /* @var $form yii\bootstrap\ActiveForm */ @@ -20,6 +21,8 @@ $options = []; $options['lookup_product_url'] = Url::toRoute(['product/lookup']); $options['clear_list_url'] = Url::toRoute(['product/clear-list']); $options['sold_items'] = $userTransfers; +$options['discounts'] = Discount::modelsToArray($discounts); + $this->registerJs ( 'new ProductSell( '. json_encode($options).');' ); diff --git a/frontend/views/transfer/_search.php b/frontend/views/transfer/_search.php index d910cf8..c4ef840 100644 --- a/frontend/views/transfer/_search.php +++ b/frontend/views/transfer/_search.php @@ -15,35 +15,25 @@ use yii\widgets\ActiveForm; 'method' => 'get', ]); ?> - field($model, 'id_transfer') ?> - - field($model, 'id_discount') ?> - - field($model, 'id_currency') ?> - - field($model, 'id_object') ?> - - field($model, 'status') ?> - - field($model, 'type') ?> - - field($model, 'item_price') ?> - - field($model, 'count') ?> - - field($model, 'money') ?> - - field($model, 'money_currency') ?> - - field($model, 'rate') ?> - - field($model, 'id_user') ?> - - field($model, 'comment') ?> - - field($model, 'created_at') ?> - - field($model, 'updated_at') ?> + +

+
+ field($model, 'start')->widget(DatePicker::classname(), [ + 'pluginOptions' => [ + 'autoclose'=>true, + 'format' => 'yyyy.mm.dd' + ] + ]) ?> +
+
+ field($model, 'end') ->widget(DatePicker::classname(), [ + 'pluginOptions' => [ + 'autoclose'=>true, + 'format' => 'yyyy.mm.dd' + ] + ]) ?> +
+
'btn btn-primary']) ?> diff --git a/frontend/web/js/product.sell.js b/frontend/web/js/product.sell.js index 93dd66e..0a719b5 100644 --- a/frontend/web/js/product.sell.js +++ b/frontend/web/js/product.sell.js @@ -19,6 +19,7 @@ function ProductSell(o){ message_paid: 'Tételek fizetve', /**list of sold items by current user*/ sold_items: [], + discounts: [], }; init(); @@ -34,12 +35,17 @@ function ProductSell(o){ setFocus(); addEnterPressedBehaviours(); addBehaviorCountChangedListener(); + addBehaviourDisocuntChanged(); createUserSoldItemsTable(); addBehaviourBtnPaid(); productChanged(); addDocumentKeypressedListener(); } + function addBehaviourDisocuntChanged(){ + $("#productsaleform-id_discount").change(refreshCalculatedValues); + } + function addDocumentKeypressedListener(){ $( document ).keypress(function( event ) { @@ -235,15 +241,23 @@ function ProductSell(o){ } function refreshCalculatedValues(){ - var count,price; + var count,price,id_discount,discount; var table; table = $('.table-product'); count = $("#productsaleform-count").val(); + id_discount = $("#productsaleform-id_discount").val(); + app.discount = findDiscount( id_discount ); count = +count; if ( isNaN(count)){ count = 0; } price = count * app.product.sale_price; + + + discount = calcDiscount(price, app.discount); + price = deductDiscount(price,discount); + price = normalizePrice(price); + table.find('.product-count').html(count); table.find('.product-price').html(price); } @@ -345,13 +359,54 @@ function ProductSell(o){ }); } - function createUserSoldItemsTable(){ $('.sold-items-container').transferList({ 'transfers' : app.defaults.sold_items }); } + function findDiscount(id_discount){ + var discount; + discount = null; + $.each( app.defaults.discounts , function(e,i){ + if ( e.id_discount == id_discount ){ + discount = e; + } + + }); + return discount; + } + function calcDiscount(price,discount){ + var result, discountMoney; + result = 0; + + if ( discount ){ + result = price * ( discount.value / 100 ); + } + + return result; + } + + function deductDiscount( price , discount ){ + var result; + result = price - discount; + return result; + } + + function normalizePrice( price ){ + var result; + result = hufRound(price); + return result; + } + + function hufRound(x) + { + if (x > 0) { + return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5; + } else { + return ((Math.abs(x) % 5) >= 2.5 ? parseInt(Math.abs(x) / 5) * 5 + 5 : parseInt(Math.abs(x) / 5) * 5)*-1; + } + } }