revise_price.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. findPrice(b_code, name, unit, unit_price) {
  22. const helper = this.ctx.helper;
  23. return this.price.find(x => {
  24. return b_code === x.b_code && name === x.name && unit === x.unit && helper.numEqual(unit_price, x.org_price);
  25. });
  26. }
  27. /**
  28. * 新增一期计量,检查单价调整
  29. * @param {Object} newStage - 新计量期
  30. * @param {Object} preStage - 上一计量期
  31. * @return { inseretPosData, insertBillsData }
  32. */
  33. async newStagePriceChange(newStage, preStage, transaction) {
  34. // 获取未执行的单价变更,无单价变更不执行
  35. const price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { tid: newStage.tid, valid: 1, use_stage: 0 } });
  36. if (price.length === 0) return;
  37. // 无截止上期数据不执行
  38. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { sid: preStage.id } });
  39. if (preBillsData.length === 0) return;
  40. // 加载树结构
  41. const bills = await this.ctx.service.ledger.getData(newStage.tid);
  42. this.ctx.helper.assignRelaData(bills, [
  43. { data: preBillsData, fields: ['id', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'unit_price', 'positive_qc_qty', 'negative_qc_qty'], prefix: 'pre_', relaId: 'lid' },
  44. ]);
  45. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  46. billsTree.loadDatas(bills);
  47. // 计算
  48. const result = { ibData: [] };
  49. const helper = this.ctx.helper;
  50. const decimal = this.ctx.tender.info.decimal;
  51. billsTree.calculateAll(node => {
  52. if (!node.pre_id) return;
  53. if (node.children && node.children.length > 0) return;
  54. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  55. if (!priceDiff) return;
  56. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  57. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  58. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  59. node.positive_qc_pc_tp = helper.mul(priceDiff, node.pre_positive_qc_qty, decimal.tp);
  60. node.negative_qc_pc_tp = helper.mul(priceDiff, node.pre_negative_qc_qty, decimal.tp);
  61. result.ibData.push({
  62. tid: newStage.tid, sid: newStage.id, sorder: newStage.order, lid: node.id, org_price: node.pre_unit_price,
  63. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  64. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  65. });
  66. });
  67. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.ibData);
  68. let contract_pc_tp = 0, qc_pc_tp = 0, pc_tp = 0, positive_qc_pc_tp = 0, negative_qc_pc_tp = 0;
  69. for (const ibc of result.ibData) {
  70. contract_pc_tp = helper.add(contract_pc_tp, ibc.contract_pc_tp);
  71. qc_pc_tp = helper.add(qc_pc_tp, ibc.qc_pc_tp);
  72. pc_tp = helper.add(pc_tp, ibc.pc_tp);
  73. positive_qc_pc_tp = helper.add(positive_qc_pc_tp, ibc.positive_qc_pc_tp);
  74. negative_qc_pc_tp = helper.add(negative_qc_pc_tp, ibc.negative_qc_pc_tp);
  75. }
  76. await transaction.update(this.ctx.service.stage.tableName,
  77. { id: newStage.id, contract_pc_tp, qc_pc_tp, pc_tp, positive_qc_pc_tp, negative_qc_pc_tp, check_calc: true, cache_time_l: new Date() });
  78. }
  79. /**
  80. * 期,重新审批,检查单价调整
  81. * @param {Object} stage - 期
  82. * @param {Number} auditOrder - 重新审批,最新审批人序号
  83. * @param {Object} transaction - 事务
  84. */
  85. async stageCheckAgainPriceChange(stage, auditOrder, transaction) {
  86. // 获取未执行的单价变更,无单价变更不执行
  87. const price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { tid: stage.tid, valid: 1, use_stage: 0 } });
  88. if (price.length === 0) return;
  89. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  90. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  91. // 加载树结构
  92. const bills = await this.ctx.service.ledger.getData(stage.tid);
  93. this.ctx.helper.assignRelaData(bills, [
  94. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'postil', 'times', 'order'], prefix: 'cur_', relaId: 'lid' },
  95. { data: preBillsData, fields: ['id', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'unit_price'], prefix: 'pre_', relaId: 'lid' },
  96. ]);
  97. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  98. billsTree.loadDatas(bills);
  99. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  100. // 计算 insertBills billsPriceChange stageChange
  101. const result = { ibData: [], bpcData: [], scData: [] };
  102. const helper = this.ctx.helper;
  103. const decimal = this.ctx.tender.info.decimal;
  104. const said = this.ctx.session.sessionUser.accountId;
  105. billsTree.calculateAll(node => {
  106. if (node.children && node.children.length > 0) return;
  107. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  108. if (!priceDiff) return;
  109. if (node.cur_id) {
  110. node.cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
  111. node.cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  112. result.ibData.push({
  113. tid: stage.tid, sid: stage.id, said,
  114. lid: node.id,
  115. times: stage.times, order: auditOrder,
  116. contract_qty: node.cur_contract_qty, contract_tp: node.cur_contract_tp, qc_qty: node.cur_qc_qty, qc_tp: node.cur_qc_tp,
  117. postil: node.postil,
  118. });
  119. }
  120. if (node.pre_id) {
  121. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  122. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  123. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  124. node.positive_qc_pc_tp = helper.mul(priceDiff, node.pre_positive_qc_qty, decimal.tp);
  125. node.negative_qc_pc_tp = helper.mul(priceDiff, node.pre_negative_qc_qty, decimal.tp);
  126. result.bpcData.push({
  127. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price,
  128. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  129. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  130. });
  131. }
  132. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  133. for (const scd of scDetail) {
  134. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  135. }
  136. });
  137. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  138. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  139. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  140. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  141. let contract_pc_tp = 0, qc_pc_tp = 0, pc_tp = 0, positive_qc_pc_tp = 0, negative_qc_pc_tp = 0;
  142. for (const bpc of result.bpcData) {
  143. contract_pc_tp = helper.add(contract_pc_tp, bpc.contract_pc_tp);
  144. qc_pc_tp = helper.add(qc_pc_tp, bpc.qc_pc_tp);
  145. pc_tp = helper.add(pc_tp, bpc.pc_tp);
  146. positive_qc_pc_tp = helper.add(positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  147. negative_qc_pc_tp = helper.add(negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  148. }
  149. await transaction.update(this.ctx.service.stage.tableName,
  150. { id: stage.id, contract_pc_tp, qc_pc_tp, pc_tp, positive_qc_pc_tp, negative_qc_pc_tp, check_calc: true, cache_time_l: new Date() });
  151. }
  152. /**
  153. * 重算工程变更(调整单价)
  154. * @param {Object} change - 工程变更
  155. * @param {Object} transaction - 事务 (无则非事务提交)
  156. */
  157. async calcChange(change, transaction) {
  158. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: change.cid } });
  159. const updateBills = [];
  160. let total_price = 0, positive_tp = 0, negative_tp = 0;
  161. for (const b of changeBills) {
  162. const p = this.findPrice(b.code, b.name, b.unit, b.unit_price);
  163. if (p) {
  164. updateBills.push({ id: b.id, unit_price: p.new_price });
  165. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(p.new_price, b.spamount, change.tp_decimal));
  166. } else {
  167. total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(b.unit_price, b.spamount, change.tp_decimal));
  168. }
  169. if (b.spamount >= 0) {
  170. positive_tp = this.ctx.helper.add(positive_tp, total_price);
  171. } else {
  172. negative_tp = this.ctx.helper.add(negative_tp, total_price);
  173. }
  174. }
  175. if (updateBills.length > 0) {
  176. const conn = transaction || this.ctx.sub_db;
  177. await conn.updateRows(this.ctx.service.changeAuditList.tableName, updateBills);
  178. await conn.update(this.ctx.service.change.tableName, { total_price, positive_tp, negative_tp }, { where: { cid: change.cid } });
  179. }
  180. }
  181. /**
  182. * 重算标段下所有工程变更(调整单价)
  183. * @param {Number} tid - 标段id
  184. * @param {Object} transaction - 事务 (无则非事务提交)
  185. */
  186. async calcAllChanges(tid, transaction) {
  187. const change = await this.ctx.service.change.getAllDataByCondition({ where: { tid, valid: 1 } });
  188. if (change.length === 0) return;
  189. for (const c of change) {
  190. await this.calcChange(c, transaction);
  191. }
  192. }
  193. async _calcStage(stage, bills, transaction) {
  194. // 无单价变更不执行
  195. if (this.price.length === 0) return;
  196. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  197. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  198. // 加载树结构
  199. this.ctx.helper.assignRelaData(bills, [
  200. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'times', 'order', 'postil'], prefix: 'cur_', relaId: 'lid' },
  201. { data: preBillsData, fields: ['id', 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'unit_price'], prefix: 'pre_', relaId: 'lid' },
  202. ]);
  203. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  204. billsTree.loadDatas(bills);
  205. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  206. // 计算 insertBills/updateBills billsPriceChange StageChange
  207. const result = { ibData: [], ubData: [], bpcData: [], scData: [] };
  208. const helper = this.ctx.helper;
  209. const decimal = this.ctx.tender.info.decimal;
  210. const said = this.ctx.session.sessionUser.accountId;
  211. billsTree.calculateAll(node => {
  212. if (node.children && node.children.length > 0) return;
  213. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  214. if (!priceDiff) return;
  215. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  216. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  217. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  218. node.positive_qc_pc_tp = helper.mul(priceDiff, node.pre_positive_qc_qty, decimal.tp);
  219. node.negative_qc_pc_tp = helper.mul(priceDiff, node.pre_negative_qc_qty, decimal.tp);
  220. if (node.cur_id) {
  221. node.cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
  222. node.cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  223. if (node.cur_times === stage.times && node.cur_order === 0) {
  224. result.ubData.push({
  225. id: node.cur_id,
  226. contract_tp: node.cur_contract_tp, qc_tp: node.cur_qc_tp,
  227. });
  228. } else {
  229. result.ibData.push({
  230. tid: stage.tid, sid: stage.id, said,
  231. lid: node.id, times: stage.times, order: 0,
  232. contract_qty: node.cur_contract_qty, contract_tp: node.cur_contract_tp, qc_qty: node.cur_qc_qty, qc_tp: node.cur_qc_tp,
  233. postil: node.cur_postil,
  234. });
  235. }
  236. }
  237. if (node.pre_id) {
  238. result.bpcData.push({
  239. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price,
  240. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  241. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  242. });
  243. }
  244. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  245. for (const scd of scDetail) {
  246. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  247. }
  248. });
  249. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  250. if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
  251. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  252. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  253. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  254. let contract_pc_tp = 0, qc_pc_tp = 0, pc_tp = 0, positive_qc_pc_tp = 0, negative_qc_pc_tp = 0;
  255. for (const bpc of result.bpcData) {
  256. contract_pc_tp = helper.add(contract_pc_tp, bpc.contract_pc_tp);
  257. qc_pc_tp = helper.add(qc_pc_tp, bpc.qc_pc_tp);
  258. pc_tp = helper.add(pc_tp, bpc.pc_tp);
  259. positive_qc_pc_tp = helper.add(positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  260. negative_qc_pc_tp = helper.add(negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  261. }
  262. await transaction.update(this.ctx.service.stage.tableName,
  263. { id: stage.id, contract_pc_tp, qc_pc_tp, pc_tp, positive_qc_pc_tp, negative_qc_pc_tp, check_calc: true, cache_time_l: new Date() });
  264. }
  265. /**
  266. * 计算修订台账
  267. * @param {Object}revise - 最新一次台账修订(此处不检查)
  268. * @param {Object} transaction - 事务 (无则非事务提交)
  269. */
  270. async calcReviseLedger(revise, transaction) {
  271. const xmj = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.bills_file);
  272. xmj.forEach(x => {
  273. delete x.is_tp;
  274. delete x.gxby_status;
  275. delete x.gxby_limit;
  276. delete x.gxby_ratio;
  277. delete x.gxby_url;
  278. delete x.dagl_status;
  279. delete x.dagl_limit;
  280. delete x.dagl_ratio;
  281. delete x.dagl_url;
  282. });
  283. const pos = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.pos_file);
  284. pos.forEach(p => {
  285. p.in_time = new Date(p.in_time);
  286. delete p.gxby_status;
  287. delete p.gxby_limit;
  288. delete p.gxby_ratio;
  289. delete p.gxby_url;
  290. delete p.dagl_status;
  291. delete p.dagl_limit;
  292. delete p.dagl_ratio;
  293. delete p.dagl_url;
  294. });
  295. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  296. if (xmj.length > 0) await transaction.insert(this.ctx.service.ledger.tableName, xmj);
  297. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  298. if (pos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, pos);
  299. // 应用到未审完成期
  300. const latestStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
  301. if (latestStage && latestStage.status !== audit.stage.status.checked) await this._calcStage(latestStage, xmj, transaction);
  302. }
  303. async calcRevise(revise, transaction) {
  304. if (revise.tid !== this.ctx.tender.id) throw '数据错误';
  305. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
  306. await this.calcReviseLedger(revise, transaction);
  307. // 引用到所有工程变更
  308. await this.calcAllChanges(revise.tid, transaction);
  309. }
  310. }
  311. module.exports = revisePriceCalc;