material_list.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. * @return {void}
  284. */
  285. async adds(datas, checklist = false) {
  286. if (!this.ctx.tender || !this.ctx.material) {
  287. throw '数据错误';
  288. }
  289. const transaction = await this.db.beginTransaction();
  290. try {
  291. const list = [];
  292. // const delList = [];
  293. // const mb_idList = [];
  294. for (const xmj of datas.xmjs) {
  295. for (const mb of datas.mbIds) {
  296. // // 旧数据兼容问题,要去删除相同已存在的工料
  297. // const mlInfo = await this.getDataByCondition({
  298. // mid: this.ctx.material.id,
  299. // gcl_id: xmj.gcl_id,
  300. // xmj_id: xmj.xmj_id,
  301. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  302. // mb_id: mb,
  303. // });
  304. // if (mlInfo) {
  305. // delList.push(mlInfo.id);
  306. // mb_idList.push(mb);
  307. // }
  308. const newLists = {
  309. tid: this.ctx.tender.id,
  310. order: this.ctx.material.order,
  311. mid: this.ctx.material.id,
  312. mb_id: mb,
  313. gcl_id: xmj.gcl_id,
  314. xmj_id: xmj.xmj_id,
  315. mx_id: xmj.mx_id,
  316. gather_qty: xmj.gather_qty,
  317. in_time: new Date(),
  318. is_join: xmj.is_join,
  319. };
  320. list.push(newLists);
  321. }
  322. }
  323. // 删除工料清单关联
  324. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  325. // 新增工料清单关联
  326. if (list.length > 0) {
  327. const result = await transaction.insert(this.tableName, list);
  328. if (result.affectedRows === 0) {
  329. throw '新增工料数据失败';
  330. }
  331. }
  332. if (checklist) {
  333. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  334. }
  335. // 重算工料和总金额
  336. // const calcMBIdList = this._.uniq(mb_idList);
  337. // if (calcMBIdList.length > 0) {
  338. // for (const select of calcMBIdList) {
  339. // await this.calcQuantityByML(transaction, select);
  340. // }
  341. // }
  342. await transaction.commit();
  343. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  344. } catch (err) {
  345. await transaction.rollback();
  346. throw err;
  347. }
  348. }
  349. /**
  350. * 删除工料清单关联(多清单对应)
  351. * @param {int} id 工料id
  352. * @return {void}
  353. */
  354. async dels(datas, checklist = false) {
  355. if (!this.ctx.tender || !this.ctx.material) {
  356. throw '数据错误';
  357. }
  358. const transaction = await this.db.beginTransaction();
  359. try {
  360. // 判断是否可删
  361. for (const xmj of datas.xmjs) {
  362. 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 });
  363. }
  364. // await transaction.delete(this.tableName, { id });
  365. await this.calcQuantityByML(transaction, datas.mb_id);
  366. if (checklist) {
  367. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  368. }
  369. await transaction.commit();
  370. // console.log(datas);
  371. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  372. } catch (err) {
  373. await transaction.rollback();
  374. throw err;
  375. }
  376. }
  377. /**
  378. * 修改工料清单关联信息(多清单对应)
  379. * @param {Object} data 工料内容
  380. * @param {int} order 期数
  381. * @return {void}
  382. */
  383. async saves(datas) {
  384. if (!this.ctx.tender || !this.ctx.material) {
  385. throw '数据错误';
  386. }
  387. const transaction = await this.db.beginTransaction();
  388. try {
  389. const mb_id = datas.mb_id;
  390. const updateDatas = [];
  391. for (const xmj of datas.xmjs) {
  392. const udata = {
  393. row: {
  394. expr: datas.expr,
  395. quantity: datas.quantity,
  396. },
  397. where: {
  398. tid: this.ctx.tender.id,
  399. mid: this.ctx.material.id,
  400. mb_id,
  401. gcl_id: xmj.gcl_id,
  402. xmj_id: xmj.xmj_id,
  403. mx_id: xmj.mx_id,
  404. },
  405. };
  406. updateDatas.push(udata);
  407. }
  408. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  409. await this.calcQuantityByML(transaction, mb_id);
  410. await transaction.commit();
  411. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  412. } catch (err) {
  413. await transaction.rollback();
  414. throw err;
  415. }
  416. }
  417. /**
  418. * 复制粘贴多工料信息(多清单对应)
  419. * @param {Object} data 工料内容
  420. * @return {void}
  421. */
  422. async savePastes(datas) {
  423. if (!this.ctx.tender || !this.ctx.material) {
  424. throw '数据错误';
  425. }
  426. // 判断是否可修改
  427. // 判断t_type是否为费用
  428. const transaction = await this.db.beginTransaction();
  429. try {
  430. for (const data of datas.pasteData) {
  431. const updateDatas = [];
  432. for (const xmj of datas.xmjs) {
  433. const udata = {
  434. row: {
  435. expr: data.expr,
  436. quantity: data.quantity,
  437. },
  438. where: {
  439. tid: this.ctx.tender.id,
  440. mid: this.ctx.material.id,
  441. mb_id: data.mb_id,
  442. gcl_id: xmj.gcl_id,
  443. xmj_id: xmj.xmj_id,
  444. mx_id: xmj.mx_id,
  445. },
  446. };
  447. updateDatas.push(udata);
  448. }
  449. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  450. await this.calcQuantityByML(transaction, data.mb_id);
  451. }
  452. await transaction.commit();
  453. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  454. } catch (err) {
  455. await transaction.rollback();
  456. throw err;
  457. }
  458. }
  459. }
  460. return MaterialList;
  461. };