# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1458050575 -19800
# Node ID a9f979246412fbfd24cab871a5e0c2b8235daacc
# Parent  5708555c6d9ca009ab204525d58d4377db2fc3e5
Verifies issue 32379 : Added automated test 'I32379_CheckDiscountByTotalBestDeal'

diff -r 5708555c6d9c -r a9f979246412 src-test/org/openbravo/test/mobile/retail/extmodules/selenium/tests/discounts_bytotal/I32379_CheckDiscountByTotalBestDeal.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/mobile/retail/extmodules/selenium/tests/discounts_bytotal/I32379_CheckDiscountByTotalBestDeal.java	Tue Mar 15 19:32:55 2016 +0530
@@ -0,0 +1,182 @@
+/*
+ *************************************************************************
+ * 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) 2015 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author RAN
+ *
+ */
+
+package org.openbravo.test.mobile.retail.extmodules.selenium.tests.discounts_bytotal;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.UUID;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.common.selenium.SeleniumHelper;
+import org.openbravo.test.mobile.common.selenium.database.DatabaseHelperInsertUpdateOrDelete;
+import org.openbravo.test.mobile.common.selenium.database.DatabaseHelperSelect;
+import org.openbravo.test.mobile.common.selenium.javascript.TestId;
+import org.openbravo.test.mobile.common.selenium.terminals.WebPOSExtModulesTerminalHelper;
+import org.openbravo.test.mobile.common.selenium.utils.OBNumber;
+
+/**
+ * Verify Discount by total
+ * 
+ */
+public class I32379_CheckDiscountByTotalBestDeal extends WebPOSExtModulesTerminalHelper {
+
+  private final String offerId = getUUID();
+  private final String offerName = "Discount Amount 200 of 1000 & above";
+  private final String offerTypeId = "4183C8EB7CDA472D9E64521DC2504B15";
+  private String isBestCase = "N";
+  private String preventLongEx = "N";
+
+  @Test
+  public void test() {
+    boolean isTestSuccessful = false;
+    final String orgId = (String) SeleniumHelper
+        .executeScriptWithReturn("OB.POS.modelterminal.get('terminal').organization");
+    try {
+
+      // Create database changes
+      createDatabaseChanges(orgId);
+
+      // Update Terminal Changes
+      updateTerminalAfterDatabaseChange();
+
+      // Add Product
+      final int productCount = 7;
+      addProduct1(productCount);
+      addProduct2(productCount);
+
+      verify(TestId.LABEL_RECEIPT_ROW1_DISCOUNTNAME, "-- " + offerName);
+      verify(TestId.LABEL_RECEIPT_ROW2_DISCOUNTNAME, "-- " + offerName);
+
+      final OBNumber totalAmount1 = new OBNumber("150.50").multiply(new OBNumber(productCount));
+      final OBNumber totalAmount2 = new OBNumber("14.50").multiply(new OBNumber(productCount));
+      final OBNumber discountAmount = new OBNumber("200");
+      final OBNumber verifyAmount = totalAmount1.add(totalAmount2).subtract(discountAmount);
+
+      // Verify Amount
+      verify(TestId.LABEL_TOTALTOPAY, verifyAmount.toString());
+
+      tap(TestId.BUTTON_PAY);
+
+      // Finish Ticket
+      verify(TestId.LABEL_TOTALTOPAY, verifyAmount.toString());
+
+      // Delete Receipt
+      tap(TestId.BUTTON_DELETERECEIPT);
+      tap(TestId.BUTTON_DELETERECEIPT_CONFIRM);
+
+      verify(TestId.LABEL_TOTALTOPAY, "0.00");
+
+      // Restore database changes
+      restoreDatabaseChanges(orgId);
+
+      // Update Terminal Changes
+      updateTerminalAfterDatabaseChange();
+
+      isTestSuccessful = true;
+    } finally {
+      if (!isTestSuccessful) {
+        // Restore database changes
+        restoreDatabaseChanges(orgId);
+      }
+    }
+  }
+
+  private void createDatabaseChanges(final String orgId) {
+    final String sql = String
+        .format(
+            "select coalesce(em_obposbd_best_deal_case, 'N') as bestcase, coalesce(em_obposbd_prevent_long_ex, 'N') as longex "
+                + "from ad_org where ad_org_id = '%s'", orgId);
+    new DatabaseHelperSelect() {
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+        isBestCase = rs.getString("bestcase");
+        preventLongEx = rs.getString("longex");
+      }
+    }.execute(sql, 1);
+
+    enableDiscountByTotalBestDeal(orgId, true);
+    addDiscount(true);
+  }
+
+  private void restoreDatabaseChanges(final String orgId) {
+    enableDiscountByTotalBestDeal(orgId, false);
+    addDiscount(false);
+  }
+
+  private void enableDiscountByTotalBestDeal(final String orgId, final boolean isForTest) {
+    final String updateOrgBestDealSQL = String
+        .format(
+            "update ad_org set em_obposbd_best_deal_case = '%s', em_obposbd_prevent_long_ex = '%s' where ad_org_id = '%s'",
+            (isForTest ? "Y" : isBestCase), (isForTest ? "Y" : preventLongEx), orgId);
+    new DatabaseHelperInsertUpdateOrDelete().execute(updateOrgBestDealSQL, 1);
+  }
+
+  private void addDiscount(final boolean isForTest) {
+    if (isForTest) {
+      final String clientId = (String) SeleniumHelper
+          .executeScriptWithReturn("OB.POS.modelterminal.get('terminal').client");
+
+      final String insertOfferSQL = String
+          .format(
+              "INSERT INTO m_offer(m_offer_id, ad_client_id, ad_org_id, createdby, updatedby, name, addamt, discount, "
+                  + "datefrom, bpartner_selection, bp_group_selection, product_selection, prod_cat_selection, pricelist_selection, m_offer_type_id, "
+                  + "apply_next, org_selection, ismultiple, characteristics_selection, em_obdisc_distribute, em_obdisc_role_selection, em_obdisc_approval_required, em_disct_totalreceipt, em_disct_totalamountdisc) "
+                  + "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', 0, 0, TO_DATE('2016-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), 'Y', 'Y', 'Y', 'Y', 'Y', "
+                  + "'%s', 'Y', 'Y', 'N', 'Y', 'N', 'Y', 'N', 1000, 200)", offerId, clientId, "0",
+              "100", "100", offerName, offerTypeId);
+      new DatabaseHelperInsertUpdateOrDelete().execute(insertOfferSQL, 1);
+    } else {
+      final String deleteSQL = String
+          .format("delete from m_offer where m_offer_id = '%s'", offerId);
+      new DatabaseHelperInsertUpdateOrDelete().execute(deleteSQL, 1);
+    }
+  }
+
+  private String getUUID() {
+    String randomUUID = UUID.randomUUID().toString();
+    randomUUID = randomUUID.toUpperCase();
+    return randomUUID.replace("-", "");
+  }
+
+  private void addProduct1(final int productCount) {
+    tap(TestId.BUTTON_SEARCH);
+    write(TestId.FIELD_SEARCH_TEXT, TestId.BUTTON_SEARCHPRODUCT_AVALANCHETRANSCEIVER.getRowName());
+    tap(TestId.BUTTON_SEARCH_EXECUTE);
+    tap(TestId.BUTTON_SEARCHPRODUCT_FIRSTROW);
+
+    tap(TestId.BUTTON_RECEIPT_ROW1);
+    tap(TestId.BUTTON_KEYPAD_7);
+    tap(TestId.BUTTON_WAREHOUSE_KEYPAD_QUANTITY);
+  }
+
+  private void addProduct2(final int productCount) {
+    tap(TestId.BUTTON_SEARCH);
+    write(TestId.FIELD_SEARCH_TEXT, TestId.BUTTON_SEARCHPRODUCT_INSECTREPELLENT.getRowName());
+    tap(TestId.BUTTON_SEARCH_EXECUTE);
+    tap(TestId.BUTTON_SEARCHPRODUCT_FIRSTROW);
+
+    tap(TestId.BUTTON_RECEIPT_ROW2);
+    tap(TestId.BUTTON_KEYPAD_7);
+    tap(TestId.BUTTON_WAREHOUSE_KEYPAD_QUANTITY);
+  }
+}
\ No newline at end of file
