material_list.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty);
  209. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  210. const updateData = {
  211. id: mb_id,
  212. quantity: newQuantity,
  213. m_tp: newTp,
  214. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
  215. };
  216. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  217. // 计算本期总金额
  218. const sql2 = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  219. const sqlParam2 = [this.ctx.tender.id];
  220. const tp = await transaction.queryOne(sql2, sqlParam2);
  221. console.log(tp);
  222. const updateData2 = {
  223. id: this.ctx.material.id,
  224. m_tp: tp.total_price,
  225. m_tax_tp: tp.tax_total_price,
  226. };
  227. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  228. }
  229. /**
  230. * 获取工料清单关联表
  231. * @param {int} tid 标段id
  232. * @param {Object} mid 期id
  233. * @return {void}
  234. */
  235. async getMaterialData(tid, mid) {
  236. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`' +
  237. ' FROM ' + this.tableName + ' as ml' +
  238. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  239. ' ON ml.`mb_id` = mb.`id`' +
  240. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  241. const sqlParam = [tid, mid];
  242. return await this.db.query(sql, sqlParam);
  243. }
  244. /**
  245. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  246. * @param transaction
  247. * @param preMaterial
  248. * @param newMid
  249. * @return {Promise<void>}
  250. */
  251. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  252. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  253. const copyMLArray = [];
  254. for (const ml of materialListData) {
  255. // 获取小计值
  256. let gather_qty = null;
  257. if (ml.mx_id !== null && ml.mx_id !== '') {
  258. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  259. } else {
  260. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  261. }
  262. const newMaterialList = {
  263. tid: ml.tid,
  264. order: ml.order,
  265. mid: newMaterial.id,
  266. mb_id: ml.mb_id,
  267. gcl_id: ml.gcl_id,
  268. xmj_id: ml.xmj_id,
  269. mx_id: ml.mx_id,
  270. gather_qty,
  271. quantity: ml.quantity,
  272. expr: ml.expr,
  273. is_join: ml.is_join,
  274. in_time: new Date(),
  275. };
  276. copyMLArray.push(newMaterialList);
  277. }
  278. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  279. }
  280. /**
  281. * 添加工料清单关联(多清单对应)
  282. * @return {void}
  283. */
  284. async adds(datas, checklist = false) {
  285. if (!this.ctx.tender || !this.ctx.material) {
  286. throw '数据错误';
  287. }
  288. const transaction = await this.db.beginTransaction();
  289. try {
  290. const list = [];
  291. // const delList = [];
  292. // const mb_idList = [];
  293. for (const xmj of datas.xmjs) {
  294. for (const mb of datas.mbIds) {
  295. // // 旧数据兼容问题,要去删除相同已存在的工料
  296. // const mlInfo = await this.getDataByCondition({
  297. // mid: this.ctx.material.id,
  298. // gcl_id: xmj.gcl_id,
  299. // xmj_id: xmj.xmj_id,
  300. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  301. // mb_id: mb,
  302. // });
  303. // if (mlInfo) {
  304. // delList.push(mlInfo.id);
  305. // mb_idList.push(mb);
  306. // }
  307. const newLists = {
  308. tid: this.ctx.tender.id,
  309. order: this.ctx.material.order,
  310. mid: this.ctx.material.id,
  311. mb_id: mb,
  312. gcl_id: xmj.gcl_id,
  313. xmj_id: xmj.xmj_id,
  314. mx_id: xmj.mx_id,
  315. gather_qty: xmj.gather_qty,
  316. in_time: new Date(),
  317. is_join: xmj.is_join,
  318. };
  319. list.push(newLists);
  320. }
  321. }
  322. // 删除工料清单关联
  323. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  324. // 新增工料清单关联
  325. if (list.length > 0) {
  326. const result = await transaction.insert(this.tableName, list);
  327. if (result.affectedRows === 0) {
  328. throw '新增工料数据失败';
  329. }
  330. }
  331. if (checklist) {
  332. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  333. }
  334. // 重算工料和总金额
  335. // const calcMBIdList = this._.uniq(mb_idList);
  336. // if (calcMBIdList.length > 0) {
  337. // for (const select of calcMBIdList) {
  338. // await this.calcQuantityByML(transaction, select);
  339. // }
  340. // }
  341. await transaction.commit();
  342. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  343. } catch (err) {
  344. await transaction.rollback();
  345. throw err;
  346. }
  347. }
  348. /**
  349. * 删除工料清单关联(多清单对应)
  350. * @param {int} id 工料id
  351. * @return {void}
  352. */
  353. async dels(datas, checklist = false) {
  354. if (!this.ctx.tender || !this.ctx.material) {
  355. throw '数据错误';
  356. }
  357. const transaction = await this.db.beginTransaction();
  358. try {
  359. // 判断是否可删
  360. for (const xmj of datas.xmjs) {
  361. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: datas.mb_id, gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id });
  362. }
  363. // await transaction.delete(this.tableName, { id });
  364. await this.calcQuantityByML(transaction, datas.mb_id);
  365. if (checklist) {
  366. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  367. }
  368. await transaction.commit();
  369. // console.log(datas);
  370. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  371. } catch (err) {
  372. await transaction.rollback();
  373. throw err;
  374. }
  375. }
  376. /**
  377. * 修改工料清单关联信息(多清单对应)
  378. * @param {Object} data 工料内容
  379. * @param {int} order 期数
  380. * @return {void}
  381. */
  382. async saves(datas) {
  383. if (!this.ctx.tender || !this.ctx.material) {
  384. throw '数据错误';
  385. }
  386. const transaction = await this.db.beginTransaction();
  387. try {
  388. const mb_id = datas.mb_id;
  389. const updateDatas = [];
  390. for (const xmj of datas.xmjs) {
  391. const udata = {
  392. row: {
  393. expr: datas.expr,
  394. quantity: datas.quantity,
  395. },
  396. where: {
  397. tid: this.ctx.tender.id,
  398. mid: this.ctx.material.id,
  399. mb_id,
  400. gcl_id: xmj.gcl_id,
  401. xmj_id: xmj.xmj_id,
  402. mx_id: xmj.mx_id,
  403. },
  404. };
  405. updateDatas.push(udata);
  406. }
  407. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  408. await this.calcQuantityByML(transaction, mb_id);
  409. await transaction.commit();
  410. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  411. } catch (err) {
  412. await transaction.rollback();
  413. throw err;
  414. }
  415. }
  416. /**
  417. * 复制粘贴多工料信息(多清单对应)
  418. * @param {Object} data 工料内容
  419. * @return {void}
  420. */
  421. async savePastes(datas) {
  422. if (!this.ctx.tender || !this.ctx.material) {
  423. throw '数据错误';
  424. }
  425. // 判断是否可修改
  426. // 判断t_type是否为费用
  427. const transaction = await this.db.beginTransaction();
  428. try {
  429. for (const data of datas.pasteData) {
  430. const updateDatas = [];
  431. for (const xmj of datas.xmjs) {
  432. const udata = {
  433. row: {
  434. expr: data.expr,
  435. quantity: data.quantity,
  436. },
  437. where: {
  438. tid: this.ctx.tender.id,
  439. mid: this.ctx.material.id,
  440. mb_id: data.mb_id,
  441. gcl_id: xmj.gcl_id,
  442. xmj_id: xmj.xmj_id,
  443. mx_id: xmj.mx_id,
  444. },
  445. };
  446. updateDatas.push(udata);
  447. }
  448. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  449. await this.calcQuantityByML(transaction, data.mb_id);
  450. }
  451. await transaction.commit();
  452. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  453. } catch (err) {
  454. await transaction.rollback();
  455. throw err;
  456. }
  457. }
  458. }
  459. return MaterialList;
  460. };