revise_price.js 15 KB

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