FormPanelController.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. Ext.define('saas.view.money.verification.FormPanelController', {
  2. extend: 'saas.view.core.form.FormPanelController',
  3. alias: 'controller.money-verification-formpanel',
  4. init: function (form) {
  5. var me = this;
  6. this.control({
  7. // 客户
  8. 'dbfindtrigger[name=vc_custname]':{
  9. beforerender:function(f){
  10. Ext.apply(f,{
  11. //赋值
  12. dbfinds:[{
  13. from: 'id', to: 'vc_custid',ignore:true
  14. }, {
  15. from:'cu_code', to:'vc_custcode'
  16. },{
  17. from:'cu_name', to:'vc_custname'
  18. },{
  19. from:'cu_currency',to:'vc_currency'
  20. },{
  21. from:'cr_rate',to:'vc_rate'
  22. }
  23. ],
  24. }) ;
  25. }
  26. },
  27. // 转入客户
  28. 'dbfindtrigger[name=vc_turncustname]':{
  29. beforerender:function(f){
  30. Ext.apply(f,{
  31. //赋值
  32. dbfinds:[{
  33. from: 'id', to: 'vc_turncustid',ignore:true
  34. }, {
  35. from:'cu_code', to:'vc_turncustcode'
  36. },{
  37. from:'cu_name', to:'vc_turncustname'
  38. }],
  39. }) ;
  40. }
  41. },
  42. // 供应商
  43. 'vendorDbfindTrigger[name=vc_vendname]':{
  44. beforerender: function (f) {
  45. if(!f.dbfinds){
  46. Ext.apply(f, {
  47. dbfinds: [{
  48. from: 'id',
  49. to: 'vc_vendid',
  50. ignore: true
  51. }, {
  52. from: 've_code',
  53. to: 'vc_vendcode'
  54. }, {
  55. from: 've_name',
  56. to: 'vc_vendname'
  57. },{
  58. from:'ve_currency',to:'vc_currency'
  59. },{
  60. from:'cr_rate',to:'vc_rate'
  61. }]
  62. });
  63. }
  64. }
  65. },
  66. // 转入供应商
  67. 'dbfindtrigger[name=vc_turnvendname]':{
  68. beforerender: function (f) {
  69. Ext.apply(f, {
  70. dbfinds: [{
  71. from: 'id',
  72. to: 'vc_turnvendid',
  73. ignore: true
  74. }, {
  75. from: 've_code',
  76. to: 'vc_turnvendcode'
  77. }, {
  78. from: 've_name',
  79. to: 'vc_turnvendname'
  80. }],
  81. });
  82. }
  83. },
  84. // 来源单号1
  85. 'oriOrderMutiDbfindTrigger[name=vd_slcode]': {
  86. beforerender: function (f) {
  87. Ext.apply(f, {
  88. dbfinds: [{
  89. from: 'id',
  90. to: 'vd_slid',ignore:true
  91. }, {
  92. from: 'sl_code',
  93. to: 'vd_slcode'
  94. },{
  95. from: 'sl_orderamount',
  96. to: 'vd_amount'
  97. },{
  98. from: 'sl_yamount',
  99. to: 'sl_yamount'
  100. },{
  101. from: 'sl_namount',
  102. to: 'sl_namount'
  103. },{
  104. from:'sl_date',
  105. to:'vd_sldate'
  106. },{
  107. from:'sl_kind',
  108. to:'vd_slkind'
  109. },{
  110. from: 'sl_namount',
  111. to: 'vd_nowbalance'
  112. }],
  113. });
  114. },
  115. beforequery: function(f) {
  116. var defaultCondition = this.getDefaultCondition(0);
  117. var form = me.getView(),
  118. viewModel = me.getViewModel();
  119. var kind=viewModel.get('vc_kind');
  120. console.log(kind);
  121. if (kind == 'receivable_to_receivable' || kind == 'payable_to_payable'){
  122. f.dataUrl='/api/money/subledger/dbfindlist';
  123. }
  124. if(!defaultCondition) {
  125. return false;
  126. }
  127. Ext.apply(f, {
  128. defaultCondition: defaultCondition
  129. });
  130. },
  131. beforetriggerclick: function(f) {
  132. console.log('beforetriggerclick');
  133. var defaultCondition = this.getDefaultCondition(0);
  134. var form = me.getView(),
  135. viewModel = me.getViewModel();
  136. var kind=viewModel.get('vc_kind');
  137. if (kind == 'receivable_to_receivable' || kind == 'payable_to_payable'){
  138. f.dataUrl='/api/money/subledger/dbfindlist';
  139. }
  140. if(!defaultCondition) {
  141. return false;
  142. }
  143. Ext.apply(f, {
  144. defaultCondition: defaultCondition
  145. });
  146. }
  147. },
  148. // 来源单号2
  149. 'oriOrderMutiDbfindTrigger[name=vcd_slcode]': {
  150. beforerender: function (f) {
  151. Ext.apply(f, {
  152. dbfinds: [{
  153. from: 'id',
  154. to: 'vcd_slid',ignore:true
  155. }, {
  156. from: 'sl_code',
  157. to: 'vcd_slcode'
  158. },{
  159. from: 'sl_orderamount',
  160. to: 'vcd_amount'
  161. },{
  162. from: 'sl_yamount',
  163. to: 'sl_yamount'
  164. },{
  165. from: 'sl_namount',
  166. to: 'sl_namount'
  167. },{
  168. from:'sl_date',
  169. to:'vcd_sldate'
  170. },{
  171. from:'sl_kind',
  172. to:'vcd_slkind'
  173. },{
  174. from: 'sl_namount',
  175. to: 'vcd_nowbalance'
  176. }],
  177. });
  178. },
  179. beforequery: function(f) {
  180. var defaultCondition = this.getDefaultCondition(1);
  181. if(!defaultCondition) {
  182. return false;
  183. }
  184. Ext.apply(f, {
  185. defaultCondition: defaultCondition
  186. });
  187. },
  188. beforetriggerclick: function(f) {
  189. var defaultCondition = this.getDefaultCondition(1);
  190. if(!defaultCondition) {
  191. return false;
  192. }
  193. Ext.apply(f, {
  194. defaultCondition: defaultCondition
  195. });
  196. }
  197. },
  198. });
  199. },
  200. getDefaultCondition: function(idx) {
  201. var me = this,
  202. form = me.getView(),
  203. formItems = form.formItems,
  204. viewModel = me.getViewModel(),
  205. acitveType = form.acitveType,
  206. etc = form.etc,
  207. defaultConditions = etc.defaultConditions,
  208. defaultConditionMode = defaultConditions[acitveType][idx];
  209. var reg = /{.+?}/g;
  210. var arr = defaultConditionMode.match(reg) || [];
  211. var fields = {};
  212. var nullFields = [];
  213. var defaultCondition = defaultConditionMode;
  214. // TODO arr未去重
  215. arr.map(function(a) {
  216. var fieldName = a.replace(/[{|}]/g, '');
  217. var field = Ext.Array.findBy(formItems, function(f) {
  218. return f.name == fieldName;
  219. });
  220. var fieldLabel = field.fieldLabel || field.name;
  221. var fieldValue = viewModel.get(fieldName);
  222. if(Ext.isDate(fieldValue)) {
  223. fieldValue = Ext.Date.format(fieldValue, 'Y-m-d');
  224. }
  225. fields[fieldName] = fieldValue;
  226. if(!fieldValue && nullFields.indexOf(fieldLabel) == -1) {
  227. nullFields.push(fieldLabel);
  228. }
  229. });
  230. if(nullFields.length == 0) {
  231. var keys = Ext.Object.getAllKeys(fields);
  232. keys.map(function(k) {
  233. var v = fields[k];
  234. defaultCondition = defaultCondition.replace(new RegExp('{' + k + '}', 'g'), v);
  235. });
  236. return defaultCondition;
  237. }else {
  238. saas.util.BaseUtil.showErrorToast('请先选择【' + '<span style="color: red;">' + nullFields.join(',') + '</span>】');
  239. return null;
  240. }
  241. },
  242. /**
  243. * 清空所有从表数据
  244. */
  245. clearDetails: function() {
  246. var me = this;
  247. me.clearDetail0();
  248. me.clearDetail1();
  249. },
  250. /**
  251. * 清空第一个从表数据
  252. */
  253. clearDetail0: function(f, n, o) {
  254. var me = this,
  255. form = me.getView(),
  256. grid = form.query('detailGridField')[0];
  257. me.clearDetail(grid);
  258. },
  259. /**
  260. * 清空第二个从表数据
  261. */
  262. clearDetail1: function() {
  263. var me = this,
  264. form = me.getView(),
  265. grid = form.query('detailGridField')[1];
  266. me.clearDetail(grid);
  267. },
  268. /**
  269. * 清空一个从表数据
  270. */
  271. clearDetail: function(grid) {
  272. var datas = [],
  273. emptyRows = grid.emptyRows,
  274. detnoColumn = grid.detnoColumn,
  275. detno = 0,
  276. store = grid.getStore();
  277. Ext.Array.each(new Array(emptyRows), function() {
  278. detno += 1;
  279. var data = {};
  280. data[detnoColumn] = detno;
  281. datas.push(data);
  282. })
  283. store.removeAll();
  284. store.add(datas);
  285. // 不写该方法合计列会错位,这里触发afterEdit事件用来刷新grid
  286. store.getAt(0) ? store.afterEdit(store.getAt(0), ['']) : null;
  287. },
  288. onSave: function() {
  289. var me = this,
  290. form = me.getView(),
  291. viewModel = me.getViewModel(),
  292. store1 = viewModel.get('detail0').detailStore,
  293. store2 = viewModel.get('detail1').detailStore;
  294. var valid = form.isValid();
  295. if(!valid) {
  296. saas.util.BaseUtil.showErrorToast(form.invalidText);
  297. return false;
  298. }
  299. vc_kind = viewModel.get('vc_kind');
  300. //应收转应收 receivable_to_receivable 客户一致时提示 “保存失败: 转出客户与转入客户相同”
  301. if(vc_kind === 'receivable_to_receivable' && (viewModel.get('vc_custid') === viewModel.get('vc_turncustid') )){
  302. saas.util.BaseUtil.showErrorToast("保存失败: 转出客户与转入客户相同");
  303. return false;
  304. }
  305. //应付转应付 payable_to_payable 供应商一致时提示 “保存失败: 转出供应商与转入供应商相同”
  306. if(vc_kind === 'payable_to_payable' && (viewModel.get('vc_vendid') === viewModel.get('vc_turnvendid') )){
  307. saas.util.BaseUtil.showErrorToast("保存失败: 转出客户与转入客户相同");
  308. return false;
  309. }
  310. var sum_detail1 = store1.sum('vd_nowbalance'); // 从表1核销合计
  311. var sum_detail2 = store2.sum('vcd_nowbalance'); // 从表2核销合计
  312. viewModel.set("vc_amount1",sum_detail1);
  313. viewModel.set("vc_amount2",sum_detail2);
  314. me.save();
  315. },
  316. save:function(){
  317. var me = this,
  318. form = this.getView(),
  319. codeField = form.getForm().findField(form._codeField),
  320. detailCount = form.detailCount,
  321. viewModel = me.getViewModel(),
  322. codeModified = !form.initId || (codeField && codeField.isDirty());
  323. //form里面数据
  324. var formData = form.getSaveData();
  325. var params = {
  326. main:formData.main
  327. };
  328. for(var i = 0; i < detailCount; i++) {
  329. params['items' + ( i + 1)] = formData['detail' + i];
  330. }
  331. params.codeModified = codeModified;
  332. form.setLoading(true);
  333. saas.util.BaseUtil.request({
  334. url: form._saveUrl,
  335. params: JSON.stringify(params),
  336. method: 'POST',
  337. })
  338. .then(function(localJson) {
  339. form.setLoading(false);
  340. if(localJson.success){
  341. var id = localJson.data.id;
  342. var code = localJson.data.code;
  343. form.initId = id;
  344. saas.util.FormUtil.loadData(form);
  345. saas.util.BaseUtil.showSuccessToast('保存成功');
  346. var newId = form.xtype + '-' + id;
  347. var newTitle = form._title + '(' + code + ')';
  348. saas.util.BaseUtil.refreshTabTitle(newId, newTitle);
  349. form.fireEvent('aftersave', true, form, localJson);
  350. }
  351. })
  352. .catch(function(e) {
  353. form.setLoading(false);
  354. saas.util.BaseUtil.showErrorToast('保存失败: ' + e.message);
  355. form.fireEvent('aftersave', false, form);
  356. });
  357. },
  358. audit: function() {
  359. var me = this,
  360. form = me.getView(),
  361. viewModel = me.getViewModel(),
  362. detailCount = form.detailCount,
  363. codeField = form.getForm().findField(form._codeField),
  364. codeModified = !form.initId || (codeField && codeField.isDirty()),
  365. store1 = viewModel.get('detail0').detailStore,
  366. store2 = viewModel.get('detail1').detailStore;
  367. var sum_detail1 = store1.sum('vd_nowbalance'); // 从表1核销合计
  368. var sum_detail2 = store2.sum('vcd_nowbalance'); // 从表2核销合计
  369. viewModel.set("vc_amount1",sum_detail1);
  370. viewModel.set("vc_amount2",sum_detail2);
  371. //form里面数据
  372. var formData = form.getFormData();
  373. var params = {
  374. main: formData.main
  375. };
  376. for(var i = 0; i < detailCount; i++) {
  377. params['items' + ( i + 1)] = formData['detail' + i];
  378. }
  379. params.codeModified = codeModified;
  380. form.setLoading(true);
  381. saas.util.BaseUtil.request({
  382. url: form._auditUrl,
  383. params: JSON.stringify(params),
  384. method: 'POST',
  385. })
  386. .then(function(localJson) {
  387. form.setLoading(false);
  388. if(localJson.success){
  389. // 未保存直接审核会返回id
  390. if(localJson.data) {
  391. var id = localJson.data.id;
  392. var code = localJson.data.code;
  393. form.initId = id;
  394. var newId = form.xtype + '-' + id;
  395. var newTitle = form._title + '(' + code + ')';
  396. saas.util.BaseUtil.refreshTabTitle(newId, newTitle);
  397. }
  398. saas.util.FormUtil.loadData(form);
  399. form.setEditable(false);
  400. saas.util.BaseUtil.showSuccessToast('审核成功' + (localJson.message ? ': ' + localJson.message : ''));
  401. form.fireEvent('afteraudit', true, form, localJson);
  402. }
  403. })
  404. .catch(function(e) {
  405. form.setLoading(false);
  406. saas.util.BaseUtil.showErrorToast('审核失败: ' + e.message);
  407. form.fireEvent('afteraudit', false, form);
  408. });
  409. },
  410. myInitCopyData: function(formData) {
  411. var me = this,
  412. form = me.getView();
  413. //录入人,录入日期,审核人,审核日期清空
  414. formData.main['creatorId'] = saas.util.BaseUtil.getCurrentUser().id;
  415. formData.main['creatorName'] = saas.util.BaseUtil.getCurrentUser().realname;
  416. formData.main['createTime'] = Ext.Date.format(new Date(), 'Y-m-d H:i:s');
  417. formData.main['updaterId'] = undefined;
  418. formData.main['updaterName'] = undefined;
  419. formData.main['updateTime'] = undefined;
  420. formData.main[form._auditmanField] = undefined;
  421. formData.main[form._auditdateField] = undefined;
  422. return formData;
  423. },
  424. refresh: function() {
  425. var me = this,
  426. form = me.getView(),
  427. xtype = form.xtype,
  428. _config = {
  429. initId: form.initId,
  430. },
  431. currentTab = saas.util.BaseUtil.getCurrentTab();
  432. var view = {
  433. _config: _config,
  434. xtype: xtype
  435. };
  436. Ext.apply(view, _config);
  437. Ext.suspendLayouts();
  438. currentTab.removeAll();
  439. currentTab.add(view);
  440. Ext.resumeLayouts(true);
  441. },
  442. });