material_list.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. const copyMLArray = [];
  290. for (const ml of materialListData) {
  291. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id });
  292. const newMaterialList = {
  293. tid: newMaterial.tid,
  294. order: ml.order,
  295. mid: newMaterial.id,
  296. mb_id: ml.mb_id,
  297. gcl_id: ml.gcl_id,
  298. xmj_id: ml.xmj_id,
  299. mx_id: ml.mx_id,
  300. gather_qty: ml.gather_qty,
  301. quantity: ml.quantity ? ml.quantity : 0,
  302. expr: ml.expr ? ml.expr : '',
  303. is_join: is_join ? 0 : 1,
  304. in_time: new Date(),
  305. };
  306. copyMLArray.push(newMaterialList);
  307. }
  308. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  309. }
  310. /**
  311. * 添加工料清单关联(多清单对应)
  312. * @return {void}
  313. */
  314. async adds(datas, checklist = false) {
  315. if (!this.ctx.tender || !this.ctx.material) {
  316. throw '数据错误';
  317. }
  318. const transaction = await this.db.beginTransaction();
  319. try {
  320. const list = [];
  321. const listGcl = [];
  322. // const delList = [];
  323. // const mb_idList = [];
  324. for (const xmj of datas.xmjs) {
  325. for (const mb of datas.mbIds) {
  326. // // 旧数据兼容问题,要去删除相同已存在的工料
  327. // const mlInfo = await this.getDataByCondition({
  328. // mid: this.ctx.material.id,
  329. // gcl_id: xmj.gcl_id,
  330. // xmj_id: xmj.xmj_id,
  331. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  332. // mb_id: mb,
  333. // });
  334. // if (mlInfo) {
  335. // delList.push(mlInfo.id);
  336. // mb_idList.push(mb);
  337. // }
  338. if (xmj.gather_qty) {
  339. const newLists = {
  340. tid: this.ctx.tender.id,
  341. order: this.ctx.material.order,
  342. mid: this.ctx.material.id,
  343. mb_id: mb,
  344. gcl_id: xmj.gcl_id,
  345. xmj_id: xmj.xmj_id,
  346. mx_id: xmj.mx_id,
  347. gather_qty: xmj.gather_qty,
  348. in_time: new Date(),
  349. is_join: xmj.is_join,
  350. };
  351. list.push(newLists);
  352. }
  353. if (this._.findIndex(listGcl, { gcl_id: xmj.gcl_id, mb_id: mb }) === -1) {
  354. const newListGcl = {
  355. tid: this.ctx.tender.id,
  356. order: this.ctx.material.order,
  357. mid: this.ctx.material.id,
  358. mb_id: mb,
  359. gcl_id: xmj.gcl_id,
  360. quantity: 0,
  361. expr: '',
  362. };
  363. listGcl.push(newListGcl);
  364. }
  365. }
  366. }
  367. // 维护list_gcl表
  368. // 删除工料清单关联
  369. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  370. // 新增工料清单关联
  371. if (list.length > 0) {
  372. const result = await transaction.insert(this.tableName, list);
  373. if (result.affectedRows === 0 ) {
  374. throw '新增工料数据失败';
  375. }
  376. }
  377. if (listGcl.length > 0) {
  378. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  379. if (result2.affectedRows === 0) {
  380. throw '新增工料关联数据失败';
  381. }
  382. }
  383. if (checklist) {
  384. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  385. }
  386. // 重算工料和总金额
  387. // const calcMBIdList = this._.uniq(mb_idList);
  388. // if (calcMBIdList.length > 0) {
  389. // for (const select of calcMBIdList) {
  390. // await this.calcQuantityByML(transaction, select);
  391. // }
  392. // }
  393. await transaction.commit();
  394. 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);
  395. } catch (err) {
  396. await transaction.rollback();
  397. throw err;
  398. }
  399. }
  400. /**
  401. * 删除工料清单关联(多清单对应)
  402. * @param {int} id 工料id
  403. * @return {void}
  404. */
  405. async dels(datas, checklist = false) {
  406. if (!this.ctx.tender || !this.ctx.material) {
  407. throw '数据错误';
  408. }
  409. const transaction = await this.db.beginTransaction();
  410. try {
  411. // 判断是否可删
  412. const listGcl = [];
  413. for (const xmj of datas.xmjs) {
  414. 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 });
  415. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  416. 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 });
  417. listGcl.push(xmj.gcl_id);
  418. }
  419. }
  420. // await transaction.delete(this.tableName, { id });
  421. await this.calcQuantityByML(transaction, datas.mb_id);
  422. if (checklist) {
  423. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  424. }
  425. await transaction.commit();
  426. // console.log(datas);
  427. 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);
  428. } catch (err) {
  429. await transaction.rollback();
  430. throw err;
  431. }
  432. }
  433. /**
  434. * 修改工料清单关联信息(多清单对应)
  435. * @param {Object} data 工料内容
  436. * @param {int} order 期数
  437. * @return {void}
  438. */
  439. async saves(datas, checklist = false) {
  440. if (!this.ctx.tender || !this.ctx.material) {
  441. throw '数据错误';
  442. }
  443. const transaction = await this.db.beginTransaction();
  444. try {
  445. const mb_id = datas.mb_id;
  446. const updateDatas = [];
  447. const updateListGcl = [];
  448. const listGcl = [];
  449. for (const xmj of datas.xmjs) {
  450. const udata = {
  451. row: {
  452. expr: datas.expr,
  453. quantity: datas.quantity,
  454. },
  455. where: {
  456. tid: this.ctx.tender.id,
  457. mid: this.ctx.material.id,
  458. mb_id,
  459. gcl_id: xmj.gcl_id,
  460. xmj_id: xmj.xmj_id,
  461. mx_id: xmj.mx_id,
  462. },
  463. };
  464. updateDatas.push(udata);
  465. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  466. listGcl.push(xmj.gcl_id);
  467. updateListGcl.push({
  468. row: {
  469. expr: datas.expr,
  470. quantity: datas.quantity,
  471. },
  472. where: {
  473. tid: this.ctx.tender.id,
  474. mid: this.ctx.material.id,
  475. mb_id,
  476. gcl_id: xmj.gcl_id,
  477. },
  478. });
  479. }
  480. }
  481. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  482. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  483. await this.calcQuantityByML(transaction, mb_id);
  484. await transaction.commit();
  485. 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);
  486. } catch (err) {
  487. await transaction.rollback();
  488. throw err;
  489. }
  490. }
  491. /**
  492. * 复制粘贴多工料信息(多清单对应)
  493. * @param {Object} data 工料内容
  494. * @return {void}
  495. */
  496. async savePastes(datas, checklist = false) {
  497. if (!this.ctx.tender || !this.ctx.material) {
  498. throw '数据错误';
  499. }
  500. // 判断是否可修改
  501. // 判断t_type是否为费用
  502. const transaction = await this.db.beginTransaction();
  503. try {
  504. for (const data of datas.pasteData) {
  505. const updateDatas = [];
  506. const updateListGcl = [];
  507. const listGcl = [];
  508. for (const xmj of datas.xmjs) {
  509. const udata = {
  510. row: {
  511. expr: data.expr,
  512. quantity: data.quantity,
  513. },
  514. where: {
  515. tid: this.ctx.tender.id,
  516. mid: this.ctx.material.id,
  517. mb_id: data.mb_id,
  518. gcl_id: xmj.gcl_id,
  519. xmj_id: xmj.xmj_id,
  520. mx_id: xmj.mx_id,
  521. },
  522. };
  523. updateDatas.push(udata);
  524. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  525. listGcl.push(xmj.gcl_id);
  526. updateListGcl.push({
  527. row: {
  528. expr: data.expr,
  529. quantity: data.quantity,
  530. },
  531. where: {
  532. tid: this.ctx.tender.id,
  533. mid: this.ctx.material.id,
  534. mb_id: data.mb_id,
  535. gcl_id: xmj.gcl_id,
  536. },
  537. });
  538. }
  539. }
  540. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  541. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  542. await this.calcQuantityByML(transaction, data.mb_id);
  543. }
  544. await transaction.commit();
  545. 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);
  546. } catch (err) {
  547. await transaction.rollback();
  548. throw err;
  549. }
  550. }
  551. }
  552. return MaterialList;
  553. };