From 86a3ae3c2fdc0cf855e7d1e3d74a2275c673068e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Thu, 18 May 2017 12:49:39 +0200 Subject: [PATCH] clean - basket.add_withdraw takes a positive amount - move models and formatters definitions with their siblings --- kfet/static/kfet/js/kfet.api.js | 124 +++++++++++++++++++++++++++++++ kfet/static/kfet/js/kpsul.js | 125 +------------------------------- kfet/templates/kfet/kpsul.html | 2 +- 3 files changed, 126 insertions(+), 125 deletions(-) diff --git a/kfet/static/kfet/js/kfet.api.js b/kfet/static/kfet/js/kfet.api.js index 125eaff7..9900fe21 100644 --- a/kfet/static/kfet/js/kfet.api.js +++ b/kfet/static/kfet/js/kfet.api.js @@ -914,6 +914,61 @@ class Transfer extends Operation { } } + +class PurchaseBasket extends Purchase { + + static get default_data() { + let defaults = $.extend({}, Purchase.default_data); + delete defaults.amount; + return defaults; + } + + formatter() { + return PurchaseBasketFormatter; + } + + for_formset() { + return { + id: this.id, + type: "purchase", + amount: 0, // avoid django error + article: this.article, + article_nb: this.article_nb + }; + } + + get amount() { + let amount_ukf = - this.article.price * this.article_nb; + if (Config.get('addcost') + && kpsul.account_manager.account.trigramme != Config.get('addcost_for') + && this.article.category.has_addcost) + amount_ukf -= Config.get('addcost_amount') * this.article_nb; + let reduc_divisor = 1; + if (kpsul.account_manager.account.is_cof) + reduc_divisor += Config.get('subvention_cof') / 100; + return amount_ukf / reduc_divisor; + } + +} + + +class SpecialOperationBasket extends SpecialOperation { + + formatter() { + return SpecialOperationBasketFormatter; + } + + for_formset() { + return { + id: this.id, + type: this.type, + amount: this.amount + }; + } + +} + + /** * Simple {@link Models.ModelObject} forest. * @memberof Models @@ -1322,6 +1377,22 @@ class OperationList extends APIModelForest { } +class BasketData extends ModelForest { + + static get structure() { + return { + purchase: { + model: PurchaseBasket + }, + specialope: { + model: SpecialOperationBasket + } + }; + } + +} + + class ForestDisplay { constructor($container, templates, data) { @@ -1965,3 +2036,56 @@ class TransferFormatter extends OperationFormatter { return a.to_acc; } } + + +class ItemBasketFormatter extends Formatter { + + static get props() { + return ['amount', 'number', 'name']; + } + + static prop_amount(o) { + let account = kpsul.account_manager.account; + return amountDisplay(o.amount, account.is_cof, account.trigramme); + } + +} + + +class PurchaseBasketFormatter extends ItemBasketFormatter { + + static get attrs() { + return ['article_id', 'low_stock']; + } + + static prop_number(o) { + return "(" + o.article_nb + "/" + o.article.stock + ")"; + } + + static prop_name(o) { + return o.article.name; + } + + static attr_article_id(o) { + return o.article.id; + } + + static attr_low_stock(o) { + let stock = o.article.stock; + return -5 <= stock && stock <= 5; + } + +} + + +class SpecialOperationBasketFormatter extends ItemBasketFormatter { + + static prop_number(o) { + return o.amount.toFixed(2) + '€'; + } + + static prop_name(o) { + return SpecialOperation.verbose_types[o.type] || ''; + } + +} diff --git a/kfet/static/kfet/js/kpsul.js b/kfet/static/kfet/js/kpsul.js index 94ade8df..04c0d541 100644 --- a/kfet/static/kfet/js/kpsul.js +++ b/kfet/static/kfet/js/kpsul.js @@ -772,7 +772,7 @@ class BasketManager { } add_withdraw(amount) { - this._add_special("withdraw", amount); + this._add_special("withdraw", - amount); } add_edit(amount) { @@ -796,129 +796,6 @@ class BasketManager { } -class BasketData extends ModelForest { - - static get structure() { - return { - purchase: { - model: PurchaseBasket - }, - specialope: { - model: SpecialOperationBasket - } - }; - } - -} - - -class PurchaseBasket extends Purchase { - - static get default_data() { - let defaults = $.extend({}, Purchase.default_data); - delete defaults.amount; - return defaults; - } - - formatter() { - return PurchaseBasketFormatter; - } - - for_formset() { - return { - id: this.id, - type: "purchase", - amount: 0, // avoid django error - article: this.article, - article_nb: this.article_nb - }; - } - - get amount() { - let amount_ukf = - this.article.price * this.article_nb; - if (Config.get('addcost') - && kpsul.account_manager.account.trigramme != Config.get('addcost_for') - && this.article.category.has_addcost) - amount_ukf -= Config.get('addcost_amount') * this.article_nb; - let reduc_divisor = 1; - if (kpsul.account_manager.account.is_cof) - reduc_divisor += Config.get('subvention_cof') / 100; - return amount_ukf / reduc_divisor; - } - -} - - -class SpecialOperationBasket extends SpecialOperation { - - formatter() { - return SpecialOperationBasketFormatter; - } - - for_formset() { - return { - id: this.id, - type: this.type, - amount: this.amount - }; - } - -} - - -class ItemBasketFormatter extends Formatter { - - static get props() { - return ['amount', 'number', 'name']; - } - - static prop_amount(o) { - let account = kpsul.account_manager.account; - return amountDisplay(o.amount, account.is_cof, account.trigramme); - } - -} - - -class PurchaseBasketFormatter extends ItemBasketFormatter { - - static get attrs() { - return ['article_id', 'low_stock']; - } - - static prop_number(o) { - return "(" + o.article_nb + "/" + o.article.stock + ")"; - } - - static prop_name(o) { - return o.article.name; - } - - static attr_article_id(o) { - return o.article.id; - } - - static attr_low_stock(o) { - let stock = o.article.stock; - return -5 <= stock && stock <= 5; - } - -} - - -class SpecialOperationBasketFormatter extends ItemBasketFormatter { - - static prop_number(o) { - return o.amount.toFixed(2) + '€'; - } - - static prop_name(o) { - return SpecialOperation.verbose_types[o.type] || ''; - } - -} - - class BasketSummary { constructor(basket) { diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 682a44b1..e2df0b93 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -275,7 +275,7 @@ $(document).ready(function() { function callback(amount) { if (!$.isNumeric(amount) || amount <= 0) return false; - kpsul.basket.add_withdraw(- amount); + kpsul.basket.add_withdraw(amount); } withdrawDialog.open({