material_month.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. const materialConst = require('../const/material');
  11. const MaterialCalculator = require('../lib/material_calc');
  12. module.exports = app => {
  13. class MaterialMonth extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'material_month';
  23. }
  24. /**
  25. * 返回月信息价列表
  26. * @return {void}
  27. */
  28. async getListByMid(mid, transaction = null) {
  29. return transaction ? await transaction.select(this.tableName, { where: { mid } }) : await this.getAllDataByCondition({ where: { mid } });
  30. }
  31. /**
  32. * 添加月信息价 并更新 工料平均单价、本期单价,调差金额等
  33. * @return {void}
  34. */
  35. async adds(data, monthList, mbList) {
  36. if (!this.ctx.tender || !this.ctx.material) {
  37. throw '数据错误';
  38. }
  39. const transaction = await this.db.beginTransaction();
  40. try {
  41. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  42. material_month.push(...data.yearmonths);
  43. material_month.sort();
  44. if (mbList.length !== 0) {
  45. const insertArray = [];
  46. const updateArray = [];
  47. for (const mb of mbList) {
  48. for (const ym of data.yearmonths) {
  49. const one_month = {
  50. tid: this.ctx.tender.id,
  51. mid: this.ctx.material.id,
  52. mb_id: mb.id,
  53. msg_tp: monthList.length !== 0 ? null : mb.msg_tp,
  54. yearmonth: ym,
  55. };
  56. insertArray.push(one_month);
  57. }
  58. if (monthList.length !== 0) {
  59. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  60. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.concat(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), insertArray[0].msg_tp), [null, '', 0]);
  61. const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(this.ctx.helper.add(mb_msg_tp_sum, insertArray[0].msg_tp), month_num), this.ctx.material.decimal.up);
  62. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  63. const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), this.ctx.material.decimal.tp);
  64. updateArray.push({
  65. id: mb.id,
  66. msg_tp: new_msg_tp,
  67. msg_spread: newmsg_spread,
  68. m_spread: newm_spread,
  69. m_tp: newTp,
  70. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), this.ctx.material.decimal.tp),
  71. });
  72. }
  73. }
  74. if (insertArray.length !== 0) await transaction.insert(this.tableName, insertArray);
  75. if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
  76. }
  77. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
  78. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  79. await transaction.commit();
  80. this.ctx.material.months = material_month.join(',');
  81. return m_tp;
  82. } catch (err) {
  83. await transaction.rollback();
  84. throw err;
  85. }
  86. }
  87. /**
  88. * 删除月信息价 并更新 工料平均单价、本期单价,调差金额等
  89. * @return {void}
  90. */
  91. async del(data, monthList, mbList) {
  92. if (!this.ctx.tender || !this.ctx.material) {
  93. throw '数据错误';
  94. }
  95. const transaction = await this.db.beginTransaction();
  96. try {
  97. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  98. this._.remove(material_month, function(n) {
  99. return data.indexOf(n) !== -1;
  100. });
  101. await transaction.delete(this.tableName, { mid: this.ctx.material.id, yearmonth: data });
  102. if (mbList.length !== 0) {
  103. const updateArray = [];
  104. for (const mb of mbList) {
  105. this._.remove(monthList, function(m) {
  106. return data.indexOf(m.yearmonth) !== -1;
  107. });
  108. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  109. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]);
  110. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), this.ctx.material.decimal.up) : null;
  111. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  112. const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), this.ctx.material.decimal.tp);
  113. updateArray.push({
  114. id: mb.id,
  115. msg_tp: new_msg_tp,
  116. msg_spread: newmsg_spread,
  117. m_spread: newm_spread,
  118. m_tp: newTp,
  119. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), this.ctx.material.decimal.tp),
  120. });
  121. }
  122. if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
  123. }
  124. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
  125. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  126. await transaction.commit();
  127. this.ctx.material.months = material_month.join(',');
  128. return m_tp;
  129. } catch (err) {
  130. await transaction.rollback();
  131. throw err;
  132. }
  133. }
  134. /**
  135. * 修改月信息价值 并更新 本期单价,调差金额等
  136. * @return {void}
  137. */
  138. async save(data) {
  139. if (!this.ctx.tender || !this.ctx.material) {
  140. throw '数据错误';
  141. }
  142. const transaction = await this.db.beginTransaction();
  143. try {
  144. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  145. await transaction.update(this.tableName, { msg_tp: data.value }, { where: { mb_id: data.mb_id, yearmonth: data.yearmonth, mid: this.ctx.material.id } });
  146. const monthList = await transaction.select(this.tableName, { where: { mb_id: data.mb_id, mid: this.ctx.material.id } });
  147. const mbInfo = await transaction.get(this.ctx.service.materialBills.tableName, { id: data.mb_id });
  148. if (monthList.length !== 0) {
  149. const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
  150. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
  151. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), this.ctx.material.decimal.up) : null;
  152. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp);
  153. const newTp = this.ctx.helper.round(this.ctx.helper.mul(mbInfo.quantity, newm_spread), this.ctx.material.decimal.tp);
  154. await transaction.update(this.ctx.service.materialBills.tableName, {
  155. id: mbInfo.id,
  156. msg_tp: new_msg_tp,
  157. msg_spread: newmsg_spread,
  158. m_spread: newm_spread,
  159. m_tp: newTp,
  160. 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),
  161. });
  162. }
  163. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  164. await transaction.commit();
  165. return m_tp;
  166. } catch (err) {
  167. await transaction.rollback();
  168. throw err;
  169. }
  170. }
  171. /**
  172. * 修改多个月信息价值 并更新 本期单价,调差金额等
  173. * @return {void}
  174. */
  175. async saveDatas(datas, mbList) {
  176. if (!this.ctx.tender || !this.ctx.material) {
  177. throw '数据错误';
  178. }
  179. const transaction = await this.db.beginTransaction();
  180. try {
  181. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  182. const updateArray = [];
  183. for (const data of datas) {
  184. for (const m of material_month) {
  185. const one_update = {
  186. row: {
  187. msg_tp: data[m],
  188. },
  189. where: {
  190. mb_id: data.mb_id,
  191. yearmonth: m,
  192. },
  193. };
  194. updateArray.push(one_update);
  195. }
  196. }
  197. await transaction.updateRows(this.tableName, updateArray);
  198. const monthList = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
  199. if (mbList.length !== 0) {
  200. const mbUpdateArray = [];
  201. for (const mb of mbList) {
  202. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  203. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]);
  204. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), this.ctx.material.decimal.up) : null;
  205. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  206. const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), this.ctx.material.decimal.tp);
  207. mbUpdateArray.push({
  208. id: mb.id,
  209. msg_tp: new_msg_tp,
  210. msg_spread: newmsg_spread,
  211. m_spread: newm_spread,
  212. m_tp: newTp,
  213. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), this.ctx.material.decimal.tp),
  214. });
  215. }
  216. if (mbUpdateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, mbUpdateArray);
  217. }
  218. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  219. await transaction.commit();
  220. return m_tp;
  221. } catch (err) {
  222. await transaction.rollback();
  223. throw err;
  224. }
  225. }
  226. async getMonthList(materialBillsData, transaction = null) {
  227. // 取月信息价表
  228. const monthsList = [];
  229. if (this.ctx.material.months) {
  230. const material_month = this.ctx.material.months.split(',');
  231. material_month.sort();
  232. const materialMonthList = await this.getListByMid(this.ctx.material.id, transaction);
  233. for (const mbd of materialBillsData) {
  234. const one_mb = {
  235. code: mbd.code,
  236. name: mbd.name,
  237. unit: mbd.unit,
  238. origin: mbd.origin,
  239. mb_id: mbd.id,
  240. order: mbd.order,
  241. };
  242. for (const m of material_month) {
  243. const mb_id = this.ctx.material.highOrder !== this.ctx.material.order ? mbd.mb_id : mbd.id;
  244. const one_mm = this._.find(materialMonthList, { mb_id, yearmonth: m });
  245. one_mb[m] = one_mm.msg_tp;
  246. }
  247. monthsList.push(one_mb);
  248. }
  249. }
  250. return monthsList;
  251. }
  252. }
  253. return MaterialMonth;
  254. };