ledger_check.js 14 KB

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