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