display barcode misfires
[xtuple] / lib / enyo-x / source / views / transaction_list.js
1 /*jshint bitwise:true, indent:2, curly:true, eqeqeq:true, immed:true,
2 latedef:true, newcap:true, noarg:true, regexp:true, undef:true,
3 trailing:true, white:true, strict:false*/
4 /*global XT:true, XM:true, XV:true, _:true, enyo:true */
5
6 (function () {
7
8   /**
9     Expected to a have a parameter widget that contains an order and
10     a transaction date.
11
12     @name XV.TransactionList
13     @extends XV.List
14    */
15   enyo.kind(
16     /** @lends XV.TransactionList */{
17     name: "XV.TransactionList",
18     kind: "XV.List",
19     published: {
20       transModule: null,
21       transWorkspace: null,
22       transFunction: null
23     },
24     events: {
25       onProcessingChanged: "",
26       onOrderChanged: "",
27       onShipmentChanged: "",
28       onUpdateHeader: ""
29     },
30     handlers: {
31       onBarcodeCapture: "captureBarcode"
32     },
33     multiSelect: true,
34     showDeleteAction: false,
35     toggleSelected: true,
36     actions: [
37       {name: "transactItem", prerequisite: "canTransactItem",
38         method: "transactItem", notify: false, isViewMethod: true},
39       {name: "transactLine", prerequisite: "canTransactItem",
40         method: "transactLine", notify: false, isViewMethod: true},
41       {name: "returnLine", prerequisite: "canReturnItem",
42         method: "returnItem", notify: false, isViewMethod: true}
43     ],
44     captureBarcode: function (inSender, inEvent) {
45       var models = _.filter(this.value.models, function (model) {
46         // match on upc code or item number
47         return model.getValue("itemSite.item.barcode") === inEvent.data ||
48           model.getValue("itemSite.item.number") === inEvent.data;
49       });
50       if (models.length > 0) {
51         this.transact(models, true, true);
52       } else {
53         inEvent.noItemFound = true;
54       }
55
56       this.doUpdateHeader(inEvent);
57     },
58     /**
59         Helper function for transacting `transact` on an array of models.
60
61         @param {Array} Models
62         @param {Boolean} Prompt user for confirmation on every model
63         @param {String} Optional to handle the transaction function name, if not passed
64         it will use the published value. Used by ReturnMaterial's actions.
65         @param {String} Optional to handle the workspace name, if not passed
66         it will use the published value. Used by ReturnMaterial's actions.
67         @param {String} Optional to handle the quantity attr name, if not passed
68         it will use the model.quantityAttribute. Used by ReturnMaterial's actions.
69     */
70     transact: function (models, prompt, transFunction, transWorkspace, transQty) {
71       var that = this,
72         i = -1,
73         callback,
74         data = [];
75
76       // Recursively transact everything we can
77       // #refactor see a simpler implementation of this sort of thing
78       // using async in inventory's ReturnListItem stomp
79       callback = function (workspace, transFunction, transWorkspace, transQty) {
80         var model,
81           options = {},
82           toTransact,
83           transDate,
84           params,
85           dispOptions = {},
86           wsOptions = {},
87           wsParams,
88           transModule = that.getTransModule();
89
90         transFunction = transFunction || that.getTransFunction();
91         transWorkspace = transWorkspace || that.getTransWorkspace();
92
93         that._printModels = [];
94
95         // If argument is false, this whole process was cancelled
96         if (workspace === false) {
97           return;
98
99         // If a workspace brought us here, process the information it obtained
100         } else if (workspace) {
101           model = workspace.getValue();
102           toTransact = transQty ? model.get(transQty) : model.get(model.quantityAttribute);
103           transDate = model.transactionDate;
104
105
106           if (workspace._printAfterPersist) {
107             that._printModels.push(model);
108           }
109
110           if (toTransact) {
111             if (transFunction === "receipt") {
112               wsOptions.freight = model.get("freight");
113             }
114             wsOptions.detail = model.formatDetail();
115             wsOptions.asOf = transDate;
116             wsParams = {
117               orderLine: model.id,
118               quantity: toTransact,
119               options: wsOptions
120             };
121             data.push(wsParams);
122           }
123           workspace.doPrevious();
124         }
125
126         i++;
127         // If we've worked through all the models then forward to the server
128         if (i === models.length) {
129           if (data[0]) {
130             that.doProcessingChanged({isProcessing: true});
131             dispOptions.success = function () {
132               _.each(that._printModels, function (printModel) {
133                 // XXX eventually replace _auxilliaryInfo with meta, probably
134                 printModel.doPrint(printModel._auxilliaryInfo);
135               });
136               that.doProcessingChanged({isProcessing: false});
137             };
138             dispOptions.error = function () {
139               that.doProcessingChanged({isProcessing: false});
140             };
141             transModule.transactItem(data, dispOptions, transFunction);
142           } else {
143             return;
144           }
145
146         // Else if there's something here we can transact, handle it
147         } else {
148           model = models[i];
149           toTransact = transQty ? model.get(transQty) : model.get(model.quantityAttribute);
150           if (toTransact === null) {
151             toTransact = model.get("balance");
152           }
153           transDate = model.transactionDate;
154
155           // See if there's anything to transact here
156           if (toTransact || prompt) {
157
158             // If prompt or distribution detail required,
159             // open a workspace to handle it
160             if (prompt || model.undistributed() || model.requiresDetail()) {
161               that.doWorkspace({
162                 workspace: transWorkspace,
163                 id: model.id,
164                 callback: callback,
165                 allowNew: false,
166                 success: function (model) {
167                   model.transactionDate = transDate;
168                 }
169               });
170
171             // Otherwise just use the data we have
172             } else {
173               if (transFunction === "receipt") {
174                 options.freight = model.get("freight");
175               }
176               options.asOf = transDate;
177               options.detail = model.formatDetail();
178               params = {
179                 orderLine: model.id,
180                 quantity: toTransact,
181                 options: options
182               };
183               data.push(params);
184               callback(null, transFunction, transWorkspace, transQty);
185             }
186
187           // Nothing to transact, move on
188           } else {
189             callback(null, transFunction, transWorkspace, transQty);
190           }
191         }
192       };
193       callback(null, transFunction, transWorkspace, transQty);
194     },
195     transactAll: function () {
196       var models = this.getValue().models;
197       this.transact(models);
198     },
199     transactLine: function () {
200       var models = this.selectedModels();
201       this.transact(models);
202     },
203     transactItem: function () {
204       var models = this.selectedModels();
205       this.transact(models, true);
206     },
207     returnItem: function () {
208       var models = this.selectedModels(),
209         that = this,
210         data =  [],
211         options = {},
212         qtyTransacted,
213         model,
214         i,
215         transModule = that.getTransModule();
216
217       for (i = 0; i < models.length; i++) {
218         model = models[i];
219         qtyTransacted = model.get(model.quantityTransactedAttribute);
220
221         // See if there's anything to transact here
222         if (qtyTransacted) {
223           data.push(model.id);
224         }
225       }
226
227       if (data.length) {
228         that.doProcessingChanged({isProcessing: true});
229         options.success = function () {
230           that.doProcessingChanged({isProcessing: false});
231         };
232         transModule.returnItem(data, options);
233       }
234     },
235     selectedModels: function () {
236       var collection = this.getValue(),
237         models = [],
238         selected,
239         prop;
240       if (collection.length) {
241         selected = this.getSelection().selected;
242         for (prop in selected) {
243           if (selected.hasOwnProperty(prop)) {
244             models.push(this.getModel(prop - 0));
245           }
246         }
247       }
248       return models;
249     }
250   });
251
252 }());
253