pos.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. 'use strict';
  2. /**
  3. * 部位明细
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Pos extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.depart = ctx.app.config.table_depart.heavy;
  20. this.tableName = 'pos';
  21. }
  22. async getPosData(condition, column) {
  23. if (!condition.tid) throw '查询计量单元缺少必要信息';
  24. return await this.db.select(this.departTableName(condition.tid), {
  25. where: condition,
  26. columns: column || ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty',
  27. 'qtcl_qty', 'in_time', 'porder', 'add_stage', 'add_stage_order', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty', 'ex_qty1',
  28. 'ex_memo1', 'ex_memo2', 'ex_memo3'],
  29. order: [['porder', 'ASC']],
  30. });
  31. }
  32. async getPosDataWithAddStageOrder(condition) {
  33. if (!condition.tid) throw '查询计量单元缺少必要信息';
  34. const sql = 'SELECT id, tid, lid, name, quantity, position, drawing_code,' +
  35. ' sgfh_qty, sjcl_qty, qtcl_qty, porder, add_stage, add_times, add_user, add_stage_order,' +
  36. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty, ex_qty1, ex_memo1, ex_memo2, ex_memo3' +
  37. ' FROM ' + this.departTableName(condition.tid) + this.ctx.helper.whereSql(condition);
  38. return await this.db.query(sql);
  39. }
  40. async getPosDataByIds(tid, ids) {
  41. if (ids instanceof Array && ids.length > 0) {
  42. const sql = 'SELECT id, tid, lid, name, quantity, position, drawing_code,' +
  43. ' sgfh_qty, sjcl_qty, qtcl_qty, add_stage, add_times, add_user, sgfh_expr, sjcl_expr, qtcl_expr, real_qty, ex_qty1,' +
  44. ' ex_memo1, ex_memo2, ex_memo3' +
  45. ' FROM ' + this.departTableName(tid) +
  46. ' WHERE id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')';
  47. return await this.db.query(sql, []);
  48. } else {
  49. return [];
  50. }
  51. // this.initSqlBuilder();
  52. // this.sqlBuilder.setAndWhere('id', {
  53. // operate: 'in',
  54. // value: ids
  55. // });
  56. // this.sqlBuilder.columns = ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'];
  57. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName)
  58. // return await this.db.query(sql, sqlParam);
  59. }
  60. async getPosDataByUnits(tenderId, units) {
  61. const sql = 'SELECT p.id, p.lid, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.ex_qty1' +
  62. ' FROM ' + this.departTableName(tenderId) + ' p' +
  63. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' b' +
  64. ' ON p.lid = b.id ' +
  65. ' WHERE p.tid = ? and b.unit IN (?)';
  66. const sqlParam = [tenderId, units];
  67. return await this.db.query(sql, sqlParam);
  68. }
  69. async _insertPosData(transaction, data, tid) {
  70. data.id = this.uuid.v4();
  71. data.tid = tid;
  72. // todo 新增期
  73. data.add_stage = 0;
  74. data.add_stage_order = 0;
  75. data.add_times = 0;
  76. data.in_time = new Date();
  77. data.add_user = this.ctx.session.sessionUser.accountId;
  78. if (data.quantity) {
  79. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  80. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  81. data.quantity = this.round(data.quantity, precision.value);
  82. }
  83. if (transaction) {
  84. const addRst = await transaction.insert(this.tableName, data);
  85. } else {
  86. const addRst = await this.db.insert(this.tableName, data);
  87. }
  88. }
  89. async _completeInsertPosData(tid, data) {
  90. data.id = this.uuid.v4();
  91. data.tid = tid;
  92. // todo 新增期
  93. data.add_stage = 0;
  94. data.add_stage_order = 0;
  95. data.add_times = 0;
  96. data.in_time = new Date();
  97. data.add_user = this.ctx.session.sessionUser.accountId;
  98. }
  99. async _getUpdateBills(data) {
  100. const datas = data instanceof Array ? data : [data];
  101. const bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  102. const billsPos = await this.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  103. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  104. const updateBills = {id: bills.id, ledger_id: bills.ledger_id};
  105. for (const bp of billsPos) {
  106. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  107. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  108. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  109. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  110. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  111. }
  112. for (const d of datas) {
  113. if (d.sgfh_qty) {
  114. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  115. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  116. }
  117. if (d.sjcl_qty) {
  118. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  119. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  120. }
  121. if (d.qtcl_qty) {
  122. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  123. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  124. }
  125. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  126. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  127. if (d.ex_qty1) {
  128. d.ex_qty1 = this.ctx.helper.round(d.ex_qty1, precision.value);
  129. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, d.ex_qty1);
  130. }
  131. }
  132. const info = this.ctx.tender.info;
  133. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  134. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  135. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  136. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  137. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  138. return updateBills;
  139. }
  140. async _addPosData(tid, data) {
  141. let updateBills = null;
  142. if (data instanceof Array) {
  143. for (const d of data) {
  144. this._completeInsertPosData(tid, d);
  145. }
  146. if (data[0].sgfh_qty !== undefined || data[0].sjcl_qty !== undefined || data[0].qtcl_qty !== undefined || data[0].ex_qty1 !== undefined) {
  147. updateBills = await this._getUpdateBills(data);
  148. }
  149. } else {
  150. this._completeInsertPosData(tid, data);
  151. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined || data.ex_qty1 !== undefined) {
  152. updateBills = await this._getUpdateBills(data);
  153. }
  154. }
  155. if (updateBills) {
  156. const transaction = await this.db.beginTransaction();
  157. try {
  158. await transaction.insert(this.tableName, data);
  159. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  160. await transaction.commit();
  161. } catch (err) {
  162. await transaction.rollback();
  163. throw err;
  164. }
  165. return {
  166. ledger: { update: [updateBills] },
  167. pos: data,
  168. }
  169. } else {
  170. await this.db.insert(this.tableName, data);
  171. return { pos: data }
  172. }
  173. }
  174. async _updatePosData(tid, data) {
  175. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined || data.ex_qty1 !== undefined) {
  176. const op = await this.getDataById(data.id);
  177. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  178. if (!bills) throw '数据错误';
  179. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  180. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  181. if (data.sgfh_qty !== undefined) {
  182. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  183. } else if (op) {
  184. data.sgfh_qty = op.sgfh_qty;
  185. }
  186. if (data.sjcl_qty !== undefined) {
  187. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  188. } else if (op) {
  189. data.sjcl_qty = op.sjcl_qty;
  190. }
  191. if (data.qtcl_qty !== undefined) {
  192. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  193. } else if (op) {
  194. data.qtcl_qty = op.qtcl_qty;
  195. }
  196. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  197. if (data.ex_qty1 !== undefined) {
  198. data.ex_qty1 = this.round(data.ex_qty1, precision.value);
  199. } else if (op) {
  200. data.ex_qty1 = op.ex_qty1;
  201. }
  202. const updateBills = {id: bills.id};
  203. for (const bp of billsPos) {
  204. const calcData = bp.id === data.id ? data : bp;
  205. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  206. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  207. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  208. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  209. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, calcData.ex_qty1);
  210. }
  211. const info = this.ctx.tender.info;
  212. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  213. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  214. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  215. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  216. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  217. const transaction = await this.db.beginTransaction();
  218. try {
  219. await transaction.update(this.tableName, data);
  220. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  221. await transaction.commit();
  222. updateBills.ledger_id = bills.ledger_id;
  223. return {
  224. ledger: { update: [updateBills] },
  225. pos: data,
  226. }
  227. } catch(err) {
  228. await transaction.rollback();
  229. throw err;
  230. }
  231. } else {
  232. await this.db.update(this.tableName, data);
  233. return {pos: data};
  234. }
  235. }
  236. async _updatePosDatas(tid, data) {
  237. if (data.length === 0) return;
  238. const op = await this.getDataById(data[0].id);
  239. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  240. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  241. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  242. let needUpdateBills;
  243. for (const d of data) {
  244. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined || d.ex_qty1 !== undefined) {
  245. if (d.sgfh_qty !== undefined) {
  246. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  247. } else if (op) {
  248. d.sgfh_qty = op.sgfh_qty;
  249. }
  250. if (d.sjcl_qty !== undefined) {
  251. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  252. } else if (op) {
  253. d.sjcl_qty = op.sjcl_qty;
  254. }
  255. if (d.qtcl_qty !== undefined) {
  256. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  257. } else if (op) {
  258. d.qtcl_qty = op.qtcl_qty;
  259. }
  260. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  261. if (d.ex_qty1 !== undefined) {
  262. d.ex_qty1 = this.round(d.ex_qty1, precision.value);
  263. } else if (op) {
  264. d.ex_qty1 = op.ex_qty1;
  265. }
  266. needUpdateBills = true;
  267. }
  268. }
  269. const updateBills = {id: bills.id};
  270. if (needUpdateBills) {
  271. for (const bp of billsPos) {
  272. const newPos = data.find(function (x) { return x.id === bp.id });
  273. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  274. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  275. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  276. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  277. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  278. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  279. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  280. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  281. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  282. updateBills.quantity = newPos && newPos.quantity !== undefined
  283. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  284. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  285. updateBills.ex_qty1 = newPos && newPos.ex_qty1 !== undefined
  286. ? this.ctx.helper.add(updateBills.ex_qty1, newPos.ex_qty1)
  287. : this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  288. }
  289. const info = this.ctx.tender.info;
  290. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  291. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  292. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  293. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  294. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  295. }
  296. const transaction = await this.db.beginTransaction();
  297. try {
  298. for (const d of data) {
  299. await transaction.update(this.tableName, d);
  300. }
  301. if (needUpdateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  302. await transaction.commit();
  303. updateBills.ledger_id = bills.ledger_id;
  304. return {
  305. ledger: { update: [updateBills] },
  306. pos: data,
  307. }
  308. } catch(err) {
  309. await transaction.rollback();
  310. throw err;
  311. }
  312. }
  313. async _deletePosData(tid, data) {
  314. if (!data || data.length === 0) throw '提交数据错误';
  315. const pos = await this.getPosData({tid: tid, id: data});
  316. if (!pos || pos.length === 0) throw '删除的计量单元不存在';
  317. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  318. const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  319. const updateBills = {id: bills.id, sgfh_qty: 0, sjcl_qty: 0, qtcl_qty: 0, quantity: 0, ex_qty1: 0};
  320. for (const bp of billsPos) {
  321. if (data.indexOf(bp.id) >= 0) continue;
  322. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  323. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  324. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  325. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  326. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  327. }
  328. const info = this.ctx.tender.info;
  329. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  330. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  331. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  332. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  333. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  334. const transaction = await this.db.beginTransaction();
  335. try {
  336. await transaction.delete(this.tableName, {tid: tid, id: data});
  337. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  338. await this.ctx.service.posCalcDetail.deletePosPartData(transaction, tid, data);
  339. await this.ctx.service.ancillaryGcl.deletePosPartData(transaction, tid, data);
  340. await this.ctx.service.ancillaryGclDetail.deletePosPartData(transaction, tid, data);
  341. await transaction.commit();
  342. updateBills.ledger_id = bills.ledger_id;
  343. return { ledger: { update: [updateBills] }, pos: data };
  344. } catch(err) {
  345. await transaction.rollback();
  346. throw err;
  347. }
  348. }
  349. async _insertPosData(tid, data) {
  350. if (!data.length) return;
  351. let porder = data[0].porder, lid = data[0].lid;
  352. for (const d of data) {
  353. this._completeInsertPosData(tid, d);
  354. porder = Math.min(porder, d.porder);
  355. }
  356. const transaction = await this.db.beginTransaction();
  357. try {
  358. this.initSqlBuilder();
  359. this.sqlBuilder.setAndWhere('lid', {
  360. value: this.db.escape(lid),
  361. operate: '=',
  362. });
  363. this.sqlBuilder.setAndWhere('porder', {
  364. value: porder,
  365. operate: '>=',
  366. });
  367. this.sqlBuilder.setUpdateData('porder', {
  368. value: data.length,
  369. selfOperate: '+',
  370. });
  371. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  372. const result = await transaction.query(sql, sqlParam);
  373. await transaction.insert(this.tableName, data);
  374. await transaction.commit();
  375. } catch (err) {
  376. await transaction.rollback();
  377. throw err;
  378. }
  379. this.initSqlBuilder();
  380. this.sqlBuilder.setAndWhere('lid', {
  381. value: this.db.escape(lid),
  382. operate: '=',
  383. });
  384. this.sqlBuilder.setAndWhere('porder', {
  385. value: porder,
  386. operate: '>=',
  387. });
  388. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  389. const update = await this.db.query(sql, sqlParam);
  390. return { pos: update }
  391. }
  392. _calcExpr(data, field, expr, defaultValue, precision) {
  393. if (expr) {
  394. try {
  395. data[field] = this.ctx.helper.round(this.ctx.helper.calcExpr(expr), precision.value);
  396. } catch (err) {
  397. }
  398. } else {
  399. data[field] = this.ctx.helper.round(defaultValue, precision.value);
  400. }
  401. }
  402. async pasteBlockData(tid, data) {
  403. if (!(data.block instanceof Array)) throw '提交数据错误';
  404. const result = { ledger: {}, pos: null };
  405. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  406. const user_id = this.ctx.session.sessionUser.accountId;
  407. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  408. const le = await this.ctx.service.ledgerExtra.getDataById(data.lid);
  409. const calcTemplate = le && le.calc_template ? await this.ctx.service.calcTmpl.getTemplate(le.calc_template) : null;
  410. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  411. const existPos = await this.ctx.service.pos.getAllDataByCondition({ where : { lid: bills.id }, orders: [['porder', 'DESC']]});
  412. const maxOrder = existPos.length > 0 ? existPos[0].porder : 0;
  413. const updateBills = existPos.length > 0
  414. ? { id: bills.id, sgfh_qty: bills.sgfh_qty, sjcl_qty: bills.sjcl_qty, qtcl_qty: bills.qtcl_qty, quantity: bills.quantity, ex_qty1: bills.ex_qty1 }
  415. : { id: bills.id, sgfh_qty: 0, sjcl_qty: 0, qtcl_qty: 0, quantity: 0, ex_qty1: 0 };
  416. const insertPos = [], insertDetail = [], insertAncGcl = [], insertAncGclDetail = [];
  417. for (const [i, b] of data.block.entries()) {
  418. const nip = {};
  419. insertPos.push(nip);
  420. this._completeInsertPosData(tid, nip);
  421. nip.lid = b.lid;
  422. nip.name = b.name || '';
  423. nip.drawing_code = b.drawing_code || '';
  424. nip.porder = maxOrder + i + 1;
  425. nip.position = b.position;
  426. nip.sgfh_expr = b.sgfh_expr ? b.sgfh_expr : '';
  427. nip.sjcl_expr = b.sjcl_expr ? b.sjcl_expr : '';
  428. nip.qtcl_expr = b.qtcl_expr ? b.qtcl_expr : '';
  429. nip.ex_memo1 = b.ex_memo1;
  430. nip.ex_memo2 = b.ex_memo2;
  431. nip.ex_memo3 = b.ex_memo3;
  432. if (calcTemplate) {
  433. let sumQty = 0;
  434. for (const cd of b.calcDetail) {
  435. const newDetail = {
  436. id: this.uuid.v4(), tid: tid, lid: bills.id, pid: nip.id,
  437. create_user_id: this.ctx.session.sessionUser.accountId,
  438. update_user_id: this.ctx.session.sessionUser.accountId,
  439. pcd_order: cd.pcd_order,
  440. };
  441. if (data.calc_template === le.calc_template) {
  442. newDetail.str1 = cd.str1 || '';
  443. newDetail.str2 = cd.str2 || '';
  444. newDetail.str3 = cd.str3 || '';
  445. newDetail.str4 = cd.str4 || '';
  446. newDetail.num_a = cd.num_a || 0;
  447. newDetail.num_b = cd.num_b || 0;
  448. newDetail.num_c = cd.num_c || 0;
  449. newDetail.num_d = cd.num_d || 0;
  450. newDetail.num_e = cd.num_e || 0;
  451. newDetail.num_f = cd.num_f || 0;
  452. newDetail.num_g = cd.num_g || 0;
  453. newDetail.num_h = cd.num_h || 0;
  454. newDetail.num_i = cd.num_i || 0;
  455. newDetail.num_j = cd.num_j || 0;
  456. newDetail.num_k = cd.num_k || 0;
  457. newDetail.num_l = cd.num_l || 0;
  458. newDetail.num_m = cd.num_m || 0;
  459. newDetail.num_n = cd.num_n || 0;
  460. newDetail.num_o = cd.num_o || 0;
  461. newDetail.num_p = cd.num_p || 0;
  462. newDetail.num_q = cd.num_q || 0;
  463. newDetail.num_r = cd.num_r || 0;
  464. newDetail.num_s = cd.num_s || 0;
  465. newDetail.num_t = cd.num_t || 0;
  466. newDetail.num_u = cd.num_u || 0;
  467. newDetail.qty = cd.qty || 0;
  468. newDetail.expr = cd.expr || '';
  469. newDetail.spec = cd.spec || '';
  470. } else {
  471. this.ctx.service.posCalcDetail._loadDataAndCalc(newDetail, cd, {}, calcTemplate);
  472. }
  473. sumQty = this.ctx.helper.add(sumQty, newDetail.qty);
  474. insertDetail.push(newDetail);
  475. }
  476. this._calcExpr(nip, 'sgfh_qty', '', sumQty, precision);
  477. } else {
  478. this._calcExpr(nip, 'sgfh_qty', b.sgfh_expr, b.sgfh_qty, precision);
  479. }
  480. this._calcExpr(nip, 'sjcl_qty', b.sjcl_expr, b.sjcl_qty, precision);
  481. this._calcExpr(nip, 'qtcl_qty', b.qtcl_expr, b.qtcl_qty, precision);
  482. nip.quantity = this.ctx.helper.add(nip.sgfh_qty, this.ctx.helper.add(nip.sjcl_qty, nip.qtcl_qty));
  483. nip.ex_qty1 = this.ctx.helper.round(b.ex_qty1, precision.value);
  484. for (const ag of b.ancGcl) {
  485. const nig = {
  486. id: this.uuid.v4(), tid: this.ctx.tender.id,
  487. add_user_id: user_id, update_user_id: user_id,
  488. lid: nip.lid, pid: nip.id, g_order: ag.g_order,
  489. is_aux: ag.is_aux || 0, name: ag.name || '', unit: ag.unit || '',
  490. drawing_code: ag.drawing_code || '', memo: ag.memo || '',
  491. quantity: ag.quantity || 0, expr: ag.expr || '', calc_template: ag.calc_template || ''
  492. };
  493. insertAncGcl.push(nig);
  494. if (!ag.calc_template) continue;
  495. let sumQty = 0;
  496. for (const cd of ag.calcDetail) {
  497. const newDetail = {
  498. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: nip.lid, pid: nip.id, ag_id: nig.id,
  499. create_user_id: user_id, update_user_id: user_id,
  500. agd_order: cd.agd_order,
  501. str1 : cd.str1 || '', str2 : cd.str1 || '', str3 : cd.str1 || '', str4 : cd.str1 || '',
  502. num_a : cd.num_a || 0, num_b : cd.num_b || 0, num_c : cd.num_c || 0, num_d : cd.num_d || 0,
  503. num_e : cd.num_e || 0, num_f : cd.num_f || 0, num_g : cd.num_g || 0, num_h : cd.num_h || 0,
  504. num_i : cd.num_i || 0, num_j : cd.num_j || 0, num_k : cd.num_k || 0, num_l : cd.num_l || 0,
  505. num_m : cd.num_m || 0, num_n : cd.num_n || 0, num_o : cd.num_o || 0, num_p : cd.num_p || 0,
  506. num_q : cd.num_q || 0, num_r : cd.num_r || 0, num_s : cd.num_s || 0, num_t : cd.num_t || 0,
  507. num_u : cd.num_u || 0,
  508. qty: cd.qty || 0, expr: cd.expr || 0, spec: cd.spec || ''
  509. };
  510. sumQty = this.ctx.helper.add(sumQty, newDetail.qty);
  511. insertAncGclDetail.push(newDetail);
  512. }
  513. nig.quantity = this.ctx.helper.round(sumQty, qtyDecimal);
  514. }
  515. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, nip.sgfh_qty);
  516. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, nip.sjcl_qty);
  517. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, nip.qtcl_qty);
  518. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, nip.ex_qty1);
  519. }
  520. const info = this.ctx.tender.info;
  521. updateBills.quantity = this.ctx.helper.add(updateBills.sgfh_qty, this.ctx.helper.add(updateBills.sjcl_qty, updateBills.qtcl_qty));
  522. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  523. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  524. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  525. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  526. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  527. const transaction = await this.db.beginTransaction();
  528. try {
  529. await transaction.insert(this.tableName, insertPos);
  530. if (insertDetail.length > 0) await transaction.insert(this.ctx.service.posCalcDetail.tableName, insertDetail);
  531. if (insertAncGcl.length > 0) await transaction.insert(this.ctx.service.ancillaryGcl.tableName, insertAncGcl);
  532. if (insertAncGclDetail.length > 0) await transaction.insert(this.ctx.service.ancillaryGclDetail.tableName, insertAncGclDetail);
  533. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  534. await transaction.commit();
  535. } catch (err) {
  536. await transaction.rollback();
  537. throw err;
  538. }
  539. result.pos = insertPos;
  540. updateBills.ledger_id = bills.ledger_id;
  541. result.ledger.update = [updateBills];
  542. result.posCalcDetail = { add: insertDetail };
  543. result.ancGcl = { add: insertAncGcl };
  544. result.ancGclDetail = { add: insertAncGclDetail };
  545. return result;
  546. }
  547. /**
  548. * 保存部位明细数据
  549. * @param data
  550. * @param {Number} tid - 标段id
  551. * @returns {Promise<{ledger: {}, pos: null}>}
  552. */
  553. async savePosData(data, tid) {
  554. switch (data.updateType) {
  555. case 'add':
  556. return await this._addPosData(tid, data.updateData);
  557. case 'update':
  558. if (data.updateData instanceof Array) {
  559. return await this._updatePosDatas(tid, data.updateData);
  560. } else {
  561. return await this._updatePosData(tid, data.updateData);
  562. }
  563. case 'delete':
  564. return await this._deletePosData(tid, data.updateData);
  565. case 'insert':
  566. return await this._insertPosData(tid, data.updateData);
  567. case 'paste-block':
  568. return await this.pasteBlockData(tid, data.updateData);
  569. }
  570. }
  571. /**
  572. * 复制粘贴 部位明细数据
  573. * @param {Array} data - 复制粘贴的数据
  574. * @param {Number} tid - 标段id
  575. * @returns {Promise<{ledger: {}, pos: null}>}
  576. */
  577. async pastePosData(data, tid) {
  578. if (!(data instanceof Array)) {
  579. throw '提交数据错误';
  580. }
  581. const transaction = await this.db.beginTransaction();
  582. const result = { ledger: {}, pos: null };
  583. const bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  584. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  585. const updateBills = {id: bills.id};
  586. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  587. for (const bp of billsPos) {
  588. const d = data.find(function (x) {
  589. return bp.id ? x.id === bp.id : false;
  590. });
  591. if (d) continue;
  592. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  593. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  594. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  595. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  596. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  597. }
  598. try {
  599. for (const d of data) {
  600. const op = d.id ? this._.find(billsPos, {id: d.id}) : null;
  601. if (d.sgfh_qty !== undefined) {
  602. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  603. } else if (op) {
  604. d.sgfh_qty = op.sgfh_qty;
  605. }
  606. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  607. if (d.sjcl_qty !== undefined) {
  608. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  609. } else if (op) {
  610. d.sjcl_qty = op.sjcl_qty;
  611. }
  612. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  613. if (d.qtcl_qty) {
  614. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  615. } else if (op) {
  616. d.qtcl_qty = op.qtcl_qty;
  617. }
  618. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  619. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  620. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  621. if (d.ex_qty1) {
  622. d.ex_qty1 = this.round(d.ex_qty1, precision.value);
  623. } else if (op) {
  624. d.ex_qty1 = op.ex_qty1;
  625. }
  626. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, d.ex_qty1);
  627. if (d.id) {
  628. await transaction.update(this.tableName, d);
  629. } else {
  630. this._completeInsertPosData(tid, d);
  631. await transaction.insert(this.tableName, d);
  632. }
  633. }
  634. const info = this.ctx.tender.info;
  635. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  636. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  637. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  638. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  639. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  640. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  641. updateBills.ledger_id = bills.ledger_id;
  642. await transaction.commit();
  643. } catch (err) {
  644. await transaction.rollback();
  645. throw err;
  646. }
  647. result.pos = data;
  648. result.ledger.update = [updateBills];
  649. return result;
  650. }
  651. /**
  652. * 删除清单下部位明细数据(删除清单时调用)
  653. *
  654. * @param transaction - 事务
  655. * @param tid - 标段id
  656. * @param lid - 清单id
  657. * @returns {Promise<void>}
  658. */
  659. async deletePosData(transaction, tid, lid) {
  660. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  661. }
  662. async getMemoData(tid, columns) {
  663. if (!columns || columns.length === 0) return [];
  664. return await this.db.select(this.departTableName(tid), {
  665. where: { tid },
  666. columns: ['id', ...columns],
  667. });
  668. }
  669. }
  670. return Pos;
  671. };