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