AddRemoveStock.js (8795B)
1 Ext.define("PartKeepr.BarcodeScanner.Actions.AddRemoveStock", { 2 extend: "PartKeepr.BarcodeScanner.Action", 3 4 statics: { 5 actionName: i18n("Add/Remove Stock"), 6 actionDescription: i18n("Searches for a part and then allows the user to increase/decrease the stock level"), 7 configure: function (configuration) 8 { 9 configuration = Ext.applyIf(configuration, { 10 searchFields: [], 11 searchMode: 'fixed' 12 }); 13 14 var modelFieldSelector = Ext.create({ 15 xtype: 'modelFieldSelector', 16 id: 'searchPartFieldSelector', 17 border: false, 18 sourceModel: PartKeepr.PartBundle.Entity.Part, 19 initiallyChecked: configuration.searchFields, 20 flex: 1 21 }); 22 23 var saveButton = Ext.create("Ext.button.Button", { 24 text: i18n("OK"), 25 iconCls: 'fugue-icon disk', 26 }); 27 28 var cancelButton = Ext.create("Ext.button.Button", { 29 text: i18n("Cancel"), 30 iconCls: 'web-icon cancel' 31 }); 32 33 var bottomToolbar = Ext.create("Ext.toolbar.Toolbar", { 34 enableOverflow: true, 35 margin: '10px', 36 defaults: {minWidth: 100}, 37 dock: 'bottom', 38 ui: 'footer', 39 items: [saveButton, cancelButton] 40 }); 41 42 var window = Ext.create('Ext.window.Window', { 43 title: i18n("Add/Remove Stock Configuration"), 44 height: 400, 45 modal: true, 46 width: 600, 47 layout: { 48 type: 'vbox', 49 pack: 'start', 50 align: 'stretch' 51 }, 52 items: [ 53 { 54 html: i18n("Select the field(s) to be searched"), 55 border: false, 56 bodyStyle: 'padding: 5px; background:transparent;', 57 }, 58 modelFieldSelector, 59 { 60 xtype: 'radiogroup', 61 layout: 'vbox', 62 itemId: 'searchMode', 63 items: [ 64 { 65 boxLabel: i18n("Search string as-is"), 66 name: 'searchMode', 67 inputValue: "fixed", 68 checked: configuration.searchMode == "fixed" ? true : false 69 }, 70 { 71 boxLabel: i18n("Search beginning of string (string*)"), 72 name: 'searchMode', 73 inputValue: "beginning", 74 checked: configuration.searchMode == "beginning" ? true : false 75 }, { 76 boxLabel: i18n("Search middle of string (*string*)"), 77 name: 'searchMode', 78 inputValue: "any", 79 checked: configuration.searchMode == "any" ? true : false 80 81 } 82 ] 83 } 84 ], 85 dockedItems: bottomToolbar 86 } 87 ).show(); 88 89 saveButton.setHandler(function () 90 { 91 var selection = modelFieldSelector.getChecked(); 92 var fields = []; 93 94 for (var i = 0; i < selection.length; i++) { 95 fields.push(selection[i].data.data.name); 96 } 97 configuration.searchFields = fields; 98 configuration.searchMode = this.down("#searchMode").getValue().searchMode; 99 this.close(); 100 }, window); 101 102 cancelButton.setHandler(function () 103 { 104 this.close(); 105 106 }, window); 107 108 } 109 }, 110 111 execute: function () 112 { 113 this.searchStore = Ext.create("Ext.data.Store", { 114 model: 'PartKeepr.PartBundle.Entity.Part', 115 autoLoad: false, 116 autoSync: false, 117 remoteFilter: true, 118 remoteSort: true 119 }); 120 121 var subFilters = []; 122 var searchValue; 123 124 switch (this.config.searchMode) { 125 case "beginning": 126 searchValue = this.data + "%"; 127 break; 128 case "any": 129 searchValue = "%" + this.data + "%"; 130 break; 131 default: 132 searchValue = this.data; 133 break; 134 } 135 136 for (var i = 0; i < this.config.searchFields.length; i++) { 137 subFilters.push(Ext.create("PartKeepr.util.Filter", { 138 property: this.config.searchFields[i], 139 operator: "LIKE", 140 value: searchValue 141 })); 142 } 143 144 this.filter = Ext.create("PartKeepr.util.Filter", { 145 type: "OR", 146 subfilters: subFilters 147 }); 148 149 this.searchStore.on("load", this.onDataLoaded, this); 150 this.searchStore.addFilter(this.filter, true); 151 this.searchStore.load({start: 0}); 152 }, 153 onDataLoaded: function () 154 { 155 if (this.searchStore.getCount() === 0) { 156 return; 157 } 158 159 if (this.searchStore.getCount() > 1) { 160 var columns = [{header: 'Name', dataIndex: 'name', flex: 1}]; 161 162 for (var i = 0; i < this.config.searchFields.length; i++) { 163 columns.push({header: this.config.searchFields[i], dataIndex: this.config.searchFields[i]}); 164 } 165 166 167 this.window = Ext.create("Ext.window.Window", { 168 itemId: "window", 169 listeners: { 170 show: function () 171 { 172 Ext.defer(function () 173 { 174 this.down("#grid").getView().focusRow(0); 175 }, 50, this); 176 177 this.down("#grid").getSelectionModel().selectRange(0, 0); 178 179 } 180 }, 181 title: i18n("Multiple Parts found"), 182 width: 800, 183 height: 400, 184 layout: 'fit', 185 buttons: [ 186 { 187 text: i18n("OK"), 188 handler: function (btn) 189 { 190 var sel = btn.up("#window").down("#grid").getSelection(); 191 192 if (sel.length === 1) { 193 btn.up("#window").fireEvent("recordSelected", sel[0]); 194 btn.up("#window").close(); 195 } 196 } 197 }, { 198 text: i18n("Cancel"), 199 handler: function (btn) 200 { 201 btn.up("#window").close(); 202 } 203 } 204 ], 205 items: { 206 xtype: 'grid', 207 store: this.searchStore, 208 columns: columns, 209 itemId: 'grid', 210 listeners: { 211 rowdblclick: function (grid, record) 212 { 213 grid.up("#window").fireEvent("recordSelected", record); 214 grid.up("#window").close(); 215 }, 216 rowkeydown: function (grid, record, tr, rowIndex, e) 217 { 218 if (e.event.code === "Escape") { 219 grid.up("#window").close(); 220 } 221 222 if (e.event.code === "Enter") { 223 grid.up("#window").fireEvent("recordSelected", record); 224 grid.up("#window").close(); 225 } 226 } 227 } 228 } 229 }); 230 231 this.window.on("recordSelected", this.combinedAddRemoveStockWindow, this); 232 this.window.show(); 233 } else { 234 this.combinedAddRemoveStockWindow(this.searchStore.getAt(0)); 235 } 236 }, 237 combinedAddRemoveStockWindow: function (record) 238 { 239 var j = Ext.create("PartKeepr.Components.Part.AddRemoveStockWindow", {record: record}); 240 241 j.show(); 242 } 243 });