material_list.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // 先删除addxmj里所有的清单工料再添加新工料,并重新计算每个工料的单价数量
  133. // 还要找出删除的工料,更新单价数量
  134. const list = [];
  135. const delList = [];
  136. const materialBills = data.materialBills;
  137. const mb_idList = [];
  138. for (const xmj of data.addXmj) {
  139. const mlList = await this.getAllDataByCondition({
  140. where: {
  141. mid: this.ctx.material.id,
  142. gcl_id: xmj.gcl_id,
  143. xmj_id: xmj.id,
  144. mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  145. },
  146. });
  147. const mbIdList = this._.map(mlList, 'mb_id');
  148. mb_idList.push(...mbIdList);
  149. delList.push(...this._.map(mlList, 'id'));
  150. for (const mb of materialBills) {
  151. const newLists = {
  152. tid: this.ctx.tender.id,
  153. order: this.ctx.material.order,
  154. mid: this.ctx.material.id,
  155. mb_id: mb.mb_id,
  156. gcl_id: xmj.gcl_id,
  157. xmj_id: xmj.id,
  158. mx_id: xmj.mx_id ? xmj.mx_id : '',
  159. gather_qty: xmj.gather_qty ? xmj.gather_qty : null,
  160. quantity: mb.quantity,
  161. in_time: new Date(),
  162. };
  163. list.push(newLists);
  164. mb_idList.push(mb.mb_id);
  165. }
  166. }
  167. // 删除工料清单关联
  168. if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  169. // 新增工料清单关联
  170. if (list.length > 0) {
  171. const result = await transaction.insert(this.tableName, list);
  172. if (result.affectedRows === 0) {
  173. throw '新增工料数据失败';
  174. }
  175. }
  176. // 重算工料和总金额
  177. const calcMBIdList = this._.uniq(mb_idList);
  178. if (calcMBIdList.length > 0) {
  179. for (const select of calcMBIdList) {
  180. await this.calcQuantityByML(transaction, select);
  181. }
  182. }
  183. // throw 'fail';
  184. // await this.calcQuantityByML(transaction, select.mb_id);
  185. await transaction.commit();
  186. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  187. } catch (err) {
  188. await transaction.rollback();
  189. throw err;
  190. }
  191. }
  192. /**
  193. * 修改material_bills的quantity值和计算本期金额
  194. * @param transaction
  195. * @param mb_id
  196. * @return {Promise<*>}
  197. */
  198. async calcQuantityByML(transaction, mb_id) {
  199. // 修改material_bills值
  200. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  201. if (!mbInfo) {
  202. throw '不存在该工料';
  203. }
  204. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  205. const sqlParam = [this.ctx.material.id, mb_id];
  206. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  207. console.log(mb_quantity);
  208. const updateData = {
  209. id: mb_id,
  210. quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
  211. m_tp: this.ctx.helper.round(this.ctx.helper.mul(this.ctx.helper.round(mb_quantity.quantity, 3), mbInfo.m_spread), 2),
  212. };
  213. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  214. // 计算本期总金额
  215. const sql2 = 'SELECT SUM(`m_tp`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  216. const sqlParam2 = [this.ctx.tender.id];
  217. const tp = await transaction.queryOne(sql2, sqlParam2);
  218. console.log(tp);
  219. const updateData2 = {
  220. id: this.ctx.material.id,
  221. m_tp: tp.total_price,
  222. };
  223. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  224. }
  225. /**
  226. * 获取工料清单关联表
  227. * @param {int} tid 标段id
  228. * @param {Object} mid 期id
  229. * @return {void}
  230. */
  231. async getMaterialData(tid, mid) {
  232. 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`' +
  233. ' FROM ' + this.tableName + ' as ml' +
  234. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  235. ' ON ml.`mb_id` = mb.`id`' +
  236. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  237. const sqlParam = [tid, mid];
  238. return await this.db.query(sql, sqlParam);
  239. }
  240. /**
  241. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  242. * @param transaction
  243. * @param preMaterial
  244. * @param newMid
  245. * @return {Promise<void>}
  246. */
  247. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  248. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  249. const copyMLArray = [];
  250. for (const ml of materialListData) {
  251. // 获取小计值
  252. let gather_qty = null;
  253. if (ml.mx_id !== null && ml.mx_id !== '') {
  254. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  255. } else {
  256. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  257. }
  258. const newMaterialList = {
  259. tid: ml.tid,
  260. order: ml.order,
  261. mid: newMaterial.id,
  262. mb_id: ml.mb_id,
  263. gcl_id: ml.gcl_id,
  264. xmj_id: ml.xmj_id,
  265. mx_id: ml.mx_id,
  266. gather_qty,
  267. quantity: ml.quantity,
  268. is_join: ml.is_join,
  269. in_time: new Date(),
  270. };
  271. copyMLArray.push(newMaterialList);
  272. }
  273. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  274. }
  275. }
  276. return MaterialList;
  277. };