revise_price.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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', 'positive_qc_tp', 'negative_qc_tp'], 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.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  60. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  61. result.ibData.push({
  62. tid: newStage.tid, sid: newStage.id, sorder: newStage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.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', 'positive_qc_qty', 'negative_qc_qty', 'postil', 'times', 'order'], prefix: 'cur_', relaId: 'lid' },
  95. { 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' },
  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. node.cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  113. node.cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  114. result.ibData.push({
  115. tid: stage.tid, sid: stage.id, said,
  116. lid: node.id,
  117. times: stage.times, order: auditOrder,
  118. contract_qty: node.cur_contract_qty, contract_tp: node.cur_contract_tp,
  119. qc_qty: node.cur_qc_qty, qc_tp: node.cur_qc_tp,
  120. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: node.cur_positive_qc_tp,
  121. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: node.cur_negative_qc_tp,
  122. postil: node.postil,
  123. });
  124. }
  125. if (node.pre_id) {
  126. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  127. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  128. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  129. node.positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  130. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  131. result.bpcData.push({
  132. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  133. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  134. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  135. });
  136. }
  137. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  138. for (const scd of scDetail) {
  139. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  140. }
  141. });
  142. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  143. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  144. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  145. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  146. let contract_pc_tp = 0, qc_pc_tp = 0, pc_tp = 0, positive_qc_pc_tp = 0, negative_qc_pc_tp = 0;
  147. for (const bpc of result.bpcData) {
  148. contract_pc_tp = helper.add(contract_pc_tp, bpc.contract_pc_tp);
  149. qc_pc_tp = helper.add(qc_pc_tp, bpc.qc_pc_tp);
  150. pc_tp = helper.add(pc_tp, bpc.pc_tp);
  151. positive_qc_pc_tp = helper.add(positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  152. negative_qc_pc_tp = helper.add(negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  153. }
  154. await transaction.update(this.ctx.service.stage.tableName,
  155. { 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() });
  156. }
  157. /**
  158. * 重算工程变更(调整单价)
  159. * @param {Object} change - 工程变更
  160. * @param {Object} transaction - 事务 (无则非事务提交)
  161. */
  162. async calcChange(change, transaction) {
  163. const decimal = this.ctx.tender.info.decimal;
  164. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: change.cid } });
  165. const updateBills = [];
  166. let total_price = 0, positive_tp = 0, negative_tp = 0;
  167. for (const b of changeBills) {
  168. const p = this.findPrice(b.code, b.name, b.unit, b.unit_price);
  169. let bills_tp;
  170. if (p) {
  171. updateBills.push({ id: b.id, unit_price: p.new_price });
  172. bills_tp = this.ctx.helper.mul(p.new_price, b.spamount, change.tp_decimal || decimal.tp);
  173. } else {
  174. bills_tp = this.ctx.helper.mul(b.unit_price, b.spamount, change.tp_decimal || decimal.tp);
  175. }
  176. total_price = this.ctx.helper.add(total_price, bills_tp);
  177. if (b.spamount >= 0) {
  178. positive_tp = this.ctx.helper.add(positive_tp, bills_tp);
  179. } else {
  180. negative_tp = this.ctx.helper.add(negative_tp, bills_tp);
  181. }
  182. }
  183. if (updateBills.length > 0) {
  184. await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateBills);
  185. await transaction.update(this.ctx.service.change.tableName, { total_price, positive_tp, negative_tp }, { where: { cid: change.cid } });
  186. }
  187. }
  188. /**
  189. * 重算标段下所有工程变更(调整单价)
  190. * @param {Number} tid - 标段id
  191. * @param {Object} transaction - 事务 (无则非事务提交)
  192. */
  193. async calcAllChanges(tid, transaction) {
  194. const change = await this.ctx.service.change.getAllDataByCondition({ where: { tid, valid: 1 } });
  195. if (change.length === 0) return;
  196. for (const c of change) {
  197. await this.calcChange(c, transaction);
  198. }
  199. }
  200. async _calcStage(stage, bills, transaction) {
  201. // 无单价变更不执行
  202. if (this.price.length === 0) return;
  203. const curBillsData = await this.ctx.service.stageBills.getLastestStageData2(stage.tid, stage.id);
  204. const preBillsData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { tid: stage.tid, sorder: stage.order - 1 } });
  205. // 加载树结构
  206. this.ctx.helper.assignRelaData(bills, [
  207. { data: curBillsData, fields: ['id', 'contract_qty', 'qc_qty', 'positive_qc_qty', 'negative_qc_qty', 'times', 'order', 'postil'], prefix: 'cur_', relaId: 'lid' },
  208. { 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' },
  209. ]);
  210. const billsTree = new Ledger.billsTree(this.ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [] });
  211. billsTree.loadDatas(bills);
  212. const stageChange = await this.ctx.service.stageChange.getFinalStageData(stage.tid, stage.id);
  213. // 计算 insertBills/updateBills billsPriceChange StageChange
  214. const result = { ibData: [], ubData: [], bpcData: [], scData: [] };
  215. const helper = this.ctx.helper;
  216. const decimal = this.ctx.tender.info.decimal;
  217. const said = this.ctx.session.sessionUser.accountId;
  218. billsTree.calculateAll(node => {
  219. if (node.children && node.children.length > 0) return;
  220. const priceDiff = helper.sub(node.unit_price, node.pre_unit_price);
  221. if (!priceDiff) return;
  222. node.contract_pc_tp = helper.sub(helper.mul(node.pre_contract_qty, node.unit_price, decimal.tp), node.pre_contract_tp);
  223. node.qc_pc_tp = helper.sub(helper.mul(node.pre_qc_qty, node.unit_price, decimal.tp), node.pre_qc_tp);
  224. node.pc_tp = helper.add(node.contract_pc_tp, node.qc_pc_tp);
  225. node.positive_qc_pc_tp = helper.sub(helper.mul(node.pre_positive_qc_qty, node.unit_price, decimal.tp), node.pre_positive_qc_tp);
  226. node.negative_qc_pc_tp = helper.sub(helper.mul(node.pre_negative_qc_qty, node.unit_price, decimal.tp), node.pre_negative_qc_tp);
  227. if (node.cur_id) {
  228. node.cur_contract_tp = helper.mul(node.cur_contract_qty, node.unit_price, decimal.tp);
  229. node.cur_qc_tp = helper.mul(node.cur_qc_qty, node.unit_price, decimal.tp);
  230. node.cur_positive_qc_tp = helper.mul(node.cur_positive_qc_qty, node.unit_price, decimal.tp);
  231. node.cur_negative_qc_tp = helper.mul(node.cur_negative_qc_qty, node.unit_price, decimal.tp);
  232. if (node.cur_times === stage.times && node.cur_order === 0) {
  233. result.ubData.push({
  234. id: node.cur_id,
  235. contract_tp: node.cur_contract_tp, qc_tp: node.cur_qc_tp,
  236. positive_qc_tp: node.cur_positive_qc_tp, negative_qc_tp: node.cur_negative_qc_tp,
  237. });
  238. } else {
  239. result.ibData.push({
  240. tid: stage.tid, sid: stage.id, said,
  241. lid: node.id, times: stage.times, order: 0,
  242. contract_qty: node.cur_contract_qty, contract_tp: node.cur_contract_tp,
  243. qc_qty: node.cur_qc_qty, qc_tp: node.cur_qc_tp,
  244. positive_qc_qty: node.cur_positive_qc_qty, positive_qc_tp: node.cur_positive_qc_tp,
  245. negative_qc_qty: node.cur_negative_qc_qty, negative_qc_tp: node.cur_negative_qc_tp,
  246. postil: node.cur_postil,
  247. });
  248. }
  249. }
  250. if (node.pre_id) {
  251. result.bpcData.push({
  252. tid: stage.tid, sid: stage.id, sorder: stage.order, lid: node.id, org_price: node.pre_unit_price, unit_price: node.unit_price,
  253. contract_pc_tp: node.contract_pc_tp, qc_pc_tp: node.qc_pc_tp, pc_tp: node.pc_tp,
  254. positive_qc_pc_tp: node.positive_qc_pc_tp, negative_qc_pc_tp: node.negative_qc_pc_tp,
  255. });
  256. }
  257. const scDetail = stageChange.filter(x => { return x.lid === node.id; });
  258. for (const scd of scDetail) {
  259. result.scData.push({ id: scd.id, unit_price: node.unit_price });
  260. }
  261. });
  262. if (result.ibData.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, result.ibData);
  263. if (result.ubData.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, result.ubData);
  264. await transaction.delete(this.ctx.service.stageBillsPc.tableName, { sid: stage.id });
  265. if (result.bpcData.length > 0) await transaction.insert(this.ctx.service.stageBillsPc.tableName, result.bpcData);
  266. if (result.scData.length > 0) await transaction.updateRows(this.ctx.service.stageChange.tableName, result.scData);
  267. let contract_pc_tp = 0, qc_pc_tp = 0, pc_tp = 0, positive_qc_pc_tp = 0, negative_qc_pc_tp = 0;
  268. for (const bpc of result.bpcData) {
  269. contract_pc_tp = helper.add(contract_pc_tp, bpc.contract_pc_tp);
  270. qc_pc_tp = helper.add(qc_pc_tp, bpc.qc_pc_tp);
  271. pc_tp = helper.add(pc_tp, bpc.pc_tp);
  272. positive_qc_pc_tp = helper.add(positive_qc_pc_tp, bpc.positive_qc_pc_tp);
  273. negative_qc_pc_tp = helper.add(negative_qc_pc_tp, bpc.negative_qc_pc_tp);
  274. }
  275. await transaction.update(this.ctx.service.stage.tableName,
  276. { 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() });
  277. }
  278. /**
  279. * 计算修订台账
  280. * @param {Object}revise - 最新一次台账修订(此处不检查)
  281. * @param {Object} transaction - 事务 (无则非事务提交)
  282. */
  283. async calcReviseLedger(revise, transaction) {
  284. const xmj = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.bills_file);
  285. xmj.forEach(x => {
  286. delete x.is_tp;
  287. delete x.gxby_status;
  288. delete x.gxby_limit;
  289. delete x.gxby_ratio;
  290. delete x.gxby_url;
  291. delete x.dagl_status;
  292. delete x.dagl_limit;
  293. delete x.dagl_ratio;
  294. delete x.dagl_url;
  295. });
  296. const pos = await this.ctx.helper.loadLedgerDataFromOss(revise.curHis.pos_file);
  297. pos.forEach(p => {
  298. p.in_time = new Date(p.in_time);
  299. delete p.gxby_status;
  300. delete p.gxby_limit;
  301. delete p.gxby_ratio;
  302. delete p.gxby_url;
  303. delete p.dagl_status;
  304. delete p.dagl_limit;
  305. delete p.dagl_ratio;
  306. delete p.dagl_url;
  307. });
  308. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  309. if (xmj.length > 0) await transaction.insert(this.ctx.service.ledger.tableName, xmj);
  310. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  311. if (pos.length > 0) await transaction.insert(this.ctx.service.pos.tableName, pos);
  312. // 应用到未审完成期
  313. const latestStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
  314. if (latestStage && latestStage.status !== audit.stage.status.checked) await this._calcStage(latestStage, xmj, transaction);
  315. }
  316. async calcRevise(revise, transaction) {
  317. if (revise.tid !== this.ctx.tender.id) throw '数据错误';
  318. this.price = await this.ctx.service.revisePrice.getAllDataByCondition({ where: { rid: revise.id } });
  319. await this.calcReviseLedger(revise, transaction);
  320. // 引用到所有工程变更
  321. await this.calcAllChanges(revise.tid, transaction);
  322. }
  323. }
  324. module.exports = revisePriceCalc;