PreForecastGrid.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. //view
  172. if(grid.selModel.views == null){
  173. grid.selModel.views = [];
  174. }
  175. //dbfind
  176. if(res.dbfinds&&res.dbfinds.length > 0){
  177. grid.dbfinds = res.dbfinds;
  178. }
  179. //toolbar
  180. //me.setToolbar(grid, res.columns, res.necessaryField, limitArr);
  181. gridcolumns=res.columns;
  182. startdate=new Date(res.date.startdate);
  183. enddate=new Date(res.date.enddate);
  184. if(startdate!=null){
  185. var lastcolumns=[];
  186. var startfield="";
  187. var endfield="";
  188. Ext.Array.each(gridcolumns,function(item,m){
  189. if(item.dataIndex=='sd_enddate'){
  190. var columns=new Array();
  191. var count=(enddate-startdate)/(86400000*7);
  192. count = Math.ceil(count);
  193. for(var i=0;i<count;i++){
  194. var sdate=Ext.Date.add(startdate,Ext.Date.DAY,7*i);
  195. startfield=Ext.Date.format(sdate,'Y-m-d');
  196. if(i <count-1){
  197. endfield=Ext.Date.format(Ext.Date.add(sdate,Ext.Date.DAY,6),'Y-m-d');
  198. } else {
  199. endfield = Ext.Date.format(enddate,'Y-m-d');
  200. }
  201. gridfields.push({
  202. name:startfield+"#"+endfield,
  203. type:'int'
  204. });
  205. columns.push({
  206. readOnly:false,
  207. header:startfield.substring(5,10)+"~"+endfield.substring(5,10),
  208. cls: "x-grid-header-1",
  209. dataIndex:startfield+"#"+endfield,
  210. width : 120,
  211. xtype:'numbercolumn',
  212. align:'right',
  213. format:'0',
  214. editor:{
  215. xtype:'numberfield',
  216. format:'0',
  217. hideTrigger: true
  218. }
  219. });
  220. }
  221. item.columns=columns;
  222. }
  223. lastcolumns[m]=item;
  224. });
  225. gridcolumns=lastcolumns;
  226. }
  227. var store = me.setStore(gridfields, data, grid.groupField, grid.necessaryField);
  228. grid.store=store;
  229. grid.columns=gridcolumns;
  230. var form = Ext.ComponentQuery.query('form')[0];
  231. if(form){
  232. if(form.readOnly){
  233. grid.readOnly = true;//grid不可编辑
  234. }
  235. }
  236. /*var lockedView = grid.view.lockedView;
  237. if(lockedView){
  238. var tableEl = lockedView.el;
  239. if(tableEl){
  240. tableEl.dom.style.marginBottom = '27px';
  241. }
  242. }*/
  243. } else {
  244. grid.hide();
  245. var form = Ext.ComponentQuery.query('form')[0];
  246. me.updateFormPosition(form);//字段较少时,修改form布局
  247. }
  248. }
  249. });
  250. },
  251. setRenderer: function(grid, column){
  252. if(!column.haveRendered && column.renderer != null && column.renderer != ""){
  253. if(!grid.RenderUtil){
  254. grid.RenderUtil = Ext.create('erp.util.RenderUtil');
  255. }
  256. var renderName = column.renderer;
  257. if(contains(column.renderer, ':', true)){
  258. var args = new Array();
  259. Ext.each(column.renderer.split(':'), function(a, index){
  260. if(index == 0){
  261. renderName = a;
  262. } else {
  263. args.push(a);
  264. }
  265. });
  266. if(!grid.RenderUtil.args[renderName]){
  267. grid.RenderUtil.args[renderName] = new Object();
  268. }
  269. grid.RenderUtil.args[renderName][column.dataIndex] = args;
  270. }
  271. column.renderer = grid.RenderUtil[renderName];
  272. column.haveRendered = true;
  273. }
  274. },
  275. setLogicType: function(grid, column){
  276. var logic = column.logic;
  277. if(logic != null){
  278. if(logic == 'detno'){
  279. grid.detno = column.dataIndex;
  280. column.width = 40;
  281. column.renderer = function(val, meta) {
  282. meta.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
  283. return val;
  284. };
  285. } else if(logic == 'keyField'){
  286. grid.keyField = column.dataIndex;
  287. } else if(logic == 'mainField'){
  288. grid.mainField = column.dataIndex;
  289. } else if(logic == 'necessaryField'){
  290. grid.necessaryField = column.dataIndex;
  291. if(!grid.necessaryFields){
  292. grid.necessaryFields = new Array();
  293. }
  294. grid.necessaryFields.push(column.dataIndex);
  295. if(!column.haveRendered){
  296. column.renderer = function(val, meta, record, x, y, store, view){
  297. var c = this.columns[y];
  298. if(val != null && val.toString().trim() != ''){
  299. if(c.xtype == 'datecolumn'){
  300. val = Ext.Date.format(val, 'Y-m-d');
  301. }
  302. return val;
  303. } else {
  304. if(c.xtype == 'datecolumn'){
  305. val = '';
  306. }
  307. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  308. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  309. }
  310. };
  311. }
  312. } else if(logic == 'groupField'){
  313. grid.groupField = column.dataIndex;
  314. }
  315. }
  316. },
  317. setStore: function(fields, data, groupField, necessaryField){
  318. griddata=data;
  319. if(!Ext.isChrome){
  320. Ext.each(fields, function(f){
  321. if(f.type == 'date'){
  322. f.dateFormat = 'Y-m-d H:m:s';
  323. }
  324. });
  325. }
  326. return Ext.create('Ext.data.Store', {
  327. fields: fields,
  328. data: data,
  329. groupField: groupField,
  330. getSum: function(records, field) {
  331. if (arguments.length < 2) {
  332. return 0;
  333. }
  334. var total = 0,
  335. i = 0,
  336. len = records.length;
  337. if(necessaryField) {
  338. for (; i < len; ++i) {//重写getSum,grid在合计时,只合计填写了必要信息的行
  339. var necessary = records[i].get(necessaryField);
  340. if(necessary != null && necessary != ''){
  341. total += records[i].get(field);
  342. }
  343. }
  344. } else {
  345. for (; i < len; ++i) {
  346. total += records[i].get(field);
  347. }
  348. }
  349. return total;
  350. },
  351. getCount: function() {
  352. if(necessaryField) {
  353. var count = 0;
  354. Ext.each(this.data.items, function(item){//重写getCount,grid在合计时,只合计填写了必要信息的行
  355. if(item.data[necessaryField] != null && item.data[necessaryField] != ''){
  356. count++;
  357. }
  358. });
  359. return count;
  360. }
  361. return this.data.items.length;
  362. }
  363. });
  364. },
  365. setToolbar: function(grid, columns, necessaryField, limitArr){
  366. var items = [];
  367. var bool = true;
  368. if(!grid.dockedItems)
  369. return;
  370. Ext.each(grid.dockedItems.items, function(item){
  371. if(item.dock == 'bottom' && item.items){//bbar已存在
  372. bool = false;
  373. }
  374. });
  375. if(bool){
  376. Ext.each(columns, function(column){
  377. if(limitArr.length == 0 || !Ext.Array.contains(limitArr, column.dataIndex)) {
  378. if(column.summaryType == 'sum'){
  379. items.push('-',{
  380. id: column.dataIndex + '_sum',
  381. itemId: column.dataIndex,
  382. xtype: 'tbtext',
  383. text: column.header + '(sum):0'
  384. });
  385. } else if(column.summaryType == 'average') {
  386. items.push('-',{
  387. id: column.dataIndex + '_average',
  388. itemId: column.dataIndex,
  389. xtype: 'tbtext',
  390. text: column.header + '(average):0'
  391. });
  392. } else if(column.summaryType == 'count') {
  393. items.push('-',{
  394. id: column.dataIndex + '_count',
  395. itemId: column.dataIndex,
  396. xtype: 'tbtext',
  397. text: column.header + '(count):0'
  398. });
  399. }
  400. }
  401. if(column.dataIndex == necessaryField){
  402. column.renderer = function(val){
  403. if(val != null && val.toString().trim() != ''){
  404. return val;
  405. } else {
  406. return '<img src="' + basePath + 'resource/images/icon/need.png" title="必填字段">' +
  407. '<span style="color:blue;padding-left:2px;" title="必填字段">' + val + '</span>';
  408. }
  409. };
  410. }
  411. });
  412. grid.addDocked({
  413. xtype: 'toolbar',
  414. dock: 'bottom',
  415. items: items
  416. });
  417. } else {
  418. var bars = Ext.ComponentQuery.query('erpToolbar');
  419. if(bars.length > 0){
  420. Ext.each(columns, function(column){
  421. if(column.summaryType == 'sum'){
  422. bars[0].add('-');
  423. bars[0].add({
  424. id: column.dataIndex + '_sum',
  425. itemId: column.dataIndex,
  426. xtype: 'tbtext',
  427. text: column.header + '(sum):0'
  428. });
  429. } else if(column.summaryType == 'average') {
  430. bars[0].add('-');
  431. bars[0].add({
  432. id: column.dataIndex + '_average',
  433. itemId: column.dataIndex,
  434. xtype: 'tbtext',
  435. text: column.header + '(average):0'
  436. });
  437. } else if(column.summaryType == 'count') {
  438. bars[0].add('-');
  439. bars[0].add({
  440. id: column.dataIndex + '_count',
  441. itemId: column.dataIndex,
  442. xtype: 'tbtext',
  443. text: column.header + '(count):0'
  444. });
  445. }
  446. });
  447. }
  448. }
  449. },
  450. add10EmptyData: function(detno, data){
  451. if(detno){
  452. var index = data.length == 0 ? 0 : Number(data[data.length-1][detno]);
  453. for(var i=0;i<10;i++){
  454. var o = new Object();
  455. o[detno] = index + i + 1;
  456. data.push(o);
  457. }
  458. } else {
  459. for(var i=0;i<10;i++){
  460. var o = new Object();
  461. data.push(o);
  462. }
  463. }
  464. },
  465. checkGridDirty: function(grid){
  466. var s = grid.getStore().data.items;//获取store里面的数据
  467. var msg = '';
  468. for(var i=0;i<s.length;i++){
  469. if(s[i].dirty){//明细行被修改过哦
  470. msg = msg + '第' + (i+1) + '行:';
  471. var changed = Ext.Object.getKeys(s[i].modified);//拿到被修改了的字段
  472. for(var j = 0;j < changed.length;j++){
  473. Ext.each(grid.columns, function(c, index){
  474. if(index == 0){
  475. return;
  476. }
  477. if(c.dataIndex == changed[j] && c.logic != 'ignore'){//由字段dataindex匹配其text
  478. msg = msg + '<font color=blue>' + c.text + '</font>&nbsp;';
  479. }
  480. });
  481. }
  482. msg = msg + ";";
  483. }
  484. }
  485. if(msg != '' && msg != ';'){//明细行被修改过哦
  486. msg = "明细行<font color=green>" + msg.substring(0, msg.length-1) + "</font>已编辑过";
  487. }
  488. return msg;
  489. },
  490. getGridStore: function(grid){
  491. if(grid == null){
  492. grid = Ext.getCmp('grid');
  493. }
  494. var me = this,
  495. jsonGridData = new Array();
  496. var form = Ext.getCmp('form');
  497. var s = grid.getStore().data.items;//获取store里面的数据
  498. var dd;
  499. for(var i=0;i<s.length;i++){//将grid里面各行的数据获取并拼成jsonGridData
  500. var data = s[i].data;
  501. dd = new Object();
  502. if(s[i].dirty && !me.isBlank(grid, data)){
  503. Ext.each(gridcolumns, function(c){
  504. if(c.logic != 'ignore'){//只需显示,无需后台操作的字段,自动略去
  505. if(c.dataIndex=='sd_enddate'){
  506. Ext.Array.each(c.columns,function(column){
  507. dd[column.dataIndex]=data[column.dataIndex];
  508. });
  509. }else {
  510. if(c.xtype == 'datecolumn'){
  511. c.format = c.format || 'Y-m-d';
  512. if(Ext.isDate(data[c.dataIndex])){
  513. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], c.format);
  514. } else {
  515. if(c.editor){
  516. dd[c.dataIndex] = Ext.Date.format(new Date(), c.format);//如果用户没输入日期,或输入有误,就给个默认日期,
  517. }
  518. }
  519. } else if(c.xtype == 'datetimecolumn'){
  520. if(Ext.isDate(data[c.dataIndex])){
  521. dd[c.dataIndex] = Ext.Date.format(data[c.dataIndex], 'Y-m-d H:i:s');//在这里把GMT日期转化成Y-m-d H:i:s格式日期
  522. } else {
  523. if(c.editor){
  524. dd[c.dataIndex] = Ext.Date.format(new Date(), 'Y-m-d H:i:s');//默认日期,
  525. }
  526. }
  527. } else if(c.xtype == 'numbercolumn'){//赋个默认值0吧,不然不好保存
  528. if(data[c.dataIndex] == null || data[c.dataIndex] == ''){
  529. dd[c.dataIndex] = '0';//也可以从data里面去掉这些字段
  530. } else {
  531. dd[c.dataIndex] = s[i].data[c.dataIndex];
  532. }
  533. } else {
  534. dd[c.dataIndex] = s[i].data[c.dataIndex];
  535. }
  536. }
  537. }
  538. });
  539. if(grid.mainField && form && form.keyField){//例如,将pu_id的值赋给pd_puid
  540. dd[grid.mainField] = Ext.getCmp(form.keyField).value;
  541. }
  542. jsonGridData.push(Ext.JSON.encode(dd));
  543. }
  544. }
  545. return jsonGridData;
  546. },
  547. isBlank: function(grid, data) {
  548. var ff = grid.necessaryFields,bool = true;
  549. if(ff) {
  550. Ext.each(ff, function(f) {
  551. if(!Ext.isEmpty(data[f]) && data[f] != 0) {
  552. bool = false;
  553. }else {
  554. bool=true;
  555. return
  556. }
  557. });
  558. } else {
  559. if(!grid.necessaryField || !Ext.isEmpty(data[grid.necessaryField])) {
  560. bool = false;
  561. }
  562. }
  563. return bool;
  564. },
  565. });