99ea887228a09c971470709cd97c3772dd5439c3
[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         @param {String} Optional to handle the transaction function name, if not passed
59         it will use the published value. Used by ReturnMaterial's actions.
60         @param {String} Optional to handle the workspace name, if not passed
61         it will use the published value. Used by ReturnMaterial's actions.
62         @param {String} Optional to handle the quantity attr name, if not passed
63         it will use the model.quantityAttribute. Used by ReturnMaterial's actions.
64     */
65     transact: function (models, prompt, transFunction, transWorkspace, transQty) {
66       var that = this,
67         i = -1,
68         callback,
69         data = [];
70
71       // Recursively transact everything we can
72       // #refactor see a simpler implementation of this sort of thing
73       // using async in inventory's ReturnListItem stomp
74       callback = function (workspace, transFunction, transWorkspace, transQty) {
75         var model,
76           options = {},
77           toTransact,
78           transDate,
79           params,
80           dispOptions = {},
81           wsOptions = {},
82           wsParams,
83           transModule = that.getTransModule();
84
85         transFunction = transFunction || that.getTransFunction();
86         transWorkspace = transWorkspace || that.getTransWorkspace();
87
88         that._printModels = [];
89
90         // If argument is false, this whole process was cancelled
91         if (workspace === false) {
92           return;
93
94         // If a workspace brought us here, process the information it obtained
95         } else if (workspace) {
96           model = workspace.getValue();
97           toTransact = transQty ? model.get(transQty) : model.get(model.quantityAttribute);
98           transDate = model.transactionDate;
99
100
101           if (workspace._printAfterPersist) {
102             that._printModels.push(model);
103           }
104
105           if (toTransact) {
106             if (transFunction === "receipt") {
107               wsOptions.freight = model.get("freight");
108             }
109             wsOptions.detail = model.formatDetail();
110             wsOptions.asOf = transDate;
111             wsParams = {
112               orderLine: model.id,
113               quantity: toTransact,
114               options: wsOptions
115             };
116             data.push(wsParams);
117           }
118           workspace.doPrevious();
119         }
120
121         i++;
122         // If we've worked through all the models then forward to the server
123         if (i === models.length) {
124           if (data[0]) {
125             that.doProcessingChanged({isProcessing: true});
126             dispOptions.success = function () {
127               _.each(that._printModels, function (printModel) {
128                 // XXX eventually replace _auxilliaryInfo with meta, probably
129                 printModel.doPrint(printModel._auxilliaryInfo);
130               });
131               that.doProcessingChanged({isProcessing: false});
132             };
133             dispOptions.error = function () {
134               that.doProcessingChanged({isProcessing: false});
135             };
136             transModule.transactItem(data, dispOptions, transFunction);
137           } else {
138             return;
139           }
140
141         // Else if there's something here we can transact, handle it
142         } else {
143           model = models[i];
144           toTransact = transQty ? model.get(transQty) : model.get(model.quantityAttribute);
145           if (toTransact === null) {
146             toTransact = model.get("balance");
147           }
148           transDate = model.transactionDate;
149
150           // See if there's anything to transact here
151           if (toTransact || prompt) {
152
153             // If prompt or distribution detail required,
154             // open a workspace to handle it
155             if (prompt || model.undistributed() || model.requiresDetail()) {
156               that.doWorkspace({
157                 workspace: transWorkspace,
158                 id: model.id,
159                 callback: callback,
160                 allowNew: false,
161                 success: function (model) {
162                   model.transactionDate = transDate;
163                 }
164               });
165
166             // Otherwise just use the data we have
167             } else {
168               if (transFunction === "receipt") {
169                 options.freight = model.get("freight");
170               }
171               options.asOf = transDate;
172               options.detail = model.formatDetail();
173               params = {
174                 orderLine: model.id,
175                 quantity: toTransact,
176                 options: options
177               };
178               data.push(params);
179               callback(null, transFunction, transWorkspace, transQty);
180             }
181
182           // Nothing to transact, move on
183           } else {
184             callback(null, transFunction, transWorkspace, transQty);
185           }
186         }
187       };
188       callback(null, transFunction, transWorkspace, transQty);
189     },
190     transactAll: function () {
191       var models = this.getValue().models;
192       this.transact(models);
193     },
194     transactLine: function () {
195       var models = this.selectedModels();
196       this.transact(models);
197     },
198     transactItem: function () {
199       var models = this.selectedModels();
200       this.transact(models, true);
201     },
202     returnItem: function () {
203       var models = this.selectedModels(),
204         that = this,
205         data =  [],
206         options = {},
207         qtyTransacted,
208         model,
209         i,
210         transModule = that.getTransModule();
211
212       for (i = 0; i < models.length; i++) {
213         model = models[i];
214         qtyTransacted = model.get(model.quantityTransactedAttribute);
215
216         // See if there's anything to transact here
217         if (qtyTransacted) {
218           data.push(model.id);
219         }
220       }
221
222       if (data.length) {
223         that.doProcessingChanged({isProcessing: true});
224         options.success = function () {
225           that.doProcessingChanged({isProcessing: false});
226         };
227         transModule.returnItem(data, options);
228       }
229     },
230     selectedModels: function () {
231       var collection = this.getValue(),
232         models = [],
233         selected,
234         prop;
235       if (collection.length) {
236         selected = this.getSelection().selected;
237         for (prop in selected) {
238           if (selected.hasOwnProperty(prop)) {
239             models.push(this.getModel(prop - 0));
240           }
241         }
242       }
243       return models;
244     }
245   });
246
247 }());
248