# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1514905721 -19800
#      Tue Jan 02 20:38:41 2018 +0530
# Node ID d4dd4866e75c1eb73dd0d5f9354a3d21f58b497d
# Parent  e64391313a875ee8d50e6ed1a82ffe96d2b94dff
Verifies issue 37436 : Added automated test 'I37436_VerifyReceiptOnPreSaveHook'

diff -r e64391313a87 -r d4dd4866e75c src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/receipts/I37436_VerifyReceiptOnPreSaveHook.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/receipts/I37436_VerifyReceiptOnPreSaveHook.java	Tue Jan 02 20:38:41 2018 +0530
@@ -0,0 +1,108 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo S.L.U.
+ * All portions are Copyright (C) 2017 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author MCA
+ *
+ */
+
+package org.openbravo.test.mobile.retail.pack.selenium.tests.receipts;
+
+import java.math.BigDecimal;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.core.selenium.SeleniumHelper;
+import org.openbravo.test.mobile.core.utils.OBNumber;
+import org.openbravo.test.mobile.retail.mobilecore.javascript.BackboneHelper;
+import org.openbravo.test.mobile.retail.pack.database.WebPOSDatabaseHelper;
+import org.openbravo.test.mobile.retail.pack.selenium.TestIdPack;
+import org.openbravo.test.mobile.retail.pack.selenium.terminals.WebPOSTerminalHelper;
+
+public class I37436_VerifyReceiptOnPreSaveHook extends WebPOSTerminalHelper {
+
+  @Test
+  public void test() {
+    SeleniumHelper.executeScript("OB.FAILCONDITION = true; "
+        + "OB.UTIL.HookManager.registerHook('OBPOS_PreOrderSave', "
+        + " function(args, callback) { if (OB.FAILCONDITION) { " + " args.cancellation = true; } "
+        + "OB.UTIL.HookManager.callbackExecutor(args,callback); });");
+    SeleniumHelper
+        .executeScript("OB.MobileApp.model.attributes.permissions['OBMOBC_SynchronizedMode'] = true;");
+
+    final String organizationId = BackboneHelper.getTerminalValue("organization");
+    final String clientId = BackboneHelper.getTerminalValue("client");
+    final String cashupId = BackboneHelper.getTerminalValue("cashUpId");
+    final String customer = get(TestIdPack.BUTTON_RECEIPT_CUSTOMER);
+
+    // Create 1 receipt
+    tap(TestIdPack.BUTTON_SEARCH);
+    write(TestIdPack.FIELD_SEARCH_TEXT, TestIdPack.BUTTON_SEARCHPRODUCT_ALPINESKIING.getRowName());
+    tap(TestIdPack.BUTTON_SEARCH_EXECUTE);
+    tap(TestIdPack.BUTTON_SEARCHPRODUCT_ALPINESKIING);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "109.90");
+    final String receiptNo1 = BackboneHelper.getDocumentNo();
+
+    // Create 2 receipt
+    tap(TestIdPack.BUTTON_NEWRECEIPT);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+    tap(TestIdPack.BUTTON_SEARCH);
+    write(TestIdPack.FIELD_SEARCH_TEXT,
+        TestIdPack.BUTTON_SEARCHPRODUCT_BASECAMPLANTERN.getRowName());
+    tap(TestIdPack.BUTTON_SEARCH_EXECUTE);
+    tap(TestIdPack.BUTTON_SEARCHPRODUCT_BASECAMPLANTERN);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "55.50");
+    final String receiptNo2 = BackboneHelper.getDocumentNo();
+
+    // Pay Open Tickets
+    tap(TestIdPack.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_MENU_PAYOPENTICKETS);
+    tap(TestIdPack.BUTTON_POPUP_OPENTICKETS_SEARCH);
+
+    verify(TestIdPack.LABEL_MULTIORDERS_SEARCHRESULTS_ROW1_TOPLINE,
+        String.format("%s - %s", receiptNo2, customer));
+    verify(TestIdPack.LABEL_MULTIORDERS_SEARCHRESULTS_ROW2_TOPLINE,
+        String.format("%s - %s", receiptNo1, customer));
+    tap(TestIdPack.BUTTON_POPUP_OPENTICKETS_ROW1);
+    tap(TestIdPack.BUTTON_POPUP_OPENTICKETS_ROW2);
+    tap(TestIdPack.BUTTON_POPUP_OPENTICKETS_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "165.40");
+
+    tap(TestIdPack.BUTTON_CARD);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "165.40");
+
+    SeleniumHelper.executeScript("OB.FAILCONDITION = false;");
+    tap(TestIdPack.BUTTON_MULTIORDER_PAYMENT_ROW1_REMOVE);
+
+    tap(TestIdPack.BUTTON_VOUCHER);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    // Verify the creation of 1 order
+    WebPOSDatabaseHelper.verifyCOrder(receiptNo1, organizationId, clientId, new OBNumber(
+        new BigDecimal("90.83")), new OBNumber(new BigDecimal("109.90")), cashupId);
+
+    // Verify the creation of 2 order
+    WebPOSDatabaseHelper.verifyCOrder(receiptNo2, organizationId, clientId, new OBNumber(
+        new BigDecimal("45.87")), new OBNumber(new BigDecimal("55.50")), cashupId);
+
+    reload();
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+  }
+}
\ No newline at end of file
