revise_price.js 21 KB

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