merge master
[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: "",
20       transWorkspace: XM.Inventory,
21       transFunction: "issueToShipping"
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             wsOptions.detail = model.formatDetail();
79             wsOptions.asOf = transDate;
80             wsParams = {
81               orderLine: model.id,
82               quantity: toTransact,
83               options: wsOptions
84             };
85             data.push(wsParams);
86           }
87           workspace.doPrevious();
88         }
89
90         i++;
91         // If we've worked through all the models then forward to the server
92         if (i === models.length) {
93           if (data[0]) {
94             that.doProcessingChanged({isProcessing: true});
95             dispOptions.success = function () {
96               that.doProcessingChanged({isProcessing: false});
97             };
98             transModule.transactItem(data, dispOptions, transFunction);
99           } else {
100             return;
101           }
102
103         // Else if there's something here we can transact, handle it
104         } else {
105           model = models[i];
106           toTransact = model.get(model.quantityAttribute);
107           if (toTransact === null) {
108             toTransact = model.get("balance");
109           }
110           transDate = model.transactionDate;
111
112           // See if there's anything to transact here
113           if (toTransact || transactStock) {
114
115             // If prompt or distribution detail required,
116             // open a workspace to handle it
117             if (prompt || model.undistributed()) {
118               that.doWorkspace({
119                 workspace: transWorkspace,
120                 id: model.id,
121                 callback: callback,
122                 allowNew: false,
123                 success: function (model) {
124                   model.transactionDate = transDate;
125                 }
126               });
127
128             // Otherwise just use the data we have
129             } else {
130               options.asOf = transDate;
131               options.detail = model.formatDetail();
132               params = {
133                 orderLine: model.id,
134                 quantity: toTransact,
135                 options: options
136               };
137               data.push(params);
138               callback();
139             }
140
141           // Nothing to transact, move on
142           } else {
143             callback();
144           }
145         }
146       };
147       callback();
148     },
149     transactAll: function () {
150       var models = this.getValue().models;
151       this.transact(models);
152     },
153     transactLine: function () {
154       var models = this.selectedModels();
155       this.transact(models);
156     },
157     transactItem: function () {
158       var models = this.selectedModels();
159       this.transact(models, true, true);
160     },
161     returnItem: function () {
162       var models = this.selectedModels(),
163         that = this,
164         data =  [],
165         options = {},
166         qtyTransacted,
167         model,
168         i,
169         transModule = that.getTransModule();
170
171       for (i = 0; i < models.length; i++) {
172         model = models[i];
173         qtyTransacted = model.get(model.quantityTransactedAttribute);
174
175         // See if there's anything to transact here
176         if (qtyTransacted) {
177           data.push(model.id);
178         }
179       }
180
181       if (data.length) {
182         that.doProcessingChanged({isProcessing: true});
183         options.success = function () {
184           that.doProcessingChanged({isProcessing: false});
185         };
186         transModule.returnItem(data, options);
187       }
188     },
189     selectedModels: function () {
190       var collection = this.getValue(),
191         models = [],
192         selected,
193         prop;
194       if (collection.length) {
195         selected = this.getSelection().selected;
196         for (prop in selected) {
197           if (selected.hasOwnProperty(prop)) {
198             models.push(this.getModel(prop - 0));
199           }
200         }
201       }
202       return models;
203     }
204   });
205
206 }());
207