# HG changeset patch
# User Rafa Alonso <rafael.alonso@openbravo.com>
# Date 1423676480 -3600
#      Wed Feb 11 18:41:20 2015 +0100
# Node ID 28c5e09a651cb827d759b431f0f8dca75efee195
# Parent  b4b15bb5aeb0109be0c466b759fc8c367da7cb1f
Better log (I)

diff -r b4b15bb5aeb0 -r 28c5e09a651c web/org.openbravo.retail.posterminal/js/data/dataordersave.js
--- a/web/org.openbravo.retail.posterminal/js/data/dataordersave.js	Fri Dec 26 10:39:05 2014 +0000
+++ b/web/org.openbravo.retail.posterminal/js/data/dataordersave.js	Wed Feb 11 18:41:20 2015 +0100
@@ -7,11 +7,10 @@
  ************************************************************************************
  */
 
-/*global B,_*/
+/*global OB, _, enyo*/
 
 (function () {
 
-  OB = window.OB || {};
   OB.DATA = window.OB.DATA || {};
 
   OB.DATA.OrderSave = function (model) {
@@ -20,6 +19,84 @@
     this.ordersToSend = OB.DEC.Zero;
     this.hasInvLayaways = false;
 
+    // start of receipt verifications
+    var maxLogLinesPerReceipt = 1;
+    var errorsFound = 0;
+    this.receipt.on('all', function (eventParams) {
+      // list of events to be processed
+      var isVerify = false;
+      isVerify = isVerify || (eventParams === 'eventExecutionDone');
+      isVerify = isVerify || (eventParams === 'calculategross');
+      isVerify = isVerify || (eventParams === 'saveCurrent');
+      isVerify = isVerify || (eventParams === 'closed');
+      if (!isVerify) {
+        return;
+      }
+      // restart the number of allowed log lines when the receipt is closed/finished
+      if (eventParams === 'closed') {
+        errorsFound = 0;
+      }
+      if (errorsFound >= maxLogLinesPerReceipt) {
+        return;
+      }
+
+      // the same header in all messages is important when looking for records in the database
+      var errorHeader = enyo.format("Verification error in receipt '%s'", this.get('documentNo'));
+      var errorCount = 0;
+
+      // protect the application against verification exceptions
+      try {
+        // get tax information
+        var totalTaxes = 0;
+        _.each(this.get('taxes'), function (tax) {
+          // tax.amount;
+          // tax.gross;
+          // tax.net;
+          totalTaxes += tax.amount;
+        }, this);
+
+        var gross = this.get('gross');
+        var net = this.get('net');
+
+        // 1. verify that the sign of the net, gross and tax is consistent
+        if (Math.abs(totalTaxes) > 0 && ((Math.sign(net) !== Math.sign(gross)) || (Math.sign(net) !== Math.sign(totalTaxes)))) {
+          // OB.UTIL.saveLogClient(JSON.stringify(signInconsistentErrorMessage), "Error");
+          OB.error(enyo.format("%s: the sign of the net, gross and tax is inconsistent. event: '%s', gross: %s, net: %s, tax: %s", errorHeader, eventParams, gross, net, totalTaxes));
+          errorCount += 1;
+        }
+
+        // 3. verify that the sum of the gross of each line equals the total gross
+        var difference;
+        var field = '';
+        if (this.get('priceIncludesTax')) {
+          difference = OB.DEC.sub(gross, totalTaxes);
+          field = 'discountedNet';
+        } else {
+          difference = gross;
+          field = 'discountedGross';
+        }
+        var isFieldUndefined = false;
+        _.each(this.get('lines').models, function (line) {
+          var fieldValue = line.get(field);
+          if (!fieldValue) {
+            isFieldUndefined = true;
+            return;
+          }
+          difference = OB.DEC.sub(difference, fieldValue);
+        });
+        if (!isFieldUndefined && difference !== 0) {
+          OB.error(enyo.format("%s: total gross does not equal the sum of the gross of each line. event: '%s', gross: %s, difference: %s", errorHeader, eventParams, gross, difference));
+          errorCount += 1;
+        }
+
+      } catch (e) {
+        // console.error(enyo.format("%s. event: %s. %s", errorHeader, eventParams, e.stack));
+        errorCount += 1;
+      }
+      errorsFound += errorCount;
+    });
+    // end of receipt verifications
+
     this.receipt.on('closed', function (eventParams) {
       this.receipt = model.get('order');
       OB.warn('Ticket closed. ' + this.receipt.getOrderDescription());
