stage_yjcl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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) 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 ? this.ctx.helper.round(d.tax, 0) : 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 = this.ctx.helper.round(d.tax, 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) {
  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) nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
  133. if (nd.qty !== undefined) nd.tp = this.ctx.helper.mul(unit_price, nd.qty, tpDecimal);
  134. uDatas.push(nd);
  135. }
  136. if (uDatas.length > 0) {
  137. await this.db.updateRows(this.tableName, uDatas);
  138. return uDatas;
  139. } else {
  140. return [];
  141. }
  142. }
  143. async updateDatas(data) {
  144. const result = { add: [], del: [], update: [] };
  145. try {
  146. if (data.add) {
  147. result.add = await this._addDatas(data.add);
  148. }
  149. if (data.update) {
  150. result.update = await this._updateDatas(data.update);
  151. }
  152. if (data.del) {
  153. result.del = await this._delDatas(data.del);
  154. }
  155. return result;
  156. } catch (err) {
  157. if (err) result.err = err;
  158. return result;
  159. }
  160. }
  161. async updateHistory4TimesOrder(stage, times, order, transaction) {
  162. const datas = await this.getStageData(stage);
  163. if (datas.length === 0) return;
  164. const updateDatas = [];
  165. for (const d of datas) {
  166. for (const curOrder of order) {
  167. const history = d.shistory ? JSON.parse(d.shistory) : [];
  168. const his = history.find(function (x) {
  169. return x.stimes === times && x.sorder === curOrder;
  170. });
  171. if (his) {
  172. his.qty = d.qty;
  173. his.tp = d.tp;
  174. } else {
  175. history.push({ stimes: times || 1, sorder: curOrder || 0, qty: d.qty, tp: d.tp });
  176. }
  177. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  178. }
  179. }
  180. await transaction.updateRows(this.tableName, updateDatas);
  181. }
  182. async updateHistory(stage, transaction) {
  183. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder], transaction);
  184. }
  185. async updateHistory4CheckAgain(stage, transaction) {
  186. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder + 1], transaction);
  187. }
  188. async addInitialStageData(stage, preStage, transaction) {
  189. if (!stage || !preStage) {
  190. throw '标段数据有误';
  191. }
  192. const preDatas = await this.getAllDataByCondition({
  193. columns: ['uuid', 'add_sid', 'add_sorder', 'add_uid', 'tid', 'name', 'm_order', 'unit', 'tax', 'arrive_time', 'source', 'bills_code', 'location', 'prepare_pos', 'memo',
  194. 'arrive_qty', 'arrive_tp', 'unit_price', 'ex_tax_up', 'qty', 'tp', 'pre_qty', 'pre_tp', 'pre_used'],
  195. where: { sid: preStage.id },
  196. });
  197. if (preDatas.length > 0) {
  198. for (const pd of preDatas) {
  199. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.qty);
  200. pd.pre_qty = this.ctx.helper.add(pd.qty, pd.pre_qty);
  201. pd.pre_tp = this.ctx.helper.add(pd.tp, pd.pre_tp);
  202. delete pd.qty;
  203. delete pd.tp;
  204. pd.sid = stage.id;
  205. pd.sorder = stage.order;
  206. }
  207. await transaction.delete(this.tableName, { sid: stage.id });
  208. const result = await transaction.insert(this.tableName, preDatas);
  209. return result.affectedRows === preDatas.length;
  210. } else {
  211. return true;
  212. }
  213. }
  214. async deleteStageTimesData(sid, times, transaction) {
  215. const datas = await this.getAllDataByCondition({ where: { sid } });
  216. if (datas.length === 0) return;
  217. const updateDatas = [];
  218. for (const d of datas) {
  219. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  220. const his = history.filter(x => { return x.stimes && x.stimes < times; });
  221. his.sort(function(x, y) {
  222. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  223. });
  224. const ud = {
  225. id: d.id,
  226. shistory: JSON.stringify(his),
  227. qty: his.length > 0 ? his[his.length - 1].qty : 0,
  228. tp: his.length > 0 ? his[his.length - 1].tp : 0,
  229. };
  230. updateDatas.push(ud);
  231. }
  232. await transaction.updateRows(this.tableName, updateDatas);
  233. }
  234. }
  235. return StageYjcl;
  236. };