Merge branch 'master' of github.com:xtuple/xtuple into 22029-cashreceipt-in-salesorder
[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             transModule.transactItem(data, dispOptions, transFunction);
103           } else {
104             return;
105           }
106
107         // Else if there's something here we can transact, handle it
108         } else {
109           model = models[i];
110           toTransact = model.get(model.quantityAttribute);
111           if (toTransact === null) {
112             toTransact = model.get("balance");
113           }
114           transDate = model.transactionDate;
115
116           // See if there's anything to transact here
117           if (toTransact || transactStock) {
118
119             // If prompt or distribution detail required,
120             // open a workspace to handle it
121             if (prompt || model.undistributed()) {
122               that.doWorkspace({
123                 workspace: transWorkspace,
124                 id: model.id,
125                 callback: callback,
126                 allowNew: false,
127                 success: function (model) {
128                   model.transactionDate = transDate;
129                 }
130               });
131
132             // Otherwise just use the data we have
133             } else {
134               if (transFunction === "receipt") {
135                 options.freight = model.get("freight");
136               }
137               options.asOf = transDate;
138               options.detail = model.formatDetail();
139               params = {
140                 orderLine: model.id,
141                 quantity: toTransact,
142                 options: options
143               };
144               data.push(params);
145               callback();
146             }
147
148           // Nothing to transact, move on
149           } else {
150             callback();
151           }
152         }
153       };
154       callback();
155     },
156     transactAll: function () {
157       var models = this.getValue().models;
158       this.transact(models);
159     },
160     transactLine: function () {
161       var models = this.selectedModels();
162       this.transact(models);
163     },
164     transactItem: function () {
165       var models = this.selectedModels();
166       this.transact(models, true, true);
167     },
168     returnItem: function () {
169       var models = this.selectedModels(),
170         that = this,
171         data =  [],
172         options = {},
173         qtyTransacted,
174         model,
175         i,
176         transModule = that.getTransModule();
177
178       for (i = 0; i < models.length; i++) {
179         model = models[i];
180         qtyTransacted = model.get(model.quantityTransactedAttribute);
181
182         // See if there's anything to transact here
183         if (qtyTransacted) {
184           data.push(model.id);
185         }
186       }
187
188       if (data.length) {
189         that.doProcessingChanged({isProcessing: true});
190         options.success = function () {
191           that.doProcessingChanged({isProcessing: false});
192         };
193         transModule.returnItem(data, options);
194       }
195     },
196     selectedModels: function () {
197       var collection = this.getValue(),
198         models = [],
199         selected,
200         prop;
201       if (collection.length) {
202         selected = this.getSelection().selected;
203         for (prop in selected) {
204           if (selected.hasOwnProperty(prop)) {
205             models.push(this.getModel(prop - 0));
206           }
207         }
208       }
209       return models;
210     }
211   });
212
213 }());
214