change_audit_list.js 11 KB

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