try around parsing upc data
[omsmith/pos-js.git] / www / assets / js / ui.js
1 /*
2  * Ask for configuration at page load
3  */
4
5 var client = {};
6
7 $('#config-modal').modal().find('#save-config').click(function (e) {
8         var authKey = $('input[name=auth-key]').val();
9         var serverLocation = $('input[name=server-location]').val();
10
11         client = new PosClientWrapper( authKey, serverLocation );
12 });
13
14 /*
15  * End configuation ask
16  */
17
18 /*
19  * Selections ( used for knowing things, and also data binding )
20  */
21
22 function Selection (upc, price) {
23         var self = this;
24
25         self.upc = upc;
26         self.priceVal = price;
27
28         self.name = ko.observable('');
29         self.size = ko.observable('');
30         self.quantity = ko.observable(1);
31         self.price = '$' + parseFloat(
32                 isNaN(price) || price === '' || price === null ? 0.00 : price
33                 ).toFixed(2);
34 }
35
36 function SelectionsViewModel () {
37         var self = this;
38
39         self.selections = ko.observableArray([]);
40
41         self.addSelection = function (data, e) {
42                 var input = $(e.target);
43                 var upc = input.val();
44                 input.val('');
45
46                 if (upc.length === 12) {
47                         for (var i in self.selections()) {
48                                 var selection = self.selections()[i];
49
50                                 if (selection.upc === upc) {
51                                         selection.quantity(selection.quantity() + 1);
52                                         return;
53                                 }
54                         }
55
56                         var selection = new Selection( upc, client.getPrice(upc) );
57                         request_upc_data(upc, function (data) {
58                                 selection.name(data.name).size(data.size);
59                         });
60
61                         self.selections.push( selection );
62
63                 }
64         };
65
66         self.removeSelection = function (selection) {
67                 self.selections.remove(selection);
68         };
69
70         self.clearSelection = function () {
71                 self.selections.removeAll();
72         };
73
74         self.purchase = function (data, e) {
75                 var selected = self.selections();
76
77                 var input = $(e.target);
78                 var cardData = input.val();
79
80                 var result = client.process(cardData, selected);
81
82                 input.val('');
83                 self.clearSelection();
84                 $('#checkout-modal').modal('hide');
85
86                 if (result === E_PURCHASE_STATUS['EPS_FAIL']) {
87                         $('#fail-modal').modal();
88                 } else if (result === E_PURCHASE_STATUS['EPS_NOMONEY']) {
89                         $('#nomoney-modal').modal();
90                 }
91         };
92
93         self.totalPrice = ko.computed(function () {
94                 var total = 0;
95                 ko.utils.arrayForEach(self.selections(), function (selection) {
96                         total += selection.priceVal * selection.quantity();
97                 });
98                 return '$' + parseFloat(
99                         isNaN(total) || total === '' || total === null ? 0.00 : total
100                         ).toFixed(2);
101         }, self);
102 }
103
104 ko.applyBindings(new SelectionsViewModel());
105
106 /*
107  * End of selections
108  */
109
110
111 function request_upc_data (upc, callback) {
112         $.ajax( 'upc.php?upc=' + upc )
113                 .done(function (data) {
114                         var obj;
115                         try {
116                                 obj = JSON.parse(data);
117                         } catch (e) {
118                                 obj = { name:'unknown', size:'unknown' }
119                         }
120
121                         callback(obj);
122                 });
123 }