Steps To Reproduce | PreorderSave hook is used to validate the data of the lines. Imagine, when selling 'gift cards and vouchers' before saving and creating the instance of the GC/Voucher, it is nice to have a validation to check if the ID is in used for another GC/Voucher or not.
In the case of the ID is in used for another GC/Voucher, the synchronization should be stopped, the user should change the ID of the voucher and user should be able to continue with the synchronization. But this behaviour is not working correctly.
Steps to reproduce:
Install 'gift cards and vouchers' module, and apply the patch is attached to the issue.
Login as 'The White Valley Group Admin'.
Go to Products and select 'Alpine skiing backpack 27 L'. Open it in form view and modify the following information:
- gift Card Type: Gift voucher
- Grouped Product: No
Login in the webPOs and create a new order. Add 'Alpine skiing backpack 27 L' to the order. In the popup write in ID field: 1
Pay and complete the order.
Now, open the develpers tool and uncomment the following code:
// gcids.push({
// gcid: '1'
// });
The previous code is adding the code 1 to the list of ids to check in the backend if it is already in use. Since we create a voucher with id 1, now that the code is uncommented, the preOrderSave won't do the synchronization with the backend.
Create a new order. Add 'Alpine skiing backpack 27 L' and in the ID write something different to 1 (in the previous code is already writing 1). Pay it and see that an error is shown because the id 1 is already in use.
Notice that 'Done' button is disabled (this could be another issue).
Then, click again in the line and notice that 'done' button is enabled again. Click in 'Done' button and notice that the price of 'Alpine skiing backpack 27 L', the tax amount, the total amount... all of them are 0. Using this hook, we should be able to fix the data (in this case the ID of the voucher) and be able to process the order, but there is a bug that is not allowing to continue with the process.
|
Attached Files | PreOrderSaveHookError.diff [^] (10,402 bytes) 2014-10-17 14:33 [Show Content] [Hide Content]diff --git a/src-db/database/sourcedata/AD_MESSAGE.xml b/src-db/database/sourcedata/AD_MESSAGE.xml
--- a/src-db/database/sourcedata/AD_MESSAGE.xml
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml
@@ -324,6 +324,18 @@
<!--7077C6064CD3434183BC8437E70FA2C5--> <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
<!--7077C6064CD3434183BC8437E70FA2C5--></AD_MESSAGE>
+<!--791F13D756954D7AA3A32B765DFB5462--><AD_MESSAGE>
+<!--791F13D756954D7AA3A32B765DFB5462--> <AD_MESSAGE_ID><![CDATA[791F13D756954D7AA3A32B765DFB5462]]></AD_MESSAGE_ID>
+<!--791F13D756954D7AA3A32B765DFB5462--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--791F13D756954D7AA3A32B765DFB5462--> <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--791F13D756954D7AA3A32B765DFB5462--> <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--791F13D756954D7AA3A32B765DFB5462--> <VALUE><![CDATA[GCNV_DuplicatedID]]></VALUE>
+<!--791F13D756954D7AA3A32B765DFB5462--> <MSGTEXT><![CDATA[Duplicated ID. Please, insert a new ID for the gift card/voucher]]></MSGTEXT>
+<!--791F13D756954D7AA3A32B765DFB5462--> <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--791F13D756954D7AA3A32B765DFB5462--> <AD_MODULE_ID><![CDATA[17B5337C4B1948DF857B12FD3ABE5094]]></AD_MODULE_ID>
+<!--791F13D756954D7AA3A32B765DFB5462--> <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--791F13D756954D7AA3A32B765DFB5462--></AD_MESSAGE>
+
<!--7DAE3726F4424AF68C881BF3579AC88D--><AD_MESSAGE>
<!--7DAE3726F4424AF68C881BF3579AC88D--> <AD_MESSAGE_ID><![CDATA[7DAE3726F4424AF68C881BF3579AC88D]]></AD_MESSAGE_ID>
<!--7DAE3726F4424AF68C881BF3579AC88D--> <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff --git a/src/org/openbravo/retail/giftcards/CheckDuplicityOfID.java b/src/org/openbravo/retail/giftcards/CheckDuplicityOfID.java
new file mode 100644
--- /dev/null
+++ b/src/org/openbravo/retail/giftcards/CheckDuplicityOfID.java
@@ -0,0 +1,74 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2014 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+
+package org.openbravo.retail.giftcards;
+
+import java.util.ArrayList;
+
+import javax.servlet.ServletException;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.hibernate.criterion.Restrictions;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.retail.giftcards.org.openbravo.retail.giftcards.GiftCardInst;
+import org.openbravo.retail.posterminal.JSONProcessSimple;
+
+public class CheckDuplicityOfID extends JSONProcessSimple {
+ @Override
+ public JSONObject exec(JSONObject jsonData) throws JSONException, ServletException {
+ String strGCId;
+ JSONArray gcids = null;
+ OBContext.setAdminMode(true);
+ JSONObject result = new JSONObject();
+ int status = 0;
+ ArrayList<Object> objIds = new ArrayList<Object>();
+ OBContext.setAdminMode();
+ try {
+ strGCId = jsonData.getString("gcid");
+ if ("null".equals(strGCId)) {
+ gcids = jsonData.getJSONArray("gcids");
+ }
+ OBCriteria<GiftCardInst> obc = OBDal.getInstance().createCriteria(GiftCardInst.class);
+ if (!"null".equals(strGCId)) {
+ obc.add(Restrictions.eq(GiftCardInst.PROPERTY_SEARCHKEY, strGCId));
+ } else {
+ for (int i = 0; i < gcids.length(); i++) {
+ objIds.add(gcids.getJSONObject(i).getString("gcid"));
+ }
+ obc.add(Restrictions.in(GiftCardInst.PROPERTY_SEARCHKEY, objIds));
+ }
+
+ obc.setMaxResults(1);
+ if (obc.uniqueResult() == null) {
+ result.put("used", false);
+ } else {
+ GiftCardInst gcins = (GiftCardInst) obc.uniqueResult();
+ result.put("used", true);
+ result.put("id", gcins.getSearchKey());
+ result.put("cancellation", true);
+ }
+
+ } catch (Exception e) {
+ throw new OBException();
+ } finally {
+ OBContext.restorePreviousMode();
+ }
+
+ JSONObject finalResult = new JSONObject();
+ finalResult.put("data", result);
+ finalResult.put("status", status);
+ return finalResult;
+ }
+
+}
\ No newline at end of file
diff --git a/src/org/openbravo/retail/giftcards/GCNVComponentProvider.java b/src/org/openbravo/retail/giftcards/GCNVComponentProvider.java
--- a/src/org/openbravo/retail/giftcards/GCNVComponentProvider.java
+++ b/src/org/openbravo/retail/giftcards/GCNVComponentProvider.java
@@ -63,6 +63,7 @@
grhelper.add("components/GiftCardMessage.js");
grhelper.add("components/GiftCardSearchDialog.js");
grhelper.add("components/GiftCardMenu.js");
+ grhelper.add("preaddpaymenthook.js");
return grhelper.getGlobalResources();
}
diff --git a/web/org.openbravo.retail.giftcards/js/components/lineproperties.js b/web/org.openbravo.retail.giftcards/js/components/lineproperties.js
--- a/web/org.openbravo.retail.giftcards/js/components/lineproperties.js
+++ b/web/org.openbravo.retail.giftcards/js/components/lineproperties.js
@@ -1,13 +1,13 @@
/*
************************************************************************************
- * Copyright (C) 2012 Openbravo S.L.U.
+ * Copyright (C) 2012-2014 Openbravo S.L.U.
* Licensed under the Openbravo Commercial License version 1.0
* You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
* or in the legal folder of this module distribution.
************************************************************************************
*/
-/*global enyo, Backbone, $ */
+/*global enyo, Backbone, $, _ */
(function () {
@@ -33,23 +33,12 @@
modelProperty: 'giftcardid',
i18nLabel: 'GCNV_LblGiftCardID',
applyValue: function (orderline) {
-
- if (this.getValue()) {
- this.doSetLineProperty({
- line: orderline,
- property: this.modelProperty,
- value: this.getValue()
- });
- return true;
- } else {
- this.propertiesDialog.owner.doShowPopup({
- popup: 'GCNV_UI_Message',
- args: {
- message: OB.I18N.getLabel('GCNV_LblGiftCardInvalid')
- }
- });
- return false;
- }
+ this.doSetLineProperty({
+ line: orderline,
+ property: this.modelProperty,
+ value: this.getValue()
+ });
+ return true;
},
showProperty: function (orderline, callback) {
// Show the property only for giftcards...
@@ -98,4 +87,40 @@
});
}
+
+ var origTap = OB.UI.ReceiptPropertiesDialogApply.prototype.tap;
+ OB.UI.ReceiptPropertiesDialogApply.prototype.tap = _.wrap(OB.UI.ReceiptPropertiesDialogApply.prototype.tap, function (wrapped) {
+
+ var tap = _.bind(origTap, this),
+ serverCallCheckDuplicity = new OB.DS.Process('org.openbravo.retail.giftcards.CheckDuplicityOfID'),
+ giftid = this.owner.owner.propertycomponents.giftcardid;
+ if (giftid.getValue()) {
+ return serverCallCheckDuplicity.exec({
+ gcid: giftid.getValue()
+ }, function (data) {
+ if (data && !data.exception) {
+ if (data.used) { // if gc ID is in used by another gc
+ giftid.propertiesDialog.owner.doShowPopup({
+ popup: 'GCNV_UI_Message',
+ args: {
+ message: OB.I18N.getLabel('GCNV_DuplicatedID')
+ }
+ });
+ return false;
+ } else {
+ tap();
+ }
+ }
+ });
+ } else {
+ giftid.propertiesDialog.owner.doShowPopup({
+ popup: 'GCNV_UI_Message',
+ args: {
+ message: OB.I18N.getLabel('GCNV_LblGiftCardInvalid')
+ }
+ });
+ return false;
+ }
+
+ });
}());
\ No newline at end of file
diff --git a/web/org.openbravo.retail.giftcards/js/preaddpaymenthook.js b/web/org.openbravo.retail.giftcards/js/preaddpaymenthook.js
new file mode 100644
--- /dev/null
+++ b/web/org.openbravo.retail.giftcards/js/preaddpaymenthook.js
@@ -0,0 +1,57 @@
+/*
+ ************************************************************************************
+ * Copyright (C) 2014 Openbravo S.L.U.
+ * Licensed under the Openbravo Commercial License version 1.0
+ * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
+ * or in the legal folder of this module distribution.
+ ************************************************************************************
+ */
+
+
+(function () {
+ //OB.MobileApp.model.hookManager.registerHook('OBPOS_preAddPayment', function (args, callbacks) {
+ OB.MobileApp.model.hookManager.registerHook('OBPOS_PreOrderSave', function (args, callbacks) {
+ var order = args.receipt,
+ i, gcids = [],
+ serverCallCheckDuplicity = new OB.DS.Process('org.openbravo.retail.giftcards.CheckDuplicityOfID');
+ for (i = 0; i < order.get('lines').length; i++) {
+ if (order.get('lines').at(i).get('product').get('giftCardType')) {
+ if (order.get('lines').at(i).get('giftcardid') === "") {
+ OB.UTIL.showError(OB.I18N.getLabel('GCNV_LblGiftCardInvalid'));
+ gcids = [];
+ break;
+ } else {
+ gcids.push({
+ gcid: order.get('lines').at(i).get('giftcardid')
+ });
+
+ }
+ }
+
+ }
+// gcids.push({
+// gcid: '1'
+// });
+ if (gcids.length > 0) {
+ serverCallCheckDuplicity.exec({
+ gcid: null,
+ gcids: gcids
+ }, function (data) {
+ if (data && !data.exception) {
+ if (data.used) { // if gc ID is in used by another gc
+ OB.UTIL.showError(data.id + ' ' + OB.I18N.getLabel('GCNV_DuplicatedID'));
+ args.cancellation = true;
+ OB.MobileApp.model.hookManager.callbackExecutor(args, callbacks);
+ } else {
+ OB.MobileApp.model.hookManager.callbackExecutor(args, callbacks);
+ }
+ }
+ });
+ } else {
+ OB.MobileApp.model.hookManager.callbackExecutor(args, callbacks);
+ }
+ return;
+
+ });
+
+}());
\ No newline at end of file
|