Add hierarchy of needed models, w/ formatters
This commit is contained in:
parent
47fe74fbb0
commit
58c57c6f89
1 changed files with 149 additions and 88 deletions
|
@ -171,8 +171,8 @@ class ModelObject {
|
||||||
* Returns a string value for the model, to use in comparisons
|
* Returns a string value for the model, to use in comparisons
|
||||||
* @see {@link Models.TreeNode.compare|TreeNode.compare}
|
* @see {@link Models.TreeNode.compare|TreeNode.compare}
|
||||||
*/
|
*/
|
||||||
comparevalue() {
|
static compare(a, b) {
|
||||||
return this.id.toString();
|
return a.id - b.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -520,15 +520,11 @@ class Article extends ModelObject {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default values for Article model instances.
|
* Default values for Article model instances.
|
||||||
* @default <tt>{ 'id': 0, 'name': '', 'price': 0, 'stock': 0,
|
* @default <tt>{ 'id': 0, 'name': '', 'price': 0, 'stock': 0 }</tt>
|
||||||
* 'category': new ArticleCategory() }</tt>
|
|
||||||
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
|
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
|
||||||
*/
|
*/
|
||||||
static get default_data() {
|
static get default_data() {
|
||||||
return {
|
return { 'id': 0, 'name': '', 'price': 0, 'stock': 0 };
|
||||||
'id': 0, 'name': '', 'price': 0, 'stock': 0,
|
|
||||||
'category': new ArticleCategory(),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -603,13 +599,43 @@ class Day extends ModelObject {
|
||||||
set date(v) { this._date = dateUTCToParis(v).startOf('date'); }
|
set date(v) { this._date = dateUTCToParis(v).startOf('date'); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OperationGroup model. Cannot be accessed through API.
|
* HistoryGroup model. Should not be used without extending.
|
||||||
* @extends Models.ModelObject
|
* @extends Models.ModelObject
|
||||||
* @memberof Models
|
* @memberof Models
|
||||||
*/
|
*/
|
||||||
class OperationGroup extends ModelObject {
|
class HistoryGroup extends ModelObject {
|
||||||
|
static get props() {
|
||||||
|
return ['id', 'at', 'comment', 'valid_by'];
|
||||||
|
}
|
||||||
|
|
||||||
|
static get default_data() {
|
||||||
|
return {'id': 0, 'at': moment(), 'comment': '',
|
||||||
|
'valid_by': '',};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comparison function between OperationGroup model instances.
|
||||||
|
* @see {@link Models.ModelObject.compare|ModelObject.compare}
|
||||||
|
*/
|
||||||
|
static compare(a, b) {
|
||||||
|
//Groups are sorted by most recent first
|
||||||
|
if (a.at < b.at) return 1;
|
||||||
|
else if (a.at > b.at) return -1;
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the date to a moment() object
|
||||||
|
get at() { return this._at; }
|
||||||
|
set at(v) { this._at = dateUTCToParis(v); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OperationGroup model. Cannot be accessed through API.
|
||||||
|
* @extends Models.HistoryGroup
|
||||||
|
* @memberof Models
|
||||||
|
*/
|
||||||
|
class OperationGroup extends HistoryGroup {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Properties associated with an opegroup.
|
* Properties associated with an opegroup.
|
||||||
|
@ -618,8 +644,7 @@ class OperationGroup extends ModelObject {
|
||||||
* @see {@link Models.ModelObject.props|ModelObject.props}
|
* @see {@link Models.ModelObject.props|ModelObject.props}
|
||||||
*/
|
*/
|
||||||
static get props() {
|
static get props() {
|
||||||
return ['id', 'amount', 'at', 'is_cof', 'comment',
|
return HistoryGroup.props.concat(['is_cof', 'trigramme']);
|
||||||
'trigramme', 'valid_by', 'day', 'type'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -629,18 +654,10 @@ class OperationGroup extends ModelObject {
|
||||||
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
|
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
|
||||||
*/
|
*/
|
||||||
static get default_data() {
|
static get default_data() {
|
||||||
return {'id': '', 'amount': 0, 'at': moment(), 'is_cof': false,
|
return $.extend({}, HistoryGroup.default_data,
|
||||||
'comment': '', 'trigramme': '', 'valid_by': '', 'day': new Day(),
|
{'amount': 0, 'is_cof': false, 'trigramme': ''});
|
||||||
'type': ''};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Verbose name for OperationGroup model.
|
|
||||||
* @default <tt>'opegroup'</tt>
|
|
||||||
* @see {@link Models.ModelObject.verbose_name|ModelObject.verbose_name}
|
|
||||||
*/
|
|
||||||
static get verbose_name() { return 'opegroup'; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @default {@link Formatters.OpegroupFormatter}
|
* @default {@link Formatters.OpegroupFormatter}
|
||||||
*/
|
*/
|
||||||
|
@ -648,27 +665,27 @@ class OperationGroup extends ModelObject {
|
||||||
return OpegroupFormatter;
|
return OpegroupFormatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Comparison function between OperationGroup model instances.
|
|
||||||
* @see {@link Models.ModelObject.compare|ModelObject.compare}
|
|
||||||
*/
|
|
||||||
static compare(a, b) {
|
|
||||||
//Opegroups are sorted by most recent first
|
|
||||||
if (a.at < b.at) return 1;
|
|
||||||
else if (a.at > b.at) return -1;
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
get amount() { return this._amount; }
|
get amount() { return this._amount; }
|
||||||
set amount(v) { this._amount = floatCheck(v); }
|
set amount(v) { this._amount = floatCheck(v); }
|
||||||
|
|
||||||
// Parse the date to a moment() object
|
|
||||||
get at() { return this._at; }
|
|
||||||
set at(v) { this._at = dateUTCToParis(v); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Operation model. Cannot be accessed through API.
|
* TransferGroup model. Cannot be accessed through API.
|
||||||
|
* @extends Models.HistoryGroup
|
||||||
|
* @memberof Models
|
||||||
|
*/
|
||||||
|
class TransferGroup extends HistoryGroup {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @default {@link Formatters.TransferGroupFormatter}
|
||||||
|
*/
|
||||||
|
formatter() {
|
||||||
|
return TransferGroupFormatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operation model. Should not be used directly.
|
||||||
* @extends Models.ModelObject
|
* @extends Models.ModelObject
|
||||||
* @memberof Models
|
* @memberof Models
|
||||||
*/
|
*/
|
||||||
|
@ -682,13 +699,7 @@ class Operation extends ModelObject {
|
||||||
* @see {@link Models.ModelObject.props|ModelObject.props}
|
* @see {@link Models.ModelObject.props|ModelObject.props}
|
||||||
*/
|
*/
|
||||||
static get props() {
|
static get props() {
|
||||||
var common = ['id', 'type', 'amount', 'canceled_at', 'canceled_by',
|
return ['id', 'amount', 'canceled_at', 'canceled_by'];
|
||||||
'is_checkout', 'opegroup'];
|
|
||||||
var transfer_only = ['from_acc', 'to_acc'];
|
|
||||||
var purchase_only = ['article_name', 'article_nb', 'addcost_for',
|
|
||||||
'addcost_amount'];
|
|
||||||
|
|
||||||
return common.concat(transfer_only, purchase_only);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -699,33 +710,12 @@ class Operation extends ModelObject {
|
||||||
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
|
* @see {@link Models.ModelObject.default_data|ModelObject.default_data}
|
||||||
*/
|
*/
|
||||||
static get default_data() {
|
static get default_data() {
|
||||||
var common = {'id': '', 'type': '', 'amount': 0, 'is_checkout': false,
|
return {'id': '', 'amount': 0, 'canceled_at': undefined, 'canceled_by': '' };
|
||||||
'canceled_at': undefined, 'canceled_by': '',
|
|
||||||
'opegroup': new OperationGroup()};
|
|
||||||
var transfer_only = {'from_acc': '', 'to_acc': ''};
|
|
||||||
var purchase_only = {'article_name': '', 'article_nb': 0,
|
|
||||||
'addcost_amount': 0, 'addcost_for': 0};
|
|
||||||
|
|
||||||
return $.extend({}, common, transfer_only, purchase_only);
|
|
||||||
}
|
|
||||||
|
|
||||||
static get verbose_name() { return 'operation'; }
|
|
||||||
|
|
||||||
formatter() {
|
|
||||||
if (this.type === 'purchase')
|
|
||||||
return PurchaseFormatter;
|
|
||||||
else if (this.type == 'transfer')
|
|
||||||
return TransferFormatter;
|
|
||||||
else
|
|
||||||
return SpecialOpeFormatter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get amount() { return this._amount; }
|
get amount() { return this._amount; }
|
||||||
set amount(v) { this._amount = floatCheck(v); }
|
set amount(v) { this._amount = floatCheck(v); }
|
||||||
|
|
||||||
get addcost_amount() { return this._addcost_amount; }
|
|
||||||
set addcost_amount(v) { this._addcost_amount = floatCheck(v); }
|
|
||||||
|
|
||||||
get canceled_at() { return this._canceled_at; }
|
get canceled_at() { return this._canceled_at; }
|
||||||
set canceled_at(v) {
|
set canceled_at(v) {
|
||||||
if (v)
|
if (v)
|
||||||
|
@ -735,6 +725,53 @@ class Operation extends ModelObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Purchase extends Operation {
|
||||||
|
static get props() {
|
||||||
|
return Operation.props.concat(
|
||||||
|
['article_name', 'article_nb', 'addcost_amount', 'addcost_for']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static get default_data() {
|
||||||
|
return $.extend({}, Operation.default_data, {
|
||||||
|
'article_name': '', 'article_nb': 0,
|
||||||
|
'addcost_amount': 0, 'addcost_for': ''
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
formatter() {
|
||||||
|
return PurchaseFormatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SpecialOperation extends Operation {
|
||||||
|
static get props() {
|
||||||
|
return Operation.props.concat(['type', 'is_checkout']);
|
||||||
|
}
|
||||||
|
|
||||||
|
static get default_data() {
|
||||||
|
return $.extend({}, Operation.default_data, {'type': '', 'is_checkout': false});
|
||||||
|
}
|
||||||
|
|
||||||
|
formatter() {
|
||||||
|
return SpecialOpeFormatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Transfer extends Operation {
|
||||||
|
static get props() {
|
||||||
|
return Operation.props.concat(['from_acc', 'to_acc']);
|
||||||
|
}
|
||||||
|
|
||||||
|
static get default_data() {
|
||||||
|
return $.extend({}, Operation.default_data, {'from_acc': '', 'to_acc': ''});
|
||||||
|
}
|
||||||
|
|
||||||
|
formatter() {
|
||||||
|
return TransferFormatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Node for ModelForest object
|
* Node for ModelForest object
|
||||||
|
@ -805,7 +842,9 @@ class ModelForest {
|
||||||
var node = new TreeNode(data.type, content);
|
var node = new TreeNode(data.type, content);
|
||||||
|
|
||||||
if (data.child_sort)
|
if (data.child_sort)
|
||||||
node.child_sort = data.child_sort
|
node.child_sort = this.constructor.models[data.child_sort];
|
||||||
|
else
|
||||||
|
node.child_sort = ModelObject;
|
||||||
|
|
||||||
if (direction <= 0) {
|
if (direction <= 0) {
|
||||||
if (data.parent) {
|
if (data.parent) {
|
||||||
|
@ -864,7 +903,7 @@ class ModelForest {
|
||||||
$container.append($rendered);
|
$container.append($rendered);
|
||||||
|
|
||||||
//dirty
|
//dirty
|
||||||
node.children.sort(this.constructor.compare.bind(null, this.constructor.models[node.child_sort]));
|
node.children.sort(this.constructor.compare.bind(null, node.child_sort));
|
||||||
|
|
||||||
for (let child of node.children) {
|
for (let child of node.children) {
|
||||||
var $child = this.render_element(child, templates, options);
|
var $child = this.render_element(child, templates, options);
|
||||||
|
@ -1018,14 +1057,21 @@ class ArticleList extends APIModelForest {
|
||||||
* @extends Models.APIModelList
|
* @extends Models.APIModelList
|
||||||
* @memberof Models
|
* @memberof Models
|
||||||
*/
|
*/
|
||||||
class OperationList extends APIModelList {
|
class OperationList extends APIModelForest {
|
||||||
/**
|
/**
|
||||||
* Default structure for OperationList instances.
|
* Default structure for OperationList instances.
|
||||||
* @default <tt>[Operation, OperationGroup, Day]</tt>
|
* @default <tt>[Operation, OperationGroup, Day]</tt>
|
||||||
* @see {@link Models.ModelList.models|ModelList.models}
|
* @see {@link Models.ModelList.models|ModelList.models}
|
||||||
*/
|
*/
|
||||||
static get models() {
|
static get models() {
|
||||||
return [Operation, OperationGroup, Day];
|
return {
|
||||||
|
'day': Day,
|
||||||
|
'opegroup': OperationGroup,
|
||||||
|
'transfergroup': TransferGroup,
|
||||||
|
'purchase': Purchase,
|
||||||
|
'specialope': SpecialOperation,
|
||||||
|
'transfer': Transfer,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1036,6 +1082,11 @@ class OperationList extends APIModelList {
|
||||||
static get url_model() {
|
static get url_model() {
|
||||||
return Urls['kfet.history.json']();
|
return Urls['kfet.history.json']();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.root_sort = Day;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1331,20 +1382,7 @@ class ArticleFormatter extends Formatter {
|
||||||
* @todo don't display trigramme in account_read
|
* @todo don't display trigramme in account_read
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//TODO display_trigramme option
|
class HistoryGroupFormatter extends Formatter {
|
||||||
class OpegroupFormatter extends Formatter {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Properties renderable to html.
|
|
||||||
* @default {@link Models.Opegroup.props}
|
|
||||||
*/
|
|
||||||
static get props() {
|
|
||||||
return OperationGroup.props.concat(['time']);
|
|
||||||
}
|
|
||||||
|
|
||||||
static prop_amount(a) {
|
|
||||||
return amountDisplay(a.amount, a.is_cof, a.trigramme);
|
|
||||||
}
|
|
||||||
|
|
||||||
static prop_time(a) {
|
static prop_time(a) {
|
||||||
return a.at.format('HH:mm:ss');
|
return a.at.format('HH:mm:ss');
|
||||||
|
@ -1358,6 +1396,29 @@ class OpegroupFormatter extends Formatter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TransferGroupFormatter extends HistoryGroupFormatter {
|
||||||
|
|
||||||
|
static get props() {
|
||||||
|
return TransferGroup.props.concat(['infos', 'time']);
|
||||||
|
}
|
||||||
|
|
||||||
|
static prop_infos(a) {
|
||||||
|
//TODO find better thing to put here
|
||||||
|
return 'Transferts';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OpegroupFormatter extends HistoryGroupFormatter {
|
||||||
|
|
||||||
|
static get props() {
|
||||||
|
return OperationGroup.props.concat(['time']);
|
||||||
|
}
|
||||||
|
|
||||||
|
static prop_amount(a) {
|
||||||
|
return amountDisplay(a.amount, a.is_cof, a.trigramme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends Formatters.Formatter
|
* @extends Formatters.Formatter
|
||||||
* @memberof Formatters
|
* @memberof Formatters
|
||||||
|
@ -1393,12 +1454,12 @@ class OperationFormatter extends Formatter {
|
||||||
}
|
}
|
||||||
|
|
||||||
static prop_amount(a) {
|
static prop_amount(a) {
|
||||||
return amountDisplay(a.amount, a.opegroup.is_cof, a.opegroup.trigramme);
|
return amountDisplay(a.amount, a.is_cof, a.trigramme);
|
||||||
}
|
}
|
||||||
|
|
||||||
static prop_addcost(a) {
|
static prop_addcost(a) {
|
||||||
if (a.addcost_for) {
|
if (a.addcost_for) {
|
||||||
return '('+amountDisplay(a.addcost_amount, a.opegroup.is_cof)
|
return '('+amountDisplay(a.addcost_amount, a.is_cof)
|
||||||
+'UKF pour '+a.addcost_for+')';
|
+'UKF pour '+a.addcost_for+')';
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
|
|
Loading…
Reference in a new issue