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

216 lines
4.5 KiB
JavaScript

import './style.scss';
export default new Class({
Extends: Hedera.Form,
Template: require('./ui.xml'),
activate() {
this.autoStepLocked = true;
this.$.assistant.stepsIndex = this.agencySteps;
this.today = new Date();
this.today.setHours(0, 0, 0, 0);
},
onValuesReady() {
const orderForm = this.$.orderForm;
const defaultsForm = this.$.defaults;
if (!(orderForm.ready && defaultsForm.ready))
return;
let date;
const row = orderForm.$ || defaultsForm.$ || {};
if (!date || date.getTime() < (new Date()).getTime()) {
date = new Date();
date.setHours(0, 0, 0, 0);
let addDays = 0;
switch(date.getDay()) {
case 6: // Saturday
addDays += 2;
break;
case 0: // Sunday
addDays++;
break;
}
if (row.deliveryMethod != 'PICKUP')
addDays++;
date.setDate(date.getDate() + addDays);
}
this.$.lot.assign({
date: date,
method: row.deliveryMethod,
agency: row.agencyModeFk,
address: row.addressFk
});
this.autoStepLocked = false;
},
disableButtons(disable) {
this.$.assistantBar.disabled = disable;
},
onConfirmClick() {
this.disableButtons(true);
const query = 'CALL myBasket_configure(#date, #method, #agency, #address)';
this.conn.execQuery(query,
this.onBasketConfigured.bind(this), this.$.lot.$);
},
onBasketConfigured(resultSet) {
this.disableButtons(false);
if (!resultSet.fetchResult())
return;
if (this.$.orderForm.numRows > 0)
Htk.Toast.showMessage(_('OrderUpdated'));
else
Htk.Toast.showMessage(_('OrderStarted'));
this.hash.setAll({form: 'ecomerce/catalog'});
},
onCancelClick() {
if (this.$.orderForm.numRows > 0)
window.history.back();
else
this.hash.setAll({form: 'ecomerce/orders'});
},
agencySteps: ['method', 'date', 'address', 'agency', 'confirm-delivery'],
pickupSteps: ['method', 'date', 'address', 'pickup', 'confirm-pickup'],
isDelivery() {
return this.$.rgMethod.value != 'PICKUP';
},
onMethodChange() {
this.$.assistant.stepsIndex = this.isDelivery() ?
this.agencySteps : this.pickupSteps;
this.onFieldChange();
},
methodValidate() {
if (!this.$.rgMethod.isSelected())
throw new Error(_('Please select an option'));
},
dateShow() {
Vn.Node.setText(this.$.dateQuestion, this.isDelivery() ?
_('OrderDateDeliveryQuestion'):
_('OrderDatePickupQuestion'));
this.$.calendar.goToSelectedMonth();
},
dateValidate() {
if (!this.$.calendar.value)
throw new Error(_('Please select a date'));
},
addressShow() {
Vn.Node.setText(this.$.addressQuestion, this.isDelivery() ?
_('AddressQuestion'):
_('AddressQuestionPickup'));
},
addressValidate() {
if (this.$.addressForm.row == -1)
throw new Error(_('Please select an address'));
},
agencyShow() {
this.$.agencies.refresh();
},
agencyValidate() {
if (this.$.agencyCombo.row == -1 && this.isDelivery())
throw new Error(_('Please select an agency'));
},
pickupShow() {
this.$.warehouses.refresh();
},
pickupValidate() {
if (this.$.warehouseCombo.row == -1)
throw new Error(_('Please select a store'));
},
onFieldChange() {
if (!this.autoStepLocked)
this.$.assistant.moveNext();
},
goNextStep() {
this.$.assistant.moveNext();
},
onAddressClick(addressId) {
this.$.lot.set('address', addressId);
this.goNextStep();
},
onAddressChange() {
if (this.selectedNode)
Vn.Node.removeClass(this.selectedNode, 'selected');
const row = this.$.addresses.search('id', this.$.lot.$.address);
if (row != -1) {
const builder = this.$.repeater.getBuilder(row);
this.selectedNode = builder.$.address;
Vn.Node.addClass(this.selectedNode, 'selected');
}
this.$.addressForm.row = row;
},
onAgenciesReady(model) {
if (!model.ready) return;
if (model.numRows > 0) {
let agency;
const agencies = [];
if (this.$.orderForm.$)
agencies.push(this.$.orderForm.$);
const defaults = this.$.defaults.$;
if (defaults)
agencies.push(
defaults.agencyModeFk,
defaults.defaultAgencyFk
);
for (let i = 0; i < agencies.length; i++) {
agency = agencies[i];
if (model.search('id', agency) !== -1)
break;
}
this.autoStepLocked = true;
this.$.lot.assign({agency});
this.autoStepLocked = false;
} else
Htk.Toast.showError(_('NoAgeciesAvailableForDate'));
},
onWarehousesReady(model) {
if (model.ready && model.numRows == 0)
Htk.Toast.showError(_('NoWarehousesAvailableForDate'));
},
calendarRestrict(date) {
return date.getTime() >= this.today.getTime();
}
});