revise_price.js 19 KB

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