change_audit_list.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. gcl_id: '0',
  47. };
  48. // 新增工料
  49. const result = await this.db.insert(this.tableName, insertData);
  50. if (result.affectedRows === 0) {
  51. throw '新增空白清单数据失败';
  52. }
  53. return await this.getDataById(result.insertId);
  54. }
  55. /**
  56. * 删除变更清单
  57. * @param {int} id 清单id
  58. * @return {void}
  59. */
  60. async del(id) {
  61. if (!this.ctx.tender || !this.ctx.change) {
  62. throw '数据错误';
  63. }
  64. const transaction = await this.db.beginTransaction();
  65. try {
  66. // 判断是否可删
  67. await transaction.delete(this.tableName, { id });
  68. // 重新算变更令总额
  69. // await this.calcQuantityByML(transaction, mb_id);
  70. await transaction.commit();
  71. return true;
  72. } catch (err) {
  73. await transaction.rollback();
  74. throw err;
  75. }
  76. }
  77. /**
  78. * 修改变更清单
  79. * @param {Object} data 工料内容
  80. * @param {int} order 期数
  81. * @return {void}
  82. */
  83. async save(data, order) {
  84. if (!this.ctx.tender || !this.ctx.change) {
  85. throw '数据错误';
  86. }
  87. const transaction = await this.db.beginTransaction();
  88. try {
  89. // const mb_id = data.mb_id;
  90. // delete data.mb_id;
  91. await transaction.update(this.tableName, data);
  92. // await this.calcQuantityByML(transaction, mb_id);
  93. await this.calcCamountSum(transaction);
  94. await transaction.commit();
  95. return true;
  96. } catch (err) {
  97. await transaction.rollback();
  98. throw err;
  99. }
  100. }
  101. /**
  102. * 修改变更清单 复制粘贴
  103. * @param {Object} datas 修改内容
  104. * @return {void}
  105. */
  106. async saveDatas(datas) {
  107. if (!this.ctx.tender || !this.ctx.change) {
  108. throw '数据错误';
  109. }
  110. // 判断是否可修改
  111. // 判断t_type是否为费用
  112. const transaction = await this.db.beginTransaction();
  113. try {
  114. // for (const data of datas) {
  115. // const mb_id = data.mb_id;
  116. // delete data.mb_id;
  117. // await transaction.update(this.tableName, data);
  118. // await this.calcQuantityByML(transaction, mb_id);
  119. // }
  120. await transaction.updateRows(this.tableName, datas);
  121. await this.calcCamountSum(transaction);
  122. await transaction.commit();
  123. return true;
  124. } catch (err) {
  125. await transaction.rollback();
  126. throw err;
  127. }
  128. }
  129. /**
  130. * 台账数据清单 重新选择
  131. * @param {Object} datas 内容
  132. * @return {void}
  133. */
  134. async saveLedgerListDatas(datas) {
  135. if (!this.ctx.tender || !this.ctx.change) {
  136. throw '数据错误';
  137. }
  138. // 判断是否可修改
  139. // 判断t_type是否为费用
  140. const transaction = await this.db.beginTransaction();
  141. try {
  142. // 先删除原本的台账清单数据
  143. const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"';
  144. const sqlParam = [this.tableName, this.ctx.change.cid];
  145. await transaction.query(sql, sqlParam);
  146. const insertDatas = [];
  147. for (const data of datas) {
  148. data.tid = this.ctx.tender.id;
  149. data.cid = this.ctx.change.cid;
  150. data.spamount = data.camount;
  151. data.samount = '';
  152. insertDatas.push(data);
  153. }
  154. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  155. await this.calcCamountSum(transaction);
  156. await transaction.commit();
  157. return true;
  158. } catch (err) {
  159. await transaction.rollback();
  160. throw err;
  161. }
  162. }
  163. async calcCamountSum(transaction) {
  164. // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
  165. // const sqlParam = [this.tableName, this.change.cid];
  166. // const tp = await transaction.queryOne(sql, sqlParam);
  167. // 防止小数位不精确,采用取值计算
  168. const sql = 'SELECT unit_price, spamount FROM ?? WHERE cid = ?';
  169. const sqlParam = [this.tableName, this.ctx.change.cid];
  170. const changeList = await transaction.query(sql, sqlParam);
  171. let total_price = 0;
  172. for (const cl of changeList) {
  173. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, this.ctx.tender.info.decimal.tp));
  174. }
  175. const updateData = {
  176. total_price,
  177. };
  178. const options = {
  179. where: {
  180. cid: this.ctx.change.cid,
  181. },
  182. };
  183. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  184. }
  185. async gatherBgBills(tid) {
  186. const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' +
  187. ' FROM ' + this.tableName + ' cb' +
  188. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  189. ' WHERE cb.tid = ? and c.status = ?' +
  190. ' GROUP BY code, name, unit, unit_price';
  191. const param = [tid, audit.flow.status.checked];
  192. const result = await this.db.query(sql, param);
  193. for (const b of result) {
  194. b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp);
  195. }
  196. return result;
  197. }
  198. /**
  199. * 报表用
  200. * Tony Kang
  201. * @param {tid} tid - 标段id
  202. * @return {void}
  203. */
  204. async getChangeAuditBills(tid) {
  205. const sql = 'SELECT cb.*' +
  206. ' FROM ' + this.tableName + ' cb' +
  207. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  208. ' WHERE c.tid = ? and c.status = 3' +
  209. ' ORDER BY cb.cid, cb.code';
  210. const param = [tid];
  211. const result = await this.db.query(sql, param);
  212. return result;
  213. }
  214. }
  215. return ChangeAuditList;
  216. };