ledger_check.js 16 KB

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