try around parsing upc data
[omsmith/pos-js.git] / www / assets / js / ui.js
index 224b248..804780d 100644 (file)
@@ -16,13 +16,15 @@ $('#config-modal').modal().find('#save-config').click(function (e) {
  */
 
 /*
- * Selections ( used for knowing things, and also data binding
+ * Selections ( used for knowing things, and also data binding )
  */
 
 function Selection (upc, price) {
        var self = this;
 
        self.upc = upc;
+       self.priceVal = price;
+
        self.name = ko.observable('');
        self.size = ko.observable('');
        self.quantity = ko.observable(1);
@@ -74,14 +76,29 @@ function SelectionsViewModel () {
 
                var input = $(e.target);
                var cardData = input.val();
-               input.val('');
 
                var result = client.process(cardData, selected);
-               if (result === 2) {
-                       self.clearSelection();
-                       $('#checkout-modal').modal('hide');
+
+               input.val('');
+               self.clearSelection();
+               $('#checkout-modal').modal('hide');
+
+               if (result === E_PURCHASE_STATUS['EPS_FAIL']) {
+                       $('#fail-modal').modal();
+               } else if (result === E_PURCHASE_STATUS['EPS_NOMONEY']) {
+                       $('#nomoney-modal').modal();
                }
        };
+
+       self.totalPrice = ko.computed(function () {
+               var total = 0;
+               ko.utils.arrayForEach(self.selections(), function (selection) {
+                       total += selection.priceVal * selection.quantity();
+               });
+               return '$' + parseFloat(
+                       isNaN(total) || total === '' || total === null ? 0.00 : total
+                       ).toFixed(2);
+       }, self);
 }
 
 ko.applyBindings(new SelectionsViewModel());
@@ -93,5 +110,14 @@ ko.applyBindings(new SelectionsViewModel());
 
 function request_upc_data (upc, callback) {
        $.ajax( 'upc.php?upc=' + upc )
-               .done(function (data) { callback(JSON.parse(data)); });
+               .done(function (data) {
+                       var obj;
+                       try {
+                               obj = JSON.parse(data);
+                       } catch (e) {
+                               obj = { name:'unknown', size:'unknown' }
+                       }
+
+                       callback(obj);
+               });
 }