# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1451372052 -19800
# Node ID e964836eb48bbb0a7cbb9df18b3468da67f3221d
# Parent  2d4657a16e454116219c6c95a42c8926573e6c83
Verifies issue 31757: Added automated test 'I31757_CashMgmtTrxDate'

diff --git a/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/cashmanagement/I31757_CashMgmtTrxDate.java b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/cashmanagement/I31757_CashMgmtTrxDate.java
new file mode 100644
--- /dev/null
+++ b/src-test/org/openbravo/test/mobile/retail/pack/selenium/tests/cashmanagement/I31757_CashMgmtTrxDate.java
@@ -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) 2015 Openbravo S.L.U.
+ * All Rights Reserved.
+ * Contributor(s):
+ ************************************************************************
+ *
+ * @author JGA
+ *
+ */
+
+package org.openbravo.test.mobile.retail.pack.selenium.tests.cashmanagement;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.TimeZone;
+
+import org.junit.Test;
+import org.openbravo.test.mobile.common.selenium.SeleniumHelper;
+import org.openbravo.test.mobile.common.selenium.database.DatabaseHelperSelect;
+import org.openbravo.test.mobile.common.selenium.database.WebPOSDatabaseConstants;
+import org.openbravo.test.mobile.common.selenium.javascript.TestId;
+import org.openbravo.test.mobile.common.selenium.terminals.WebPOSTerminalHelper;
+import org.openbravo.test.mobile.common.selenium.testhelpers.WebPOSSnippet;
+import org.openbravo.test.mobile.common.selenium.utils.OBUtils;
+
+public class I31757_CashMgmtTrxDate extends WebPOSTerminalHelper {
+
+  @Test
+  public void test() {
+    WebPOSSnippet.cleanCashup(this);
+
+    final String actualCashUpId = (String) SeleniumHelper
+        .executeScriptWithReturn("OB.MobileApp.model.get('terminal').cashUpId");
+    final String cashUpAmount = "10.57";
+
+    depositCash(cashUpAmount);
+
+    final StringBuffer jsGetCashUpDetails = new StringBuffer();
+    jsGetCashUpDetails.append("OBCustomVariable1 = ''; OBCustomVariable2 = '';");
+    jsGetCashUpDetails.append("OB.Dal.find(OB.Model.CashManagement, {}, function(cashMgmt) { ");
+    jsGetCashUpDetails
+        .append("OBCustomVariable1 = JSON.parse(cashMgmt.at(0).get('objToSend')).creationDate;");
+    jsGetCashUpDetails.append("});");
+
+    SeleniumHelper.executeScript(jsGetCashUpDetails.toString());
+    OBUtils.pause(1000);
+    final String cashMgmtTrxDateTime = (String) SeleniumHelper
+        .executeScriptWithReturn("OBCustomVariable1");
+
+    final DateFormat dateFormatISO = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
+    final TimeZone tz = TimeZone.getTimeZone("UTC");
+    dateFormatISO.setTimeZone(tz);
+
+    final String cashMgmtTrxISODateTime = cashMgmtTrxDateTime.substring(0,
+        cashMgmtTrxDateTime.length() - 8)
+        + "Z";
+
+    // Checking Front office Created date with Back office Transaction Date
+    final String sqlBackoffice = String
+        .format(
+            "select statementdate, dateacct from fin_finacc_transaction "
+                + " where fin_financial_account_id = '%s' and em_obpos_app_cashup_id = '%s' and depositamt = %s "
+                + " order by created desc",
+            WebPOSDatabaseConstants.VBS1001_CASHBOOK_STORE_CASH_FIN_ACCOUNT_ID, actualCashUpId,
+            cashUpAmount);
+    new DatabaseHelperSelect() {
+      @Override
+      protected void yieldResultSet(final ResultSet rs) throws SQLException {
+        assertThat("Cash Management Time from front office differs from Transaction date",
+            cashMgmtTrxISODateTime, equalTo(dateFormatISO.format(rs.getTimestamp("statementdate"))));
+        assertThat("Cash Management Time from front office differs from Accounting date",
+            cashMgmtTrxISODateTime, equalTo(dateFormatISO.format(rs.getTimestamp("statementdate"))));
+      }
+    }.execute(sqlBackoffice, 1);
+
+    WebPOSSnippet.cleanCashup(this);
+  }
+
+  private void depositCash(final String cashUpAmount) {
+    tap(TestId.BUTTON_MENU);
+    tap(TestId.BUTTON_MENU_CASHMANAGEMENT);
+
+    tap(TestId.BUTTON_CASHMANAGEMENT_CASHDEPOSIT);
+    write(TestId.INPUT_KEYPAD_CASHMGMT_EDITBOX, cashUpAmount);
+    tap(TestId.BUTTON_KEYPAD_ENTER);
+    tap(TestId.BUTTON_SELECTDEPOSIT_BACOFFICEVBS);
+    tap(TestId.BUTTON_CASHMANAGEMENT_DONE);
+    tap(TestId.BUTTON_POPUP_OK);
+    verify(TestId.LABEL_TOTALTOPAY, "0.00");
+  }
+}
