ledger_check.js 15 KB

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