material_list.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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, ms_id = null) {
  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. is_join: data.is_join,
  42. is_self: 1,
  43. ms_id: ms_id ? ms_id : null,
  44. in_time: new Date(),
  45. };
  46. list.push(newLists);
  47. }
  48. // 新增工料
  49. const result = await this.db.insert(this.tableName, list);
  50. if (result.affectedRows === 0) {
  51. throw '新增工料数据失败';
  52. }
  53. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  54. }
  55. /**
  56. * 删除工料清单关联
  57. * @param {int} id 工料id
  58. * @return {void}
  59. */
  60. async del(id, mb_id, ms_id = null) {
  61. if (!this.ctx.tender || !this.ctx.material) {
  62. throw '数据错误';
  63. }
  64. const transaction = await this.db.beginTransaction();
  65. try {
  66. // 判断是否可删
  67. await transaction.delete(this.tableName, { id });
  68. await this.calcQuantityByML(transaction, mb_id, ms_id);
  69. await transaction.commit();
  70. return true;
  71. } catch (err) {
  72. await transaction.rollback();
  73. throw err;
  74. }
  75. }
  76. /**
  77. * 修改工料清单关联信息
  78. * @param {Object} data 工料内容
  79. * @param {int} order 期数
  80. * @return {void}
  81. */
  82. async save(data, ms_id = null) {
  83. if (!this.ctx.tender || !this.ctx.material) {
  84. throw '数据错误';
  85. }
  86. const transaction = await this.db.beginTransaction();
  87. try {
  88. const mb_id = data.mb_id;
  89. delete data.mb_id;
  90. await transaction.update(this.tableName, data);
  91. await this.calcQuantityByML(transaction, mb_id, ms_id);
  92. await transaction.commit();
  93. return true;
  94. } catch (err) {
  95. await transaction.rollback();
  96. throw err;
  97. }
  98. }
  99. /**
  100. * 修改工料信息
  101. * @param {Object} data 工料内容
  102. * @return {void}
  103. */
  104. async saveDatas(datas, ms_id = null) {
  105. if (!this.ctx.tender || !this.ctx.material) {
  106. throw '数据错误';
  107. }
  108. // 判断是否可修改
  109. // 判断t_type是否为费用
  110. const transaction = await this.db.beginTransaction();
  111. try {
  112. for (const data of datas) {
  113. const mb_id = data.mb_id;
  114. delete data.mb_id;
  115. await transaction.update(this.tableName, data);
  116. await this.calcQuantityByML(transaction, mb_id, ms_id);
  117. }
  118. await transaction.commit();
  119. return true;
  120. } catch (err) {
  121. await transaction.rollback();
  122. throw err;
  123. }
  124. }
  125. /**
  126. * 应用工料清单到其它清单中
  127. * @return {void}
  128. */
  129. async addOther(data) {
  130. if (!this.ctx.tender || !this.ctx.material) {
  131. throw '数据错误';
  132. }
  133. const transaction = await this.db.beginTransaction();
  134. try {
  135. // 先删除addxmj里所有的清单工料再添加新工料,并重新计算每个工料的单价数量
  136. // 还要找出删除的工料,更新单价数量
  137. const list = [];
  138. const delList = [];
  139. const materialBills = data.materialBills;
  140. const mb_idList = [];
  141. for (const xmj of data.addXmj) {
  142. const mlList = await this.getAllDataByCondition({
  143. where: {
  144. mid: this.ctx.material.id,
  145. gcl_id: xmj.gcl_id,
  146. xmj_id: xmj.id,
  147. mx_id: xmj.mx_id,
  148. },
  149. });
  150. const mbIdList = this._.map(mlList, 'mb_id');
  151. mb_idList.push(...mbIdList);
  152. delList.push(...this._.map(mlList, 'id'));
  153. for (const mb of materialBills) {
  154. const newLists = {
  155. tid: this.ctx.tender.id,
  156. order: mb.order,
  157. mid: this.ctx.material.id,
  158. mb_id: mb.mb_id,
  159. gcl_id: xmj.gcl_id,
  160. xmj_id: xmj.id,
  161. mx_id: xmj.mx_id,
  162. gather_qty: xmj.gather_qty ? xmj.gather_qty : null,
  163. quantity: mb.quantity,
  164. expr: mb.expr,
  165. in_time: new Date(),
  166. };
  167. list.push(newLists);
  168. mb_idList.push(mb.mb_id);
  169. }
  170. }
  171. // 删除工料清单关联
  172. if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  173. // 新增工料清单关联
  174. if (list.length > 0) {
  175. const result = await transaction.insert(this.tableName, list);
  176. if (result.affectedRows === 0) {
  177. throw '新增工料数据失败';
  178. }
  179. }
  180. // 重算工料和总金额
  181. const calcMBIdList = this._.uniq(mb_idList);
  182. if (calcMBIdList.length > 0) {
  183. for (const select of calcMBIdList) {
  184. await this.calcQuantityByML(transaction, select);
  185. }
  186. }
  187. // throw 'fail';
  188. // await this.calcQuantityByML(transaction, select.mb_id);
  189. await transaction.commit();
  190. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  191. } catch (err) {
  192. await transaction.rollback();
  193. throw err;
  194. }
  195. }
  196. /**
  197. * 修改material_bills的quantity值和计算本期金额
  198. * @param transaction
  199. * @param mb_id
  200. * @return {Promise<*>}
  201. */
  202. async calcQuantityByML(transaction, mb_id, ms_id = null, updateAllStage = '') {
  203. // 修改material_bills值
  204. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  205. if (!mbInfo) {
  206. throw '不存在该工料';
  207. }
  208. let m_spread = mbInfo.m_spread;
  209. let updateId = mb_id;
  210. if (ms_id) {
  211. const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({ mid: this.ctx.material.id, mb_id, ms_id });
  212. m_spread = msbInfo.m_spread;
  213. updateId = msbInfo.id;
  214. }
  215. const msSql = ms_id ? ' AND `ms_id` = ' + ms_id : '';
  216. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?' + msSql + ' AND `is_join`=1';
  217. const sqlParam = [this.ctx.material.id, mb_id];
  218. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  219. console.log(mb_quantity);
  220. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty);
  221. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, m_spread), this.ctx.material.decimal.tp);
  222. const updateData = {
  223. id: updateId,
  224. quantity: newQuantity,
  225. m_tp: newTp,
  226. 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),
  227. };
  228. if (ms_id) {
  229. await transaction.update(this.ctx.service.materialStageBills.tableName, updateData);
  230. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  231. if (updateAllStage === 'all') {
  232. const updateDatas = [];
  233. const updateMsIds = [];
  234. const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id } });
  235. for (const msb of msbList) {
  236. if (msb.ms_id !== parseInt(ms_id)) {
  237. const sql4 = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `ms_id`=? AND `is_join`=1';
  238. const sqlParam4 = [this.ctx.material.id, mb_id, msb.ms_id];
  239. const mb_quantity4 = await transaction.queryOne(sql4, sqlParam4);
  240. const newQuantity4 = this.ctx.helper.round(mb_quantity4.quantity, this.ctx.material.decimal.qty);
  241. const newTp4 = this.ctx.helper.round(this.ctx.helper.mul(newQuantity4, msb.m_spread), this.ctx.material.decimal.tp);
  242. const updateData4 = {
  243. id: msb.id,
  244. quantity: newQuantity4,
  245. m_tp: newTp4,
  246. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp4, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
  247. };
  248. updateDatas.push(updateData4);
  249. updateMsIds.push(msb.ms_id);
  250. }
  251. }
  252. if (updateDatas.length > 0) {
  253. await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateDatas);
  254. for (const msId of updateMsIds) {
  255. await this.ctx.service.materialStage.updateMtp(transaction, msId);
  256. }
  257. }
  258. }
  259. // 还要更新bills表的m_tp和m_tax_tp值
  260. const sql3 = '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.materialStageBills.tableName + ' WHERE `tid` = ? AND `mid` = ? AND `mb_id` = ? AND `is_summary` = 1';
  261. const sqlParam3 = [this.ctx.tender.id, this.ctx.material.id, mb_id];
  262. const tp3 = await transaction.queryOne(sql3, sqlParam3);
  263. const updateBillsData = {
  264. id: mb_id,
  265. m_tp: tp3.total_price,
  266. m_tax_tp: tp3.tax_total_price,
  267. };
  268. await transaction.update(this.ctx.service.materialBills.tableName, updateBillsData);
  269. } else {
  270. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  271. }
  272. // 计算本期总金额
  273. 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';
  274. const sqlParam2 = [this.ctx.tender.id];
  275. const tp = await transaction.queryOne(sql2, sqlParam2);
  276. console.log(tp);
  277. const updateData2 = {
  278. id: this.ctx.material.id,
  279. m_tp: tp.total_price,
  280. m_tax_tp: tp.tax_total_price,
  281. };
  282. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  283. }
  284. /**
  285. * 获取工料清单关联表
  286. * @param {int} tid 标段id
  287. * @param {Object} mid 期id
  288. * @return {void}
  289. */
  290. async getMaterialData(tid, mid) {
  291. 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.`ms_id`, ml.`tid`, ml.`mid`, mb.m_spread, ml.ms_id' +
  292. ' FROM ' + this.tableName + ' as ml' +
  293. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  294. ' ON ml.`mb_id` = mb.`id`' +
  295. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  296. const sqlParam = [tid, mid];
  297. return await this.db.query(sql, sqlParam);
  298. }
  299. async getPreMaterialData(tid, mid) {
  300. 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.`ms_id`, ml.`tid`, ml.`mid`, mbh.m_spread, ml.ms_id' +
  301. ' FROM ' + this.tableName + ' as ml' +
  302. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  303. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' as mbh ON ml.`mb_id` = mbh.`mb_id` and mbh.mid = ?' +
  304. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  305. const sqlParam = [mid, tid, mid];
  306. return await this.db.query(sql, sqlParam);
  307. }
  308. async getMaterialStageData(tid, mid) {
  309. 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.`ms_id`, ml.`tid`, ml.`mid`, msb.m_spread, ml.ms_id, ms.sid, ms.order as s_order' +
  310. ' FROM ' + this.tableName + ' as ml' +
  311. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  312. ' LEFT JOIN ' + this.ctx.service.materialStageBills.tableName + ' as msb ON ml.mb_id = msb.mb_id AND ml.ms_id = msb.ms_id' +
  313. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' as ms ON ml.`ms_id` = ms.`id`' +
  314. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  315. const sqlParam = [tid, mid];
  316. return await this.db.query(sql, sqlParam);
  317. }
  318. async getPreMaterialStageData(tid, mid) {
  319. 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.`ms_id`, ml.`tid`, ml.`mid`, msb.m_spread, ml.ms_id, ms.sid, ms.order as s_order' +
  320. ' FROM ' + this.tableName + ' as ml' +
  321. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  322. ' LEFT JOIN ' + this.ctx.service.materialStageBills.tableName + ' as msb ON ml.`mb_id` = msb.mb_id AND ml.ms_id = msb.ms_id And ml.mid = msb.mid' +
  323. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' as ms ON ml.`ms_id` = ms.`id`' +
  324. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  325. const sqlParam = [tid, mid];
  326. console.log(this.db.format(sql, sqlParam));
  327. return await this.db.query(sql, sqlParam);
  328. }
  329. /**
  330. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  331. * @param transaction
  332. * @param preMaterial
  333. * @param newMid
  334. * @return {Promise<void>}
  335. */
  336. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  337. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  338. const copyMLArray = [];
  339. for (const ml of materialListData) {
  340. // 获取小计值
  341. let gather_qty = null;
  342. if (ml.mx_id !== null && ml.mx_id !== '') {
  343. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  344. } else {
  345. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  346. }
  347. const newMaterialList = {
  348. tid: ml.tid,
  349. order: ml.order,
  350. mid: newMaterial.id,
  351. mb_id: ml.mb_id,
  352. gcl_id: ml.gcl_id,
  353. xmj_id: ml.xmj_id,
  354. mx_id: ml.mx_id,
  355. gather_qty,
  356. quantity: ml.quantity,
  357. expr: ml.expr,
  358. is_join: ml.is_join,
  359. in_time: new Date(),
  360. };
  361. copyMLArray.push(newMaterialList);
  362. }
  363. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  364. }
  365. /**
  366. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  367. * @param transaction
  368. * @param preMaterial
  369. * @param newMid
  370. * @return {Promise<void>}
  371. */
  372. async copyPreMaterialList2(transaction, materialListData, materialSelfListData, notJoinList, newMaterial, materialStageData) {
  373. if (materialListData && materialListData.length > 0) {
  374. const copyMLArray = [];
  375. for (const ml of materialListData) {
  376. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id });
  377. const newMaterialList = {
  378. tid: newMaterial.tid,
  379. order: ml.order,
  380. mid: newMaterial.id,
  381. mb_id: ml.mb_id,
  382. gcl_id: ml.gcl_id,
  383. xmj_id: ml.xmj_id,
  384. mx_id: ml.mx_id,
  385. gather_qty: ml.gather_qty,
  386. quantity: ml.quantity ? ml.quantity : 0,
  387. expr: ml.expr ? ml.expr : '',
  388. is_join: is_join ? 0 : 1,
  389. in_time: new Date(),
  390. };
  391. if (ml.sid) {
  392. const ms = this._.find(materialStageData, { sid: ml.sid });
  393. if (ms && ms.id) newMaterialList.ms_id = ms.id;
  394. }
  395. copyMLArray.push(newMaterialList);
  396. }
  397. if (copyMLArray.length !== 0) await transaction.insert(this.tableName, copyMLArray);
  398. }
  399. if (materialSelfListData && materialSelfListData.length > 0) {
  400. const copyMLArray2 = [];
  401. for (const ml of materialSelfListData) {
  402. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id });
  403. const newMaterialList = {
  404. tid: newMaterial.tid,
  405. order: ml.order,
  406. mid: newMaterial.id,
  407. mb_id: ml.mb_id,
  408. gcl_id: ml.gcl_id,
  409. xmj_id: ml.xmj_id,
  410. mx_id: ml.mx_id,
  411. gather_qty: ml.gather_qty,
  412. quantity: ml.quantity ? ml.quantity : 0,
  413. expr: ml.expr ? ml.expr : '',
  414. is_join: is_join ? 0 : 1,
  415. is_self: 1,
  416. in_time: new Date(),
  417. };
  418. if (ml.sid) {
  419. const ms = this._.find(materialStageData, { sid: ml.sid });
  420. if (ms && ms.id) newMaterialList.ms_id = ms.id;
  421. }
  422. copyMLArray2.push(newMaterialList);
  423. }
  424. if (copyMLArray2.length !== 0) await transaction.insert(this.tableName, copyMLArray2);
  425. }
  426. }
  427. /**
  428. * 添加工料清单关联(多清单对应)
  429. * @return {void}
  430. */
  431. async adds(datas, checklist = false) {
  432. if (!this.ctx.tender || !this.ctx.material) {
  433. throw '数据错误';
  434. }
  435. const transaction = await this.db.beginTransaction();
  436. try {
  437. const list = [];
  438. const listGcl = [];
  439. // const delList = [];
  440. // const mb_idList = [];
  441. for (const xmj of datas.xmjs) {
  442. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  443. for (const mb of datas.mbIds) {
  444. // // 旧数据兼容问题,要去删除相同已存在的工料
  445. // const mlInfo = await this.getDataByCondition({
  446. // mid: this.ctx.material.id,
  447. // gcl_id: xmj.gcl_id,
  448. // xmj_id: xmj.xmj_id,
  449. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  450. // mb_id: mb,
  451. // });
  452. // if (mlInfo) {
  453. // delList.push(mlInfo.id);
  454. // mb_idList.push(mb);
  455. // }
  456. if (xmj.gather_qty && this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  457. const newLists = {
  458. tid: this.ctx.tender.id,
  459. order: this.ctx.material.order,
  460. mid: this.ctx.material.id,
  461. mb_id: mb,
  462. gcl_id: xmj.gcl_id,
  463. xmj_id: xmj.xmj_id,
  464. mx_id: xmj.mx_id,
  465. ms_id: xmj.ms_id ? xmj.ms_id : null,
  466. gather_qty: xmj.gather_qty,
  467. in_time: new Date(),
  468. is_join: xmj.is_join,
  469. };
  470. list.push(newLists);
  471. }
  472. if (this._.findIndex(listGcl, { gcl_id: xmj.gcl_id, mb_id: mb }) === -1) {
  473. const newListGcl = {
  474. tid: this.ctx.tender.id,
  475. order: this.ctx.material.order,
  476. mid: this.ctx.material.id,
  477. mb_id: mb,
  478. gcl_id: xmj.gcl_id,
  479. quantity: 0,
  480. expr: '',
  481. };
  482. listGcl.push(newListGcl);
  483. }
  484. }
  485. }
  486. // 维护list_gcl表
  487. // 删除工料清单关联
  488. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  489. // 新增工料清单关联
  490. if (list.length > 0) {
  491. const result = await transaction.insert(this.tableName, list);
  492. if (result.affectedRows === 0) {
  493. throw '新增工料数据失败';
  494. }
  495. }
  496. if (listGcl.length > 0) {
  497. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  498. if (result2.affectedRows === 0) {
  499. throw '新增工料关联数据失败';
  500. }
  501. }
  502. if (checklist) {
  503. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  504. }
  505. // 重算工料和总金额
  506. // const calcMBIdList = this._.uniq(mb_idList);
  507. // if (calcMBIdList.length > 0) {
  508. // for (const select of calcMBIdList) {
  509. // await this.calcQuantityByML(transaction, select);
  510. // }
  511. // }
  512. await transaction.commit();
  513. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  514. return checklist ? gclList : {
  515. gclList,
  516. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  517. };
  518. } catch (err) {
  519. await transaction.rollback();
  520. throw err;
  521. }
  522. }
  523. /**
  524. * 删除工料清单关联(多清单对应)
  525. * @param {int} id 工料id
  526. * @return {void}
  527. */
  528. async dels(datas, checklist = false, fromCheckList = false, ms_id = null) {
  529. if (!this.ctx.tender || !this.ctx.material) {
  530. throw '数据错误';
  531. }
  532. const transaction = await this.db.beginTransaction();
  533. try {
  534. // 判断是否可删
  535. const listGcl = [];
  536. for (const xmj of datas.xmjs) {
  537. 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, is_self: 0 });
  538. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  539. 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 });
  540. listGcl.push(xmj.gcl_id);
  541. }
  542. }
  543. // await transaction.delete(this.tableName, { id });
  544. await this.calcQuantityByML(transaction, datas.mb_id, ms_id, 'all');
  545. if (checklist) {
  546. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  547. }
  548. await transaction.commit();
  549. // console.log(datas);
  550. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  551. return fromCheckList ? gclList : {
  552. gclList,
  553. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  554. };
  555. } catch (err) {
  556. await transaction.rollback();
  557. throw err;
  558. }
  559. }
  560. /**
  561. * 修改工料清单关联信息(多清单对应)
  562. * @param {Object} data 工料内容
  563. * @param {int} order 期数
  564. * @return {void}
  565. */
  566. async saves(datas, checklist = false, ms_id = null) {
  567. if (!this.ctx.tender || !this.ctx.material) {
  568. throw '数据错误';
  569. }
  570. const transaction = await this.db.beginTransaction();
  571. try {
  572. const mb_id = datas.mb_id;
  573. const updateDatas = [];
  574. const updateListGcl = [];
  575. const listGcl = [];
  576. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  577. for (const xmj of datas.xmjs) {
  578. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  579. const udata = {
  580. row: {
  581. expr: datas.expr,
  582. quantity: datas.quantity,
  583. },
  584. where: {
  585. tid: this.ctx.tender.id,
  586. mid: this.ctx.material.id,
  587. mb_id,
  588. gcl_id: xmj.gcl_id,
  589. xmj_id: xmj.xmj_id,
  590. mx_id: xmj.mx_id,
  591. },
  592. };
  593. updateDatas.push(udata);
  594. }
  595. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  596. listGcl.push(xmj.gcl_id);
  597. updateListGcl.push({
  598. row: {
  599. expr: datas.expr,
  600. quantity: datas.quantity,
  601. },
  602. where: {
  603. tid: this.ctx.tender.id,
  604. // mid: this.ctx.material.id,
  605. mb_id,
  606. gcl_id: xmj.gcl_id,
  607. },
  608. });
  609. }
  610. }
  611. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  612. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  613. await this.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  614. await transaction.commit();
  615. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  616. return checklist ? gclList : {
  617. gclList,
  618. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  619. };
  620. } catch (err) {
  621. await transaction.rollback();
  622. throw err;
  623. }
  624. }
  625. /**
  626. * 复制粘贴多工料信息(多清单对应)
  627. * @param {Object} data 工料内容
  628. * @return {void}
  629. */
  630. async savePastes(datas, checklist = false, ms_id = null) {
  631. if (!this.ctx.tender || !this.ctx.material) {
  632. throw '数据错误';
  633. }
  634. // 判断是否可修改
  635. // 判断t_type是否为费用
  636. const transaction = await this.db.beginTransaction();
  637. try {
  638. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  639. for (const data of datas.pasteData) {
  640. const updateDatas = [];
  641. const updateListGcl = [];
  642. const listGcl = [];
  643. for (const xmj of datas.xmjs) {
  644. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  645. const udata = {
  646. row: {
  647. expr: data.expr,
  648. quantity: data.quantity,
  649. },
  650. where: {
  651. tid: this.ctx.tender.id,
  652. mid: this.ctx.material.id,
  653. mb_id: data.mb_id,
  654. gcl_id: xmj.gcl_id,
  655. xmj_id: xmj.xmj_id,
  656. mx_id: xmj.mx_id,
  657. },
  658. };
  659. updateDatas.push(udata);
  660. }
  661. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  662. listGcl.push(xmj.gcl_id);
  663. updateListGcl.push({
  664. row: {
  665. expr: data.expr,
  666. quantity: data.quantity,
  667. },
  668. where: {
  669. tid: this.ctx.tender.id,
  670. // mid: this.ctx.material.id,
  671. mb_id: data.mb_id,
  672. gcl_id: xmj.gcl_id,
  673. },
  674. });
  675. }
  676. }
  677. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  678. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  679. await this.calcQuantityByML(transaction, data.mb_id, ms_id, 'all');
  680. }
  681. await transaction.commit();
  682. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  683. return checklist ? gclList : {
  684. gclList,
  685. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  686. };
  687. } catch (err) {
  688. await transaction.rollback();
  689. throw err;
  690. }
  691. }
  692. }
  693. return MaterialList;
  694. };