ledger_check.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 xmj = [];
  113. const addXmjCheck = function (node) {
  114. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  115. for (const child of node.children) {
  116. addXmjCheck(child);
  117. }
  118. };
  119. for (const topLevel of ledgerTree.children) {
  120. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  121. addXmjCheck(topLevel);
  122. }
  123. let check = null;
  124. while (xmj.length > 0) {
  125. [check, xmj] = _.partition(xmj, x => { return x.code === xmj[0].code; });
  126. if (check.length > 1) {
  127. error.push(...check);
  128. }
  129. }
  130. return error;
  131. },
  132. check3fLimit: function (ledgerTree, ledgerPos, decimal, option) {
  133. const error = {};
  134. for (const i of ledgerCheckType.limit3f.items) {
  135. error[i.key] = [];
  136. }
  137. if (option.checkType.length === 0) return error;
  138. const findPrecision = function (list, unit) {
  139. if (unit) {
  140. for (const p in list) {
  141. if (list[p].unit && list[p].unit === unit) {
  142. return list[p];
  143. }
  144. }
  145. }
  146. return list.other;
  147. };
  148. const getRatio = function (type, status) {
  149. const statusConst = type === 'gxby' ? option.status.gxby : option.status.dagl;
  150. const sc = statusConst.find(x => { return x.value === status });
  151. return sc ? sc.ratio : null;
  152. };
  153. const getValid = function (type, status, limit) {
  154. if (limit) {
  155. const statusConst = type === 'gxby' ? option.status.gxby : option.status.dagl;
  156. const sc = statusConst.find(x => { return x.value === status; });
  157. return sc ? (sc.limit ? 1 : 0) : 0;
  158. } else {
  159. return -1;
  160. }
  161. };
  162. const check3f = function (data, limit, ratio) {
  163. if (limit === 0) {
  164. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  165. }
  166. if (limit === 1) {
  167. if (ratio === 0) {
  168. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  169. } else {
  170. const tp = ZhCalc.mul(data.total_price, ZhCalc.div(ratio, 100, 4), this.ctx.tender.info.decimal.tp);
  171. const checkTp = ZhCalc.add(data.contract_tp, data.pre_contract_tp);
  172. if (tp > checkTp) return 1; // 违规
  173. if (tp < checkTp) return 2; // 漏计
  174. }
  175. }
  176. return 0; // 合法
  177. };
  178. const check3fQty = function (data, limit, ratio, unit) {
  179. if (limit === 0) {
  180. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  181. }
  182. if (limit === 1) {
  183. if (!ratio || ratio === 0) {
  184. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  185. } else {
  186. const precision = findPrecision(tenderInfo.precision, unit);
  187. const checkQty = ZhCalc.mul(data.quantity, ZhCalc.div(ratio, 100, 4), precision.value);
  188. const qty = ZhCalc.add(data.contract_qty, data.pre_contract_qty) || 0;
  189. if (qty > checkQty) return 1; // 违规
  190. if (qty < checkQty) return 2; // 漏计
  191. }
  192. }
  193. return 0; // 合法
  194. };
  195. const checkLeafBills3fLimit = function(bills, checkInfo) {
  196. const over = [], lost = [];
  197. const posRange = ledgerPos.getLedgerPos(bills.id);
  198. if (posRange && posRange.length > 0) {
  199. for (const p of posRange) {
  200. const posCheckInfo = _.assign({}, checkInfo);
  201. for (const ct of option.checkType) {
  202. if (p[ct + '_limit'] > 0) {
  203. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  204. }
  205. }
  206. for (const ct of option.checkType) {
  207. const checkResult = check3fQty(p, getValid(ct, p[ct + '_status'], posCheckInfo[ct + '_limit']),
  208. getRatio(ct, p[ct + '_status']), bills.unit);
  209. if (checkResult === 1) {
  210. if (over.indexOf(ct) === -1) over.push(ct);
  211. }
  212. if (checkResult === 2) {
  213. if (lost.indexOf(ct) === -1) lost.push(ct);
  214. }
  215. }
  216. }
  217. } else {
  218. for (const ct of option.checkType) {
  219. const checkResult = bills.is_tp
  220. ? check3f(bills, getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), getRatio(ct, bills[ct + '_status']))
  221. : check3fQty(bills, getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), getRatio(ct, bills[ct + '_status']), bills.unit);
  222. if (checkResult === 1) {
  223. if (over.indexOf(ct) === -1) over.push(ct);
  224. }
  225. if (checkResult === 2) {
  226. if (lost.indexOf(ct) === -1) lost.push(ct);
  227. }
  228. }
  229. }
  230. if (over.indexOf('gxby') >= 0) error.gxbyOver.push(bills);
  231. if (over.indexOf('dagl') >= 0) error.daglOver.push(bills);
  232. if (lost.indexOf('gxby') >= 0) error.gxbyLost.push(bills);
  233. if (lost.indexOf('dagl') >= 0) error.daglLost.push(bills);
  234. };
  235. const recursiveCheckBills3fLimit = function (bills, parentCheckInfo) {
  236. const checkInfo = _.assign({}, parentCheckInfo);
  237. for (const ct of option.checkType) {
  238. if (bills[ct + '_limit'] > 0) {
  239. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  240. }
  241. }
  242. if (bills.children && bills.children.length > 0) {
  243. for (const c of bills.children) {
  244. recursiveCheckBills3fLimit(c, checkInfo);
  245. }
  246. } else {
  247. checkLeafBills3fLimit(bills, checkInfo);
  248. }
  249. };
  250. for (const b of ledgerTree.children) {
  251. recursiveCheckBills3fLimit(b, {});
  252. }
  253. return error;
  254. },
  255. };
  256. const ledgerCheck2 = function (setting) {
  257. const ledger = setting.ledgerTree, ledgerPos = setting.ledgerPos, decimal = setting.decimal;
  258. const checkOption = setting.checkOption;
  259. const assignWarningData = function (nodes, checkType, warningData) {
  260. for (const node of nodes) {
  261. warningData.push({
  262. type: checkType,
  263. ledger_id: node.ledger_id,
  264. code: node.code,
  265. b_code: node.b_code,
  266. name: node.name,
  267. })
  268. }
  269. };
  270. const checkData = {
  271. check_time: new Date(),
  272. warning_data: [],
  273. };
  274. const progressData = [];
  275. for (const prop in ledgerCheckType) {
  276. if (!checkOption[prop] || !checkOption[prop].enable) continue;
  277. if (ledgerCheckType[prop].items) {
  278. const errors = ledgerCheckUtil[ledgerCheckType[prop].fun](ledger, ledgerPos, decimal, checkOption[prop]) || {};
  279. for (const i of ledgerCheckType[prop].items) {
  280. if (checkOption[prop].checkType.indexOf(i.type) < 0) continue;
  281. assignWarningData(errors[i.key], i.value, checkData.warning_data);
  282. progressData.push({key: prop + i.key, caption: i.text, error: errors[i.key].length});
  283. }
  284. } else {
  285. const errors = ledgerCheckUtil[ledgerCheckType[prop].fun](ledger, ledgerPos, decimal, checkOption[prop]) || [];
  286. assignWarningData(errors, ledgerCheckType[prop].value, checkData.warning_data);
  287. progressData.push({key: prop, caption: ledgerCheckType[prop].text, error: errors.length});
  288. }
  289. }
  290. setting.checkList.clearCheckData();
  291. if (checkData.warning_data.length > 0) {
  292. setting.checkList.loadCheckData(checkData);
  293. } else {
  294. setting.checkList.hide();
  295. }
  296. return progressData;
  297. };
  298. const getCheckType = function (option) {
  299. const result = {};
  300. for (const o in option) {
  301. if (option[o].enable) {
  302. if (ledgerCheckType[o].items) {
  303. for (const i of ledgerCheckType[o].items) {
  304. result[o + i.key] = i;
  305. }
  306. } else {
  307. result[o] = ledgerCheckType[o];
  308. }
  309. }
  310. }
  311. return result;
  312. };