stage_yjcl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const decimal = { up: 6, qty: 3 };
  11. module.exports = app => {
  12. class StageYjcl extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'stage_yjcl';
  22. }
  23. async getStageData(stage) {
  24. const data = await this.getAllDataByCondition({ where: { sid: stage.id } });
  25. if (!stage.readOnly || stage.status === auditConst.status.checked) return data;
  26. for (const d of data) {
  27. const his = d.shistory ? JSON.parse(d.shistory) : [];
  28. const h = this.ctx.helper._.find(his, {
  29. stimes: stage.curTimes, sorder: stage.curOrder,
  30. });
  31. d.qty = h ? h.qty : 0;
  32. d.tp = h ? h.tp : 0;
  33. }
  34. return data;
  35. }
  36. async _addDatas(data) {
  37. const tpDecimal = this.ctx.tender.info.decimal.extra
  38. ? this.ctx.tender.info.decimal.extraTp
  39. : this.ctx.tender.info.decimal.tp;
  40. const datas = data instanceof Array ? data : [data];
  41. const insertData = [];
  42. for (const d of datas) {
  43. if (!d.name || !d.m_order) throw '新增永久材料,提交的数据错误';
  44. const nd = {
  45. uuid: this.uuid.v4(),
  46. add_sid: this.ctx.stage.id,
  47. add_sorder: this.ctx.stage.order,
  48. add_uid: this.ctx.session.sessionUser.accountId,
  49. tid: this.ctx.tender.id,
  50. sid: this.ctx.stage.id,
  51. sorder: this.ctx.stage.order,
  52. };
  53. nd.name = d.name;
  54. nd.m_order = d.m_order;
  55. nd.spec = d.spec || '';
  56. nd.unit = d.unit || '';
  57. nd.tax = d.tax ? Math.min(Math.max(0, this.ctx.helper.round(d.tax, 0)), 100) : 0;
  58. nd.arrive_time = d.arrive_time || '';
  59. nd.source = d.source || '';
  60. nd.bills_code = d.bills_code || '';
  61. nd.location = d.location || '';
  62. nd.prepare_pos = d.prepare_pos || '';
  63. nd.memo = d.memo || '';
  64. const extraPre = 1 + this.ctx.helper.div(nd.tax, 100, 2);
  65. nd.arrive_qty = d.arrive_qty ? this.ctx.helper.round(d.arrive_qty, decimal.qty) : 0;
  66. nd.arrive_tp = d.arrive_tp ? this.ctx.helper.round(d.arrive_tp, tpDecimal) : 0;
  67. nd.unit_price = d.arrive_qty ? this.ctx.helper.div(d.arrive_tp, d.arrive_qty, decimal.up) : 0;
  68. nd.ex_tax_up = d.arrive_qty ? this.ctx.helper.div(this.ctx.helper.div(d.arrive_tp, d.arrive_qty), extraPre, decimal.up) : 0;
  69. // nd.unit_price = d.unit_price ? this.ctx.helper.round(d.unit_price, decimal.up) : 0;
  70. // const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit);
  71. // if (d.arrive_qty) {
  72. // nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, decimal.qty);
  73. // nd.arrive_tp = this.ctx.helper.mul(this.ctx.helper.mul(nd.unit_price, nd.arrive_qty), extraPre, tpDecimal);
  74. // }
  75. nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
  76. // nd.tp = this.ctx.helper.mul(this.ctx.helper.mul(nd.unit_price, nd.qty), extraPre, tpDecimal);
  77. nd.tp = this.ctx.helper.mul(nd.unit_price, nd.qty, tpDecimal);
  78. insertData.push(nd);
  79. }
  80. await this.db.insert(this.tableName, insertData);
  81. return await this.getAllDataByCondition({
  82. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') },
  83. });
  84. }
  85. async _delDatas(data) {
  86. const datas = data instanceof Array ? data : [data];
  87. const orgDatas = await this.getAllDataByCondition({
  88. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') },
  89. });
  90. for (const od of orgDatas) {
  91. if (od.pre_used) throw '永久材料往期已经计量,不可删除';
  92. }
  93. await this.db.delete(this.tableName, { id: datas });
  94. return datas;
  95. }
  96. async _updateDatas(data) {
  97. const tpDecimal = this.ctx.tender.info.decimal.extra
  98. ? this.ctx.tender.info.decimal.extraTp
  99. : this.ctx.tender.info.decimal.tp;
  100. const datas = data instanceof Array ? data : [data];
  101. const orgDatas = await this.getAllDataByCondition({
  102. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') },
  103. });
  104. const uDatas = [];
  105. for (const d of datas) {
  106. const od = this.ctx.helper._.find(orgDatas, { id: d.id });
  107. if (!od) continue;
  108. const nd = { id: od.id };
  109. if (d.name !== undefined) nd.name = d.name;
  110. if (d.m_order !== undefined) nd.m_order = d.m_order;
  111. if (d.spec !== undefined) nd.spec = d.spec || '';
  112. if (d.unit !== undefined) nd.unit = d.unit || '';
  113. if (d.tax !== undefined) nd.tax = d.tax ? Math.min(Math.max(0, this.ctx.helper.round(d.tax, 0)), 100) : 0;
  114. if (d.arrive_time !== undefined) nd.arrive_time = d.arrive_time || '';
  115. if (d.source !== undefined) nd.source = d.source || '';
  116. if (d.bills_code !== undefined) nd.bills_code = d.bills_code || '';
  117. if (d.location !== undefined) nd.location = d.location || '';
  118. if (d.prepare_pos !== undefined) nd.prepare_pos = d.prepare_pos || '';
  119. if (d.memo !== undefined)nd.memo = d.memo || '';
  120. if (d.arrive_qty !== undefined) nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, decimal.qty);
  121. if (d.arrive_tp !== undefined) nd.arrive_tp = this.ctx.helper.round(d.arrive_tp, tpDecimal);
  122. let unit_price = od.unit_price, ex_tax_up = od.ex_tax_up;
  123. if (nd.arrive_qty !== undefined || nd.arrive_tp !== undefined || nd.tax !== undefined) {
  124. const tp = nd.arrive_tp !== undefined ? nd.arrive_tp : od.arrive_tp;
  125. const qty = nd.arrive_qty !== undefined ? nd.arrive_qty : od.arrive_qty;
  126. const extraPre = nd.tax !== undefined ? 1 + this.ctx.helper.div(nd.tax, 100, 2) : 1 + this.ctx.helper.div(od.tax, 100, 2);
  127. nd.unit_price = qty ? this.ctx.helper.div(tp, qty, decimal.up) : 0;
  128. nd.ex_tax_up = qty ? this.ctx.helper.div(this.ctx.helper.div(tp, qty), extraPre, decimal.up) : 0;
  129. unit_price = nd.unit_price;
  130. ex_tax_up = nd.ex_tax_up;
  131. }
  132. if (d.qty !== undefined) {
  133. nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
  134. } else if (nd.unit_price) {
  135. nd.qty = od.qty;
  136. }
  137. if (nd.qty !== undefined) nd.tp = this.ctx.helper.mul(unit_price, nd.qty, tpDecimal);
  138. uDatas.push(nd);
  139. }
  140. if (uDatas.length > 0) {
  141. await this.db.updateRows(this.tableName, uDatas);
  142. return uDatas;
  143. } else {
  144. return [];
  145. }
  146. }
  147. async updateDatas(data) {
  148. const result = { add: [], del: [], update: [] };
  149. try {
  150. if (data.add) {
  151. result.add = await this._addDatas(data.add);
  152. }
  153. if (data.update) {
  154. result.update = await this._updateDatas(data.update);
  155. }
  156. if (data.del) {
  157. result.del = await this._delDatas(data.del);
  158. }
  159. return result;
  160. } catch (err) {
  161. if (err) result.err = err;
  162. return result;
  163. }
  164. }
  165. async updateHistory4TimesOrder(stage, times, order, transaction) {
  166. const datas = await this.getStageData(stage);
  167. if (datas.length === 0) return;
  168. const updateDatas = [];
  169. for (const d of datas) {
  170. for (const curOrder of order) {
  171. const history = d.shistory ? JSON.parse(d.shistory) : [];
  172. const his = history.find(function (x) {
  173. return x.stimes === times && x.sorder === curOrder;
  174. });
  175. if (his) {
  176. his.qty = d.qty;
  177. his.tp = d.tp;
  178. } else {
  179. history.push({ stimes: times || 1, sorder: curOrder || 0, qty: d.qty, tp: d.tp });
  180. }
  181. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  182. }
  183. }
  184. await transaction.updateRows(this.tableName, updateDatas);
  185. }
  186. async updateHistory(stage, transaction) {
  187. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder], transaction);
  188. }
  189. async updateHistory4CheckAgain(stage, transaction) {
  190. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder + 1], transaction);
  191. }
  192. async addInitialStageData(stage, preStage, transaction) {
  193. if (!stage || !preStage) {
  194. throw '标段数据有误';
  195. }
  196. const preDatas = await this.getAllDataByCondition({
  197. columns: ['uuid', 'add_sid', 'add_sorder', 'add_uid', 'tid', 'name', 'spec', 'm_order', 'unit', 'tax', 'arrive_time', 'source', 'bills_code', 'location', 'prepare_pos', 'memo',
  198. 'arrive_qty', 'arrive_tp', 'unit_price', 'ex_tax_up', 'qty', 'tp', 'pre_qty', 'pre_tp', 'pre_used'],
  199. where: { sid: preStage.id },
  200. });
  201. if (preDatas.length > 0) {
  202. for (const pd of preDatas) {
  203. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.qty);
  204. pd.pre_qty = this.ctx.helper.add(pd.qty, pd.pre_qty);
  205. pd.pre_tp = this.ctx.helper.add(pd.tp, pd.pre_tp);
  206. delete pd.qty;
  207. delete pd.tp;
  208. pd.sid = stage.id;
  209. pd.sorder = stage.order;
  210. }
  211. await transaction.delete(this.tableName, { sid: stage.id });
  212. const result = await transaction.insert(this.tableName, preDatas);
  213. return result.affectedRows === preDatas.length;
  214. } else {
  215. return true;
  216. }
  217. }
  218. async deleteStageTimesData(sid, times, transaction) {
  219. const datas = await this.getAllDataByCondition({ where: { sid } });
  220. if (datas.length === 0) return;
  221. const updateDatas = [];
  222. for (const d of datas) {
  223. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  224. const his = history.filter(x => { return x.stimes && x.stimes < times; });
  225. his.sort(function(x, y) {
  226. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  227. });
  228. const ud = {
  229. id: d.id,
  230. shistory: JSON.stringify(his),
  231. qty: his.length > 0 ? his[his.length - 1].qty : 0,
  232. tp: his.length > 0 ? his[his.length - 1].tp : 0,
  233. };
  234. updateDatas.push(ud);
  235. }
  236. await transaction.updateRows(this.tableName, updateDatas);
  237. }
  238. }
  239. return StageYjcl;
  240. };