AbstractStockHistoryGrid.js (5297B)
1 /** 2 * Represents the stock history grid. 3 */ 4 Ext.define('PartKeepr.AbstractStockHistoryGrid', { 5 extend: 'PartKeepr.BaseGrid', 6 7 pageSize: 25, 8 9 defineColumns: function () { 10 this.columns = [ 11 { 12 header: "", 13 xtype: 'actioncolumn', 14 renderer: function (val, p, rec) { 15 if (rec.get("stockLevel") < 0) { 16 return '<span title="' + i18n( 17 "Parts removed") + '" style="vertical-align: top;" class="web-icon brick_delete">ad</span>'; 18 } else { 19 return '<span title="' + i18n( 20 "Parts added") + '" style="vertical-align: top;" class="web-icon brick_add"></span>'; 21 } 22 }, 23 width: 20 24 }, 25 {header: i18n("Date"), dataIndex: 'dateTime', width: 120}, 26 { 27 header: i18n("User"), 28 flex: 1, 29 minWidth: 80, 30 renderer: function (val, p, rec) { 31 if (rec.getUser() !== null) { 32 return rec.getUser().get("username"); 33 } 34 }, 35 editor: { 36 xtype: 'UserComboBox' 37 } 38 }, 39 { 40 header: i18n("Amount"), dataIndex: 'stockLevel', width: 50, 41 editor: { 42 xtype: 'numberfield', 43 allowBlank: false 44 } 45 }, 46 47 { 48 header: i18n("Price"), 49 editor: { 50 xtype: 'CurrencyField', 51 allowBlank: false 52 }, 53 dataIndex: 'price', 54 width: 60, 55 renderer: function (val, p, rec) { 56 if (rec.get("dir") == "out") { 57 return "-"; 58 } else { 59 return PartKeepr.getApplication().formatCurrency(val); 60 } 61 } 62 }, { 63 header: i18n("Comment"), 64 dataIndex: 'comment', 65 renderer: Ext.util.Format.htmlEncode, 66 width: 60, 67 editor: { 68 xtype: 'textfield', 69 allowBlank: true 70 } 71 } 72 ]; 73 }, 74 model: 'PartKeepr.StockBundle.Entity.StockEntry', 75 /** 76 * Initializes the stock history grid. 77 */ 78 initComponent: function () { 79 80 this.defineColumns(); 81 82 var config = { 83 autoLoad: false, 84 autoSync: true, 85 remoteFilter: true, 86 remoteSort: true, 87 model: this.model, 88 sorters: [ 89 { 90 property: 'dateTime', 91 direction: 'DESC' 92 } 93 ], 94 pageSize: this.pageSize 95 }; 96 97 this.store = Ext.create('Ext.data.Store', config); 98 99 this.editing = Ext.create('Ext.grid.plugin.CellEditing', { 100 clicksToEdit: 1 101 }); 102 103 this.plugins = [this.editing]; 104 105 this.bottomToolbar = Ext.create("PartKeepr.PagingToolbar", { 106 store: this.store, 107 enableOverflow: true, 108 dock: 'bottom', 109 displayInfo: false, 110 grid: this 111 }); 112 113 114 this.dockedItems = []; 115 this.dockedItems.push(this.bottomToolbar); 116 117 this.editing.on("beforeedit", this.onBeforeEdit, this); 118 119 this.callParent(); 120 }, 121 /** 122 * Called before editing a cell. Checks if the user may actually make the requested changes. 123 * 124 * @param e Passed from ExtJS 125 * @returns {Boolean} 126 */ 127 onBeforeEdit: function (editor, context, eOpts) { 128 var sameUser = false; 129 130 // Checks if the usernames match 131 if (context.record.getUser() !== null) { 132 sameUser = context.record.getUser().getId() == PartKeepr.getApplication().getLoginManager().getUser().getId(); 133 } 134 135 switch (context.field) { 136 case "price": 137 // Check the direction is "out". If yes, editing the price field is not allowed 138 if (context.record.get("direction") == "out") { 139 return false; 140 } 141 142 // If it's not the same user or an admin, editing is not allowed 143 if (!sameUser && !PartKeepr.getApplication().isAdmin()) { 144 return false; 145 } 146 break; 147 case "stockLevel": 148 // Only an admin may edit the amount. Regular users must put the stock back in manually. 149 if (!PartKeepr.getApplication().isAdmin()) { 150 return false; 151 } 152 break; 153 case "user": 154 if (!PartKeepr.getApplication().isAdmin()) { 155 return false; 156 } 157 break; 158 case "comment": 159 if (!sameUser && !PartKeepr.getApplication().isAdmin()) { 160 return false; 161 } 162 break; 163 default: 164 return true; 165 } 166 167 return true; 168 } 169 });