material_list.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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 newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mb_id, ms_id);
  216. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, m_spread), this.ctx.material.decimal.tp);
  217. const updateData = {
  218. id: updateId,
  219. quantity: newQuantity,
  220. m_tp: newTp,
  221. 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),
  222. };
  223. if (ms_id) {
  224. await transaction.update(this.ctx.service.materialStageBills.tableName, updateData);
  225. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  226. if (updateAllStage === 'all') {
  227. const updateDatas = [];
  228. const updateMsIds = [];
  229. const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id } });
  230. for (const msb of msbList) {
  231. if (msb.ms_id !== parseInt(ms_id)) {
  232. const newQuantity4 = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mb_id, msb.ms_id);
  233. const newTp4 = this.ctx.helper.round(this.ctx.helper.mul(newQuantity4, msb.m_spread), this.ctx.material.decimal.tp);
  234. const updateData4 = {
  235. id: msb.id,
  236. quantity: newQuantity4,
  237. m_tp: newTp4,
  238. 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),
  239. };
  240. updateDatas.push(updateData4);
  241. updateMsIds.push(msb.ms_id);
  242. }
  243. }
  244. if (updateDatas.length > 0) {
  245. await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateDatas);
  246. for (const msId of updateMsIds) {
  247. await this.ctx.service.materialStage.updateMtp(transaction, msId);
  248. }
  249. }
  250. }
  251. // 还要更新bills表的m_tp和m_tax_tp值
  252. 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';
  253. const sqlParam3 = [this.ctx.tender.id, this.ctx.material.id, mb_id];
  254. const tp3 = await transaction.queryOne(sql3, sqlParam3);
  255. const updateBillsData = {
  256. id: mb_id,
  257. m_tp: tp3.total_price,
  258. m_tax_tp: tp3.tax_total_price,
  259. };
  260. await transaction.update(this.ctx.service.materialBills.tableName, updateBillsData);
  261. } else {
  262. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  263. }
  264. // 计算本期总金额
  265. 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';
  266. const sqlParam2 = [this.ctx.tender.id];
  267. const tp = await transaction.queryOne(sql2, sqlParam2);
  268. console.log(tp);
  269. const updateData2 = {
  270. id: this.ctx.material.id,
  271. m_tp: tp.total_price,
  272. m_tax_tp: tp.tax_total_price,
  273. };
  274. const result = await transaction.update(this.ctx.service.material.tableName, updateData2);
  275. // 找出当前人并更新tp_data
  276. const tp_data = await this.ctx.service.materialAudit.getTpData(transaction, this.ctx.material.id);
  277. if (this.ctx.material.status === auditConst.status.uncheck || this.ctx.material.status === auditConst.status.checkNo) {
  278. await transaction.update(this.ctx.service.material.tableName, {
  279. id: this.ctx.material.id,
  280. tp_data: JSON.stringify(tp_data),
  281. });
  282. } else if (this.ctx.material.curAuditor) {
  283. await transaction.update(this.ctx.service.materialAudit.tableName, {
  284. id: this.ctx.material.curAuditor.id,
  285. tp_data: JSON.stringify(tp_data),
  286. });
  287. }
  288. return result;
  289. }
  290. /**
  291. * 修改material_bills的quantity值和计算本期金额
  292. * @param transaction
  293. * @param mb_id
  294. * @return {Promise<*>}
  295. */
  296. async calcAllQuantityByML(transaction, mbIds) {
  297. const mbList = await transaction.select(this.ctx.service.materialBills.tableName, { where: { id: mbIds } });
  298. // 修改material_bills值
  299. for (const mbInfo of mbList) {
  300. if (!mbInfo) {
  301. throw '不存在该工料';
  302. }
  303. if (this.ctx.material.is_stage_self) {
  304. const updateDatas = [];
  305. const updateMsIds = [];
  306. const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: mbInfo.id } });
  307. for (const msb of msbList) {
  308. const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mbInfo.id, msb.ms_id);
  309. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msb.m_spread), this.ctx.material.decimal.tp);
  310. const updateData = {
  311. id: msb.id,
  312. quantity: newQuantity,
  313. m_tp: newTp,
  314. 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),
  315. };
  316. updateDatas.push(updateData);
  317. updateMsIds.push(msb.ms_id);
  318. }
  319. if (updateDatas.length > 0) {
  320. await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateDatas);
  321. for (const msId of updateMsIds) {
  322. await this.ctx.service.materialStage.updateMtp(transaction, msId);
  323. }
  324. }
  325. // 还要更新bills表的m_tp和m_tax_tp值
  326. 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';
  327. const sqlParam3 = [this.ctx.tender.id, this.ctx.material.id, mbInfo.id];
  328. const tp3 = await transaction.queryOne(sql3, sqlParam3);
  329. const updateBillsData = {
  330. id: mbInfo.id,
  331. m_tp: tp3.total_price,
  332. m_tax_tp: tp3.tax_total_price,
  333. };
  334. await transaction.update(this.ctx.service.materialBills.tableName, updateBillsData);
  335. } else {
  336. const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mbInfo.id);
  337. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  338. const updateData = {
  339. id: mbInfo.id,
  340. quantity: newQuantity,
  341. m_tp: newTp,
  342. 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),
  343. };
  344. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  345. }
  346. }
  347. // 计算本期总金额
  348. 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';
  349. const sqlParam2 = [this.ctx.tender.id];
  350. const tp = await transaction.queryOne(sql2, sqlParam2);
  351. console.log(tp);
  352. const updateData2 = {
  353. id: this.ctx.material.id,
  354. m_tp: tp.total_price,
  355. m_tax_tp: tp.tax_total_price,
  356. };
  357. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  358. }
  359. /**
  360. * 获取工料清单关联表
  361. * @param {int} tid 标段id
  362. * @param {Object} mid 期id
  363. * @return {void}
  364. */
  365. async getMaterialData(tid, mid) {
  366. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`contract_qty`, ml.`qc_qty`, ml.`qc_minus_qty`, ml.`gather_qty`, 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, ml.is_join' +
  367. ' FROM ' + this.tableName + ' as ml' +
  368. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  369. ' ON ml.`mb_id` = mb.`id`' +
  370. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  371. const sqlParam = [tid, mid];
  372. return await this.db.query(sql, sqlParam);
  373. }
  374. async getPreMaterialData(tid, mid) {
  375. 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' +
  376. ' FROM ' + this.tableName + ' as ml' +
  377. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  378. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' as mbh ON ml.`mb_id` = mbh.`mb_id` and mbh.mid = ?' +
  379. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  380. const sqlParam = [mid, tid, mid];
  381. return await this.db.query(sql, sqlParam);
  382. }
  383. async getMaterialStageData(tid, mid) {
  384. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, msb.id AS 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' +
  385. ' FROM ' + this.tableName + ' as ml' +
  386. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  387. ' LEFT JOIN ' + this.ctx.service.materialStageBills.tableName + ' as msb ON ml.mb_id = msb.mb_id AND ml.ms_id = msb.ms_id' +
  388. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' as ms ON ml.`ms_id` = ms.`id`' +
  389. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  390. const sqlParam = [tid, mid];
  391. return await this.db.query(sql, sqlParam);
  392. }
  393. async getPreMaterialStageData(tid, mid) {
  394. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, msb.id AS 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' +
  395. ' FROM ' + this.tableName + ' as ml' +
  396. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  397. ' 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' +
  398. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' as ms ON ml.`ms_id` = ms.`id`' +
  399. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  400. const sqlParam = [tid, mid];
  401. return await this.db.query(sql, sqlParam);
  402. }
  403. /**
  404. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  405. * @param transaction
  406. * @param preMaterial
  407. * @param newMid
  408. * @return {Promise<void>}
  409. */
  410. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  411. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  412. const copyMLArray = [];
  413. for (const ml of materialListData) {
  414. // 获取小计值
  415. let qtys = null;
  416. if (ml.mx_id !== null && ml.mx_id !== '') {
  417. qtys = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  418. } else {
  419. qtys = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  420. }
  421. const newMaterialList = {
  422. tid: ml.tid,
  423. order: ml.order,
  424. mid: newMaterial.id,
  425. mb_id: ml.mb_id,
  426. gcl_id: ml.gcl_id,
  427. xmj_id: ml.xmj_id,
  428. mx_id: ml.mx_id,
  429. contract_qty: qtys ? qtys.contract_qty : null,
  430. qc_qty: qtys ? qtys.qc_qty : null,
  431. qc_minus_qty: qtys ? qtys.qc_minus_qty : null,
  432. gather_qty: qtys ? qtys.gather_qty : null,
  433. quantity: ml.quantity,
  434. expr: ml.expr,
  435. is_join: ml.is_join,
  436. in_time: new Date(),
  437. };
  438. copyMLArray.push(newMaterialList);
  439. }
  440. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  441. }
  442. /**
  443. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  444. * @param transaction
  445. * @param preMaterial
  446. * @param newMid
  447. * @return {Promise<void>}
  448. */
  449. async copyPreMaterialList2(transaction, materialListData, materialSelfListData, notJoinList, newMaterial, materialStageData) {
  450. if (materialListData && materialListData.length > 0) {
  451. const copyMLArray = [];
  452. for (const ml of materialListData) {
  453. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 1 });
  454. const is_change = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 2 });
  455. const newMaterialList = {
  456. tid: newMaterial.tid,
  457. order: ml.order,
  458. mid: newMaterial.id,
  459. mb_id: ml.mb_id,
  460. gcl_id: ml.gcl_id,
  461. xmj_id: ml.xmj_id,
  462. mx_id: ml.mx_id,
  463. contract_qty: ml.contract_qty,
  464. qc_qty: ml.qc_qty,
  465. qc_minus_qty: ml.qc_minus_qty,
  466. gather_qty: ml.gather_qty,
  467. quantity: ml.quantity ? ml.quantity : 0,
  468. expr: ml.expr ? ml.expr : '',
  469. is_join: is_join ? 0 : is_change ? 2 : 1,
  470. in_time: new Date(),
  471. };
  472. if (ml.sid) {
  473. const ms = this._.find(materialStageData, { sid: ml.sid });
  474. if (ms && ms.id) newMaterialList.ms_id = ms.id;
  475. }
  476. copyMLArray.push(newMaterialList);
  477. }
  478. if (copyMLArray.length !== 0) await transaction.insert(this.tableName, copyMLArray);
  479. }
  480. if (materialSelfListData && materialSelfListData.length > 0) {
  481. const copyMLArray2 = [];
  482. for (const ml of materialSelfListData) {
  483. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 1 });
  484. const is_change = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 2 });
  485. const newMaterialList = {
  486. tid: newMaterial.tid,
  487. order: ml.order,
  488. mid: newMaterial.id,
  489. mb_id: ml.mb_id,
  490. gcl_id: ml.gcl_id,
  491. xmj_id: ml.xmj_id,
  492. mx_id: ml.mx_id,
  493. contract_qty: ml.contract_qty,
  494. qc_qty: ml.qc_qty,
  495. qc_minus_qty: ml.qc_minus_qty,
  496. gather_qty: ml.gather_qty,
  497. quantity: ml.quantity ? ml.quantity : 0,
  498. expr: ml.expr ? ml.expr : '',
  499. is_join: is_join ? 0 : is_change ? 2 : 1,
  500. is_self: 1,
  501. in_time: new Date(),
  502. };
  503. if (ml.sid) {
  504. const ms = this._.find(materialStageData, { sid: ml.sid });
  505. if (ms && ms.id) newMaterialList.ms_id = ms.id;
  506. }
  507. copyMLArray2.push(newMaterialList);
  508. }
  509. if (copyMLArray2.length !== 0) await transaction.insert(this.tableName, copyMLArray2);
  510. }
  511. }
  512. /**
  513. * 添加工料清单关联(多清单对应)
  514. * @return {void}
  515. */
  516. async adds(datas, checklist = false) {
  517. if (!this.ctx.tender || !this.ctx.material) {
  518. throw '数据错误';
  519. }
  520. const transaction = await this.db.beginTransaction();
  521. try {
  522. const list = [];
  523. const listGcl = [];
  524. const uplist = [];
  525. const uplistGcl = [];
  526. // const delList = [];
  527. // const mb_idList = [];
  528. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  529. const oldGclList = await transaction.select(this.ctx.service.materialListGcl.tableName, { where: { tid: this.ctx.tender.id } });
  530. const oldMaterialList = await transaction.select(this.ctx.service.materialList.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  531. for (const xmj of datas.xmjs) {
  532. for (const mb of datas.mbIds) {
  533. // // 旧数据兼容问题,要去删除相同已存在的工料
  534. // const mlInfo = await this.getDataByCondition({
  535. // mid: this.ctx.material.id,
  536. // gcl_id: xmj.gcl_id,
  537. // xmj_id: xmj.xmj_id,
  538. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  539. // mb_id: mb,
  540. // });
  541. // if (mlInfo) {
  542. // delList.push(mlInfo.id);
  543. // mb_idList.push(mb);
  544. // }
  545. const mbId = typeof mb === 'object' ? mb.id : mb;
  546. const quantity = typeof mb === 'object' ? mb.quantity : 0;
  547. if (xmj.gather_qty && this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  548. const mlInfo = this._.find(oldMaterialList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id, mb_id: mbId, ms_id: xmj.ms_id ? xmj.ms_id : null });
  549. if (mlInfo) {
  550. uplist.push({ id: mlInfo.id, quantity, expr: '' });
  551. } else {
  552. const newLists = {
  553. tid: this.ctx.tender.id,
  554. order: this.ctx.material.order,
  555. mid: this.ctx.material.id,
  556. mb_id: mbId,
  557. gcl_id: xmj.gcl_id,
  558. xmj_id: xmj.xmj_id,
  559. mx_id: xmj.mx_id,
  560. ms_id: xmj.ms_id ? xmj.ms_id : null,
  561. contract_qty: xmj.contract_qty,
  562. qc_qty: xmj.qc_qty,
  563. qc_minus_qty: xmj.qc_minus_qty,
  564. gather_qty: xmj.gather_qty,
  565. quantity,
  566. in_time: new Date(),
  567. is_join: xmj.is_join,
  568. };
  569. list.push(newLists);
  570. }
  571. }
  572. const gclIndex = this._.findIndex(oldGclList, { gcl_id: xmj.gcl_id, mb_id: mbId });
  573. if (gclIndex === -1 && this._.findIndex(listGcl, { gcl_id: xmj.gcl_id, mb_id: mbId }) === -1) {
  574. const newListGcl = {
  575. tid: this.ctx.tender.id,
  576. order: this.ctx.material.order,
  577. mid: this.ctx.material.id,
  578. mb_id: mbId,
  579. gcl_id: xmj.gcl_id,
  580. quantity,
  581. expr: '',
  582. };
  583. listGcl.push(newListGcl);
  584. } else if (gclIndex !== -1 && this._.findIndex(uplistGcl, { id: oldGclList[gclIndex].id }) === -1) {
  585. uplistGcl.push({
  586. id: oldGclList[gclIndex].id,
  587. expr: '',
  588. quantity,
  589. });
  590. }
  591. }
  592. }
  593. // 维护list_gcl表
  594. // 删除工料清单关联
  595. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  596. // 新增工料清单关联
  597. if (list.length > 0) {
  598. const result = await transaction.insert(this.tableName, list);
  599. if (result.affectedRows === 0) {
  600. throw '新增工料数据失败';
  601. }
  602. }
  603. if (listGcl.length > 0) {
  604. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  605. if (result2.affectedRows === 0) {
  606. throw '新增工料关联数据失败';
  607. }
  608. }
  609. // 覆盖
  610. if (uplist.length > 0) {
  611. await transaction.updateRows(this.tableName, uplist);
  612. }
  613. if (uplistGcl.length > 0) {
  614. await transaction.updateRows(this.ctx.service.materialListGcl.tableName, uplistGcl);
  615. }
  616. if (checklist) {
  617. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  618. }
  619. // 重算工料和总金额
  620. // const calcMBIdList = this._.uniq(mb_idList);
  621. // if (calcMBIdList.length > 0) {
  622. // for (const select of calcMBIdList) {
  623. // await this.calcQuantityByML(transaction, select);
  624. // }
  625. // }
  626. if ((list.length > 0 || uplist.length > 0) && datas.export) {
  627. await this.calcAllQuantityByML(transaction, this._.map(datas.mbIds, 'id'));
  628. }
  629. await transaction.commit();
  630. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  631. return checklist ? gclList : {
  632. gclList,
  633. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  634. };
  635. } catch (err) {
  636. await transaction.rollback();
  637. throw err;
  638. }
  639. }
  640. /**
  641. * 删除工料清单关联(多清单对应)
  642. * @param {int} id 工料id
  643. * @return {void}
  644. */
  645. async dels(datas, checklist = false, fromCheckList = false, ms_id = null) {
  646. if (!this.ctx.tender || !this.ctx.material) {
  647. throw '数据错误';
  648. }
  649. const transaction = await this.db.beginTransaction();
  650. try {
  651. // 判断是否可删
  652. const listGcl = [];
  653. for (const xmj of datas.xmjs) {
  654. 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 });
  655. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  656. 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 });
  657. listGcl.push(xmj.gcl_id);
  658. }
  659. }
  660. // await transaction.delete(this.tableName, { id });
  661. await this.calcQuantityByML(transaction, datas.mb_id, ms_id, 'all');
  662. if (checklist) {
  663. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  664. }
  665. await transaction.commit();
  666. // console.log(datas);
  667. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  668. return fromCheckList ? gclList : {
  669. gclList,
  670. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  671. };
  672. } catch (err) {
  673. await transaction.rollback();
  674. throw err;
  675. }
  676. }
  677. /**
  678. * 修改工料清单关联信息(多清单对应)
  679. * @param {Object} data 工料内容
  680. * @param {int} order 期数
  681. * @return {void}
  682. */
  683. async saves(datas, checklist = false, ms_id = null) {
  684. if (!this.ctx.tender || !this.ctx.material) {
  685. throw '数据错误';
  686. }
  687. const transaction = await this.db.beginTransaction();
  688. try {
  689. const mb_id = datas.mb_id;
  690. const updateDatas = [];
  691. const updateListGcl = [];
  692. const listGcl = [];
  693. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  694. for (const xmj of datas.xmjs) {
  695. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  696. const udata = {
  697. row: {
  698. expr: datas.expr,
  699. quantity: datas.quantity,
  700. },
  701. where: {
  702. tid: this.ctx.tender.id,
  703. mid: this.ctx.material.id,
  704. mb_id,
  705. gcl_id: xmj.gcl_id,
  706. xmj_id: xmj.xmj_id,
  707. mx_id: xmj.mx_id,
  708. },
  709. };
  710. updateDatas.push(udata);
  711. }
  712. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  713. listGcl.push(xmj.gcl_id);
  714. updateListGcl.push({
  715. row: {
  716. expr: datas.expr,
  717. quantity: datas.quantity,
  718. },
  719. where: {
  720. tid: this.ctx.tender.id,
  721. // mid: this.ctx.material.id,
  722. mb_id,
  723. gcl_id: xmj.gcl_id,
  724. },
  725. });
  726. }
  727. }
  728. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  729. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  730. await this.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  731. await transaction.commit();
  732. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  733. return checklist ? gclList : {
  734. gclList,
  735. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  736. };
  737. } catch (err) {
  738. await transaction.rollback();
  739. throw err;
  740. }
  741. }
  742. /**
  743. * 复制粘贴多工料信息(多清单对应)
  744. * @param {Object} data 工料内容
  745. * @return {void}
  746. */
  747. async savePastes(datas, checklist = false, ms_id = null) {
  748. if (!this.ctx.tender || !this.ctx.material) {
  749. throw '数据错误';
  750. }
  751. // 判断是否可修改
  752. // 判断t_type是否为费用
  753. const transaction = await this.db.beginTransaction();
  754. try {
  755. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  756. for (const data of datas.pasteData) {
  757. const updateDatas = [];
  758. const updateListGcl = [];
  759. const listGcl = [];
  760. for (const xmj of datas.xmjs) {
  761. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  762. const udata = {
  763. row: {
  764. expr: data.expr,
  765. quantity: data.quantity,
  766. },
  767. where: {
  768. tid: this.ctx.tender.id,
  769. mid: this.ctx.material.id,
  770. mb_id: data.mb_id,
  771. gcl_id: xmj.gcl_id,
  772. xmj_id: xmj.xmj_id,
  773. mx_id: xmj.mx_id,
  774. },
  775. };
  776. updateDatas.push(udata);
  777. }
  778. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  779. listGcl.push(xmj.gcl_id);
  780. updateListGcl.push({
  781. row: {
  782. expr: data.expr,
  783. quantity: data.quantity,
  784. },
  785. where: {
  786. tid: this.ctx.tender.id,
  787. // mid: this.ctx.material.id,
  788. mb_id: data.mb_id,
  789. gcl_id: xmj.gcl_id,
  790. },
  791. });
  792. }
  793. }
  794. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  795. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  796. await this.calcQuantityByML(transaction, data.mb_id, ms_id, 'all');
  797. }
  798. await transaction.commit();
  799. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  800. return checklist ? gclList : {
  801. gclList,
  802. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  803. };
  804. } catch (err) {
  805. await transaction.rollback();
  806. throw err;
  807. }
  808. }
  809. async getMbQuantity(transaction, mid, qty_source, qty_decimal, mb_id, ms_id = null, needRound = 1) {
  810. const msSql = ms_id ? ' AND `ms_id` = ' + ms_id : '';
  811. const sql = 'SELECT SUM(' + this.ctx.helper.getQtySource(qty_source) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?' + msSql + ' AND `is_join`=1';
  812. const sqlParam = [mid, mb_id];
  813. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  814. const sql2 = 'SELECT SUM(' + this.ctx.helper.getQtySource(qty_source, 1) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?' + msSql + ' AND `is_join`=2';
  815. const sqlParam2 = [mid, mb_id];
  816. const mb_quantity2 = await transaction.queryOne(sql2, sqlParam2);
  817. const newQuantity = this.ctx.helper.add(mb_quantity.quantity, mb_quantity2.quantity);
  818. return needRound ? this.ctx.helper.round(newQuantity, qty_decimal) : newQuantity;
  819. }
  820. }
  821. return MaterialList;
  822. };