# HG changeset patch
# User Silambarasan Sekar <silambarasan@qualiantech.com>
# Date 1473765702 -19800
#      Tue Sep 13 16:51:42 2016 +0530
# Node ID 5ed7dacef5b4d0d94d505d92aad2289dfce67d41
# Parent  1e96d6efd9c781e937ea215bb691cb616a25f5ff
Added cashvat test

diff -r 1e96d6efd9c7 -r 5ed7dacef5b4 src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/receipts/CashVatInSalesOrder.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/receipts/CashVatInSalesOrder.java	Tue Sep 13 16:51:42 2016 +0530
@@ -0,0 +1,129 @@
+/*
+ *************************************************************************
+ * 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) 2016 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author SIM
+ *
+ */
+
+package org.openbravo.test.mobile.retail.pack.selenium.tests.receipts;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.common.selenium.SeleniumHelper;
+import org.openbravo.test.mobile.common.selenium.WebPOSTerminalHelper;
+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.BackboneHelper;
+import org.openbravo.test.mobile.retail.pack.selenium.TestIdPack;
+
+public class CashVatInSalesOrder extends WebPOSTerminalHelper {
+
+  @Test
+  public void test() {
+
+    // sales order with cashvat-false
+    disableCashVatTax();
+    disableCashVat();
+
+    tap(TestIdPack.BUTTON_BROWSE);
+    tap(TestIdPack.BUTTON_CATEGORY_BESTSELLERS);
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_AVALANCHETRANSCEIVER);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_INSECTREPELLENT);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "165.00");
+    String receiptNo = BackboneHelper.getDocumentNo();
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    String sql = "SELECT iscashvat FROM c_order WHERE documentno = '" + receiptNo + "'";
+
+    new DatabaseHelperSelect() {
+
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+
+        final String isCashVat = rs.getString("iscashvat");
+
+        logger.info(String.format(
+            "Verifying that the database record (%s) '%s' is one of the expected", isCashVat, "N"));
+        assertThat("Sales Order has cashvat", isCashVat, equalTo("N"));
+      }
+    }.execute(sql, 1);
+
+    // sales order with cashvat-true
+    enableCashVatTax();
+    enableCashVat();
+
+    tap(TestIdPack.BUTTON_BROWSE);
+    tap(TestIdPack.BUTTON_CATEGORY_BESTSELLERS);
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_AVALANCHETRANSCEIVER);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_INSECTREPELLENT);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "165.00");
+    receiptNo = BackboneHelper.getDocumentNo();
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    sql = "SELECT iscashvat FROM c_order WHERE documentno = '" + receiptNo + "'";
+    new DatabaseHelperSelect() {
+
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+
+        final String isCashVat = rs.getString("iscashvat");
+
+        logger.info(String.format(
+            "Verifying that the database record (%s) '%s' is one of the expected", isCashVat, "Y"));
+        assertThat("Sales Order has cashvat", isCashVat, equalTo("Y"));
+      }
+    }.execute(sql, 1);
+  }
+
+  private void enableCashVat() {
+    SeleniumHelper.executeScript("OB.MobileApp.model.receipt.set('cashVAT', true)");
+    SeleniumHelper.executeScript("OB.MobileApp.model.get('terminal').cashVat=true");
+  }
+
+  private void disableCashVat() {
+    SeleniumHelper.executeScript("OB.MobileApp.model.receipt.set('cashVAT', false)");
+    SeleniumHelper.executeScript("OB.MobileApp.model.get('terminal').cashVat=false");
+  }
+
+  private void disableCashVatTax() {
+    String updateTax = "update c_tax set istaxexempt='N' , iswithholdingtax='N' where name = 'Entregas IVA 21%' "
+        + "and ad_client_id='39363B0921BB4293B48383844325E84C'";
+    new DatabaseHelperInsertUpdateOrDelete().execute(updateTax, 1);
+    reload();
+  }
+
+  private void enableCashVatTax() {
+    String updateTax = "update c_tax set istaxexempt='Y' , iswithholdingtax='Y' where name = 'Entregas IVA 21%' "
+        + "and ad_client_id='39363B0921BB4293B48383844325E84C'";
+    new DatabaseHelperInsertUpdateOrDelete().execute(updateTax, 1);
+    reload();
+  }
+}
\ No newline at end of file
diff -r 1e96d6efd9c7 -r 5ed7dacef5b4 src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/receipts/InvoiceWithAndWithOutCashvat.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/receipts/InvoiceWithAndWithOutCashvat.java	Tue Sep 13 16:51:42 2016 +0530
@@ -0,0 +1,218 @@
+/*
+ *************************************************************************
+ * 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) 2016 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author SIM
+ *
+ */
+
+package org.openbravo.test.mobile.retail.pack.selenium.tests.receipts;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.common.selenium.SeleniumHelper;
+import org.openbravo.test.mobile.common.selenium.WebPOSTerminalHelper;
+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.BackboneHelper;
+import org.openbravo.test.mobile.retail.pack.selenium.TestIdPack;
+
+public class InvoiceWithAndWithOutCashvat extends WebPOSTerminalHelper {
+
+  String receiptNo, nextReceiptNo, sql;
+
+  @Test
+  public void test() {
+
+    cashUp();
+
+    // sales order with cashvat-true
+    enableCashVatTax();
+    enableCashVat();
+
+    tap(TestIdPack.BUTTON_BROWSE);
+    tap(TestIdPack.BUTTON_CATEGORY_BESTSELLERS);
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_AVALANCHETRANSCEIVER);
+    receiptNo = BackboneHelper.getDocumentNo();
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    cashUp();
+
+    // getting cashvat, documentno, totalpaid, linegrossamount for created invoice
+
+    sql = "select inv.iscashvat as isCashVat, ord.documentno as documentNo, sum(invl.line_gross_amount) as lineGrossAmnt ,"
+        + "sum(inv.totalpaid) as totalPaid  from c_invoice inv "
+        + "join c_invoiceline invl on invl.c_invoice_id=inv.c_invoice_id "
+        + "join c_orderline ordl on ordl.c_orderline_id=invl.c_orderline_id "
+        + "join c_order ord on ord.c_order_id = ordl.c_order_id "
+        + "where ord.c_order_id=(select c_order_id from c_order where documentno ='"
+        + receiptNo
+        + "') group by inv.iscashvat, ord.documentno";
+
+    verifyDatabase(sql, "Y", receiptNo);
+
+    // sales order with cashvat-true
+    enableCashVat();
+
+    tap(TestIdPack.BUTTON_BROWSE);
+    tap(TestIdPack.BUTTON_CATEGORY_BESTSELLERS);
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_AVALANCHETRANSCEIVER);
+    receiptNo = BackboneHelper.getDocumentNo();
+    verify(TestIdPack.LABEL_TOTALTOPAY, "150.50");
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    // sales order with cashvat-false
+    disableCashVatTax();
+    disableCashVat();
+
+    tap(TestIdPack.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_BROWSE);
+    tap(TestIdPack.BUTTON_CATEGORY_BESTSELLERS);
+    tap(TestIdPack.BUTTON_PRODUCT_BESTSELLERS_INSECTREPELLENT);
+    nextReceiptNo = BackboneHelper.getDocumentNo();
+    verify(TestIdPack.LABEL_TOTALTOPAY, "14.50");
+    tap(TestIdPack.BUTTON_PAY);
+    tap(TestIdPack.BUTTON_PAY_EXACT);
+    tap(TestIdPack.BUTTON_PAY_DONE);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+    cashUp();
+
+    // getting cashvat, documentno, totalpaid, linegrossamount for created invoice
+
+    sql = "select inv.iscashvat as isCashVat, ord.documentno as documentNo, sum(invl.line_gross_amount) as lineGrossAmnt ,"
+        + "sum(inv.totalpaid) as totalPaid  from c_invoice inv "
+        + "join c_invoiceline invl on invl.c_invoice_id=inv.c_invoice_id "
+        + "join c_orderline ordl on ordl.c_orderline_id=invl.c_orderline_id "
+        + "join c_order ord on ord.c_order_id = ordl.c_order_id "
+        + "where ord.c_order_id=(select c_order_id from c_order where documentno ='"
+        + receiptNo
+        + "') group by inv.iscashvat, ord.documentno";
+
+    verifyDatabase(sql, "Y", receiptNo);
+
+    sql = "select inv.iscashvat as isCashVat, ord.documentno as documentNo, sum(invl.line_gross_amount) as lineGrossAmnt ,"
+        + "sum(inv.totalpaid) as totalPaid  from c_invoice inv "
+        + "join c_invoiceline invl on invl.c_invoice_id=inv.c_invoice_id "
+        + "join c_orderline ordl on ordl.c_orderline_id=invl.c_orderline_id "
+        + "join c_order ord on ord.c_order_id = ordl.c_order_id "
+        + "where ord.c_order_id=(select c_order_id from c_order where documentno ='"
+        + nextReceiptNo + "') group by inv.iscashvat, ord.documentno";
+
+    verifyDatabase(sql, "N", nextReceiptNo);
+
+  }
+
+  private void verifyDatabase(String query, final String isCashvat, final String documentNo) {
+    new DatabaseHelperSelect() {
+
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+
+        final String cashvat = rs.getString("isCashVat");
+        final String ordNo = rs.getString("documentNo");
+        final Double grossAmnt = rs.getDouble("lineGrossAmnt");
+        final Double totalPaid = rs.getDouble("totalPaid");
+
+        assertThat("Cashvat is not same", cashvat, equalTo(isCashvat));
+        assertThat("documentno is not same", ordNo, equalTo(documentNo));
+        assertThat("total is not same", grossAmnt, equalTo(totalPaid));
+
+      }
+    }.execute(query, 1);
+  }
+
+  private void cashUp() {
+    tap(TestIdPack.BUTTON_MENU);
+    tap(TestIdPack.BUTTON_MENU_CASHUP);
+
+    // step 1 of 4
+    verify(TestIdPack.LABEL_CASHUP_STEP2_TITLE, "Step 1 of 4: Count Cash");
+    tap(TestIdPack.BUTTON_CASHUP_NEXT);
+
+    // step 2 of 4
+    verify(TestIdPack.LABEL_CASHUP_STEP3_TITLE, "Step 2 of 4: Count Cash");
+    tap(TestIdPack.BUTTON_CASHUP_CARD_OK);
+    tap(TestIdPack.BUTTON_CASHUP_CASHUSA_OK);
+    tap(TestIdPack.BUTTON_CASHUP_VOUCHER_OK);
+    tap(TestIdPack.BUTTON_CASHUP_NEXT);
+
+    // step 3 of 4
+    // cash
+    verify(TestIdPack.LABEL_CASHUP_STEP4_TITLE, "Step 3 of 4: Select Cash to keep");
+    verify(TestIdPack.LABEL_CASHUP_KEEPNOTHING, "Nothing");
+    tap(TestIdPack.BUTTON_CASHUP_KEEPNOTHING);
+    tap(TestIdPack.BUTTON_CASHUP_NEXT);
+    // usa cash
+    verify(TestIdPack.LABEL_CASHUP_STEP4_TITLE, "Step 3 of 4: Select USA Cash to keep");
+    verify(TestIdPack.LABEL_CASHUP_KEEPNOTHING, "Nothing");
+    tap(TestIdPack.BUTTON_CASHUP_KEEPNOTHING);
+    tap(TestIdPack.BUTTON_CASHUP_NEXT);
+    // voucher
+    verify(TestIdPack.LABEL_CASHUP_STEP4_TITLE, "Step 3 of 4: Select Voucher to keep");
+    verify(TestIdPack.LABEL_CASHUP_KEEPNOTHING, "Nothing");
+    tap(TestIdPack.BUTTON_CASHUP_KEEPNOTHING);
+    tap(TestIdPack.BUTTON_CASHUP_NEXT);
+
+    // step 4 of 4
+    verify(TestIdPack.LABEL_CASHUP_STEP5_TITLE, "Step 4 of 4: Post, print and close");
+    verify(TestIdPack.LABEL_CASHUP_POSTPRINTANDCLOSE, "Post, Print & Close");
+    tap(TestIdPack.BUTTON_CASHUP_POSTPRINTANDCLOSE);
+
+    // done
+    verify(TestIdPack.LABEL_POPUP_TITLE, "Good job!");
+    tap(TestIdPack.BUTTON_POPUP_OK);
+    verify(TestIdPack.LABEL_TOTALTOPAY, "0.00");
+
+  }
+
+  private void enableCashVat() {
+    SeleniumHelper.executeScript("OB.MobileApp.model.receipt.set('cashVAT', true)");
+    SeleniumHelper.executeScript("OB.MobileApp.model.get('terminal').cashVat=true");
+  }
+
+  private void disableCashVat() {
+    SeleniumHelper.executeScript("OB.MobileApp.model.receipt.set('cashVAT', false)");
+    SeleniumHelper.executeScript("OB.MobileApp.model.get('terminal').cashVat=false");
+  }
+
+  private void disableCashVatTax() {
+    String updateTax = "update c_tax set istaxexempt='N' , iswithholdingtax='N' where name = 'Entregas IVA 21%' "
+        + "and ad_client_id='39363B0921BB4293B48383844325E84C'";
+    new DatabaseHelperInsertUpdateOrDelete().execute(updateTax, 1);
+    reload();
+  }
+
+  private void enableCashVatTax() {
+    String updateTax = "update c_tax set istaxexempt='Y' , iswithholdingtax='Y' where name = 'Entregas IVA 21%' "
+        + "and ad_client_id='39363B0921BB4293B48383844325E84C'";
+    new DatabaseHelperInsertUpdateOrDelete().execute(updateTax, 1);
+    reload();
+  }
+}
