ledger_check.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const ledgerCheckType = {
  10. sibling: {value: 1, text: '项目节、清单同层', fun: 'checkSibling', },
  11. empty_code: {value: 2, text: '项目节、清单编号同时为空', fun: 'checkCodeEmpty', },
  12. calc: {value: 3, text: '清单数量不等于计量单元之和', fun: 'checkCalc', },
  13. zero: {value: 4, text: '清单数量或单价为0', fun: 'checkZero', },
  14. tp: {value: 5, text: '清单金额≠数量×单价', fun: 'checkTp', },
  15. over: {value: 6, text: '超计', fun: 'checkOver', },
  16. same_code: {value: 7, text: '重复项目节', fun: 'checkSameCode', },
  17. limit3f: {
  18. fun: 'check3fLimit', items: [
  19. { value: 8, text: '违规计量(工序报验)', key: 'gxbyOver', type: 'gxby', },
  20. { value: 9, text: '遗漏计量(工序报验)', key: 'gxbyLost', type: 'gxby', },
  21. { value: 10, text: '违规计量(档案管理)', key: 'daglOver', type: 'dagl', },
  22. { value: 11, text: '遗漏计量(档案管理)', key: 'daglLost', type: 'dagl', },
  23. ]
  24. },
  25. };
  26. const ledgerCheckUtil = {
  27. checkSibling: function (ledgerTree, ledgerPos, decimal, option) {
  28. const error = [];
  29. for (const node of ledgerTree.nodes) {
  30. if (!node.children || node.children.length === 0) continue;
  31. let hasXmj, hasGcl;
  32. for (const child of node.children) {
  33. if (child.b_code) hasXmj = true;
  34. if (!child.b_code) hasGcl = true;
  35. }
  36. if (hasXmj && hasGcl) error.push(node);
  37. }
  38. return error;
  39. },
  40. checkCodeEmpty: function (ledgerTree, ledgerPos, decimal, option) {
  41. const error = [];
  42. const checkNodeCode = function (node) {
  43. if ((!node.code || node.code === '') && (!node.b_code || node.b_code === '')) error.push(node);
  44. if (node.children && node.children.length > 0) {
  45. for (const child of node.children) {
  46. checkNodeCode(child);
  47. }
  48. }
  49. };
  50. for (const topLevel of ledgerTree.children) {
  51. if ([1, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  52. checkNodeCode(topLevel);
  53. }
  54. return error;
  55. },
  56. checkCalc: function (ledgerTree, ledgerPos, decimal, option) {
  57. const error = [];
  58. for (const node of ledgerTree.nodes) {
  59. if (node.children && node.children.length > 0) continue;
  60. const nodePos = ledgerPos.getLedgerPos(node.id);
  61. if (!nodePos || nodePos.length === 0) continue;
  62. const checkData = {}, calcData = {};
  63. for (const f of option.fields) {
  64. checkData[f] = node[f] || 0;
  65. calcData[f] = 0;
  66. }
  67. for (const np of nodePos) {
  68. for (const f of option.fields) {
  69. calcData[f] = ZhCalc.add(calcData[f], np[f]) || 0;
  70. }
  71. }
  72. if (!_.isMatch(checkData, calcData)) error.push(node);
  73. }
  74. return error;
  75. },
  76. checkZero: function (ledgerTree, ledgerPos, decimal, option) {
  77. const error = [];
  78. for (const node of ledgerTree.nodes) {
  79. if ((!node.b_code || node.b_code === '')) continue;
  80. if (node.children && node.children.length > 0) continue;
  81. if ((checkZero(node.sgfh_qty) && checkZero(node.qtcl_qty) && checkZero(node.sjcl_qty)
  82. && checkZero(node.deal_qty) && checkZero(node.quantity))
  83. || checkZero(node.unit_price)) error.push(node);
  84. }
  85. return error;
  86. },
  87. checkTp: function (ledgerTree, ledgerPos, decimal, option) {
  88. const error = [];
  89. for (const node of ledgerTree.nodes) {
  90. if (node.children && node.children.length > 0) continue;
  91. if (option.filter && option.filter(node)) continue;
  92. const checkData = {}, calcData = {};
  93. for (const f of option.fields) {
  94. checkData[f.tp] = node[f.tp] || 0;
  95. calcData[f.tp] = ZhCalc.mul(node.unit_price, node[f.qty], decimal.tp) || 0;
  96. }
  97. if (!_.isMatch(checkData, calcData)) error.push(node);
  98. }
  99. return error;
  100. },
  101. checkOver: function(ledgerTree, ledgerPos, decimal, option) {
  102. const error = [];
  103. for (const node of ledgerTree.nodes) {
  104. if (node.children && node.children.length > 0) continue;
  105. if (checkUtils.billsOver(node, option.isTz, ledgerPos)) error.push(node);
  106. }
  107. return error;
  108. },
  109. checkSameCode: function (ledgerTree, ledgerPos, decimal, option) {
  110. const error = [];
  111. let xmj = ledgerTree.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  112. let check = null;
  113. while (xmj.length > 0) {
  114. [check, xmj] = _.partition(xmj, x => { return x.code === xmj[0].code; });
  115. if (check.length > 1) {
  116. error.push(...check);
  117. }
  118. }
  119. return error;
  120. },
  121. check3fLimit: function (ledgerTree, ledgerPos, decimal, option) {
  122. const error = {};
  123. for (const i of ledgerCheckType.limit3f.items) {
  124. error[i.key] = [];
  125. }
  126. if (option.checkType.length === 0) return error;
  127. const findPrecision = function (list, unit) {
  128. if (unit) {
  129. for (const p in list) {
  130. if (list[p].unit && list[p].unit === unit) {
  131. return list[p];
  132. }
  133. }
  134. }
  135. return list.other;
  136. };
  137. const check3f = function (data, limit, ratio) {
  138. if (limit === 0) {
  139. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  140. }
  141. if (limit === 1) {
  142. if (ratio === 0) {
  143. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  144. } else {
  145. const tp = ZhCalc.mul(data.total_price, ratio, this.ctx.tender.info.decimal.tp);
  146. const checkTp = ZhCalc.add(data.contract_tp, data.pre_contract_tp);
  147. if (tp > checkTp) return 1; // 违规
  148. if (tp < checkTp) return 2; // 漏计
  149. }
  150. }
  151. return 0; // 合法
  152. };
  153. const check3fQty = function (data, limit, ratio, unit) {
  154. if (limit === 0) {
  155. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  156. }
  157. if (limit === 1) {
  158. if (ratio <= 0) {
  159. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  160. } else {
  161. const precision = findPrecision(tenderInfo.precision, unit);
  162. const checkQty = ZhCalc.mul(data.quantity, ratio, precision.value);
  163. const qty = ZhCalc.add(data.contract_qty, data.pre_contract_qty);
  164. if (qty > checkQty) return 1; // 违规
  165. if (qty < checkQty) return 2; // 漏计
  166. }
  167. }
  168. return 0; // 合法
  169. };
  170. const checkLeafBills3fLimit = function(bills, checkInfo) {
  171. const over = [], lost = [];
  172. const posRange = ledgerPos.getLedgerPos(bills.id);
  173. if (posRange && posRange.length > 0) {
  174. for (const p of posRange) {
  175. const posCheckInfo = _.assign({}, checkInfo);
  176. for (const ct of option.checkType) {
  177. if (p[ct + '_limit'] >= 0) {
  178. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  179. posCheckInfo[ct + '_ratio'] = p[ct + '_ratio'];
  180. }
  181. }
  182. for (const ct of option.checkType) {
  183. const checkResult = check3fQty(p, posCheckInfo[ct + '_limit'], posCheckInfo[ct + '_ratio'], bills.unit);
  184. if (checkResult === 1) {
  185. if (over.indexOf(ct) === -1) over.push(ct);
  186. }
  187. if (checkResult === 2) {
  188. if (lost.indexOf(ct) === -1) lost.push(ct);
  189. }
  190. }
  191. }
  192. } else {
  193. for (const ct of option.checkType) {
  194. const checkResult = bills.is_tp
  195. ? check3f(bills, checkInfo[ct + '_limit'], checkInfo[ct + '_ratio'])
  196. : check3fQty(bills, checkInfo[ct + '_limit'], checkInfo[ct + '_ratio'], bills.unit);
  197. if (checkResult === 1) {
  198. if (over.indexOf(ct) === -1) over.push(ct);
  199. }
  200. if (checkResult === 2) {
  201. if (lost.indexOf(ct) === -1) lost.push(ct);
  202. }
  203. }
  204. }
  205. if (over.indexOf('gxby') >= 0) error.gxbyOver.push(bills);
  206. if (over.indexOf('dagl') >= 0) error.daglOver.push(bills);
  207. if (lost.indexOf('gxby') >= 0) error.gxbyLost.push(bills);
  208. if (lost.indexOf('dagl') >= 0) error.daglLost.push(bills);
  209. };
  210. const recursiveCheckBills3fLimit = function (bills, parentCheckInfo) {
  211. const checkInfo = _.assign({}, parentCheckInfo);
  212. for (const ct of option.checkType) {
  213. if (bills[ct + '_limit'] >= 0) {
  214. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  215. checkInfo[ct + '_ratio'] = bills[ct + '_ratio'];
  216. }
  217. }
  218. if (bills.children && bills.children.length > 0) {
  219. for (const c of bills.children) {
  220. recursiveCheckBills3fLimit(c, checkInfo);
  221. }
  222. } else {
  223. checkLeafBills3fLimit(bills, checkInfo);
  224. }
  225. };
  226. for (const b of ledgerTree.children) {
  227. recursiveCheckBills3fLimit(b, {});
  228. }
  229. return error;
  230. },
  231. };
  232. const ledgerCheck2 = function (setting) {
  233. const ledger = setting.ledgerTree, ledgerPos = setting.ledgerPos, decimal = setting.decimal;
  234. const checkOption = setting.checkOption;
  235. const assignWarningData = function (nodes, checkType, warningData) {
  236. for (const node of nodes) {
  237. warningData.push({
  238. type: checkType,
  239. ledger_id: node.ledger_id,
  240. code: node.code,
  241. b_code: node.b_code,
  242. name: node.name,
  243. })
  244. }
  245. };
  246. const checkData = {
  247. check_time: new Date(),
  248. warning_data: [],
  249. };
  250. const progressData = [];
  251. for (const prop in ledgerCheckType) {
  252. if (!checkOption[prop] || !checkOption[prop].enable) continue;
  253. if (ledgerCheckType[prop].items) {
  254. const errors = ledgerCheckUtil[ledgerCheckType[prop].fun](ledger, ledgerPos, decimal, checkOption[prop]) || {};
  255. for (const i of ledgerCheckType[prop].items) {
  256. if (checkOption[prop].checkType.indexOf(i.type) < 0) continue;
  257. assignWarningData(errors[i.key], i.value, checkData.warning_data);
  258. progressData.push({key: prop + i.key, caption: i.text, error: errors[i.key].length});
  259. }
  260. } else {
  261. const errors = ledgerCheckUtil[ledgerCheckType[prop].fun](ledger, ledgerPos, decimal, checkOption[prop]) || [];
  262. assignWarningData(errors, ledgerCheckType[prop].value, checkData.warning_data);
  263. progressData.push({key: prop, caption: ledgerCheckType[prop].text, error: errors.length});
  264. }
  265. }
  266. setting.checkList.clearCheckData();
  267. if (checkData.warning_data.length > 0) {
  268. setting.checkList.loadCheckData(checkData);
  269. } else {
  270. setting.checkList.hide();
  271. }
  272. return progressData;
  273. };
  274. const getCheckType = function (option) {
  275. const result = {};
  276. for (const o in option) {
  277. if (option[o].enable) {
  278. if (ledgerCheckType[o].items) {
  279. for (const i of ledgerCheckType[o].items) {
  280. result[o + i.key] = i;
  281. }
  282. } else {
  283. result[o] = ledgerCheckType[o];
  284. }
  285. }
  286. }
  287. return result;
  288. };