material_list.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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: mb.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. expr: mb.expr,
  162. in_time: new Date(),
  163. };
  164. list.push(newLists);
  165. mb_idList.push(mb.mb_id);
  166. }
  167. }
  168. // 删除工料清单关联
  169. if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  170. // 新增工料清单关联
  171. if (list.length > 0) {
  172. const result = await transaction.insert(this.tableName, list);
  173. if (result.affectedRows === 0) {
  174. throw '新增工料数据失败';
  175. }
  176. }
  177. // 重算工料和总金额
  178. const calcMBIdList = this._.uniq(mb_idList);
  179. if (calcMBIdList.length > 0) {
  180. for (const select of calcMBIdList) {
  181. await this.calcQuantityByML(transaction, select);
  182. }
  183. }
  184. // throw 'fail';
  185. // await this.calcQuantityByML(transaction, select.mb_id);
  186. await transaction.commit();
  187. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  188. } catch (err) {
  189. await transaction.rollback();
  190. throw err;
  191. }
  192. }
  193. /**
  194. * 修改material_bills的quantity值和计算本期金额
  195. * @param transaction
  196. * @param mb_id
  197. * @return {Promise<*>}
  198. */
  199. async calcQuantityByML(transaction, mb_id) {
  200. // 修改material_bills值
  201. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  202. if (!mbInfo) {
  203. throw '不存在该工料';
  204. }
  205. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  206. const sqlParam = [this.ctx.material.id, mb_id];
  207. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  208. console.log(mb_quantity);
  209. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty);
  210. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  211. const updateData = {
  212. id: mb_id,
  213. quantity: newQuantity,
  214. m_tp: newTp,
  215. 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),
  216. };
  217. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  218. // 计算本期总金额
  219. 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';
  220. const sqlParam2 = [this.ctx.tender.id];
  221. const tp = await transaction.queryOne(sql2, sqlParam2);
  222. console.log(tp);
  223. const updateData2 = {
  224. id: this.ctx.material.id,
  225. m_tp: tp.total_price,
  226. m_tax_tp: tp.tax_total_price,
  227. };
  228. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  229. }
  230. /**
  231. * 获取工料清单关联表
  232. * @param {int} tid 标段id
  233. * @param {Object} mid 期id
  234. * @return {void}
  235. */
  236. async getMaterialData(tid, mid) {
  237. 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`' +
  238. ' FROM ' + this.tableName + ' as ml' +
  239. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  240. ' ON ml.`mb_id` = mb.`id`' +
  241. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  242. const sqlParam = [tid, mid];
  243. return await this.db.query(sql, sqlParam);
  244. }
  245. /**
  246. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  247. * @param transaction
  248. * @param preMaterial
  249. * @param newMid
  250. * @return {Promise<void>}
  251. */
  252. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  253. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  254. const copyMLArray = [];
  255. for (const ml of materialListData) {
  256. // 获取小计值
  257. let gather_qty = null;
  258. if (ml.mx_id !== null && ml.mx_id !== '') {
  259. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  260. } else {
  261. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  262. }
  263. const newMaterialList = {
  264. tid: ml.tid,
  265. order: ml.order,
  266. mid: newMaterial.id,
  267. mb_id: ml.mb_id,
  268. gcl_id: ml.gcl_id,
  269. xmj_id: ml.xmj_id,
  270. mx_id: ml.mx_id,
  271. gather_qty,
  272. quantity: ml.quantity,
  273. expr: ml.expr,
  274. is_join: ml.is_join,
  275. in_time: new Date(),
  276. };
  277. copyMLArray.push(newMaterialList);
  278. }
  279. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  280. }
  281. /**
  282. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  283. * @param transaction
  284. * @param preMaterial
  285. * @param newMid
  286. * @return {Promise<void>}
  287. */
  288. async copyPreMaterialList2(transaction, materialListData, notJoinList, newMaterial) {
  289. if (materialListData && materialListData.length > 0) {
  290. const copyMLArray = [];
  291. for (const ml of materialListData) {
  292. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id });
  293. const newMaterialList = {
  294. tid: newMaterial.tid,
  295. order: ml.order,
  296. mid: newMaterial.id,
  297. mb_id: ml.mb_id,
  298. gcl_id: ml.gcl_id,
  299. xmj_id: ml.xmj_id,
  300. mx_id: ml.mx_id,
  301. gather_qty: ml.gather_qty,
  302. quantity: ml.quantity ? ml.quantity : 0,
  303. expr: ml.expr ? ml.expr : '',
  304. is_join: is_join ? 0 : 1,
  305. in_time: new Date(),
  306. };
  307. copyMLArray.push(newMaterialList);
  308. }
  309. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  310. }
  311. }
  312. /**
  313. * 添加工料清单关联(多清单对应)
  314. * @return {void}
  315. */
  316. async adds(datas, checklist = false) {
  317. if (!this.ctx.tender || !this.ctx.material) {
  318. throw '数据错误';
  319. }
  320. const transaction = await this.db.beginTransaction();
  321. try {
  322. const list = [];
  323. const listGcl = [];
  324. // const delList = [];
  325. // const mb_idList = [];
  326. for (const xmj of datas.xmjs) {
  327. for (const mb of datas.mbIds) {
  328. // // 旧数据兼容问题,要去删除相同已存在的工料
  329. // const mlInfo = await this.getDataByCondition({
  330. // mid: this.ctx.material.id,
  331. // gcl_id: xmj.gcl_id,
  332. // xmj_id: xmj.xmj_id,
  333. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  334. // mb_id: mb,
  335. // });
  336. // if (mlInfo) {
  337. // delList.push(mlInfo.id);
  338. // mb_idList.push(mb);
  339. // }
  340. if (xmj.gather_qty) {
  341. const newLists = {
  342. tid: this.ctx.tender.id,
  343. order: this.ctx.material.order,
  344. mid: this.ctx.material.id,
  345. mb_id: mb,
  346. gcl_id: xmj.gcl_id,
  347. xmj_id: xmj.xmj_id,
  348. mx_id: xmj.mx_id,
  349. gather_qty: xmj.gather_qty,
  350. in_time: new Date(),
  351. is_join: xmj.is_join,
  352. };
  353. list.push(newLists);
  354. }
  355. if (this._.findIndex(listGcl, { gcl_id: xmj.gcl_id, mb_id: mb }) === -1) {
  356. const newListGcl = {
  357. tid: this.ctx.tender.id,
  358. order: this.ctx.material.order,
  359. mid: this.ctx.material.id,
  360. mb_id: mb,
  361. gcl_id: xmj.gcl_id,
  362. quantity: 0,
  363. expr: '',
  364. };
  365. listGcl.push(newListGcl);
  366. }
  367. }
  368. }
  369. // 维护list_gcl表
  370. // 删除工料清单关联
  371. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  372. // 新增工料清单关联
  373. if (list.length > 0) {
  374. const result = await transaction.insert(this.tableName, list);
  375. if (result.affectedRows === 0 ) {
  376. throw '新增工料数据失败';
  377. }
  378. }
  379. if (listGcl.length > 0) {
  380. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  381. if (result2.affectedRows === 0) {
  382. throw '新增工料关联数据失败';
  383. }
  384. }
  385. if (checklist) {
  386. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  387. }
  388. // 重算工料和总金额
  389. // const calcMBIdList = this._.uniq(mb_idList);
  390. // if (calcMBIdList.length > 0) {
  391. // for (const select of calcMBIdList) {
  392. // await this.calcQuantityByML(transaction, select);
  393. // }
  394. // }
  395. await transaction.commit();
  396. return checklist ? await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } }) : await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  397. } catch (err) {
  398. await transaction.rollback();
  399. throw err;
  400. }
  401. }
  402. /**
  403. * 删除工料清单关联(多清单对应)
  404. * @param {int} id 工料id
  405. * @return {void}
  406. */
  407. async dels(datas, checklist = false) {
  408. if (!this.ctx.tender || !this.ctx.material) {
  409. throw '数据错误';
  410. }
  411. const transaction = await this.db.beginTransaction();
  412. try {
  413. // 判断是否可删
  414. const listGcl = [];
  415. for (const xmj of datas.xmjs) {
  416. 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 });
  417. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  418. await transaction.delete(this.service.materialListGcl.tableName, { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: datas.mb_id, gcl_id: xmj.gcl_id });
  419. listGcl.push(xmj.gcl_id);
  420. }
  421. }
  422. // await transaction.delete(this.tableName, { id });
  423. await this.calcQuantityByML(transaction, datas.mb_id);
  424. if (checklist) {
  425. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  426. }
  427. await transaction.commit();
  428. // console.log(datas);
  429. return checklist ? await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } }) : await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  430. } catch (err) {
  431. await transaction.rollback();
  432. throw err;
  433. }
  434. }
  435. /**
  436. * 修改工料清单关联信息(多清单对应)
  437. * @param {Object} data 工料内容
  438. * @param {int} order 期数
  439. * @return {void}
  440. */
  441. async saves(datas, checklist = false) {
  442. if (!this.ctx.tender || !this.ctx.material) {
  443. throw '数据错误';
  444. }
  445. const transaction = await this.db.beginTransaction();
  446. try {
  447. const mb_id = datas.mb_id;
  448. const updateDatas = [];
  449. const updateListGcl = [];
  450. const listGcl = [];
  451. for (const xmj of datas.xmjs) {
  452. const udata = {
  453. row: {
  454. expr: datas.expr,
  455. quantity: datas.quantity,
  456. },
  457. where: {
  458. tid: this.ctx.tender.id,
  459. mid: this.ctx.material.id,
  460. mb_id,
  461. gcl_id: xmj.gcl_id,
  462. xmj_id: xmj.xmj_id,
  463. mx_id: xmj.mx_id,
  464. },
  465. };
  466. updateDatas.push(udata);
  467. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  468. listGcl.push(xmj.gcl_id);
  469. updateListGcl.push({
  470. row: {
  471. expr: datas.expr,
  472. quantity: datas.quantity,
  473. },
  474. where: {
  475. tid: this.ctx.tender.id,
  476. // mid: this.ctx.material.id,
  477. mb_id,
  478. gcl_id: xmj.gcl_id,
  479. },
  480. });
  481. }
  482. }
  483. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  484. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  485. await this.calcQuantityByML(transaction, mb_id);
  486. await transaction.commit();
  487. return checklist ? await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } }) : await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  488. } catch (err) {
  489. await transaction.rollback();
  490. throw err;
  491. }
  492. }
  493. /**
  494. * 复制粘贴多工料信息(多清单对应)
  495. * @param {Object} data 工料内容
  496. * @return {void}
  497. */
  498. async savePastes(datas, checklist = false) {
  499. if (!this.ctx.tender || !this.ctx.material) {
  500. throw '数据错误';
  501. }
  502. // 判断是否可修改
  503. // 判断t_type是否为费用
  504. const transaction = await this.db.beginTransaction();
  505. try {
  506. for (const data of datas.pasteData) {
  507. const updateDatas = [];
  508. const updateListGcl = [];
  509. const listGcl = [];
  510. for (const xmj of datas.xmjs) {
  511. const udata = {
  512. row: {
  513. expr: data.expr,
  514. quantity: data.quantity,
  515. },
  516. where: {
  517. tid: this.ctx.tender.id,
  518. mid: this.ctx.material.id,
  519. mb_id: data.mb_id,
  520. gcl_id: xmj.gcl_id,
  521. xmj_id: xmj.xmj_id,
  522. mx_id: xmj.mx_id,
  523. },
  524. };
  525. updateDatas.push(udata);
  526. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  527. listGcl.push(xmj.gcl_id);
  528. updateListGcl.push({
  529. row: {
  530. expr: data.expr,
  531. quantity: data.quantity,
  532. },
  533. where: {
  534. tid: this.ctx.tender.id,
  535. // mid: this.ctx.material.id,
  536. mb_id: data.mb_id,
  537. gcl_id: xmj.gcl_id,
  538. },
  539. });
  540. }
  541. }
  542. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  543. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  544. await this.calcQuantityByML(transaction, data.mb_id);
  545. }
  546. await transaction.commit();
  547. return checklist ? await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } }) : await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  548. } catch (err) {
  549. await transaction.rollback();
  550. throw err;
  551. }
  552. }
  553. }
  554. return MaterialList;
  555. };