revise_price.js 27 KB

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