Merge remote-tracking branch 'XTUPLE/master' into 22243-item-label
[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         that._printModels = [];
82
83         // If argument is false, this whole process was cancelled
84         if (workspace === false) {
85           return;
86
87         // If a workspace brought us here, process the information it obtained
88         } else if (workspace) {
89           model = workspace.getValue();
90           toTransact = model.get(model.quantityAttribute);
91           transDate = model.transactionDate;
92
93
94           if (workspace._printAfterPersist) {
95             that._printModels.push(model);
96           }
97
98           if (toTransact) {
99             if (transFunction === "receipt") {
100               wsOptions.freight = model.get("freight");
101             }
102             wsOptions.detail = model.formatDetail();
103             wsOptions.asOf = transDate;
104             wsParams = {
105               orderLine: model.id,
106               quantity: toTransact,
107               options: wsOptions
108             };
109             data.push(wsParams);
110           }
111           workspace.doPrevious();
112         }
113
114         i++;
115         // If we've worked through all the models then forward to the server
116         if (i === models.length) {
117           if (data[0]) {
118             that.doProcessingChanged({isProcessing: true});
119             dispOptions.success = function () {
120               _.each(that._printModels, function (printModel) {
121                 // XXX eventually replace _auxilliaryInfo with meta, probably
122                 printModel.doPrint(printModel._auxilliaryInfo);
123               });
124               that.doProcessingChanged({isProcessing: false});
125             };
126             dispOptions.error = function () {
127               that.doProcessingChanged({isProcessing: false});
128             };
129             transModule.transactItem(data, dispOptions, transFunction);
130           } else {
131             return;
132           }
133
134         // Else if there's something here we can transact, handle it
135         } else {
136           model = models[i];
137           toTransact = model.get(model.quantityAttribute);
138           if (toTransact === null) {
139             toTransact = model.get("balance");
140           }
141           transDate = model.transactionDate;
142
143           // See if there's anything to transact here
144           if (toTransact || transactStock) {
145
146             // If prompt or distribution detail required,
147             // open a workspace to handle it
148             if (prompt || model.undistributed()) {
149               that.doWorkspace({
150                 workspace: transWorkspace,
151                 id: model.id,
152                 callback: callback,
153                 allowNew: false,
154                 success: function (model) {
155                   model.transactionDate = transDate;
156                 }
157               });
158
159             // Otherwise just use the data we have
160             } else {
161               if (transFunction === "receipt") {
162                 options.freight = model.get("freight");
163               }
164               options.asOf = transDate;
165               options.detail = model.formatDetail();
166               params = {
167                 orderLine: model.id,
168                 quantity: toTransact,
169                 options: options
170               };
171               data.push(params);
172               callback();
173             }
174
175           // Nothing to transact, move on
176           } else {
177             callback();
178           }
179         }
180       };
181       callback();
182     },
183     transactAll: function () {
184       var models = this.getValue().models;
185       this.transact(models);
186     },
187     transactLine: function () {
188       var models = this.selectedModels();
189       this.transact(models);
190     },
191     transactItem: function () {
192       var models = this.selectedModels();
193       this.transact(models, true, true);
194     },
195     returnItem: function () {
196       var models = this.selectedModels(),
197         that = this,
198         data =  [],
199         options = {},
200         qtyTransacted,
201         model,
202         i,
203         transModule = that.getTransModule();
204
205       for (i = 0; i < models.length; i++) {
206         model = models[i];
207         qtyTransacted = model.get(model.quantityTransactedAttribute);
208
209         // See if there's anything to transact here
210         if (qtyTransacted) {
211           data.push(model.id);
212         }
213       }
214
215       if (data.length) {
216         that.doProcessingChanged({isProcessing: true});
217         options.success = function () {
218           that.doProcessingChanged({isProcessing: false});
219         };
220         transModule.returnItem(data, options);
221       }
222     },
223     selectedModels: function () {
224       var collection = this.getValue(),
225         models = [],
226         selected,
227         prop;
228       if (collection.length) {
229         selected = this.getSelection().selected;
230         for (prop in selected) {
231           if (selected.hasOwnProperty(prop)) {
232             models.push(this.getModel(prop - 0));
233           }
234         }
235       }
236       return models;
237     }
238   });
239
240 }());
241