change_audit_list.js 9.0 KB

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