revise_price.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. 'use strict';
  2. /**
  3. *
  4. * 单价调整计算:
  5. * 1. 台账修订上报,保存台账修订历史时,使用当前单价调整计算一次
  6. * 2. 台账修订完成:计算台账、应用到未审完成期、应用到所有工程变更
  7. * 3. 新增期:检查是否存在未应用的单价变更
  8. * 4. 期审批完成:所有未应用的单价调整,记录应用到该期(记录revise_price.use_stage/use_stage_order) - 一条sql即可
  9. * 5. 期重现审批:上一次应用的所有单价调整,回到未应用状态(revise_price.use_stage/use_stage_order均回到0) - 一条sql即可
  10. *
  11. * @author Mai
  12. * @date
  13. * @version
  14. */
  15. const Ledger = require('./ledger');
  16. const audit = require('../const/audit');
  17. class revisePriceCalc {
  18. constructor(ctx) {
  19. this.ctx = ctx;
  20. }
  21. set price(price) {
  22. this._price = price;
  23. this.common_price_c = [];
  24. this.rela_price_c = [];
  25. price.forEach(x => {
  26. x.rela_lid = x.rela_lid ? x.rela_lid.split(',') : [];
  27. if (x.rela_cid) {
  28. x.rela_cid = x.rela_cid.split(',');
  29. this.rela_price_c.push(x);
  30. } else {
  31. x.his_rela_cid = [];
  32. this.common_price_c.push(x);
  33. }
  34. });
  35. }
  36. get price() {
  37. return this._price;
  38. }
  39. findChangeBillsPrice(b_code, name, unit, unit_price, cid) {
  40. const helper = this.ctx.helper;
  41. const p = this.rela_price_c.find(x => {
  42. return b_code === x.b_code && name === x.name && unit === x.unit && helper.numEqual(unit_price, x.org_price) && x.rela_cid.indexOf(cid) >= 0;
  43. });
  44. if (p) return p;
  45. return this.common_price_c.find(x => {
  46. return b_code === x.b_code && name === x.name && unit === x.unit && helper.numEqual(unit_price, x.org_price);
  47. });
  48. }
  49. findCommonChangeBillsPrice(b_code, name, unit, unit_price) {
  50. const helper = this.ctx.helper;
  51. const p = this.price.find(x => {
  52. return b_code === x.b_code && name === x.name && unit === x.unit && helper.numEqual(unit_price, x.org_price);
  53. });
  54. return p;
  55. }
  56. /**
  57. * 新增一期计量,检查单价调整
  58. * @param {Object} newStage - 新计量期
  59. * @param {Object} preStage - 上一计量期
  60. * @return { inseretPosData, insertBillsData }
  61. */
  62. async newStagePriceChange(newStage, preStage, transaction) {
  63. const pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
  64. // 获取未执行的单价变更,无单价变更不执行
  65. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { tid: newStage.tid, valid: 1, use_stage: 0 } });
  66. if (this.price.length === 0) return pcTp;
  67. // 无截止上期数据不执行
  68. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { sid: preStage.id } });
  69. if (preBillsData.length === 0) return pcTp;
  70. // 加载树结构
  71. const bills = await this.ctx.service.ledger.getData(newStage.tid);
  72. this.ctx.helper.assignRelaData(bills, [
  73. { data: preBillsData, fields: ['id', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'unit_price', 'positive_qc_qty', 'negative_qc_qty', 'positive_qc_tp', 'negative_qc_tp'], prefix: 'pre_', relaId: 'lid' },
  74. ]);
  75. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  76. billsTree.loadDatas(bills);
  77. // 计算
  78. const result = { ibData: [] };
  79. const helper = this.ctx.helper;
  80. const decimal = this.ctx.tender.info.decimal;
  81. const calcType = this.ctx.tender.info.calc_type;
  82. billsTree.calculateAll(node => {
  83. if (!node.pre_id) return;
  84. if (!node.pre_contract_qty && !node.pre_qc_qty) return;
  85. if (node.children && node.children.length > 0) return;
  86. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  87. if (!priceDiff) return;
  88. node.contract_pc_tp = calcType === 'up' ? helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp) : 0;
  89. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  90. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  91. node.positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  92. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  93. result.ibData.push({
  94. tid: newStage.tid, sid: newStage.id, sorder: newStage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  95. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  96. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  97. });
  98. });
  99. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.ibData);
  100. for (const ibc of result.ibData) {
  101. pcTp.contract_pc_tp = helper.add(pcTp.contract_pc_tp, ibc.contract_pc_tp);
  102. pcTp.qc_pc_tp = helper.add(pcTp.qc_pc_tp, ibc.qc_pc_tp);
  103. pcTp.pc_tp = helper.add(pcTp.pc_tp, ibc.pc_tp);
  104. pcTp.positive_qc_pc_tp = helper.add(pcTp.positive_qc_pc_tp, ibc.positive_qc_pc_tp);
  105. pcTp.negative_qc_pc_tp = helper.add(pcTp.negative_qc_pc_tp, ibc.negative_qc_pc_tp);
  106. }
  107. await transaction.update(this.ctx.service.stage.tableName,
  108. { id: newStage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
  109. return pcTp;
  110. }
  111. /**
  112. * 期,重新审批,检查单价调整
  113. * @param {Object} stage - 期
  114. * @param {Number} auditOrder - 重新审批,最新审批人序号
  115. * @param {Object} transaction - 事务
  116. */
  117. async stageCheckAgainPriceChange(stage, auditOrder, transaction) {
  118. const pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
  119. // 获取未执行的单价变更,无单价变更不执行
  120. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { tid: stage.tid, valid: 1, use_stage: 0 } });
  121. if (this.price.length === 0) return pcTp;
  122. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  123. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  124. // 加载树结构
  125. const bills = await this.ctx.service.ledger.getData(stage.tid);
  126. this.ctx.helper.assignRelaData(bills, [
  127. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'postil', 'times', 'order', 'ex_stage_qty1'], prefix: 'cur_', relaId: 'lid' },
  128. { data: preBillsData, fields: ['id', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'unit_price', 'positive_qc_qty', 'negative_qc_qty', 'positive_qc_tp', 'negative_qc_tp'], prefix: 'pre_', relaId: 'lid' },
  129. ]);
  130. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  131. billsTree.loadDatas(bills);
  132. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  133. // 计算 insertBills billsPriceChange stageChange
  134. const result = { ibData: [], bpcData: [], scData: [] };
  135. const helper = this.ctx.helper;
  136. const decimal = this.ctx.tender.info.decimal;
  137. const said = this.ctx.session.sessionUser.accountId;
  138. const calcType = this.ctx.tender.info.calc_type;
  139. billsTree.calculateAll(node => {
  140. if (node.children && node.children.length > 0) return;
  141. // const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  142. // if (!priceDiff) return;
  143. if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty || node.cur_ex_stage_qty1)) {
  144. const cur_contract_tp = calcType === 'up' ? helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp) : node.cur_contract_tp;
  145. const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  146. const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  147. const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  148. const cur_ex_stage_tp1 = helper.mul(node.cur_ex_stage_qty1, node.unit_price, decimal.tp);
  149. if (cur_contract_tp !== node.cur_contract_tp || cur_qc_tp !== node.cur_qc_tp || cur_positive_qc_tp !== node.cur_positive_qc_tp || cur_negative_qc_tp !== node.cur_positive_qc_tp || cur_ex_stage_tp1 !== node.cur_ex_stage_tp1) {
  150. result.ibData.push({
  151. tid: stage.tid, sid: stage.id, said,
  152. lid: node.id,
  153. times: stage.times, order: auditOrder,
  154. contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
  155. qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
  156. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
  157. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
  158. ex_stage_qty1: node.cur_ex_stage_qty1, ex_stage_tp1: cur_ex_stage_tp1,
  159. postil: node.postil,
  160. });
  161. }
  162. }
  163. if (node.pre_id && (node.pre_contract_qty || node.pre_qc_qty)) {
  164. const contract_pc_tp = calcType === 'up' ? helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp) : 0;
  165. const qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  166. const pc_tp = helper.add(contract_pc_tp, qc_pc_tp);
  167. const positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  168. const negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  169. if (contract_pc_tp || qc_pc_tp || pc_tp || positive_qc_pc_tp || negative_qc_pc_tp) {
  170. result.bpcData.push({
  171. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  172. contract_pc_tp, qc_pc_tp, pc_tp, positive_qc_pc_tp, negative_qc_pc_tp,
  173. });
  174. }
  175. }
  176. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  177. for (const scd of scDetail) {
  178. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  179. }
  180. });
  181. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  182. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  183. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  184. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  185. for (const bpc of result.bpcData) {
  186. pcTp.contract_pc_tp = helper.add(pcTp.contract_pc_tp, bpc.contract_pc_tp);
  187. pcTp.qc_pc_tp = helper.add(pcTp.qc_pc_tp, bpc.qc_pc_tp);
  188. pcTp.pc_tp = helper.add(pcTp.pc_tp, bpc.pc_tp);
  189. pcTp.positive_qc_pc_tp = helper.add(pcTp.positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  190. pcTp.negative_qc_pc_tp = helper.add(pcTp.negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  191. }
  192. await transaction.update(this.ctx.service.stage.tableName,
  193. { id: stage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
  194. return pcTp;
  195. }
  196. /**
  197. * 重算工程变更(调整单价)
  198. * @param {Object} change - 工程变更
  199. * @param {Object} transaction - 事务 (无则非事务提交)
  200. */
  201. async calcChange(change, transaction) {
  202. const decimal = this.ctx.tender.info.decimal;
  203. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: change.cid } });
  204. const updateBills = [];
  205. const sumBills = [];
  206. for (const b of changeBills) {
  207. const p = this.findChangeBillsPrice(b.code, b.name, b.unit, b.unit_price, change.cid);
  208. const settleGcl = b.gcl_id ? this.settleBills.find(x => { return x.lid === b.gcl_id; }) : null;
  209. const settlePos = b.mx_id ? this.settlePos.find(x => { return x.pid === b.mx_id; }): null;
  210. let bills_tp;
  211. if (p && !settleGcl && !settlePos) {
  212. bills_tp = this.ctx.helper.mul(p.new_price, b.spamount, change.tp_decimal || decimal.tp);
  213. updateBills.push({ id: b.id, unit_price: p.new_price, checked_price: bills_tp });
  214. if (!p.rela_cid) p.his_rela_cid.push(change.cid);
  215. } else {
  216. bills_tp = this.ctx.helper.mul(b.unit_price, b.spamount, change.tp_decimal || decimal.tp);
  217. }
  218. let sb;
  219. if (b.gcl_id) {
  220. sb = sumBills.find(x => { return x.gcl_id === b.gcl_id });
  221. if (!sb) {
  222. sb = {gcl_id: b.gcl_id, unit_price: p ? p.new_price : b.unit_price, v_qty: 0, uv_qty: 0, p_qty: 0, n_qty: 0 };
  223. sumBills.push(sb);
  224. }
  225. } else {
  226. sb = { gcl_id: b.gcl_id, unit_price: p ? p.new_price : b.unit_price, v_qty: 0, uv_qty: 0, p_qty: 0, n_qty: 0 };
  227. sumBills.push(sb);
  228. }
  229. if (b.spamount >= 0) {
  230. sb.p_qty = this.ctx.helper.add(sb.p_qty, b.spamount);
  231. } else {
  232. sb.n_qty = this.ctx.helper.add(sb.n_qty, b.spamount);
  233. }
  234. if (b.is_valuation) {
  235. sb.v_qty = this.ctx.helper.add(sb.v_qty, b.spamount);
  236. } else {
  237. sb.uv_qty = this.ctx.helper.add(sb.uv_qty, b.spamount);
  238. }
  239. }
  240. let total_price = 0, positive_tp = 0, negative_tp = 0, valuation_tp = 0, unvaluation_tp = 0;
  241. for (const sb of sumBills) {
  242. sb.qty = this.ctx.helper.add(sb.v_qty, sb.uv_qty);
  243. sb.tp = this.ctx.helper.mul(sb.qty, sb.unit_price, change.tp_decimal || decimal.tp);
  244. sb.v_tp = this.ctx.helper.mul(sb.v_qty, sb.unit_price, change.tp_decimal || decimal.tp);
  245. sb.uv_tp = this.ctx.helper.mul(sb.uv_qty, sb.unit_price, change.tp_decimal || decimal.tp);
  246. sb.p_tp = this.ctx.helper.mul(sb.p_qty, sb.unit_price, change.tp_decimal || decimal.tp);
  247. sb.n_tp = this.ctx.helper.mul(sb.n_qty, sb.unit_price, change.tp_decimal || decimal.tp);
  248. total_price = this.ctx.helper.add(sb.tp, total_price);
  249. valuation_tp = this.ctx.helper.add(sb.v_tp, valuation_tp);
  250. unvaluation_tp = this.ctx.helper.add(sb.uv_tp, unvaluation_tp);
  251. positive_tp = this.ctx.helper.add(sb.p_tp, positive_tp);
  252. negative_tp = this.ctx.helper.add(sb.n_tp, negative_tp);
  253. }
  254. // const total_price = this.ctx.helper.add(valuation_tp, unvaluation_tp);
  255. if (updateBills.length > 0) {
  256. await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateBills);
  257. await transaction.update(this.ctx.service.change.tableName, { total_price, positive_tp, negative_tp, valuation_tp, unvaluation_tp }, { where: { cid: change.cid } });
  258. }
  259. }
  260. /**
  261. * 重算标段下所有工程变更(调整单价)
  262. * @param {Number} tid - 标段id
  263. * @param {Object} transaction - 事务 (无则非事务提交)
  264. */
  265. async calcAllChanges(tid, transaction) {
  266. const change = await this.ctx.service.change.getAllDataByCondition({ where: { tid, valid: 1 } });
  267. if (change.length === 0) return;
  268. for (const c of change) {
  269. await this.calcChange(c, transaction);
  270. }
  271. const revisePriceUpdate = [];
  272. for (const p of this.common_price_c) {
  273. if (p.his_rela_cid.length > 0) revisePriceUpdate.push({id: p.id, his_rela_cid: p.his_rela_cid.join(',')});
  274. }
  275. if (revisePriceUpdate.length > 0) await transaction.updateRows(this.ctx.service.revisePrice.tableName, revisePriceUpdate);
  276. }
  277. /**
  278. * 重算变更方案(调整单价)
  279. * @param {Object} change - 工程变更
  280. * @param {Object} transaction - 事务 (无则非事务提交)
  281. */
  282. async calcChangePlan(changePlan, transaction) {
  283. const decimal = changePlan.decimal ? JSON.parse(changePlan.decimal) : this.ctx.tender.info.decimal;
  284. const changeBills = await this.ctx.service.changePlanList.getAllDataByCondition({ where: { cpid: changePlan.id } });
  285. const updateBills = [];
  286. let total_price = 0;
  287. for (const b of changeBills) {
  288. const p = this.findCommonChangeBillsPrice(b.code, b.name, b.unit, b.unit_price);
  289. let bills_tp;
  290. if (p) {
  291. bills_tp = this.ctx.helper.mul(p.new_price, b.spamount, decimal.tp);
  292. updateBills.push({ id: b.id, unit_price: p.new_price });
  293. } else {
  294. bills_tp = this.ctx.helper.mul(b.unit_price, b.spamount, decimal.tp);
  295. }
  296. total_price = this.ctx.helper.add(total_price, bills_tp);
  297. }
  298. if (updateBills.length > 0) {
  299. await transaction.updateRows(this.ctx.service.changePlanList.tableName, updateBills);
  300. await transaction.update(this.ctx.service.changePlan.tableName, { id: changePlan.id, total_price});
  301. }
  302. }
  303. async calcAllChangePlan(tid, transaction) {
  304. const changePlan = await this.ctx.service.changePlan.getAllDataByCondition({ where: { tid } });
  305. if (changePlan.length === 0) return;
  306. for (const c of changePlan) {
  307. await this.calcChangePlan(c, transaction);
  308. }
  309. }
  310. /**
  311. * 重算变更申请(调整单价)
  312. * @param {Object} change - 工程变更
  313. * @param {Object} transaction - 事务 (无则非事务提交)
  314. */
  315. async calcChangeApply(changeApply, transaction) {
  316. const decimal = changeApply.decimal ? JSON.parse(changeApply.decimal) : this.ctx.tender.info.decimal;
  317. const changeBills = await this.ctx.service.changeApplyList.getAllDataByCondition({ where: { caid: changeApply.id } });
  318. const updateBills = [];
  319. let total_price = 0;
  320. for (const b of changeBills) {
  321. const p = this.findCommonChangeBillsPrice(b.code, b.name, b.unit, b.unit_price);
  322. let bills_tp;
  323. if (p) {
  324. bills_tp = this.ctx.helper.mul(p.new_price, b.camount, decimal.tp);
  325. updateBills.push({ id: b.id, unit_price: p.new_price });
  326. } else {
  327. bills_tp = this.ctx.helper.mul(b.unit_price, b.camount, decimal.tp);
  328. }
  329. total_price = this.ctx.helper.add(total_price, bills_tp);
  330. }
  331. if (updateBills.length > 0) {
  332. await transaction.updateRows(this.ctx.service.changeApplyList.tableName, updateBills);
  333. await transaction.update(this.ctx.service.changeApply.tableName, { id: changeApply.id, total_price});
  334. }
  335. }
  336. async calcAllChangeApply(tid, transaction) {
  337. const changeApply = await this.ctx.service.changeApply.getAllDataByCondition({ where: { tid } });
  338. if (changeApply.length === 0) return;
  339. for (const c of changeApply) {
  340. await this.calcChangeApply(c, transaction);
  341. }
  342. }
  343. async _calcStage(stage, bills, transaction) {
  344. const pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
  345. // 无单价变更不执行
  346. if (this.price.length === 0) return pcTp;
  347. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  348. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  349. // 加载树结构
  350. this.ctx.helper.assignRelaData(bills, [
  351. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'times', 'order', 'postil', 'ex_stage_qty1', 'ex_stage_tp1'], prefix: 'cur_', relaId: 'lid' },
  352. { data: preBillsData, fields: ['id', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'unit_price', 'positive_qc_qty', 'negative_qc_qty', 'positive_qc_tp', 'negative_qc_tp'], prefix: 'pre_', relaId: 'lid' },
  353. ], 'id', true);
  354. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  355. billsTree.loadDatas(bills);
  356. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  357. // 计算 insertBills/updateBills billsPriceChange StageChange
  358. const result = { ibData: [], ubData: [], bpcData: [], scData: [] };
  359. const helper = this.ctx.helper;
  360. const decimal = this.ctx.tender.info.decimal;
  361. const said = this.ctx.session.sessionUser.accountId;
  362. const calcType = this.ctx.tender.info.calc_type;
  363. billsTree.calculateAll(node => {
  364. if (node.children && node.children.length > 0) return;
  365. if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty || node.cur_ex_stage_qty1)) {
  366. const cur_contract_tp = calcType === 'up' ? helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp) : node.cur_contract_tp;
  367. const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  368. const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  369. const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  370. const cur_ex_stage_tp1 = helper.mul(node.cur_ex_stage_qty1, node.unit_price, decimal.tp);
  371. if (cur_contract_tp !== node.cur_contract_tp || cur_qc_tp !== node.cur_qc_tp || cur_positive_qc_tp !== node.cur_positive_qc_tp || cur_negative_qc_tp !== node.cur_positive_qc_tp || cur_ex_stage_tp1 !== node.cur_ex_stage_tp1) {
  372. if (node.cur_times === stage.times && node.cur_order === 0) {
  373. result.ubData.push({
  374. id: node.cur_id,
  375. contract_tp: cur_contract_tp, qc_tp: cur_qc_tp,
  376. positive_qc_tp: cur_positive_qc_tp, negative_qc_tp: cur_negative_qc_tp,
  377. ex_stage_tp1: cur_ex_stage_tp1,
  378. });
  379. } else {
  380. result.ibData.push({
  381. tid: stage.tid, sid: stage.id, said,
  382. lid: node.id, times: stage.times, order: 0,
  383. contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
  384. qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
  385. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
  386. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
  387. ex_stage_qty1: node.cur_ex_stage_qty1, ex_stage_tp1: cur_ex_stage_tp1,
  388. postil: node.cur_postil,
  389. });
  390. }
  391. }
  392. }
  393. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  394. for (const scd of scDetail) {
  395. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  396. }
  397. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  398. if (!priceDiff) return;
  399. if (node.pre_id && (node.pre_contract_qty || node.pre_qc_qty)) {
  400. node.contract_pc_tp = calcType === 'up' ? helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp) : 0;
  401. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  402. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  403. node.positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  404. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  405. result.bpcData.push({
  406. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  407. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  408. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  409. });
  410. }
  411. });
  412. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  413. if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
  414. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  415. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  416. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  417. for (const bpc of result.bpcData) {
  418. pcTp.contract_pc_tp = helper.add(pcTp.contract_pc_tp, bpc.contract_pc_tp);
  419. pcTp.qc_pc_tp = helper.add(pcTp.qc_pc_tp, bpc.qc_pc_tp);
  420. pcTp.pc_tp = helper.add(pcTp.pc_tp, bpc.pc_tp);
  421. pcTp.positive_qc_pc_tp = helper.add(pcTp.positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  422. pcTp.negative_qc_pc_tp = helper.add(pcTp.negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  423. }
  424. await transaction.update(this.ctx.service.stage.tableName,
  425. { id: stage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
  426. return pcTp;
  427. }
  428. async _calcStageWithoutPc(stage, bills, transaction) {
  429. // 无单价变更不执行
  430. if (this.price.length === 0) return;
  431. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  432. // 加载树结构
  433. this.ctx.helper.assignRelaData(bills, [
  434. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'times', 'order', 'postil'], prefix: 'cur_', relaId: 'lid' },
  435. ], 'id', true);
  436. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  437. billsTree.loadDatas(bills);
  438. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  439. // 计算 insertBills/updateBills StageChange
  440. const result = { ibData: [], ubData: [], scData: [] };
  441. const helper = this.ctx.helper;
  442. const decimal = this.ctx.tender.info.decimal;
  443. const said = this.ctx.session.sessionUser.accountId;
  444. const calcType = this.ctx.tender.info.calc_type;
  445. billsTree.calculateAll(node => {
  446. if (node.children && node.children.length > 0) return;
  447. if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty)) {
  448. const cur_contract_tp = calcType === 'up' ? helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp) : node.cur_contract_tp;
  449. const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  450. const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  451. const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  452. if (cur_contract_tp !== node.cur_contract_tp || cur_qc_tp !== node.cur_qc_tp || cur_positive_qc_tp !== node.cur_positive_qc_tp || cur_negative_qc_tp !== node.cur_positive_qc_tp) {
  453. if (node.cur_times === stage.times && node.cur_order === 0) {
  454. result.ubData.push({
  455. id: node.cur_id,
  456. contract_tp: cur_contract_tp, qc_tp: cur_qc_tp,
  457. positive_qc_tp: cur_positive_qc_tp, negative_qc_tp: cur_negative_qc_tp,
  458. });
  459. } else {
  460. result.ibData.push({
  461. tid: stage.tid, sid: stage.id, said,
  462. lid: node.id, times: stage.times, order: 0,
  463. contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
  464. qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
  465. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
  466. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
  467. postil: node.cur_postil,
  468. });
  469. }
  470. }
  471. }
  472. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  473. for (const scd of scDetail) {
  474. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  475. }
  476. });
  477. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  478. if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
  479. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  480. if (result.ibData.length > 0 || result.ubData.length > 0) await transaction.update(this.ctx.service.stage.tableName, { id: stage.id, check_calc: true});
  481. }
  482. /**
  483. * 计算修订台账
  484. * @param {Object}revise - 最新一次台账修订(此处不检查)
  485. * @param {Object} transaction - 事务 (无则非事务提交)
  486. */
  487. async calcReviseLedger(revise, transaction) {
  488. const xmj = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.bills_file);
  489. xmj.forEach(x => {
  490. delete x.is_tp;
  491. delete x.gxby_status;
  492. delete x.gxby_limit;
  493. delete x.gxby_ratio;
  494. delete x.gxby_url;
  495. delete x.dagl_status;
  496. delete x.dagl_limit;
  497. delete x.dagl_ratio;
  498. delete x.dagl_url;
  499. });
  500. const pos = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.pos_file);
  501. pos.forEach(p => {
  502. p.in_time = new Date(p.in_time);
  503. delete p.gxby_status;
  504. delete p.gxby_limit;
  505. delete p.gxby_ratio;
  506. delete p.gxby_url;
  507. delete p.dagl_status;
  508. delete p.dagl_limit;
  509. delete p.dagl_ratio;
  510. delete p.dagl_url;
  511. });
  512. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  513. if (xmj.length > 0) await transaction.insert(this.ctx.service.ledger.tableName, xmj);
  514. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  515. if (pos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, pos);
  516. // 应用到未审完成期
  517. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: revise.tid }, orders: [['order', 'asc']] });
  518. const latestStage = stages[stages.length - 1];
  519. let pcTp;
  520. if (latestStage && latestStage.status !== audit.stage.status.checked) {
  521. for (const s of stages) {
  522. if (s.status === audit.stage.status.checked) continue;
  523. if (!pcTp) {
  524. pcTp = await this._calcStage(s, xmj, transaction);
  525. } else {
  526. await this._calcStageWithoutPc(s, xmj, transaction);
  527. }
  528. }
  529. }
  530. return pcTp;
  531. }
  532. async calcRevise(revise, transaction) {
  533. if (revise.tid !== this.ctx.tender.id) throw '数据错误';
  534. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
  535. this.settleStatus = this.ctx.service.settle.settleStatus;
  536. this.settleBills = revise.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: revise.readySettle.id, settle_status: this.settleStatus.finish } }) : [];
  537. this.settlePos = revise.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: revise.readySettle.id }}) : [];
  538. const pcTp = await this.calcReviseLedger(revise, transaction);
  539. // 引用到所有工程变更
  540. await this.calcAllChanges(revise.tid, transaction);
  541. await this.calcAllChangePlan(revise.tid, transaction);
  542. await this.calcAllChangeApply(revise.tid, transaction);
  543. return pcTp;
  544. }
  545. }
  546. module.exports = revisePriceCalc;