revise_price.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. billsTree.calculateAll(node => {
  82. if (!node.pre_id) return;
  83. if (!node.pre_contract_qty && !node.pre_qc_qty) return;
  84. if (node.children && node.children.length > 0) return;
  85. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  86. if (!priceDiff) return;
  87. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  88. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  89. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  90. node.positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  91. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  92. result.ibData.push({
  93. tid: newStage.tid, sid: newStage.id, sorder: newStage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  94. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  95. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  96. });
  97. });
  98. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.ibData);
  99. for (const ibc of result.ibData) {
  100. pcTp.contract_pc_tp = helper.add(pcTp.contract_pc_tp, ibc.contract_pc_tp);
  101. pcTp.qc_pc_tp = helper.add(pcTp.qc_pc_tp, ibc.qc_pc_tp);
  102. pcTp.pc_tp = helper.add(pcTp.pc_tp, ibc.pc_tp);
  103. pcTp.positive_qc_pc_tp = helper.add(pcTp.positive_qc_pc_tp, ibc.positive_qc_pc_tp);
  104. pcTp.negative_qc_pc_tp = helper.add(pcTp.negative_qc_pc_tp, ibc.negative_qc_pc_tp);
  105. }
  106. await transaction.update(this.ctx.service.stage.tableName,
  107. { id: newStage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
  108. return pcTp;
  109. }
  110. /**
  111. * 期,重新审批,检查单价调整
  112. * @param {Object} stage - 期
  113. * @param {Number} auditOrder - 重新审批,最新审批人序号
  114. * @param {Object} transaction - 事务
  115. */
  116. async stageCheckAgainPriceChange(stage, auditOrder, transaction) {
  117. const pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
  118. // 获取未执行的单价变更,无单价变更不执行
  119. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { tid: stage.tid, valid: 1, use_stage: 0 } });
  120. if (this.price.length === 0) return pcTp;
  121. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  122. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  123. // 加载树结构
  124. const bills = await this.ctx.service.ledger.getData(stage.tid);
  125. this.ctx.helper.assignRelaData(bills, [
  126. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'postil', 'times', 'order'], prefix: 'cur_', relaId: 'lid' },
  127. { 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' },
  128. ]);
  129. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  130. billsTree.loadDatas(bills);
  131. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  132. // 计算 insertBills billsPriceChange stageChange
  133. const result = { ibData: [], bpcData: [], scData: [] };
  134. const helper = this.ctx.helper;
  135. const decimal = this.ctx.tender.info.decimal;
  136. const said = this.ctx.session.sessionUser.accountId;
  137. billsTree.calculateAll(node => {
  138. if (node.children && node.children.length > 0) return;
  139. // const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  140. // if (!priceDiff) return;
  141. if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty)) {
  142. const cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
  143. const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  144. const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  145. const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  146. 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) {
  147. result.ibData.push({
  148. tid: stage.tid, sid: stage.id, said,
  149. lid: node.id,
  150. times: stage.times, order: auditOrder,
  151. contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
  152. qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
  153. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
  154. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
  155. postil: node.postil,
  156. });
  157. }
  158. }
  159. if (node.pre_id && (node.pre_contract_qty || node.pre_qc_qty)) {
  160. const contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  161. const qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  162. const pc_tp = helper.add(contract_pc_tp, qc_pc_tp);
  163. const positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  164. const negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  165. if (contract_pc_tp || qc_pc_tp || pc_tp || positive_qc_pc_tp || negative_qc_pc_tp) {
  166. result.bpcData.push({
  167. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  168. contract_pc_tp, qc_pc_tp, pc_tp, positive_qc_pc_tp, negative_qc_pc_tp,
  169. });
  170. }
  171. }
  172. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  173. for (const scd of scDetail) {
  174. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  175. }
  176. });
  177. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  178. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  179. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  180. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  181. for (const bpc of result.bpcData) {
  182. pcTp.contract_pc_tp = helper.add(pcTp.contract_pc_tp, bpc.contract_pc_tp);
  183. pcTp.qc_pc_tp = helper.add(pcTp.qc_pc_tp, bpc.qc_pc_tp);
  184. pcTp.pc_tp = helper.add(pcTp.pc_tp, bpc.pc_tp);
  185. pcTp.positive_qc_pc_tp = helper.add(pcTp.positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  186. pcTp.negative_qc_pc_tp = helper.add(pcTp.negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  187. }
  188. await transaction.update(this.ctx.service.stage.tableName,
  189. { id: stage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
  190. return pcTp;
  191. }
  192. /**
  193. * 重算工程变更(调整单价)
  194. * @param {Object} change - 工程变更
  195. * @param {Object} transaction - 事务 (无则非事务提交)
  196. */
  197. async calcChange(change, transaction) {
  198. const decimal = this.ctx.tender.info.decimal;
  199. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: change.cid } });
  200. const updateBills = [];
  201. let total_price = 0, positive_tp = 0, negative_tp = 0;
  202. for (const b of changeBills) {
  203. const p = this.findChangeBillsPrice(b.code, b.name, b.unit, b.unit_price, change.cid);
  204. const settleGcl = b.gcl_id ? this.settleBills.find(x => { return x.lid === b.gcl_id; }) : null;
  205. const settlePos = b.mx_id ? this.settlePos.find(x => { return x.pid === b.mx_id; }): null;
  206. let bills_tp;
  207. if (p && !settleGcl && !settlePos) {
  208. bills_tp = this.ctx.helper.mul(p.new_price, b.spamount, change.tp_decimal || decimal.tp);
  209. updateBills.push({ id: b.id, unit_price: p.new_price, checked_price: bills_tp });
  210. if (!p.rela_cid) p.his_rela_cid.push(change.cid);
  211. } else {
  212. bills_tp = this.ctx.helper.mul(b.unit_price, b.spamount, change.tp_decimal || decimal.tp);
  213. }
  214. total_price = this.ctx.helper.add(total_price, bills_tp);
  215. if (b.spamount >= 0) {
  216. positive_tp = this.ctx.helper.add(positive_tp, bills_tp);
  217. } else {
  218. negative_tp = this.ctx.helper.add(negative_tp, bills_tp);
  219. }
  220. }
  221. if (updateBills.length > 0) {
  222. await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateBills);
  223. await transaction.update(this.ctx.service.change.tableName, { total_price, positive_tp, negative_tp }, { where: { cid: change.cid } });
  224. }
  225. }
  226. /**
  227. * 重算标段下所有工程变更(调整单价)
  228. * @param {Number} tid - 标段id
  229. * @param {Object} transaction - 事务 (无则非事务提交)
  230. */
  231. async calcAllChanges(tid, transaction) {
  232. const change = await this.ctx.service.change.getAllDataByCondition({ where: { tid, valid: 1 } });
  233. if (change.length === 0) return;
  234. for (const c of change) {
  235. await this.calcChange(c, transaction);
  236. }
  237. const revisePriceUpdate = [];
  238. for (const p of this.common_price_c) {
  239. if (p.his_rela_cid.length > 0) revisePriceUpdate.push({id: p.id, his_rela_cid: p.his_rela_cid.join(',')});
  240. }
  241. if (revisePriceUpdate.length > 0) await transaction.updateRows(this.ctx.service.revisePrice.tableName, revisePriceUpdate);
  242. }
  243. /**
  244. * 重算变更方案(调整单价)
  245. * @param {Object} change - 工程变更
  246. * @param {Object} transaction - 事务 (无则非事务提交)
  247. */
  248. async calcChangePlan(changePlan, transaction) {
  249. const decimal = changePlan.decimal ? JSON.parse(changePlan.decimal) : this.ctx.tender.info.decimal;
  250. const changeBills = await this.ctx.service.changePlanList.getAllDataByCondition({ where: { cpid: changePlan.id } });
  251. const updateBills = [];
  252. let total_price = 0;
  253. for (const b of changeBills) {
  254. const p = this.findCommonChangeBillsPrice(b.code, b.name, b.unit, b.unit_price);
  255. let bills_tp;
  256. if (p) {
  257. bills_tp = this.ctx.helper.mul(p.new_price, b.spamount, decimal.tp);
  258. updateBills.push({ id: b.id, unit_price: p.new_price });
  259. } else {
  260. bills_tp = this.ctx.helper.mul(b.unit_price, b.spamount, decimal.tp);
  261. }
  262. total_price = this.ctx.helper.add(total_price, bills_tp);
  263. }
  264. if (updateBills.length > 0) {
  265. await transaction.updateRows(this.ctx.service.changePlanList.tableName, updateBills);
  266. await transaction.update(this.ctx.service.changePlan.tableName, { id: changePlan.id, total_price});
  267. }
  268. }
  269. async calcAllChangePlan(tid, transaction) {
  270. const changePlan = await this.ctx.service.changePlan.getAllDataByCondition({ where: { tid } });
  271. if (changePlan.length === 0) return;
  272. for (const c of changePlan) {
  273. await this.calcChangePlan(c, transaction);
  274. }
  275. }
  276. /**
  277. * 重算变更申请(调整单价)
  278. * @param {Object} change - 工程变更
  279. * @param {Object} transaction - 事务 (无则非事务提交)
  280. */
  281. async calcChangeApply(changeApply, transaction) {
  282. const decimal = changeApply.decimal ? JSON.parse(changeApply.decimal) : this.ctx.tender.info.decimal;
  283. const changeBills = await this.ctx.service.changeApplyList.getAllDataByCondition({ where: { caid: changeApply.id } });
  284. const updateBills = [];
  285. let total_price = 0;
  286. for (const b of changeBills) {
  287. const p = this.findCommonChangeBillsPrice(b.code, b.name, b.unit, b.unit_price);
  288. let bills_tp;
  289. if (p) {
  290. bills_tp = this.ctx.helper.mul(p.new_price, b.camount, decimal.tp);
  291. updateBills.push({ id: b.id, unit_price: p.new_price });
  292. } else {
  293. bills_tp = this.ctx.helper.mul(b.unit_price, b.camount, decimal.tp);
  294. }
  295. total_price = this.ctx.helper.add(total_price, bills_tp);
  296. }
  297. if (updateBills.length > 0) {
  298. await transaction.updateRows(this.ctx.service.changeApplyList.tableName, updateBills);
  299. await transaction.update(this.ctx.service.changeApply.tableName, { id: changeApply.id, total_price});
  300. }
  301. }
  302. async calcAllChangeApply(tid, transaction) {
  303. const changeApply = await this.ctx.service.changeApply.getAllDataByCondition({ where: { tid } });
  304. if (changeApply.length === 0) return;
  305. for (const c of changeApply) {
  306. await this.calcChangeApply(c, transaction);
  307. }
  308. }
  309. async _calcStage(stage, bills, transaction) {
  310. const pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
  311. // 无单价变更不执行
  312. if (this.price.length === 0) return pcTp;
  313. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  314. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  315. // 加载树结构
  316. this.ctx.helper.assignRelaData(bills, [
  317. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'times', 'order', 'postil'], prefix: 'cur_', relaId: 'lid' },
  318. { 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' },
  319. ]);
  320. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  321. billsTree.loadDatas(bills);
  322. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  323. // 计算 insertBills/updateBills billsPriceChange StageChange
  324. const result = { ibData: [], ubData: [], bpcData: [], scData: [] };
  325. const helper = this.ctx.helper;
  326. const decimal = this.ctx.tender.info.decimal;
  327. const said = this.ctx.session.sessionUser.accountId;
  328. billsTree.calculateAll(node => {
  329. if (node.children && node.children.length > 0) return;
  330. // const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  331. // if (!priceDiff) return;
  332. if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty)) {
  333. const cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
  334. const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  335. const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  336. const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  337. 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) {
  338. if (node.cur_times === stage.times && node.cur_order === 0) {
  339. result.ubData.push({
  340. id: node.cur_id,
  341. contract_tp: cur_contract_tp, qc_tp: cur_qc_tp,
  342. positive_qc_tp: cur_positive_qc_tp, negative_qc_tp: cur_negative_qc_tp,
  343. });
  344. } else {
  345. result.ibData.push({
  346. tid: stage.tid, sid: stage.id, said,
  347. lid: node.id, times: stage.times, order: 0,
  348. contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
  349. qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
  350. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
  351. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
  352. postil: node.cur_postil,
  353. });
  354. }
  355. }
  356. }
  357. if (node.pre_id && (node.pre_contract_qty || node.pre_qc_qty)) {
  358. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  359. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  360. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  361. node.positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  362. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  363. result.bpcData.push({
  364. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  365. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  366. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  367. });
  368. }
  369. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  370. for (const scd of scDetail) {
  371. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  372. }
  373. });
  374. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  375. if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
  376. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  377. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  378. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  379. for (const bpc of result.bpcData) {
  380. pcTp.contract_pc_tp = helper.add(pcTp.contract_pc_tp, bpc.contract_pc_tp);
  381. pcTp.qc_pc_tp = helper.add(pcTp.qc_pc_tp, bpc.qc_pc_tp);
  382. pcTp.pc_tp = helper.add(pcTp.pc_tp, bpc.pc_tp);
  383. pcTp.positive_qc_pc_tp = helper.add(pcTp.positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  384. pcTp.negative_qc_pc_tp = helper.add(pcTp.negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  385. }
  386. await transaction.update(this.ctx.service.stage.tableName,
  387. { id: stage.id, ...pcTp, check_calc: true, cache_time_l: new Date() });
  388. return pcTp;
  389. }
  390. async _calcStageWithoutPc(stage, bills, transaction) {
  391. // 无单价变更不执行
  392. if (this.price.length === 0) return;
  393. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  394. // 加载树结构
  395. this.ctx.helper.assignRelaData(bills, [
  396. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'times', 'order', 'postil'], prefix: 'cur_', relaId: 'lid' },
  397. ]);
  398. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  399. billsTree.loadDatas(bills);
  400. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  401. // 计算 insertBills/updateBills StageChange
  402. const result = { ibData: [], ubData: [], scData: [] };
  403. const helper = this.ctx.helper;
  404. const decimal = this.ctx.tender.info.decimal;
  405. const said = this.ctx.session.sessionUser.accountId;
  406. billsTree.calculateAll(node => {
  407. if (node.children && node.children.length > 0) return;
  408. // const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  409. // if (!priceDiff) return;
  410. if (node.cur_id && (node.cur_contract_qty || node.cur_qc_qty)) {
  411. const cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
  412. const cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  413. const cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  414. const cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  415. 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) {
  416. if (node.cur_times === stage.times && node.cur_order === 0) {
  417. result.ubData.push({
  418. id: node.cur_id,
  419. contract_tp: cur_contract_tp, qc_tp: cur_qc_tp,
  420. positive_qc_tp: cur_positive_qc_tp, negative_qc_tp: cur_negative_qc_tp,
  421. });
  422. } else {
  423. result.ibData.push({
  424. tid: stage.tid, sid: stage.id, said,
  425. lid: node.id, times: stage.times, order: 0,
  426. contract_qty: node.cur_contract_qty, contract_tp: cur_contract_tp,
  427. qc_qty: node.cur_qc_qty, qc_tp: cur_qc_tp,
  428. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: cur_positive_qc_tp,
  429. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: cur_negative_qc_tp,
  430. postil: node.cur_postil,
  431. });
  432. }
  433. }
  434. }
  435. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  436. for (const scd of scDetail) {
  437. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  438. }
  439. });
  440. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  441. if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
  442. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  443. if (result.ibData.length > 0 || result.ubData.length > 0) await transaction.update(this.ctx.service.stage.tableName, { id: stage.id, check_calc: true});
  444. }
  445. /**
  446. * 计算修订台账
  447. * @param {Object}revise - 最新一次台账修订(此处不检查)
  448. * @param {Object} transaction - 事务 (无则非事务提交)
  449. */
  450. async calcReviseLedger(revise, transaction) {
  451. const xmj = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.bills_file);
  452. xmj.forEach(x => {
  453. delete x.is_tp;
  454. delete x.gxby_status;
  455. delete x.gxby_limit;
  456. delete x.gxby_ratio;
  457. delete x.gxby_url;
  458. delete x.dagl_status;
  459. delete x.dagl_limit;
  460. delete x.dagl_ratio;
  461. delete x.dagl_url;
  462. });
  463. const pos = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.pos_file);
  464. pos.forEach(p => {
  465. p.in_time = new Date(p.in_time);
  466. delete p.gxby_status;
  467. delete p.gxby_limit;
  468. delete p.gxby_ratio;
  469. delete p.gxby_url;
  470. delete p.dagl_status;
  471. delete p.dagl_limit;
  472. delete p.dagl_ratio;
  473. delete p.dagl_url;
  474. });
  475. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  476. if (xmj.length > 0) await transaction.insert(this.ctx.service.ledger.tableName, xmj);
  477. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  478. if (pos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, pos);
  479. // 应用到未审完成期
  480. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: revise.tid }, orders: [['order', 'asc']] });
  481. const latestStage = stages[stages.length - 1];
  482. let pcTp;
  483. if (latestStage && latestStage.status !== audit.stage.status.checked) {
  484. for (const s of stages) {
  485. if (s.status === audit.stage.status.checked) continue;
  486. if (!pcTp) {
  487. pcTp = await this._calcStage(s, xmj, transaction);
  488. } else {
  489. await this._calcStageWithoutPc(s, xmj, transaction);
  490. }
  491. }
  492. }
  493. return pcTp;
  494. }
  495. async calcRevise(revise, transaction) {
  496. if (revise.tid !== this.ctx.tender.id) throw '数据错误';
  497. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
  498. this.settleStatus = this.ctx.service.settle.settleStatus;
  499. this.settleBills = revise.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: revise.readySettle.id, settle_status: this.settleStatus.finish } }) : [];
  500. this.settlePos = revise.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: revise.readySettle.id }}) : [];
  501. const pcTp = await this.calcReviseLedger(revise, transaction);
  502. // 引用到所有工程变更
  503. await this.calcAllChanges(revise.tid, transaction);
  504. await this.calcAllChangePlan(revise.tid, transaction);
  505. await this.calcAllChangeApply(revise.tid, transaction);
  506. return pcTp;
  507. }
  508. }
  509. module.exports = revisePriceCalc;