material_month.js 14 KB

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