- basket.add_withdraw takes a positive amount
- move models and formatters definitions with their siblings
This commit is contained in:
Aurélien Delobelle 2017-05-18 12:49:39 +02:00
parent a0503d0c53
commit 86a3ae3c2f
3 changed files with 126 additions and 125 deletions

View file

@ -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] || '';
}
}

View file

@ -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) {

View file

@ -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({