hedera-web/forms/ecomerce/checkout/index.js

237 lines
5.0 KiB
JavaScript
Raw Normal View History

2022-11-16 01:44:39 +00:00
import './style.scss';
2015-02-01 03:21:54 +00:00
2022-11-16 01:44:39 +00:00
export default new Class({
Extends: Hedera.Form,
2022-11-16 01:44:39 +00:00
Template: require('./ui.xml'),
2015-02-01 03:21:54 +00:00
2022-11-16 01:46:44 +00:00
activate() {
2015-07-10 12:30:08 +00:00
this.autoStepLocked = true;
2022-05-28 01:18:06 +00:00
this.$.assistant.stepsIndex = this.agencySteps;
2015-07-23 15:58:48 +00:00
this.today = Date.vnNew();
this.today.setHours(0, 0, 0, 0);
},
2015-07-15 13:39:07 +00:00
2022-11-16 01:46:44 +00:00
onValuesReady() {
2022-07-15 05:55:18 +00:00
const orderForm = this.$.orderForm;
const defaultsForm = this.$.defaults;
2015-07-10 12:30:08 +00:00
2015-07-15 13:39:07 +00:00
if (!(orderForm.ready && defaultsForm.ready))
return;
2022-07-15 05:55:18 +00:00
let date;
const row = orderForm.$ || defaultsForm.$ || {};
if (!date || date.getTime() < (Date.vnNew()).getTime()) {
date = Date.vnNew();
2020-05-04 19:55:18 +00:00
date.setHours(0, 0, 0, 0);
2022-07-15 05:55:18 +00:00
let addDays = 0;
2018-09-14 13:13:28 +00:00
switch(date.getDay()) {
case 6: // Saturday
addDays += 2;
break;
case 0: // Sunday
addDays++;
break;
}
2022-05-28 15:49:46 +00:00
if (row.deliveryMethod != 'PICKUP')
2018-09-14 13:13:28 +00:00
addDays++;
2020-05-04 19:55:18 +00:00
date.setDate(date.getDate() + addDays);
2015-07-10 12:30:08 +00:00
}
2015-07-15 13:39:07 +00:00
2022-05-30 01:30:33 +00:00
this.$.lot.assign({
2023-01-30 10:08:26 +00:00
date,
2022-05-30 01:30:33 +00:00
method: row.deliveryMethod,
agency: row.agencyModeFk,
address: row.addressFk
});
2015-07-15 13:39:07 +00:00
this.autoStepLocked = false;
},
2015-12-10 13:48:43 +00:00
2022-11-16 01:46:44 +00:00
disableButtons(disable) {
2022-05-28 01:18:06 +00:00
this.$.assistantBar.disabled = disable;
},
2015-07-15 13:39:07 +00:00
2022-11-28 08:51:31 +00:00
async onConfirmClick() {
this.disableButtons(true);
2015-12-10 13:48:43 +00:00
2023-02-15 13:07:09 +00:00
let id = this.$.params.$.id;
2023-01-30 10:08:26 +00:00
const params = Object.assign({}, this.$.lot.$);
let query;
if (id) {
params.id = id;
query = 'CALL myOrder_configure(#id, #date, #method, #agency, #address)';
} else {
2023-02-15 13:07:09 +00:00
query = 'CALL myOrder_create(@orderId, #date, #method, #agency, #address); SELECT @orderId;';
2023-01-30 10:08:26 +00:00
}
2022-11-28 08:51:31 +00:00
let resultSet;
try {
2023-01-30 10:08:26 +00:00
resultSet = await this.conn.execQuery(query, params);
2022-11-28 08:51:31 +00:00
} finally {
this.disableButtons(false);
}
2015-12-10 13:48:43 +00:00
if (!resultSet.fetchResult())
2015-07-15 13:39:07 +00:00
return;
let redirect;
const basket = new Hedera.Basket(this.app);
2023-02-15 13:07:09 +00:00
if (id) {
Htk.Toast.showMessage(_('OrderUpdated'));
2023-02-15 13:07:09 +00:00
switch(this.hash.$.continue) {
case 'catalog':
redirect = {form: 'ecomerce/catalog'};
2023-02-15 13:07:09 +00:00
break;
default:
redirect = {form: 'ecomerce/basket'};
if (id !== basket.orderId)
redirect.id = id;
2023-02-15 13:07:09 +00:00
}
} else {
basket.loadIntoBasket(resultSet.fetchValue());
redirect = {form: 'ecomerce/catalog'};
2023-02-15 13:07:09 +00:00
}
this.hash.setAll(redirect);
},
2015-07-10 12:30:08 +00:00
2022-11-16 01:46:44 +00:00
onCancelClick() {
2023-01-30 10:08:26 +00:00
if (this.$.params.$.id)
window.history.back();
2015-07-15 13:39:07 +00:00
else
2022-05-30 01:30:33 +00:00
this.hash.setAll({form: 'ecomerce/orders'});
},
agencySteps: ['method', 'date', 'address', 'agency', 'confirm-delivery'],
pickupSteps: ['method', 'date', 'address', 'pickup', 'confirm-pickup'],
2022-11-16 01:46:44 +00:00
isDelivery() {
2022-05-28 01:18:06 +00:00
return this.$.rgMethod.value != 'PICKUP';
},
2022-11-16 01:46:44 +00:00
onMethodChange() {
2022-05-28 01:18:06 +00:00
this.$.assistant.stepsIndex = this.isDelivery() ?
this.agencySteps : this.pickupSteps;
this.onFieldChange();
},
2022-11-16 01:46:44 +00:00
methodValidate() {
2022-05-28 01:18:06 +00:00
if (!this.$.rgMethod.isSelected())
throw new Error(_('Please select an option'));
},
2022-11-16 01:46:44 +00:00
dateShow() {
2022-05-28 01:18:06 +00:00
Vn.Node.setText(this.$.dateQuestion, this.isDelivery() ?
_('OrderDateDeliveryQuestion'):
_('OrderDatePickupQuestion'));
2022-05-28 01:18:06 +00:00
this.$.calendar.goToSelectedMonth();
},
2022-11-16 01:46:44 +00:00
dateValidate() {
2022-05-28 01:18:06 +00:00
if (!this.$.calendar.value)
throw new Error(_('Please select a date'));
},
2022-11-16 01:46:44 +00:00
addressShow() {
2022-05-28 01:18:06 +00:00
Vn.Node.setText(this.$.addressQuestion, this.isDelivery() ?
_('AddressQuestion'):
_('AddressQuestionPickup'));
},
2022-11-16 01:46:44 +00:00
addressValidate() {
2022-05-28 01:18:06 +00:00
if (this.$.addressForm.row == -1)
throw new Error(_('Please select an address'));
},
2022-11-16 01:46:44 +00:00
agencyShow() {
2022-05-28 01:18:06 +00:00
this.$.agencies.refresh();
},
2022-11-16 01:46:44 +00:00
agencyValidate() {
2022-05-28 01:18:06 +00:00
if (this.$.agencyCombo.row == -1 && this.isDelivery())
throw new Error(_('Please select an agency'));
},
2022-11-16 01:46:44 +00:00
pickupShow() {
2022-05-28 01:18:06 +00:00
this.$.warehouses.refresh();
},
2022-11-16 01:46:44 +00:00
pickupValidate() {
2022-05-28 01:18:06 +00:00
if (this.$.warehouseCombo.row == -1)
throw new Error(_('Please select a store'));
},
2022-11-16 01:46:44 +00:00
onFieldChange() {
2015-07-10 12:30:08 +00:00
if (!this.autoStepLocked)
2022-05-28 01:18:06 +00:00
this.$.assistant.moveNext();
},
2022-11-16 01:46:44 +00:00
onAddressClick(addressId) {
2022-05-30 01:30:33 +00:00
this.$.lot.set('address', addressId);
2022-12-09 11:51:51 +00:00
this.$.assistant.moveNext();
},
2022-11-16 01:46:44 +00:00
onAddressChange() {
2015-07-23 15:58:48 +00:00
if (this.selectedNode)
Vn.Node.removeClass(this.selectedNode, 'selected');
2015-07-23 15:58:48 +00:00
2022-07-15 05:55:18 +00:00
const row = this.$.addresses.search('id', this.$.lot.$.address);
2015-07-23 15:58:48 +00:00
if (row != -1) {
2022-07-15 05:55:18 +00:00
const builder = this.$.repeater.getBuilder(row);
2015-07-23 15:58:48 +00:00
2022-05-28 01:18:06 +00:00
this.selectedNode = builder.$.address;
Vn.Node.addClass(this.selectedNode, 'selected');
2015-07-23 15:58:48 +00:00
}
2022-05-28 01:18:06 +00:00
this.$.addressForm.row = row;
},
2015-07-15 13:39:07 +00:00
2022-11-16 01:46:44 +00:00
onAgenciesReady(model) {
2022-11-29 18:13:32 +00:00
this.selectAgency(model, 'NoAgeciesAvailableForDate');
},
onWarehousesReady(model) {
this.selectAgency(model, 'NoWarehousesAvailableForDate');
},
selectAgency(model, emptyMessage) {
2018-09-14 13:13:28 +00:00
if (!model.ready) return;
2022-11-29 18:13:32 +00:00
let agencyId = null;
2018-09-14 13:13:28 +00:00
if (model.numRows > 0) {
2022-11-29 18:13:32 +00:00
const agencies = [this.$.lot.$.agency];
const defaults = this.$.defaults.$ || {};
if (defaults.agencyModeFk)
2022-12-01 08:04:33 +00:00
agencies.push(defaults.agencyModeFk);
2022-11-29 18:13:32 +00:00
if (defaults.defaultAgencyFk)
2022-12-01 08:04:33 +00:00
agencies.push(defaults.defaultAgencyFk);
2022-11-29 18:13:32 +00:00
for (const agency of agencies)
if (model.search('id', agency) !== -1) {
agencyId = agency;
break;
2018-09-14 13:13:28 +00:00
}
} else
2022-11-29 18:13:32 +00:00
Htk.Toast.showError(_(emptyMessage));
this.autoStepLocked = true;
this.$.lot.assign({agency: agencyId});
this.autoStepLocked = false;
},
2015-08-17 18:02:14 +00:00
2022-11-16 01:46:44 +00:00
calendarRestrict(date) {
return date.getTime() >= this.today.getTime();
2015-07-15 13:39:07 +00:00
}
2015-02-01 03:21:54 +00:00
});