cs_gcl_gather.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. (function($){
  10. $.cs_gclGather = function (setting) {
  11. if (!setting.selector) return;
  12. if (!setting.spreadSetting) {
  13. setting.spreadSetting = {
  14. cols: [
  15. {title: '清单编号', field: 'b_code', width: 80, formatter: '@'},
  16. {title: '名称', field: 'name', width: 150, formatter: '@'},
  17. {title: '单位', field: 'unit', width: 50, formatter: '@'},
  18. {title: '单价', field: 'unit_price', width: 60, formatter: '@'},
  19. {title: '数量', field: 'quantity', width: 60, },
  20. {title: '数量', field: 'total_price', width: 60, },
  21. ],
  22. emptyRows: 0,
  23. headRows: 1,
  24. headRowHeight: [32],
  25. defaultRowHeight: 21,
  26. headerFont: '12px 微软雅黑',
  27. font: '12px 微软雅黑',
  28. selectedBackColor: '#fffacd',
  29. readOnly: true,
  30. };
  31. }
  32. if (!setting.gatherFields) setting.gatherFields = ['quantity', 'total_price'];
  33. const resultId = setting.id + '-spread';
  34. const obj = $(setting.selector);
  35. obj.html(
  36. '<div class="sjs-bar">\n' +
  37. ` <div class="pb-1 d-flex"><button class="btn btn-primary btn-sm" id="gcl-gather-refresh">汇总</button>\n` +
  38. ' <span class="pl-2" id="' + setting.id + '-info"></span>\n' +
  39. ' </div>\n' +
  40. '</div>' +
  41. '<div id="' + resultId + '" class="sjs-sh">\n' +
  42. '</div>'
  43. );
  44. autoFlashHeight();
  45. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  46. const sheet = spread.getActiveSheet();
  47. const result = [];
  48. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  49. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, result);
  50. const recursiveGather = function(nodes) {
  51. for (const node of nodes) {
  52. if (node.children && node.children.length > 0) {
  53. recursiveGather(node.children);
  54. } else {
  55. if (!node.b_code) continue;
  56. let gcl = result.find(x => {
  57. return x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price;
  58. });
  59. if (!gcl) {
  60. gcl = { b_code: node.b_code, name: node.name, unit: node.unit, unit_price: node.unit_price };
  61. result.push(gcl);
  62. }
  63. for (const f of setting.gatherFields) {
  64. gcl[f] = ZhCalc.add(gcl[f], node[f]);
  65. }
  66. }
  67. }
  68. };
  69. const gather = function (node) {
  70. $(`#${setting.id}-info`).html(`${node.code || ''}${node.b_code || ''} ${node.name || ''} - ${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}`);
  71. recursiveGather([node]);
  72. result.sort((a, b) => {
  73. return checkUtils.compareCode(a.b_code, b.b_code);
  74. });
  75. SpreadJsObj.reLoadSheetData(sheet);
  76. };
  77. $('#gcl-gather-refresh').click(() => {
  78. gather(SpreadJsObj.getSelectObject(setting.relaSheet));
  79. });
  80. return { spread, sheet, gather}
  81. };
  82. })(jQuery);