Merge remote-tracking branch 'XTUPLE/master' into 22816-searchpath
[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     },
29     handlers: {
30       onBarcodeCapture: "captureBarcode"
31     },
32     multiSelect: true,
33     showDeleteAction: false,
34     toggleSelected: true,
35     actions: [
36       {name: "transactItem", prerequisite: "canTransactItem",
37         method: "transactItem", notify: false, isViewMethod: true},
38       {name: "transactLine", prerequisite: "canTransactItem",
39         method: "transactLine", notify: false, isViewMethod: true},
40       {name: "returnLine", prerequisite: "canReturnItem",
41         method: "returnItem", notify: false, isViewMethod: true}
42     ],
43     captureBarcode: function (inSender, inEvent) {
44       var models = _.filter(this.value.models, function (model) {
45         // match on upc code or item number
46         return model.getValue("itemSite.item.barcode") === inEvent.data ||
47           model.getValue("itemSite.item.number") === inEvent.data;
48       });
49       if (models.length > 0) {
50         this.transact(models, true, true);
51       }
52     },
53     /**
54         Helper function for transacting `transact` on an array of models
55
56         @param {Array} Models
57         @param {Boolean} Prompt user for confirmation on every model
58     */
59     transact: function (models, prompt, transactStock) {
60       var that = this,
61         i = -1,
62         callback,
63         data = [];
64
65       // Recursively transact everything we can
66       // #refactor see a simpler implementation of this sort of thing
67       // using async in inventory's ReturnListItem stomp
68       callback = function (workspace) {
69         var model,
70           options = {},
71           toTransact,
72           transDate,
73           params,
74           dispOptions = {},
75           wsOptions = {},
76           wsParams,
77           transModule = that.getTransModule(),
78           transFunction = that.getTransFunction(),
79           transWorkspace = that.getTransWorkspace();
80
81         // If argument is false, this whole process was cancelled
82         if (workspace === false) {
83           return;
84
85         // If a workspace brought us here, process the information it obtained
86         } else if (workspace) {
87           model = workspace.getValue();
88           toTransact = model.get(model.quantityAttribute);
89           transDate = model.transactionDate;
90
91           if (toTransact) {
92             if (transFunction === "receipt") {
93               wsOptions.freight = model.get("freight");
94             }
95             wsOptions.detail = model.formatDetail();
96             wsOptions.asOf = transDate;
97             wsParams = {
98               orderLine: model.id,
99               quantity: toTransact,
100               options: wsOptions
101             };
102             data.push(wsParams);
103           }
104           workspace.doPrevious();
105         }
106
107         i++;
108         // If we've worked through all the models then forward to the server
109         if (i === models.length) {
110           if (data[0]) {
111             that.doProcessingChanged({isProcessing: true});
112             dispOptions.success = function () {
113               that.doProcessingChanged({isProcessing: false});
114             };
115             dispOptions.error = function () {
116               that.doProcessingChanged({isProcessing: false});
117             };
118             transModule.transactItem(data, dispOptions, transFunction);
119           } else {
120             return;
121           }
122
123         // Else if there's something here we can transact, handle it
124         } else {
125           model = models[i];
126           toTransact = model.get(model.quantityAttribute);
127           if (toTransact === null) {
128             toTransact = model.get("balance");
129           }
130           transDate = model.transactionDate;
131
132           // See if there's anything to transact here
133           if (toTransact || transactStock) {
134
135             // If prompt or distribution detail required,
136             // open a workspace to handle it
137             if (prompt || model.undistributed()) {
138               that.doWorkspace({
139                 workspace: transWorkspace,
140                 id: model.id,
141                 callback: callback,
142                 allowNew: false,
143                 success: function (model) {
144                   model.transactionDate = transDate;
145                 }
146               });
147
148             // Otherwise just use the data we have
149             } else {
150               if (transFunction === "receipt") {
151                 options.freight = model.get("freight");
152               }
153               options.asOf = transDate;
154               options.detail = model.formatDetail();
155               params = {
156                 orderLine: model.id,
157                 quantity: toTransact,
158                 options: options
159               };
160               data.push(params);
161               callback();
162             }
163
164           // Nothing to transact, move on
165           } else {
166             callback();
167           }
168         }
169       };
170       callback();
171     },
172     transactAll: function () {
173       var models = this.getValue().models;
174       this.transact(models);
175     },
176     transactLine: function () {
177       var models = this.selectedModels();
178       this.transact(models);
179     },
180     transactItem: function () {
181       var models = this.selectedModels();
182       this.transact(models, true, true);
183     },
184     returnItem: function () {
185       var models = this.selectedModels(),
186         that = this,
187         data =  [],
188         options = {},
189         qtyTransacted,
190         model,
191         i,
192         transModule = that.getTransModule();
193
194       for (i = 0; i < models.length; i++) {
195         model = models[i];
196         qtyTransacted = model.get(model.quantityTransactedAttribute);
197
198         // See if there's anything to transact here
199         if (qtyTransacted) {
200           data.push(model.id);
201         }
202       }
203
204       if (data.length) {
205         that.doProcessingChanged({isProcessing: true});
206         options.success = function () {
207           that.doProcessingChanged({isProcessing: false});
208         };
209         transModule.returnItem(data, options);
210       }
211     },
212     selectedModels: function () {
213       var collection = this.getValue(),
214         models = [],
215         selected,
216         prop;
217       if (collection.length) {
218         selected = this.getSelection().selected;
219         for (prop in selected) {
220           if (selected.hasOwnProperty(prop)) {
221             models.push(this.getModel(prop - 0));
222           }
223         }
224       }
225       return models;
226     }
227   });
228
229 }());
230