PreForecastGrid.js 20 KB

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