VoucherCreate.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. Ext.QuickTips.init();
  2. Ext.define('erp.controller.fa.VoucherCreate', {
  3. extend: 'Ext.app.Controller',
  4. FormUtil: Ext.create('erp.util.FormUtil'),
  5. GridUtil: Ext.create('erp.util.GridUtil'),
  6. views: ['fa.VoucherCreate', 'core.form.Panel', 'core.form.MonthDateField', 'core.form.ConDateField',
  7. 'core.form.MultiField', 'core.trigger.DbfindTrigger', 'core.button.Export',
  8. 'core.button.Close', 'core.button.Query', 'core.button.VoCreate','core.trigger.AddDbfindTrigger'],
  9. init:function(){
  10. var me = this;
  11. this.control({
  12. 'field[name=yearmonth]': {
  13. afterrender: function(f) {
  14. me.getMonth(f);
  15. },
  16. change: function(f) {
  17. if(!Ext.isEmpty(f.value)) {
  18. var d = Ext.ComponentQuery.query('condatefield');
  19. if(d && d.length > 0)
  20. d[0].setMonthValue(f.value);
  21. }
  22. }
  23. },
  24. 'condatefield[name=rb_date]': {
  25. afterrender: function(f) {
  26. f.hideScope(true);
  27. }
  28. },
  29. 'panel[id=vc-panel]': {
  30. afterrender: function(p) {
  31. me.getVsSet(p);
  32. }
  33. },
  34. 'erpQueryButton': {
  35. click: function(btn) {
  36. var condition = this.getCondition(btn.ownerCt.ownerCt);
  37. if(condition != null) {
  38. var grid = Ext.getCmp('grid');
  39. this.GridUtil.loadNewStore(grid, {caller: caller, condition: condition, start: 1, end: c_type == 'merge' ? 1000 : 200});
  40. }
  41. }
  42. },
  43. 'gridpanel': {
  44. storeloaded: function(grid) {
  45. this.generateSummaryData(grid);
  46. }
  47. },
  48. 'erpVoCreateButton': {
  49. afterrender: function(btn) {
  50. if(!isCreate) {
  51. btn.setText($I18N.common.button.erpVoUnCreateButton);
  52. } else if(c_type == 'single'){
  53. btn.ownerCt.insert(3, {
  54. xtype: 'button',
  55. iconCls: 'x-button-icon-add',
  56. cls: 'x-btn-gray',
  57. text: '合并生成',
  58. id: 'mergecreate'
  59. });
  60. }
  61. },
  62. click: function(btn) {
  63. var form = btn.ownerCt.ownerCt,
  64. ym = form.down('#yearmonth').value;
  65. var grid = Ext.getCmp('grid'),s = new Array(), c = null;
  66. var dateField = me.voucherSet.vs_datefield,
  67. first = grid.store.first();
  68. if(dateField && first.get(dateField)) {
  69. var val = first.get(dateField), dateYm = 0;
  70. if(!Ext.isDate(val)) {
  71. if(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(val))
  72. dateYm = Ext.Date.format(Ext.Date.parse(val, 'Y-m-d H:i:s'), 'Ym');
  73. else if(/\d{4}-\d{2}-\d{2}/.test(val))
  74. dateYm = Ext.Date.format(Ext.Date.parse(val, 'Y-m-d'), 'Ym');
  75. } else
  76. dateYm = Ext.Date.format(val, 'Ym');
  77. if(Number(dateYm) != Number(ym)) {
  78. showError('单据日期与表头选择的期次是不一致,请重新筛选后再继续操作');
  79. return;
  80. }
  81. }
  82. if(c_type == 'single') {
  83. grid.store.each(function(item){
  84. c = item.get(grid.prikey);
  85. if(!Ext.isEmpty(c)) {
  86. if(grid.selModel.isSelected(item)) {
  87. s.push("'" + c + "'");
  88. }
  89. }
  90. });
  91. if(s.length > 0) {
  92. if(isCreate) {
  93. if(s.length > 40) {// 超过40条的,换成分批制作
  94. me.largeCreate(ym, cls, s);
  95. } else {
  96. me.create(ym, cls, s.join(','));
  97. }
  98. } else {
  99. me.unCreate(s.join(','));
  100. }
  101. } else
  102. showError('没有数据!');
  103. } else {// 合并制作的改为直接传递条件
  104. if(grid.store.data.length > 0 && !Ext.isEmpty(first.get(grid.prikey))) {
  105. var condition = this.getCondition(form);
  106. if(condition != null) {
  107. if(isCreate) {
  108. me.create(ym, cls, condition);
  109. } else {
  110. me.unCreate(condition);
  111. }
  112. }
  113. } else
  114. showError('没有数据!');
  115. }
  116. }
  117. },
  118. '#mergecreate': {
  119. click: function(btn) {
  120. var form = btn.ownerCt.ownerCt,
  121. ym = form.down('#yearmonth').value;
  122. var grid = Ext.getCmp('grid'),s = new Array(), c = null;
  123. var dateField = me.voucherSet.vs_datefield,
  124. first = grid.store.first();
  125. if(dateField && first.get(dateField)) {
  126. var val = first.get(dateField), dateYm = 0;
  127. if(!Ext.isDate(val)) {
  128. if(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(val))
  129. dateYm = Ext.Date.format(Ext.Date.parse(val, 'Y-m-d H:i:s'), 'Ym');
  130. else if(/\d{4}-\d{2}-\d{2}/.test(val))
  131. dateYm = Ext.Date.format(Ext.Date.parse(val, 'Y-m-d'), 'Ym');
  132. } else
  133. dateYm = Ext.Date.format(val, 'Ym');
  134. if(Number(dateYm) != Number(ym)) {
  135. showError('单据日期与表头选择的期次是不一致,请重新筛选后再继续操作');
  136. return;
  137. }
  138. }
  139. grid.store.each(function(item){
  140. c = item.get(grid.prikey);
  141. if(!Ext.isEmpty(c)) {
  142. if(grid.selModel.isSelected(item)) {
  143. s.push("'" + c + "'");
  144. }
  145. }
  146. });
  147. if(s.length > 0) {
  148. var cond = grid.prikey + ' in (' + s.join(',') + ')';
  149. if(isCreate) {
  150. me.create(ym, cls, cond, 'merge');
  151. } else {
  152. me.unCreate(cond);
  153. }
  154. } else
  155. showError('没有数据!');
  156. }
  157. }
  158. });
  159. },
  160. getCondition: function(form, _get) {
  161. var items = form.items.items,
  162. val = null,value = new Array();
  163. Ext.each(items, function(item){
  164. if(item.logic != null) {
  165. val = item.value;
  166. if(!Ext.isEmpty(val)) {
  167. if(contains(val.toString(), 'BETWEEN', true)
  168. && contains(val.toString(), 'AND', true)){
  169. value.push('(' + item.logic + ' ' + val + ')');
  170. } else if(item.xtype == 'combo' && val == '$ALL'){
  171. var condition = '';
  172. if(item.store.data.length > 1) {
  173. item.store.each(function(d, idx){
  174. if(d.data.value != '$ALL') {
  175. if(condition == ''){
  176. condition += item.logic + "='" + d.data.value + "'";
  177. } else {
  178. condition += ' OR ' + item.logic + "='" + d.data.value + "'";
  179. }
  180. }
  181. });
  182. }
  183. if(condition != ''){
  184. value.push('(' + condition + ')');
  185. }
  186. } else if(item.xtype=='adddbfindtrigger' && item.value != null && item.value != ''){
  187. var condition = '';
  188. if(condition == ''){
  189. condition += item.logic + ' in (' ;
  190. }
  191. var str=item.value,constr="";
  192. for(var i=0;i<str.split("#").length;i++){
  193. if(i<str.split("#").length-1){
  194. constr+="'"+str.split("#")[i]+"',";
  195. }else constr+="'"+str.split("#")[i]+"'";
  196. }
  197. condition += constr;
  198. condition += ') ';
  199. if(condition != ''){
  200. value.push('(' + condition + ')');
  201. }
  202. } else {
  203. if(String(val).indexOf('%') > 0) {
  204. value.push('(' + item.logic + ' like \'' + val + '\')');
  205. } else {
  206. value.push('(' + item.logic + '=\'' + val + '\')');
  207. }
  208. }
  209. }
  210. }
  211. });
  212. if(value.length > 0) {
  213. var grid = Ext.getCmp('grid');
  214. var condition = value.join(' AND ');
  215. condition += ' AND (' + (('nvl(' + grid.voucfield + ',\' \')') + (isCreate && !_get ? '=' : '<>') + '\' \')');
  216. condition += Ext.isEmpty(grid.defaultCondition) ? '' : (' AND ' + grid.defaultCondition);
  217. if(!isCreate) {
  218. condition += ' AND (' + grid.voucfield + ' IN (SELECT vo_code FROM Voucher WHERE ' +
  219. 'vo_createkind=\'' + (c_type == 'merge' ? '合并制作' : '单张制作') + '\'))';
  220. }
  221. if (form.classfield && cls) {
  222. condition += ' AND (' + form.classfield + '=\'' + cls + '\')';
  223. }
  224. return condition;
  225. }
  226. return null;
  227. },
  228. /**
  229. * 根据VoucherStyle--vs_datalist取Grid配置
  230. */
  231. getVsSet: function(p) {
  232. var me = this;
  233. Ext.Ajax.request({
  234. url : basePath + 'common/getFieldsData.action',
  235. params: {
  236. caller: 'VoucherStyle',
  237. fields: 'vs_datalist,vs_classfield,vs_prikey1,vs_voucfield,vs_datacondition,vs_detailtable,vs_datefield',
  238. condition: 'vs_code=\'' + vs_code + '\''
  239. },
  240. method : 'post',
  241. callback : function(options,success,response){
  242. var rs = new Ext.decode(response.responseText);
  243. if(rs.exceptionInfo){
  244. showError(rs.exceptionInfo);
  245. } else if(rs.success){
  246. if(rs.data != null){
  247. var form = Ext.getCmp('form');
  248. form.classfield = rs.data.vs_classfield;
  249. me.voucherSet = rs.data;
  250. me.getGridSet(p, (window.caller || rs.data.vs_datalist), rs.data.vs_prikey1,
  251. rs.data.vs_voucfield, rs.data.vs_datacondition);
  252. }
  253. }
  254. }
  255. });
  256. },
  257. getGridSet: function(p, cal, prikey, voucfield, condition) {
  258. var grid = Ext.create('Ext.grid.Panel', {
  259. anchor: '100% 100%',
  260. id: 'grid',
  261. columnLines: true,
  262. columns: [],
  263. store: [],
  264. prikey: prikey,
  265. voucfield: voucfield,
  266. defaultCondition: condition,
  267. caller: cal,
  268. listeners: {
  269. scrollershow: function(scroller) {
  270. if (scroller && scroller.scrollEl) {
  271. scroller.clearManagedListeners();
  272. scroller.mon(scroller.scrollEl, 'scroll', scroller.onElScroll, scroller);
  273. }
  274. }
  275. },
  276. selModel: c_type == 'single' ? new Ext.selection.CheckboxModel({checkOnly : true,
  277. ignoreRightMouseSelection : false}) : new Ext.selection.Model()//单张制作时,有checkbox
  278. });
  279. p.add(grid);
  280. this.GridUtil.getGridColumnsAndStore(grid, 'common/singleGridPanel.action', {caller: caller, condition: null});
  281. },
  282. /**
  283. * 制作凭证
  284. */
  285. create: function(ym, kind, data, mode) {
  286. var me = this, grid = Ext.getCmp('grid');
  287. grid.setLoading(true);
  288. Ext.Ajax.request({
  289. url: basePath + 'fa/vc/createVoucher.action',
  290. params: {
  291. vs_code: vs_code,
  292. yearmonth: ym,
  293. datas: data,
  294. mode: (mode || c_type),
  295. kind: kind,
  296. vomode: vo_type
  297. },
  298. timeout: 60000,
  299. callback: function(opt, s, r) {
  300. grid.setLoading(false);
  301. var rs = Ext.decode(r.responseText);
  302. if(rs.exceptionInfo) {
  303. showError(rs.exceptionInfo);
  304. } else if(rs.error) {
  305. showMessage('提示', rs.error);
  306. } else {
  307. showMessage('提示', '制作成功!');
  308. // 这个code在合并制作成功后,取凭证号的时候用到
  309. var btn = Ext.getCmp('querybtn'), code = grid.store.first().get(grid.prikey);
  310. btn.fireEvent('click', btn);
  311. me.getVoucher(vs_code, kind, data, mode != null, grid.prikey + '=\'' + code + '\'');
  312. }
  313. }
  314. });
  315. },
  316. /**
  317. * 制作凭证
  318. */
  319. syncCreate: function(ym, kind, data) {
  320. var res = true;
  321. Ext.Ajax.request({
  322. url: basePath + 'fa/vc/createVoucher.action',
  323. params: {
  324. vs_code: vs_code,
  325. yearmonth: ym,
  326. datas: data,
  327. mode: c_type,
  328. kind: kind,
  329. vomode: vo_type
  330. },
  331. async: false,
  332. callback: function(opt, s, r) {
  333. var rs = Ext.decode(r.responseText);
  334. if(rs.exceptionInfo) {
  335. showError(rs.exceptionInfo);
  336. res = false;
  337. } else if(rs.error) {
  338. showMessage('提示', rs.error);
  339. res = false;
  340. }
  341. }
  342. });
  343. return res;
  344. },
  345. /**
  346. * 超过40条的制作
  347. */
  348. largeCreate: function(ym, kind, datas) {
  349. var size = 20, len = datas.length, t = Math.ceil(len / size), a = 0,
  350. grid = Ext.getCmp('grid'), res = true;
  351. grid.setLoading(true);
  352. for (var i = 0;i < t;i++ ) {
  353. var s = [], b = Math.min(a + size, len);
  354. for(;a < b;a++ ) {
  355. s.push(datas[a]);
  356. }
  357. res = this.syncCreate(ym, kind, s.join(','));
  358. if(!res)
  359. break;
  360. }
  361. grid.setLoading(false);
  362. if(res) {
  363. showMessage('提示', '制作成功!');
  364. var btn = Ext.getCmp('querybtn');
  365. btn.fireEvent('click', btn);
  366. this.getVoucher(vs_code, kind, datas.join(','));
  367. }
  368. },
  369. /**
  370. * 取消凭证
  371. */
  372. unCreate: function(data) {
  373. Ext.Ajax.request({
  374. url: basePath + 'fa/vc/unCreateVoucher.action',
  375. params: {
  376. vs_code: vs_code,
  377. mode: c_type,
  378. kind: cls,
  379. datas: data,
  380. vomode: vo_type
  381. },
  382. callback: function(opt, s, r) {
  383. var rs = Ext.decode(r.responseText);
  384. if(rs.exceptionInfo) {
  385. showError(rs.exceptionInfo);
  386. } else if(rs.error) {
  387. showMessage('提示', rs.error);
  388. } else {
  389. showMessage('提示', '取消成功!');
  390. var btn = Ext.getCmp('querybtn');
  391. btn.fireEvent('click', btn);
  392. }
  393. }
  394. });
  395. },
  396. getMonth: function(f) {
  397. Ext.Ajax.request({
  398. url: basePath + 'fa/getMonth.action',
  399. params: {
  400. votype: 'GL'//vo_type//直接取总账期间
  401. },
  402. callback: function(opt, s, r) {
  403. var rs = Ext.decode(r.responseText);
  404. if(rs.data) {
  405. f.setValue(rs.data.PD_DETNO);
  406. }
  407. }
  408. });
  409. },
  410. getVoucher: function(vs, kind, data, singlemerge, codeStr) {
  411. var me = this, s = me.voucherSet, cond = '';
  412. if('merge' == c_type) {
  413. cond = this.getCondition(Ext.getCmp('form'), true);
  414. codeStr && (cond += ' and (' + codeStr + ')');
  415. } else if(singlemerge) {
  416. cond = data;
  417. if(s.vs_classfield)
  418. cond += ' and ' + s.vs_classfield + '=\'' + kind + '\'';
  419. } else {
  420. cond = s.vs_prikey1 + ' in (' + data + ')';
  421. if (s.vs_classfield)
  422. cond += ' and ' + s.vs_classfield + '=\'' + kind + '\'';
  423. }
  424. Ext.Ajax.request({
  425. url : basePath + 'common/getFieldsDatas.action',
  426. params: {
  427. caller: s.vs_detailtable + ' left join voucher vo on vo.vo_code=' + s.vs_voucfield,
  428. fields: 'distinct vo.vo_id,' + s.vs_voucfield,
  429. condition: cond
  430. },
  431. method : 'post',
  432. callback : function(options,success,response){
  433. var rs = new Ext.decode(response.responseText);
  434. if(rs.exceptionInfo){
  435. showError(rs.exceptionInfo);
  436. } else if(rs.data){
  437. var msg = '凭证:<br>', json = Ext.decode(rs.data);
  438. var f = s.vs_voucfield.toUpperCase(), id = 0;
  439. for(var i in json) {
  440. id = json[i]['VO_ID'];
  441. msg += '<hr><a href="javascript:openUrl(\'jsps/fa/ars/voucher.jsp?formCondition=vo_idIS' +
  442. id + '&gridCondition=vd_voidIS' + id + '\')">' + json[i][f] + '</a>';
  443. }
  444. showMessage('提示', msg);
  445. }
  446. }
  447. });
  448. },
  449. generateSummaryData : function(grid) {
  450. var me = this, store = grid.store,
  451. columns = grid.columns, s = grid.features[grid.features.length - 1],
  452. i = 0, length = columns.length, comp, bar = grid.down('toolbar[dock=bottom]');
  453. if (!bar) return;
  454. for (; i < length; i++ ) {
  455. comp = columns[i];
  456. if(comp.summaryType) {
  457. var tb = Ext.getCmp(comp.dataIndex + '_' + comp.summaryType);
  458. if(!tb){
  459. bar.add('-');
  460. tb = bar.add({
  461. id: comp.dataIndex + '_' + comp.summaryType,
  462. itemId: comp.dataIndex,
  463. xtype: 'tbtext'
  464. });
  465. }
  466. var val = me.getSummary(store, comp.summaryType, comp.dataIndex, false);
  467. if(comp.xtype == 'numbercolumn') {
  468. val = Ext.util.Format.number(val, (comp.format || '0,000.000'));
  469. }
  470. tb.setText(comp.text + ':' + val);
  471. }
  472. }
  473. },
  474. getSummary: function(store, type, field, group){
  475. if (type) {
  476. if (Ext.isFunction(type)) {
  477. return store.aggregate(type, null, group);
  478. }
  479. switch (type) {
  480. case 'count':
  481. return store.count(group);
  482. case 'min':
  483. return store.min(field, group);
  484. case 'max':
  485. return store.max(field, group);
  486. case 'sum':
  487. return store.sum(field, group);
  488. case 'average':
  489. return store.average(field, group);
  490. default:
  491. return group ? {} : '';
  492. }
  493. }
  494. }
  495. });