revise_price.js 20 KB

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