material_list.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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,
  302. expr: ml.expr,
  303. is_join: is_join ? 1 : 0,
  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. const newLists = {
  339. tid: this.ctx.tender.id,
  340. order: this.ctx.material.order,
  341. mid: this.ctx.material.id,
  342. mb_id: mb,
  343. gcl_id: xmj.gcl_id,
  344. xmj_id: xmj.xmj_id,
  345. mx_id: xmj.mx_id,
  346. gather_qty: xmj.gather_qty,
  347. in_time: new Date(),
  348. is_join: xmj.is_join,
  349. };
  350. list.push(newLists);
  351. if (this._.findIndex(listGcl, { gcl_id: xmj.gcl_id }) === -1) {
  352. const newListGcl = {
  353. tid: this.ctx.tender.id,
  354. order: this.ctx.material.order,
  355. mid: this.ctx.material.id,
  356. mb_id: mb,
  357. gcl_id: xmj.gcl_id,
  358. quantity: 0,
  359. expr: '',
  360. };
  361. listGcl.push(newListGcl);
  362. }
  363. }
  364. }
  365. // 维护list_gcl表
  366. // 删除工料清单关联
  367. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  368. // 新增工料清单关联
  369. if (list.length > 0) {
  370. const result = await transaction.insert(this.tableName, list);
  371. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  372. if (result.affectedRows === 0 || result2.affectedRows === 0) {
  373. throw '新增工料数据失败';
  374. }
  375. }
  376. if (checklist) {
  377. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  378. }
  379. // 重算工料和总金额
  380. // const calcMBIdList = this._.uniq(mb_idList);
  381. // if (calcMBIdList.length > 0) {
  382. // for (const select of calcMBIdList) {
  383. // await this.calcQuantityByML(transaction, select);
  384. // }
  385. // }
  386. await transaction.commit();
  387. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  388. } catch (err) {
  389. await transaction.rollback();
  390. throw err;
  391. }
  392. }
  393. /**
  394. * 删除工料清单关联(多清单对应)
  395. * @param {int} id 工料id
  396. * @return {void}
  397. */
  398. async dels(datas, checklist = false) {
  399. if (!this.ctx.tender || !this.ctx.material) {
  400. throw '数据错误';
  401. }
  402. const transaction = await this.db.beginTransaction();
  403. try {
  404. // 判断是否可删
  405. const listGcl = [];
  406. for (const xmj of datas.xmjs) {
  407. 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 });
  408. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  409. 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 });
  410. listGcl.push(xmj.gcl_id);
  411. }
  412. }
  413. // await transaction.delete(this.tableName, { id });
  414. await this.calcQuantityByML(transaction, datas.mb_id);
  415. if (checklist) {
  416. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  417. }
  418. await transaction.commit();
  419. // console.log(datas);
  420. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  421. } catch (err) {
  422. await transaction.rollback();
  423. throw err;
  424. }
  425. }
  426. /**
  427. * 修改工料清单关联信息(多清单对应)
  428. * @param {Object} data 工料内容
  429. * @param {int} order 期数
  430. * @return {void}
  431. */
  432. async saves(datas) {
  433. if (!this.ctx.tender || !this.ctx.material) {
  434. throw '数据错误';
  435. }
  436. const transaction = await this.db.beginTransaction();
  437. try {
  438. const mb_id = datas.mb_id;
  439. const updateDatas = [];
  440. const updateListGcl = [];
  441. const listGcl = [];
  442. for (const xmj of datas.xmjs) {
  443. const udata = {
  444. row: {
  445. expr: datas.expr,
  446. quantity: datas.quantity,
  447. },
  448. where: {
  449. tid: this.ctx.tender.id,
  450. mid: this.ctx.material.id,
  451. mb_id,
  452. gcl_id: xmj.gcl_id,
  453. xmj_id: xmj.xmj_id,
  454. mx_id: xmj.mx_id,
  455. },
  456. };
  457. updateDatas.push(udata);
  458. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  459. listGcl.push(xmj.gcl_id);
  460. updateListGcl.push({
  461. row: {
  462. expr: datas.expr,
  463. quantity: datas.quantity,
  464. },
  465. where: {
  466. tid: this.ctx.tender.id,
  467. mid: this.ctx.material.id,
  468. mb_id,
  469. gcl_id: xmj.gcl_id,
  470. },
  471. });
  472. }
  473. }
  474. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  475. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  476. await this.calcQuantityByML(transaction, mb_id);
  477. await transaction.commit();
  478. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  479. } catch (err) {
  480. await transaction.rollback();
  481. throw err;
  482. }
  483. }
  484. /**
  485. * 复制粘贴多工料信息(多清单对应)
  486. * @param {Object} data 工料内容
  487. * @return {void}
  488. */
  489. async savePastes(datas) {
  490. if (!this.ctx.tender || !this.ctx.material) {
  491. throw '数据错误';
  492. }
  493. // 判断是否可修改
  494. // 判断t_type是否为费用
  495. const transaction = await this.db.beginTransaction();
  496. try {
  497. for (const data of datas.pasteData) {
  498. const updateDatas = [];
  499. for (const xmj of datas.xmjs) {
  500. const udata = {
  501. row: {
  502. expr: data.expr,
  503. quantity: data.quantity,
  504. },
  505. where: {
  506. tid: this.ctx.tender.id,
  507. mid: this.ctx.material.id,
  508. mb_id: data.mb_id,
  509. gcl_id: xmj.gcl_id,
  510. xmj_id: xmj.xmj_id,
  511. mx_id: xmj.mx_id,
  512. },
  513. };
  514. updateDatas.push(udata);
  515. }
  516. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  517. await this.calcQuantityByML(transaction, data.mb_id);
  518. }
  519. await transaction.commit();
  520. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  521. } catch (err) {
  522. await transaction.rollback();
  523. throw err;
  524. }
  525. }
  526. }
  527. return MaterialList;
  528. };