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

213 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-02-01 03:21:54 +00:00
2019-05-21 14:16:27 +00:00
Hedera.Checkout = new Class({
Extends: Hedera.Form,
2015-02-01 03:21:54 +00:00
activate: function() {
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 = new Date();
this.today.setHours(0, 0, 0, 0);
},
2015-07-15 13:39:07 +00:00
onValuesReady: function() {
2022-05-28 01:18:06 +00:00
var orderForm = this.$.orderForm;
var 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;
var date;
2022-05-28 15:49:46 +00:00
const row = orderForm.$ || defaultsForm.$;
2018-09-14 13:13:28 +00:00
if (!date || date.getTime() < (new Date()).getTime()) {
date = new Date();
2020-05-04 19:55:18 +00:00
date.setHours(0, 0, 0, 0);
2018-09-14 13:13:28 +00:00
var addDays = 0;
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({
date: date,
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
disableButtons: function(disable) {
2022-05-28 01:18:06 +00:00
this.$.assistantBar.disabled = disable;
},
2015-07-15 13:39:07 +00:00
onConfirmClick: function() {
this.disableButtons(true);
2015-12-10 13:48:43 +00:00
2019-05-21 14:16:27 +00:00
var query = 'CALL myBasket_configure(#date, #method, #agency, #address)';
this.conn.execQuery(query,
2022-05-30 01:30:33 +00:00
this.onBasketConfigured.bind(this), this.$.lot.$);
},
2015-07-10 12:30:08 +00:00
onBasketConfigured: function(resultSet) {
this.disableButtons(false);
2015-12-10 13:48:43 +00:00
if (!resultSet.fetchResult())
2015-07-15 13:39:07 +00:00
return;
2022-05-28 01:18:06 +00:00
if (this.$.orderForm.numRows > 0)
Htk.Toast.showMessage(_('OrderUpdated'));
2015-07-15 13:39:07 +00:00
else
Htk.Toast.showMessage(_('OrderStarted'));
2022-05-30 01:30:33 +00:00
this.hash.setAll({form: 'ecomerce/catalog'});
},
2015-07-10 12:30:08 +00:00
onCancelClick: function() {
2022-05-28 01:18:06 +00:00
if (this.$.orderForm.numRows > 0)
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'],
isDelivery: function() {
2022-05-28 01:18:06 +00:00
return this.$.rgMethod.value != 'PICKUP';
},
onMethodChange: function() {
2022-05-28 01:18:06 +00:00
this.$.assistant.stepsIndex = this.isDelivery() ?
this.agencySteps : this.pickupSteps;
this.onFieldChange();
},
methodValidate: function() {
2022-05-28 01:18:06 +00:00
if (!this.$.rgMethod.isSelected())
throw new Error(_('Please select an option'));
},
dateShow: function() {
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();
},
dateValidate: function() {
2022-05-28 01:18:06 +00:00
if (!this.$.calendar.value)
throw new Error(_('Please select a date'));
},
addressShow: function() {
2022-05-28 01:18:06 +00:00
Vn.Node.setText(this.$.addressQuestion, this.isDelivery() ?
_('AddressQuestion'):
_('AddressQuestionPickup'));
},
addressValidate: function() {
2022-05-28 01:18:06 +00:00
if (this.$.addressForm.row == -1)
throw new Error(_('Please select an address'));
},
agencyShow: function() {
2022-05-28 01:18:06 +00:00
this.$.agencies.refresh();
},
agencyValidate: function() {
2022-05-28 01:18:06 +00:00
if (this.$.agencyCombo.row == -1 && this.isDelivery())
throw new Error(_('Please select an agency'));
},
pickupShow: function() {
2022-05-28 01:18:06 +00:00
this.$.warehouses.refresh();
},
pickupValidate: function() {
2022-05-28 01:18:06 +00:00
if (this.$.warehouseCombo.row == -1)
throw new Error(_('Please select a store'));
},
onFieldChange: function() {
2015-07-10 12:30:08 +00:00
if (!this.autoStepLocked)
2022-05-28 01:18:06 +00:00
this.$.assistant.moveNext();
},
goNextStep: function() {
2022-05-28 01:18:06 +00:00
this.$.assistant.moveNext();
},
2015-07-23 15:58:48 +00:00
addressRenderer: function(builder, form) {
2022-05-28 01:18:06 +00:00
builder.$.address.addEventListener('click',
2022-05-28 15:49:46 +00:00
this.onAddressClick.bind(this, form.$.id));
},
onAddressClick: function(addressId) {
2022-05-30 01:30:33 +00:00
this.$.lot.set('address', addressId);
this.goNextStep();
},
onAddressChange: function() {
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-05-30 01:30:33 +00:00
var row = this.$.addresses.search('id', this.$.lot.$.address);
2015-07-23 15:58:48 +00:00
if (row != -1) {
2022-05-28 01:18:06 +00:00
var 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
onAgenciesReady: function(model) {
2018-09-14 13:13:28 +00:00
if (!model.ready) return;
if (model.numRows > 0) {
var agency;
var defaults = [
2022-05-28 15:49:46 +00:00
this.$.orderForm.$.agencyModeFk,
this.$.defaults.$.agencyModeFk,
this.$.defaults.$.defaultAgencyFk
2018-09-14 13:13:28 +00:00
];
for (var i = 0; i < defaults.length; i++) {
agency = defaults[i];
if (model.search('id', agency) !== -1)
break;
}
this.autoStepLocked = true;
2022-05-28 01:18:06 +00:00
this.$.agency.value = agency;
2018-09-14 13:13:28 +00:00
this.autoStepLocked = false;
} else
Htk.Toast.showError(_('NoAgeciesAvailableForDate'));
},
2015-08-17 18:02:14 +00:00
onWarehousesReady: function(model) {
2015-08-17 18:02:14 +00:00
if (model.ready && model.numRows == 0)
Htk.Toast.showError(_('NoWarehousesAvailableForDate'));
},
2015-08-17 18:02:14 +00:00
calendarRestrict: function(date) {
return date.getTime() >= this.today.getTime();
2015-07-15 13:39:07 +00:00
}
2015-02-01 03:21:54 +00:00
});