material_list.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 'use strict';
  2. /**
  3. * 调差清单关联工料表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. module.exports = app => {
  11. class MaterialList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_list';
  21. }
  22. /**
  23. * 添加工料清单关联
  24. * @return {void}
  25. */
  26. async add(data) {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const list = [];
  31. for (const mb of data.mb_id) {
  32. const newLists = {
  33. tid: this.ctx.tender.id,
  34. order: this.ctx.material.order,
  35. mid: this.ctx.material.id,
  36. mb_id: mb,
  37. gcl_id: data.gcl_id,
  38. xmj_id: data.xmj_id,
  39. mx_id: data.mx_id,
  40. gather_qty: data.gather_qty,
  41. in_time: new Date(),
  42. };
  43. list.push(newLists);
  44. }
  45. // 新增工料
  46. const result = await this.db.insert(this.tableName, list);
  47. if (result.affectedRows === 0) {
  48. throw '新增工料数据失败';
  49. }
  50. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  51. }
  52. /**
  53. * 删除工料清单关联
  54. * @param {int} id 工料id
  55. * @return {void}
  56. */
  57. async del(id, mb_id) {
  58. if (!this.ctx.tender || !this.ctx.material) {
  59. throw '数据错误';
  60. }
  61. const transaction = await this.db.beginTransaction();
  62. try {
  63. // 判断是否可删
  64. await transaction.delete(this.tableName, { id });
  65. await this.calcQuantityByML(transaction, mb_id);
  66. await transaction.commit();
  67. return true;
  68. } catch (err) {
  69. await transaction.rollback();
  70. throw err;
  71. }
  72. }
  73. /**
  74. * 修改工料清单关联信息
  75. * @param {Object} data 工料内容
  76. * @param {int} order 期数
  77. * @return {void}
  78. */
  79. async save(data, order) {
  80. if (!this.ctx.tender || !this.ctx.material) {
  81. throw '数据错误';
  82. }
  83. const transaction = await this.db.beginTransaction();
  84. try {
  85. const mb_id = data.mb_id;
  86. delete data.mb_id;
  87. await transaction.update(this.tableName, data);
  88. await this.calcQuantityByML(transaction, mb_id);
  89. await transaction.commit();
  90. return true;
  91. } catch (err) {
  92. await transaction.rollback();
  93. throw err;
  94. }
  95. }
  96. /**
  97. * 修改工料信息
  98. * @param {Object} data 工料内容
  99. * @return {void}
  100. */
  101. async saveDatas(datas) {
  102. if (!this.ctx.tender || !this.ctx.material) {
  103. throw '数据错误';
  104. }
  105. // 判断是否可修改
  106. // 判断t_type是否为费用
  107. const transaction = await this.db.beginTransaction();
  108. try {
  109. for (const data of datas) {
  110. const mb_id = data.mb_id;
  111. delete data.mb_id;
  112. await transaction.update(this.tableName, data);
  113. await this.calcQuantityByML(transaction, mb_id);
  114. }
  115. await transaction.commit();
  116. return true;
  117. } catch (err) {
  118. await transaction.rollback();
  119. throw err;
  120. }
  121. }
  122. /**
  123. * 应用工料清单到其它清单中
  124. * @return {void}
  125. */
  126. async addOther(data) {
  127. if (!this.ctx.tender || !this.ctx.material) {
  128. throw '数据错误';
  129. }
  130. const transaction = await this.db.beginTransaction();
  131. try {
  132. const list = [];
  133. const select = data.select;
  134. for (const xmj of data.addXmj) {
  135. const newLists = {
  136. tid: this.ctx.tender.id,
  137. order: this.ctx.material.order,
  138. mid: this.ctx.material.id,
  139. mb_id: select.mb_id,
  140. gcl_id: xmj.gcl_id,
  141. xmj_id: xmj.id,
  142. mx_id: xmj.mx_id,
  143. gather_qty: xmj.gather_qty,
  144. quantity: select.quantity,
  145. in_time: new Date(),
  146. };
  147. list.push(newLists);
  148. }
  149. // 新增工料
  150. const result = await transaction.insert(this.tableName, list);
  151. if (result.affectedRows === 0) {
  152. throw '新增工料数据失败';
  153. }
  154. await this.calcQuantityByML(transaction, select.mb_id);
  155. await transaction.commit();
  156. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. }
  162. /**
  163. * 修改material_bills的quantity值和计算本期金额
  164. * @param transaction
  165. * @param mb_id
  166. * @returns {Promise<*>}
  167. */
  168. async calcQuantityByML(transaction, mb_id) {
  169. // 修改material_bills值
  170. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  171. if (!mbInfo) {
  172. throw '不存在该工料';
  173. }
  174. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  175. const sqlParam = [this.ctx.material.id, mb_id];
  176. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  177. console.log(mb_quantity);
  178. const updateData = {
  179. id: mb_id,
  180. quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
  181. m_tp: this.ctx.helper.round(this.ctx.helper.mul(this.ctx.helper.round(mb_quantity.quantity, 3), mbInfo.m_spread), 2),
  182. };
  183. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  184. // 计算本期总金额
  185. const sql2 = 'SELECT SUM(`m_tp`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ?';
  186. const sqlParam2 = [this.ctx.tender.id];
  187. const tp = await transaction.queryOne(sql2, sqlParam2);
  188. console.log(tp);
  189. const updateData2 = {
  190. id: this.ctx.material.id,
  191. m_tp: tp.total_price,
  192. };
  193. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  194. }
  195. /**
  196. * 获取工料清单关联表
  197. * @param {int} tid 标段id
  198. * @param {Object} mid 期id
  199. * @return {void}
  200. */
  201. async getMaterialData(tid, mid) {
  202. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`' +
  203. ' FROM ' + this.tableName + ' as ml' +
  204. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  205. ' ON ml.`mb_id` = mb.`id`' +
  206. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  207. const sqlParam = [tid, mid];
  208. return await this.db.query(sql, sqlParam);
  209. }
  210. /**
  211. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  212. * @param transaction
  213. * @param preMaterial
  214. * @param newMid
  215. * @returns {Promise<void>}
  216. */
  217. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  218. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  219. const copyMLArray = [];
  220. for (const ml of materialListData) {
  221. // 获取小计值
  222. let gather_qty = null;
  223. if (ml.mx_id !== null && ml.mx_id !== '') {
  224. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  225. } else {
  226. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  227. }
  228. const newMaterialList = {
  229. tid: ml.tid,
  230. order: ml.order,
  231. mid: newMaterial.id,
  232. mb_id: ml.mb_id,
  233. gcl_id: ml.gcl_id,
  234. xmj_id: ml.xmj_id,
  235. mx_id: ml.mx_id,
  236. gather_qty,
  237. quantity: ml.quantity,
  238. is_join: ml.is_join,
  239. in_time: new Date(),
  240. };
  241. copyMLArray.push(newMaterialList);
  242. }
  243. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  244. }
  245. }
  246. return MaterialList;
  247. };