ledger_check.js 13 KB

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