MetaPartSubgrid.js (5481B)
1 /** 2 * This is a workaround to make grid linking possible. 3 */ 4 Ext.define('PartKeepr.Components.Project.MetaPartSubgrid', { 5 extend: "Ext.grid.Panel", 6 xtype: 'gridfoo', 7 8 parentRecord: null, 9 setParentRecord: function (v) 10 { 11 this.parentRecord = v; 12 }, 13 autoLoad: false, 14 15 16 defaultListenerScope: true, 17 columns: [ 18 { 19 text: i18n("Use"), 20 xtype: 'checkcolumn', 21 listeners: { 22 checkchange: "onCheckStateChange" 23 }, 24 dataIndex: 'use' 25 }, 26 { 27 text: i18n("Part Name"), 28 dataIndex: "name" 29 }, { 30 text: i18n("Description"), 31 dataIndex: "description" 32 }, { 33 text: i18n("Production Remarks"), 34 dataIndex: "productionRemarks" 35 }, { 36 text: i18n("Storage Location"), 37 renderer: function (v, m, r) 38 { 39 return r.get("storageLocation.name"); 40 } 41 }, { 42 text: i18n("Stock Level"), 43 dataIndex: 'stockLevel', 44 renderer: function (value, metaData, record) { 45 return value + " " + record.getPartUnit().get("shortName"); 46 } 47 }, { 48 text: i18n("Stock to use"), 49 dataIndex: 'stockToUse', 50 editor: { 51 field: { 52 xtype: 'numberfield' 53 } 54 }, 55 renderer: function (value, metaData, record) { 56 if (typeof(value) === "undefined") { 57 value = 0; 58 } 59 return value + " " + record.getPartUnit().get("shortName"); 60 } 61 } 62 ], 63 bbar: [ 64 { 65 xtype: 'button', 66 text: i18n("Apply Parts"), 67 disabled: true, 68 handler: "onApplyMetaPartsClick", 69 itemId: 'applyPartsButton' 70 } 71 ], 72 initComponent: function () { 73 this.subGridEditing = Ext.create('Ext.grid.plugin.CellEditing', { 74 clicksToEdit: 1, 75 listeners: { 76 edit: this.onAfterSubGridEdit, 77 scope: this 78 } 79 }); 80 81 this.plugins = [this.subGridEditing]; 82 83 this.callParent(arguments); 84 }, 85 onApplyMetaPartsClick: function (button) 86 { 87 var parentRecord = button.up("grid").parentRecord; 88 89 this.convertMetaPartsToParts(parentRecord); 90 }, 91 /** 92 * Converts meta parts to parts. Iterates over the sub parts and figures out which actual parts to create, then 93 * removes the original meta part. 94 * 95 * @param record 96 */ 97 convertMetaPartsToParts: function (record) 98 { 99 var missing; 100 101 var i, projectReportItem, subPart; 102 103 for (i = 0; i < record.subParts().getCount(); i++) 104 { 105 subPart = record.subParts().getAt(i); 106 107 if (subPart.get("use")) 108 { 109 missing = subPart.get("stockLevel") - subPart.get("stockToUse"); 110 111 if (missing >= 0) { 112 missing = 0; 113 } else { 114 missing = Math.abs(missing); 115 } 116 117 projectReportItem = Ext.create("PartKeepr.ProjectBundle.Entity.ReportPart"); 118 projectReportItem.setPart(subPart); 119 projectReportItem.set("quantity", subPart.get("stockToUse")); 120 projectReportItem.setReport(this.up("#projectReportResult").projectReport); 121 122 123 record.store.add(projectReportItem); 124 } 125 } 126 127 record.store.remove(record); 128 }, 129 130 /** 131 * Handles the change of the meta parts subgrid checkbox. 132 * 133 * @param check 134 * @param rowIndex 135 * @param checked 136 * @param record 137 */ 138 onCheckStateChange: function (check, rowIndex, checked, record) 139 { 140 var grid = check.up("grid"); 141 142 if (checked) 143 { 144 if (record.get("stockToUse") === 0 || record.get("stockToUse") === undefined) 145 { 146 var total = this.getAppliedPartCount(grid); 147 var missing = grid.parentRecord.get("quantity") - total; 148 149 if (missing <= record.get("stockLevel")) { 150 record.set("stockToUse", missing); 151 } else { 152 record.set("stockToUse", record.get("stockLevel")); 153 } 154 } 155 } 156 157 Ext.defer(this.updateSubGrid, 100, this, [check.up("grid")]); 158 }, 159 onAfterSubGridEdit: function (editor, context) 160 { 161 context.record.set("stockToUse", context.value); 162 163 Ext.defer(this.updateSubGrid, 100, this, [context.grid]); 164 }, 165 updateSubGrid: function (grid) 166 { 167 var total = this.getAppliedPartCount(grid); 168 169 if (total === grid.parentRecord.get("quantity")) 170 { 171 grid.down("#applyPartsButton").enable(); 172 } else 173 { 174 grid.down("#applyPartsButton").disable(); 175 } 176 }, 177 getAppliedPartCount: function (grid) { 178 179 var subParts = grid.parentRecord.subParts(); 180 var i, total; 181 182 total = 0; 183 for (i = 0; i < subParts.getCount(); i++) 184 { 185 if (subParts.getAt(i).get("use")) 186 { 187 if (!isNaN(subParts.getAt(i).get("stockToUse"))) { 188 total += subParts.getAt(i).get("stockToUse"); 189 } 190 } 191 } 192 return total; 193 } 194 195 });