material_list.js 43 KB

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