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 */
9 Expected to a have a parameter widget that contains an order and
12 @name XV.TransactionList
16 /** @lends XV.TransactionList */{
17 name: "XV.TransactionList",
25 onProcessingChanged: "",
30 onBarcodeCapture: "captureBarcode"
33 showDeleteAction: false,
36 {name: "transactItem", prerequisite: "canTransactItem",
37 method: "transactItem", notify: false, isViewMethod: true},
38 {name: "transactLine", prerequisite: "canTransactItem",
39 method: "transactLine", notify: false, isViewMethod: true},
40 {name: "returnLine", prerequisite: "canReturnItem",
41 method: "returnItem", notify: false, isViewMethod: true}
43 captureBarcode: function (inSender, inEvent) {
44 var models = _.filter(this.value.models, function (model) {
45 // match on upc code or item number
46 return model.getValue("itemSite.item.barcode") === inEvent.data ||
47 model.getValue("itemSite.item.number") === inEvent.data;
49 if (models.length > 0) {
50 this.transact(models, true, true);
54 Helper function for transacting `transact` on an array of models.
57 @param {Boolean} Prompt user for confirmation on every model
58 @param {String} Optional to handle the transaction function name, if not passed
59 it will use the published value. Used by ReturnMaterial's actions.
60 @param {String} Optional to handle the workspace name, if not passed
61 it will use the published value. Used by ReturnMaterial's actions.
62 @param {String} Optional to handle the quantity attr name, if not passed
63 it will use the model.quantityAttribute. Used by ReturnMaterial's actions.
65 transact: function (models, prompt, transFunction, transWorkspace, transQty) {
71 // Recursively transact everything we can
72 // #refactor see a simpler implementation of this sort of thing
73 // using async in inventory's ReturnListItem stomp
74 callback = function (workspace, transFunction, transWorkspace, transQty) {
83 transModule = that.getTransModule();
85 transFunction = transFunction || that.getTransFunction();
86 transWorkspace = transWorkspace || that.getTransWorkspace();
88 that._printModels = [];
90 // If argument is false, this whole process was cancelled
91 if (workspace === false) {
94 // If a workspace brought us here, process the information it obtained
95 } else if (workspace) {
96 model = workspace.getValue();
97 toTransact = transQty ? model.get(transQty) : model.get(model.quantityAttribute);
98 transDate = model.transactionDate;
101 if (workspace._printAfterPersist) {
102 that._printModels.push(model);
106 if (transFunction === "receipt") {
107 wsOptions.freight = model.get("freight");
109 wsOptions.detail = model.formatDetail();
110 wsOptions.asOf = transDate;
113 quantity: toTransact,
118 workspace.doPrevious();
122 // If we've worked through all the models then forward to the server
123 if (i === models.length) {
125 that.doProcessingChanged({isProcessing: true});
126 dispOptions.success = function () {
127 _.each(that._printModels, function (printModel) {
128 // XXX eventually replace _auxilliaryInfo with meta, probably
129 printModel.doPrint(printModel._auxilliaryInfo);
131 that.doProcessingChanged({isProcessing: false});
133 dispOptions.error = function () {
134 that.doProcessingChanged({isProcessing: false});
136 transModule.transactItem(data, dispOptions, transFunction);
141 // Else if there's something here we can transact, handle it
144 toTransact = transQty ? model.get(transQty) : model.get(model.quantityAttribute);
145 if (toTransact === null) {
146 toTransact = model.get("balance");
148 transDate = model.transactionDate;
150 // See if there's anything to transact here
151 if (toTransact || prompt) {
153 // If prompt or distribution detail required,
154 // open a workspace to handle it
155 if (prompt || model.undistributed() || model.requiresDetail()) {
157 workspace: transWorkspace,
161 success: function (model) {
162 model.transactionDate = transDate;
166 // Otherwise just use the data we have
168 if (transFunction === "receipt") {
169 options.freight = model.get("freight");
171 options.asOf = transDate;
172 options.detail = model.formatDetail();
175 quantity: toTransact,
179 callback(null, transFunction, transWorkspace, transQty);
182 // Nothing to transact, move on
184 callback(null, transFunction, transWorkspace, transQty);
188 callback(null, transFunction, transWorkspace, transQty);
190 transactAll: function () {
191 var models = this.getValue().models;
192 this.transact(models);
194 transactLine: function () {
195 var models = this.selectedModels();
196 this.transact(models);
198 transactItem: function () {
199 var models = this.selectedModels();
200 this.transact(models, true);
202 returnItem: function () {
203 var models = this.selectedModels(),
210 transModule = that.getTransModule();
212 for (i = 0; i < models.length; i++) {
214 qtyTransacted = model.get(model.quantityTransactedAttribute);
216 // See if there's anything to transact here
223 that.doProcessingChanged({isProcessing: true});
224 options.success = function () {
225 that.doProcessingChanged({isProcessing: false});
227 transModule.returnItem(data, options);
230 selectedModels: function () {
231 var collection = this.getValue(),
235 if (collection.length) {
236 selected = this.getSelection().selected;
237 for (prop in selected) {
238 if (selected.hasOwnProperty(prop)) {
239 models.push(this.getModel(prop - 0));