Newer
Older
* @module package/quiqqer/coupons/bin/backend/controls/Manager
define('package/quiqqer/coupons/bin/backend/controls/Manager', [
'qui/controls/desktop/Panel',
'qui/controls/loader/Loader',
'qui/controls/windows/Popup',
'qui/controls/windows/Confirm',
'qui/controls/buttons/Button',
'qui/controls/buttons/Separator',
'controls/grid/Grid',
'qui/utils/Form',
'package/quiqqer/coupons/bin/backend/CouponCodes',
'text!package/quiqqer/coupons/bin/backend/controls/Manager.html',
'text!package/quiqqer/coupons/bin/backend/controls/Manager.Create.html',
'text!package/quiqqer/coupons/bin/backend/controls/Manager.Usages.html',
'css!package/quiqqer/coupons/bin/backend/controls/Manager.css'
], function (QUI, QUIPanel, QUILoader, QUIPopup, QUIConfirm, QUIButton, QUISeparator,
Grid, QUIFormUtils, CouponCodes, QUILocale, Mustache, template, templateCreate,
templateUsages) {
"use strict";
var lg = 'quiqqer/coupons';
return new Class({
Extends: QUIPanel,
Type : 'package/quiqqer/coupons/bin/backend/controls/Manager',
Binds: [
'$onCreate',
'$onResize',
'$listRefresh',
'$onRefresh',
'$load',
'$setGridData',
'$toggleActiveStatus',
'$managePackages',
'$delete',
'$editBundle',
'refresh',
'$openUserPanel',
'$sendMail'
],
options: {
title: QUILocale.get(lg, 'controls.manager.title')
},
initialize: function (options) {
this.parent(options);
this.Loader = new QUILoader();
this.$User = null;
this.$Grid = null;
this.addEvents({
onCreate : this.$onCreate,
onRefresh: this.$onRefresh,
onResize : this.$onResize
});
},
/**
* Event: onCreate
*/
$onCreate: function () {
var self = this;
this.Loader.inject(this.$Elm);
this.addButton({
name : 'create',
text : QUILocale.get(lg, 'controls.manager.tbl.btn.create'),
textimage: 'fa fa-plus',
events : {
onClick: function () {
name : 'edit',
text : QUILocale.get(lg, 'controls.manager.tbl.btn.edit'),
textimage: 'fa fa-edit',
self.$showDetails(Object.clone(self.$Grid.getSelectedData()[0]));
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
}
}
});
this.addButton({
name : 'delete',
text : QUILocale.get(lg, 'controls.manager.tbl.btn.delete'),
textimage: 'fa fa-trash',
events : {
onClick: this.$delete
}
});
this.$load();
},
/**
* Refresh data
*/
refresh: function () {
if (this.$Grid) {
this.$Grid.refresh();
}
},
/**
* event: onResize
*/
$onResize: function () {
if (this.$GridParent && this.$Grid) {
var size = this.$GridParent.getSize();
this.$Grid.setHeight(size.y);
this.$Grid.resize();
}
},
/**
* Load Grid
*/
$load: function () {
var self = this;
this.setContent(Mustache.render(template));
var Content = this.getContent();
this.$GridParent = Content.getElement(
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
columnModel : [
{
header : QUILocale.get('quiqqer/system', 'id'),
dataIndex: 'id',
dataType : 'number',
width : 50
},
{
header : QUILocale.get(lg, 'controls.manager.tbl.header.code'),
dataIndex: 'code',
dataType : 'string',
width : 150
},
{
header : QUILocale.get(lg, 'controls.manager.tbl.header.title'),
dataIndex: 'title',
dataType : 'string',
width : 200
},
{
header : QUILocale.get(lg, 'controls.manager.tbl.header.status'),
dataIndex: 'status',
dataType : 'node',
width : 200,
className: 'clickable'
},
{
header : QUILocale.get(lg, 'controls.manager.tbl.header.validUntilDate'),
dataIndex: 'validUntilDateText',
dataType : 'string',
width : 150
},
{
header : QUILocale.get(lg, 'controls.manager.tbl.header.reusable'),
dataIndex: 'maxUsageLabel',
dataType : 'string',
width : 150
},
{
header : QUILocale.get(lg, 'controls.manager.tbl.header.createDate'),
dataIndex: 'createDate',
dataType : 'string',
width : 150
}
],
pagination : true,
serverSort : true,
selectable : true,
multipleSelection: true
});
this.$Grid.addEvents({
onDblClick: function () {
self.$showDetails(Object.clone(self.$Grid.getSelectedData()[0]));
},
onClick : function (event) {
var selected = self.$Grid.getSelectedData();
self.getButtons('delete').enable();
if (selected.length === 1) {
self.getButtons('edit').enable();
} else {
self.getButtons('edit').disable();
}
if (!event.cell.hasClass('clickable')) {
return;
}
var Row = selected[0];
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
},
onRefresh : this.$listRefresh
});
this.resize();
this.$Grid.refresh();
},
/**
* Event: onRefresh
*/
$onRefresh: function () {
if (this.$Grid) {
this.$Grid.refresh();
}
},
/**
* Refresh bundle list
*
* @param {Object} Grid
*/
$listRefresh: function (Grid) {
if (!this.$Grid) {
return;
}
var self = this;
self.getButtons('delete').disable();
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var GridParams = {
sortOn : Grid.getAttribute('sortOn'),
sortBy : Grid.getAttribute('sortBy'),
perPage: Grid.getAttribute('perPage'),
page : Grid.getAttribute('page')
};
switch (GridParams.sortOn) {
case 'status':
GridParams.sortOn = 'useDate';
break;
case 'user':
GridParams.sortOn = 'userId';
break;
}
this.Loader.show();
CouponCodes.getList(GridParams).then(function (ResultData) {
self.Loader.hide();
self.$setGridData(ResultData);
});
},
/**
* Set license data to grid
*
* @param {Object} GridData
*/
$setGridData: function (GridData) {
var textUnlimited = QUILocale.get(lg, 'controls.manager.tbl.validUntil.unlimited');
for (var i = 0, len = GridData.data.length; i < len; i++) {
var Row = GridData.data[i];
var StatusElm = new Element('span', {
var usageCount = Row.usages.length;
if (!Row.isValid) {
StatusElm.set('html', QUILocale.get(lg, 'controls.manager.tbl.status.invalid', {
usageCount: usageCount
}));
StatusElm.addClass('quiqqer-coupons-manager-tbl-status-invalid');
StatusElm.set('html', QUILocale.get(lg, 'controls.manager.tbl.status.valid', {
usageCount: usageCount
StatusElm.addClass('quiqqer-coupons-manager-tbl-status-used');
}
Row.status = StatusElm;
if (!Row.validUntilDate) {
Row.validUntilDateText = textUnlimited;
} else {
Row.validUntilDateText = Row.validUntilDate;
Row.maxUsageLabel = QUILocale.get(lg, 'controls.manager.tbl.maxUsageLabel.' + Row.maxUsages);
* Create new CouponCode or edit an existing one
* @param {Object} [CouponData] - If omitted create new CouponCode
var FuncSubmit = () => {
if (!Form.reportValidity()) {
return;
}
CouponCodes.edit(CouponData.id, QUIFormUtils.getFormData(Form)).then((couponCodeId) => {
if (!couponCodeId) {
Popup.Loader.hide();
return;
}
Popup.close();
});
return;
}
CouponCodes.create(QUIFormUtils.getFormData(Form)).then((couponCodeId) => {
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
require([
'package/quiqqer/translator/bin/classes/Translator'
], (Translator) => {
new Translator().publish(lg).then(() => {
this.refresh();
Popup.close();
}).then(() => {
this.refresh();
Popup.close();
});
});
}).catch(() => {
Popup.Loader.hide();
});
};
var openDiscount = () => {
Popup.Loader.show();
require([
'package/quiqqer/discount/bin/controls/Discounts',
'utils/Panels'
], function (DiscountsManagerPanel, PanelUtils) {
const Panel = new DiscountsManagerPanel();
PanelUtils.openPanelInTasks(Panel).then((PanelOpened) => {
PanelOpened.editChild(parseInt(DiscountSelect.getValue()));
Popup.close();
});
});
};
// open popup
var lgPrefix = 'controls.manager.create.template.';
var Popup = new QUIPopup({
icon : 'fa fa-plus',
title : CouponData ?
QUILocale.get(lg, 'controls.manager.details.popup.title_edit', {code: CouponData.code}) :
QUILocale.get(lg, 'controls.manager.details.popup.title_new'),
Form.addEvent('submit', function (event) {
event.stop();
FuncSubmit();
});
const Code = Content.getElement('input[name="code"]');
Code.addEvent('keyup', function () {
if (Code.value !== '') {
Amount.disabled = true;
} else {
Amount.disabled = false;
}
});
if (CouponData) {
CouponData.discountIds = CouponData.discountIds.join(',');
QUIFormUtils.setDataToForm(CouponData, Form);
EditDiscountBtn = new QUIButton({
'class' : 'optional',
textimage: 'fa fa-percent',
text : QUILocale.get(lg, 'controls.manager.details.popup.btn.open_discount'),
title : QUILocale.get(lg, 'controls.manager.details.popup.btn.open_discount'),
events : {
onClick: openDiscount
},
styles : {
float: 'right'
}
}).inject(
Content.getElement('.quiqqer-coupons-manager-create-advanced-discount-buttons')
);
QUI.parse(Content).then(() => {
Win.Loader.hide();
if (!CouponData) {
return;
}
DiscountSelect = QUI.Controls.getById(
Content.getElement('input[name="discountIds"]').get('data-quiid')
);
DiscountSelect.addEvents({
onAddItem : () => {
EditDiscountBtn.enable();
},
onRemoveItem: () => {
EditDiscountBtn.disable();
});
}
},
closeButton: true,
content : Mustache.render(templateCreate, {
labelTitle : QUILocale.get(lg, lgPrefix + 'labelTitle'),
labelCode : QUILocale.get(lg, lgPrefix + 'labelCode'),
labelUsers : QUILocale.get(lg, lgPrefix + 'labelUsers'),
labelGroups : QUILocale.get(lg, lgPrefix + 'labelGroups'),
labelDate : QUILocale.get(lg, lgPrefix + 'labelDate'),
labelAmount : QUILocale.get(lg, lgPrefix + 'labelAmount'),
labelReusable : QUILocale.get(lg, lgPrefix + 'labelReusable'),
labelReusableOncePerUser : QUILocale.get(lg, lgPrefix + 'labelReusableOncePerUser'),
labelReusableOnce : QUILocale.get(lg, lgPrefix + 'labelReusableOnce'),
labelReusableUnlimited : QUILocale.get(lg, lgPrefix + 'labelReusableUnlimited'),
labelDiscount : QUILocale.get(lg, lgPrefix + 'labelDiscount'),
headerBasics : QUILocale.get(lg, lgPrefix + 'headerBasics'),
headerDiscount : QUILocale.get(lg, lgPrefix + 'headerDiscount'),
headerRestrictions : QUILocale.get(lg, lgPrefix + 'headerRestrictions'),
descCode : QUILocale.get(lg, lgPrefix + 'descCode'),
descTitle : QUILocale.get(lg, lgPrefix + 'descTitle'),
labelDiscountAmount : QUILocale.get(lg, lgPrefix + 'labelDiscountAmount'),
labelDiscountType : QUILocale.get(lg, lgPrefix + 'labelDiscountType'),
labelDiscountTypeFlat : QUILocale.get(lg, lgPrefix + 'labelDiscountTypeFlat'),
labelDiscountTypePercentage: QUILocale.get(lg, lgPrefix + 'labelDiscountTypePercentage'),
isCreate: !CouponData
})
});
Popup.open();
Popup.addButton(new QUIButton({
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_text_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_text_new'),
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_new'),
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_edit') :
QUILocale.get(lg, 'controls.manager.details.popup.btn.confirm_new'),
onClick: FuncSubmit
}
}));
},
/**
* Remove all selected licenses
*/
$delete: function () {
var deleteIds = [];
var rows = this.$Grid.getSelectedData();
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
for (var i = 0, len = rows.length; i < len; i++) {
deleteData.push(
rows[i].title + ' (ID: #' + rows[i].id + ')'
);
deleteIds.push(rows[i].id);
}
// open popup
var Popup = new QUIConfirm({
'maxHeight': 300,
'autoclose': false,
'information': QUILocale.get(
lg,
'controls.manager.delete.popup.info', {
codes: deleteData.join('<br/>')
}
),
'title' : QUILocale.get(lg, 'controls.manager.delete.popup.title'),
'texticon' : 'fa fa-trash',
text : QUILocale.get(lg, 'controls.manager.delete.popup.title'),
'icon' : 'fa fa-trash',
cancel_button: {
text : false,
textimage: 'icon-remove fa fa-remove'
},
ok_button : {
text : false,
textimage: 'icon-ok fa fa-check'
},
events : {
onSubmit: function () {
Popup.Loader.show();
CouponCodes.delete(deleteIds).then(function (success) {
if (!success) {
Popup.Loader.hide();
return;
}
Popup.close();
self.refresh();
});
}
}
});
Popup.open();
},
/**
* Open user panel
*
$showUsages: function (RowData) {
new QUIConfirm({
maxHeight: 550,
autoclose: false,
title: QUILocale.get(lg, 'controls.Manager.usages.title', {code: RowData.code}),
icon : 'fa fa-user',
cancel_button: false,
ok_button : {
text : QUILocale.get(lg, 'controls.Manager.usages.btn_ok'),
onSubmit: function (Popup) {
Popup.close();
},
onOpen : function (Popup) {
var lgPrefix = 'controls.Manager.usages.template.';
Popup.setContent(Mustache.render(templateUsages, {
headerUser : QUILocale.get(lg, lgPrefix + 'headerUser'),
headerDate : QUILocale.get(lg, lgPrefix + 'headerDate'),
headerOrderPrefixedId: QUILocale.get(lg, lgPrefix + 'headerOrderPrefixedId')
var TableBody = Content.getElement('tbody');
for (var i = 0, len = RowData.usages.length; i < len; i++) {
var usage = RowData.usages[i];
var Row = new Element('tr', {
'<td>' + usage.userId + ' (' + usage.userName + ')</td>'
new Element('td', {
html: usage.orderPrefixedId ? usage.orderPrefixedId : '-'
}).inject(Row);