WCPlanMaterialLackGrid.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. Ext.define('erp.view.pm.make.WCPlanMaterialLackGrid',{
  2. extend: 'Ext.grid.Panel',
  3. alias: 'widget.erpBatchDealGridPanel',
  4. id: 'batchDealGridPanel',
  5. emptyText : $I18N.common.grid.emptyText,
  6. columnLines : true,
  7. autoScroll : true,
  8. store: [],
  9. region: 'south',
  10. layout : 'fit',
  11. columns: new Array(),
  12. height:height,
  13. GridUtil: Ext.create('erp.util.GridUtil'),
  14. bodyStyle:'background-color:#f1f1f1;',
  15. selModel: Ext.create('Ext.selection.CheckboxModel',{
  16. headerWidth: 0
  17. }),
  18. /*enableLocking:true,*/
  19. /*lockable: true,*/
  20. GridUtil: Ext.create('erp.util.GridUtil'),
  21. BaseUtil: Ext.create('erp.util.BaseUtil'),
  22. plugins: [Ext.create('Ext.grid.plugin.CellEditing', {
  23. clicksToEdit: 1,
  24. listeners:{
  25. 'edit':function(editor,e,Opts){
  26. var record=e.record,data=null,qty=0;
  27. if(record){
  28. data=record.data;
  29. for(var property in data){
  30. if(!/^[a-zA-Z]/.test(property)){
  31. qty+=data[property];
  32. }
  33. }
  34. if(qty>(data.ma_qty-data.ma_madeqty)){
  35. showError('排产数不能超过未交数!请重新输入');
  36. e.record.reject();
  37. }
  38. }
  39. }
  40. }
  41. })],
  42. /* plugins: [Ext.create('Ext.ux.grid.GridHeaderFilters')],*/
  43. initComponent : function(){
  44. var condition = this.condition;
  45. if(!condition){
  46. var urlCondition = this.BaseUtil.getUrlParam('gridCondition');
  47. urlCondition = urlCondition == null || urlCondition == "null" ? "" : urlCondition;
  48. gridCondition = (gridCondition == null || gridCondition == "null") ? "" : gridCondition;
  49. gridCondition = gridCondition + urlCondition;
  50. gridCondition = gridCondition.replace(/IS/g, "=");
  51. if(gridCondition.search(/!/) != -1){
  52. gridCondition = gridCondition.substring(0, gridCondition.length - 4);
  53. }
  54. condition = gridCondition;
  55. }
  56. var gridParam = {caller: this.caller || caller, condition: condition};
  57. this.getGridColumnsAndStore(this, 'pm/make/singleGridPanel.action', gridParam, "");//从后台拿到gridpanel的配置及数据
  58. this.callParent(arguments);
  59. },
  60. loadNewStore:function(grid, url, param, no){
  61. Ext.Ajax.request({//拿到grid的columns
  62. url : basePath + url,
  63. params: param,
  64. method : 'post',
  65. callback : function(options,success,response){
  66. var res = new Ext.decode(response.responseText);
  67. if(res.exceptionInfo){
  68. showError(res.exceptionInfo);return;
  69. }
  70. var data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  71. //对data 进行处理 必须要先取到主记录的开始时间和结束时间
  72. var Map={},arr=new Array();
  73. var len = data.length;
  74. for(var i=0;i<len;i++){
  75. if(Map[data[i].wd_makecode] == undefined){
  76. var smalldata=data[i];
  77. var startfield=Ext.Date.format(new Date(startdate),'Y-m-d');
  78. var endfield=Ext.Date.format(new Date(enddate),'Y-m-d');
  79. if(new Date(smalldata.ml_begindate)<new Date(startdate)){
  80. smalldata[startfield]=smalldata.ml_mrponhandlack;
  81. }else if(new Date(smalldata.ml_begindate)>new Date(enddate)) smalldata[endfield]=smalldata.ml_mrponhandlack;
  82. else smalldata[smalldata.ml_begindate.substring(0,10)]=smalldata.ml_mrponhandlack;
  83. Map[data[i].ml_makecode] =smalldata;
  84. }else{
  85. var smalldata=Map[data[i].ml_makecode];
  86. smalldata[data[i].ml_begindate.substring(0,10)]=data[i].ml_mrponhandlack;
  87. Map[data[i].ml_makecode]=smalldata;
  88. }
  89. }
  90. for(var property in Map){
  91. arr.push(Map[property]);
  92. }
  93. data=arr;
  94. grid.store.loadData(data);
  95. }
  96. });
  97. },
  98. getGridColumnsAndStore: function(grid, url, param, no){
  99. var me = this;
  100. grid.setLoading(true);
  101. Ext.Ajax.request({//拿到grid的columns
  102. url : basePath + url,
  103. params: param,
  104. async:false,
  105. method : 'post',
  106. callback : function(options,success,response){
  107. grid.setLoading(false);
  108. var res = new Ext.decode(response.responseText);
  109. if(res.exceptionInfo){
  110. showError(res.exceptionInfo);return;
  111. }
  112. if(res.columns){
  113. var limits = res.limits, limitArr = new Array();
  114. if(limits != null && limits.length > 0) {//权限外字段
  115. limitArr = Ext.Array.pluck(limits, 'lf_field');
  116. }
  117. Ext.each(res.columns, function(column, y){
  118. //power
  119. if(limitArr.length > 0 && Ext.Array.contains(limitArr, column.dataIndex)) {
  120. column.hidden = true;
  121. }
  122. //renderer
  123. me.setRenderer(grid, column);
  124. //logictype
  125. me.setLogicType(grid, column);
  126. });
  127. //data
  128. var data = [];
  129. var gridfields=res.fields;
  130. if(!res.data || res.data.length == 2){
  131. me.add10EmptyData(grid.detno, data);
  132. me.add10EmptyData(grid.detno, data);//添加20条空白数据
  133. } else {
  134. data = Ext.decode(res.data.replace(/,}/g, '}').replace(/,]/g, ']'));
  135. //对data 进行处理 必须要先取到主记录的开始时间和结束时间
  136. var Map={},arr=new Array();
  137. var len = data.length;
  138. for(var i=0;i<len;i++){
  139. if(Map[data[i].ml_makecode] == undefined){
  140. var smalldata=data[i];
  141. smalldata[smalldata.ml_begindate.substring(0,10)]=smalldata.ml_mrponhandlack;
  142. Map[data[i].ml_makecode] =smalldata;
  143. }else{
  144. var smalldata=Map[data[i].ml_makecode];
  145. smalldata[data[i].ml_begindate.substring(0,10)]=data[i].ml_mrponhandlack;
  146. smalldata.ml_mrponhandlack+=data[i].ml_mrponhandlack;
  147. Map[data[i].ml_makecode]=smalldata;
  148. }
  149. }
  150. for(var property in Map){
  151. arr.push(Map[property]);
  152. }
  153. data=arr;
  154. }
  155. //store
  156. //view
  157. if(grid.selModel.views == null){
  158. grid.selModel.views = [];
  159. }
  160. //dbfind
  161. if(res.dbfinds&&res.dbfinds.length > 0){
  162. grid.dbfinds = res.dbfinds;
  163. }
  164. //toolbar
  165. me.setToolbar(grid, res.columns, res.necessaryField, limitArr);
  166. gridcolumns=res.columns;
  167. startdate=Ext.Date.add(new Date(startdate), Ext.Date.DAY, -1);
  168. enddate=Ext.Date.add(new Date(enddate), Ext.Date.DAY, -1);
  169. if(startdate!=null){
  170. var lastcolumns=[];
  171. Ext.Array.each(gridcolumns,function(item,m){
  172. if(item.dataIndex=='ml_begindate'){
  173. var columns=new Array();
  174. var count=(enddate-startdate)/86400000;
  175. for(var i=0;i<count+1;i++){
  176. var field=Ext.Date.format(Ext.Date.add(startdate,Ext.Date.DAY,i),'Y-m-d');
  177. gridfields.push({
  178. name:field,
  179. type:'int'
  180. });
  181. columns.push({
  182. readOnly:false,
  183. header:field.substring(5,10),
  184. cls: "x-grid-header-1",
  185. dataIndex:field,
  186. width : 60,
  187. xtype:'numbercolumn',
  188. align:'right',
  189. format:'0',
  190. editor:{
  191. xtype:'numberfield',
  192. format:'0',
  193. hideTrigger: true
  194. }
  195. });
  196. }
  197. item.columns=columns;
  198. }
  199. lastcolumns[m]=item;
  200. });
  201. gridcolumns=lastcolumns;
  202. }
  203. var store = me.setStore(gridfields, data, grid.groupField, grid.necessaryField);
  204. grid.store=store;
  205. grid.columns=gridcolumns;
  206. var form = Ext.ComponentQuery.query('form')[0];
  207. if(form){
  208. if(form.readOnly){
  209. grid.readOnly = true;//grid不可编辑
  210. }
  211. }
  212. /*var lockedView = grid.view.lockedView;
  213. if(lockedView){
  214. var tableEl = lockedView.el;
  215. if(tableEl){
  216. tableEl.dom.style.marginBottom = '27px';
  217. }
  218. }*/
  219. } else {
  220. grid.hide();
  221. var form = Ext.ComponentQuery.query('form')[0];
  222. me.updateFormPosition(form);//字段较少时,修改form布局
  223. }
  224. }
  225. });
  226. },
  227. setRenderer: function(grid, column){
  228. if(!column.haveRendered && column.renderer != null && column.renderer != ""){
  229. if(!grid.RenderUtil){
  230. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  231. }
  232. var renderName = column.renderer;
  233. if(contains(column.renderer, ':', true)){
  234. var args = new Array();
  235. Ext.each(column.renderer.split(':'), function(a, index){
  236. if(index == 0){
  237. renderName = a;
  238. } else {
  239. args.push(a);
  240. }
  241. });
  242. if(!grid.RenderUtil.args[renderName]){
  243. grid.RenderUtil.args[renderName] = new Object();
  244. }
  245. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  246. }
  247. column.renderer = grid.RenderUtil[renderName];
  248. column.haveRendered = true;
  249. }
  250. },
  251. setLogicType: function(grid, column){
  252. var logic = column.logic;
  253. if(logic != null){
  254. if(logic == 'detno'){
  255. grid.detno = column.dataIndex;
  256. column.width = 40;
  257. column.renderer = function(val, meta) {
  258. meta.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
  259. return val;
  260. };
  261. } else if(logic == 'keyField'){
  262. grid.keyField = column.dataIndex;
  263. } else if(logic == 'mainField'){
  264. grid.mainField = column.dataIndex;
  265. } else if(logic == 'necessaryField'){
  266. grid.necessaryField = column.dataIndex;
  267. if(!grid.necessaryFields){
  268. grid.necessaryFields = new Array();
  269. }
  270. grid.necessaryFields.push(column.dataIndex);
  271. if(!column.haveRendered){
  272. column.renderer = function(val, meta, record, x, y, store, view){
  273. var c = this.columns[y];
  274. if(val != null && val.toString().trim() != ''){
  275. if(c.xtype == 'datecolumn'){
  276. val = Ext.Date.format(val, 'Y-m-d');
  277. }
  278. return val;
  279. } else {
  280. if(c.xtype == 'datecolumn'){
  281. val = '';
  282. }
  283. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  284. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  285. }
  286. };
  287. }
  288. } else if(logic == 'groupField'){
  289. grid.groupField = column.dataIndex;
  290. }
  291. }
  292. },
  293. setStore: function(fields, data, groupField, necessaryField){
  294. griddata=data;
  295. if(!Ext.isChrome){
  296. Ext.each(fields, function(f){
  297. if(f.type == 'date'){
  298. f.dateFormat = 'Y-m-d H:m:s';
  299. }
  300. });
  301. }
  302. return Ext.create('Ext.data.Store', {
  303. fields: fields,
  304. data: data,
  305. groupField: groupField,
  306. getSum: function(records, field) {
  307. if (arguments.length < 2) {
  308. return 0;
  309. }
  310. var total = 0,
  311. i = 0,
  312. len = records.length;
  313. if(necessaryField) {
  314. for (; i < len; ++i) {//重写getSum,grid在合计时,只合计填写了必要信息的行
  315. var necessary = records[i].get(necessaryField);
  316. if(necessary != null && necessary != ''){
  317. total += records[i].get(field);
  318. }
  319. }
  320. } else {
  321. for (; i < len; ++i) {
  322. total += records[i].get(field);
  323. }
  324. }
  325. return total;
  326. },
  327. getCount: function() {
  328. if(necessaryField) {
  329. var count = 0;
  330. Ext.each(this.data.items, function(item){//重写getCount,grid在合计时,只合计填写了必要信息的行
  331. if(item.data[necessaryField] != null && item.data[necessaryField] != ''){
  332. count++;
  333. }
  334. });
  335. return count;
  336. }
  337. return this.data.items.length;
  338. }
  339. });
  340. },
  341. setToolbar: function(grid, columns, necessaryField, limitArr){
  342. var items = [];
  343. var bool = true;
  344. if(!grid.dockedItems)
  345. return;
  346. Ext.each(grid.dockedItems.items, function(item){
  347. if(item.dock == 'bottom' && item.items){//bbar已存在
  348. bool = false;
  349. }
  350. });
  351. if(bool){
  352. Ext.each(columns, function(column){
  353. if(limitArr.length == 0 || !Ext.Array.contains(limitArr, column.dataIndex)) {
  354. if(column.summaryType == 'sum'){
  355. items.push('-',{
  356. id: column.dataIndex + '_sum',
  357. itemId: column.dataIndex,
  358. xtype: 'tbtext',
  359. text: column.header + '(sum):0'
  360. });
  361. } else if(column.summaryType == 'average') {
  362. items.push('-',{
  363. id: column.dataIndex + '_average',
  364. itemId: column.dataIndex,
  365. xtype: 'tbtext',
  366. text: column.header + '(average):0'
  367. });
  368. } else if(column.summaryType == 'count') {
  369. items.push('-',{
  370. id: column.dataIndex + '_count',
  371. itemId: column.dataIndex,
  372. xtype: 'tbtext',
  373. text: column.header + '(count):0'
  374. });
  375. }
  376. }
  377. if(column.dataIndex == necessaryField){
  378. column.renderer = function(val){
  379. if(val != null && val.toString().trim() != ''){
  380. return val;
  381. } else {
  382. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  383. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  384. }
  385. };
  386. }
  387. });
  388. grid.addDocked({
  389. xtype: 'toolbar',
  390. dock: 'bottom',
  391. items: items
  392. });
  393. } else {
  394. var bars = Ext.ComponentQuery.query('erpToolbar');
  395. if(bars.length > 0){
  396. Ext.each(columns, function(column){
  397. if(column.summaryType == 'sum'){
  398. bars[0].add('-');
  399. bars[0].add({
  400. id: column.dataIndex + '_sum',
  401. itemId: column.dataIndex,
  402. xtype: 'tbtext',
  403. text: column.header + '(sum):0'
  404. });
  405. } else if(column.summaryType == 'average') {
  406. bars[0].add('-');
  407. bars[0].add({
  408. id: column.dataIndex + '_average',
  409. itemId: column.dataIndex,
  410. xtype: 'tbtext',
  411. text: column.header + '(average):0'
  412. });
  413. } else if(column.summaryType == 'count') {
  414. bars[0].add('-');
  415. bars[0].add({
  416. id: column.dataIndex + '_count',
  417. itemId: column.dataIndex,
  418. xtype: 'tbtext',
  419. text: column.header + '(count):0'
  420. });
  421. }
  422. });
  423. }
  424. }
  425. },
  426. add10EmptyData: function(detno, data){
  427. if(detno){
  428. var index = data.length == 0 ? 0 : Number(data[data.length-1][detno]);
  429. for(var i=0;i<10;i++){
  430. var o = new Object();
  431. o[detno] = index + i + 1;
  432. data.push(o);
  433. }
  434. } else {
  435. for(var i=0;i<10;i++){
  436. var o = new Object();
  437. data.push(o);
  438. }
  439. }
  440. },
  441. checkGridDirty: function(grid){
  442. var s = grid.getStore().data.items;//获取store里面的数据
  443. var msg = '';
  444. for(var i=0;i<s.length;i++){
  445. if(s[i].dirty){//明细行被修改过哦
  446. msg = msg + '第' + (i+1) + '行:';
  447. var changed = Ext.Object.getKeys(s[i].modified);//拿到被修改了的字段
  448. for(var j = 0;j < changed.length;j++){
  449. Ext.each(grid.columns, function(c, index){
  450. if(index == 0){
  451. return;
  452. }
  453. if(c.dataIndex == changed[j] && c.logic != 'ignore'){//由字段dataindex匹配其text
  454. msg = msg + '<font color=blue>' + c.text + '</font>&nbsp;';
  455. }
  456. });
  457. }
  458. msg = msg + ";";
  459. }
  460. }
  461. if(msg != '' && msg != ';'){//明细行被修改过哦
  462. msg = "明细行<font color=green>" + msg.substring(0, msg.length-1) + "</font>已编辑过";
  463. }
  464. return msg;
  465. },
  466. getGridStore: function(grid){
  467. if(grid == null){
  468. grid = Ext.getCmp('grid');
  469. }
  470. var me = this,
  471. jsonGridData = new Array();
  472. var form = Ext.getCmp('form');
  473. var s = grid.getStore().data.items;//获取store里面的数据
  474. var dd;
  475. for(var i=0;i<s.length;i++){//将grid里面各行的数据获取并拼成jsonGridData
  476. var data = s[i].data;
  477. dd = new Object();
  478. if(s[i].dirty && !me.isBlank(grid, data)){
  479. Ext.each(gridcolumns, function(c){
  480. if(c.logic != 'ignore'){//只需显示,无需后台操作的字段,自动略去
  481. if(c.dataIndex=='wd_date'){
  482. Ext.Array.each(c.columns,function(column){
  483. dd[column.dataIndex]=data[column.dataIndex];
  484. });
  485. }else {
  486. if(c.xtype == 'datecolumn'){
  487. c.format = c.format || 'Y-m-d';
  488. if(Ext.isDate(data[c.dataIndex])){
  489. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], c.format);
  490. } else {
  491. if(c.editor){
  492. dd[c.dataIndex] = Ext.Date.format(new Date(), c.format);//如果用户没输入日期,或输入有误,就给个默认日期,
  493. }
  494. }
  495. } else if(c.xtype == 'datetimecolumn'){
  496. if(Ext.isDate(data[c.dataIndex])){
  497. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], 'Y-m-d H:i:s');//在这里把GMT日期转化成Y-m-d H:i:s格式日期
  498. } else {
  499. if(c.editor){
  500. dd[c.dataIndex] = Ext.Date.format(new Date(), 'Y-m-d H:i:s');//默认日期,
  501. }
  502. }
  503. } else if(c.xtype == 'numbercolumn'){//赋个默认值0吧,不然不好保存
  504. if(data[c.dataIndex] == null || data[c.dataIndex] == ''){
  505. dd[c.dataIndex] = '0';//也可以从data里面去掉这些字段
  506. } else {
  507. dd[c.dataIndex] = s[i].data[c.dataIndex];
  508. }
  509. } else {
  510. dd[c.dataIndex] = s[i].data[c.dataIndex];
  511. }
  512. }
  513. }
  514. });
  515. if(grid.mainField && form && form.keyField){//例如,将pu_id的值赋给pd_puid
  516. dd[grid.mainField] = Ext.getCmp(form.keyField).value;
  517. }
  518. jsonGridData.push(Ext.JSON.encode(dd));
  519. }
  520. }
  521. return jsonGridData;
  522. },
  523. isBlank: function(grid, data) {
  524. var ff = grid.necessaryFields,bool = true;
  525. if(ff) {
  526. Ext.each(ff, function(f) {
  527. if(!Ext.isEmpty(data[f]) && data[f] != 0) {
  528. bool = false;
  529. }else {
  530. bool=true;
  531. return
  532. }
  533. });
  534. } else {
  535. if(!grid.necessaryField || !Ext.isEmpty(data[grid.necessaryField])) {
  536. bool = false;
  537. }
  538. }
  539. return bool;
  540. },
  541. });