change_audit_list.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class ChangeAuditList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'change_audit_list';
  21. }
  22. /**
  23. * 添加空白变更清单
  24. * @return {void}
  25. */
  26. async add(data) {
  27. if (!this.ctx.tender || !this.ctx.change) {
  28. throw '数据错误';
  29. }
  30. const insertData = {
  31. tid: this.ctx.tender.id,
  32. cid: this.ctx.change.cid,
  33. lid: '0',
  34. code: '',
  35. name: '',
  36. bwmx: '',
  37. unit: '',
  38. unit_price: null,
  39. oamount: 0,
  40. camount: 0,
  41. samount: '',
  42. detail: '',
  43. spamount: 0,
  44. xmj_code: null,
  45. xmj_jldy: null,
  46. };
  47. // 新增工料
  48. const result = await this.db.insert(this.tableName, insertData);
  49. if (result.affectedRows === 0) {
  50. throw '新增空白清单数据失败';
  51. }
  52. return await this.getDataById(result.insertId);
  53. }
  54. /**
  55. * 删除变更清单
  56. * @param {int} id 清单id
  57. * @return {void}
  58. */
  59. async del(id) {
  60. if (!this.ctx.tender || !this.ctx.change) {
  61. throw '数据错误';
  62. }
  63. const transaction = await this.db.beginTransaction();
  64. try {
  65. // 判断是否可删
  66. await transaction.delete(this.tableName, { id });
  67. // 重新算变更令总额
  68. // await this.calcQuantityByML(transaction, mb_id);
  69. await transaction.commit();
  70. return true;
  71. } catch (err) {
  72. await transaction.rollback();
  73. throw err;
  74. }
  75. }
  76. /**
  77. * 修改变更清单
  78. * @param {Object} data 工料内容
  79. * @param {int} order 期数
  80. * @return {void}
  81. */
  82. async save(data, order) {
  83. if (!this.ctx.tender || !this.ctx.change) {
  84. throw '数据错误';
  85. }
  86. const transaction = await this.db.beginTransaction();
  87. try {
  88. // const mb_id = data.mb_id;
  89. // delete data.mb_id;
  90. await transaction.update(this.tableName, data);
  91. // await this.calcQuantityByML(transaction, mb_id);
  92. await this.calcCamountSum(transaction);
  93. await transaction.commit();
  94. return true;
  95. } catch (err) {
  96. await transaction.rollback();
  97. throw err;
  98. }
  99. }
  100. /**
  101. * 修改变更清单 复制粘贴
  102. * @param {Object} datas 修改内容
  103. * @return {void}
  104. */
  105. async saveDatas(datas) {
  106. if (!this.ctx.tender || !this.ctx.change) {
  107. throw '数据错误';
  108. }
  109. // 判断是否可修改
  110. // 判断t_type是否为费用
  111. const transaction = await this.db.beginTransaction();
  112. try {
  113. // for (const data of datas) {
  114. // const mb_id = data.mb_id;
  115. // delete data.mb_id;
  116. // await transaction.update(this.tableName, data);
  117. // await this.calcQuantityByML(transaction, mb_id);
  118. // }
  119. await transaction.updateRows(this.tableName, datas);
  120. await this.calcCamountSum(transaction);
  121. await transaction.commit();
  122. return true;
  123. } catch (err) {
  124. await transaction.rollback();
  125. throw err;
  126. }
  127. }
  128. async calcCamountSum(transaction) {
  129. // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
  130. // const sqlParam = [this.tableName, this.change.cid];
  131. // const tp = await transaction.queryOne(sql, sqlParam);
  132. // 防止小数位不精确,采用取值计算
  133. const sql = 'SELECT unit_price, spamount FROM ?? WHERE cid = ?';
  134. const sqlParam = [this.tableName, this.ctx.change.cid];
  135. const changeList = await transaction.query(sql, sqlParam);
  136. let total_price = 0;
  137. for (const cl of changeList) {
  138. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, this.ctx.tender.info.decimal.tp));
  139. }
  140. const updateData = {
  141. total_price,
  142. };
  143. const options = {
  144. where: {
  145. cid: this.ctx.change.cid,
  146. },
  147. };
  148. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  149. }
  150. async gatherBgBills(tid) {
  151. const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' +
  152. ' FROM ' + this.tableName + ' cb' +
  153. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  154. ' WHERE cb.tid = ? and c.status = ?' +
  155. ' GROUP BY code, name, unit, unit_price';
  156. const param = [tid, audit.flow.status.checked];
  157. const result = await this.db.query(sql, param);
  158. for (const b of result) {
  159. b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp);
  160. }
  161. return result;
  162. }
  163. /**
  164. * 报表用
  165. * Tony Kang
  166. * @param {tid} tid - 标段id
  167. * @return {void}
  168. */
  169. async getChangeAuditBills(tid) {
  170. const sql = 'SELECT cb.*' +
  171. ' FROM ' + this.tableName + ' cb' +
  172. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  173. ' WHERE c.tid = ? and c.status = 3' +
  174. ' ORDER BY cb.cid, cb.code';
  175. const param = [tid];
  176. const result = await this.db.query(sql, param);
  177. return result;
  178. }
  179. }
  180. return ChangeAuditList;
  181. };