material_month.js 12 KB

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