ledger_check.js 17 KB

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