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