|
|
@@ -19,6 +19,7 @@ costFields.calcFields = [...costFields.curFields];
|
|
|
costFields.editQueryFields = [...costFields.baseFields, ...costFields.textFields, ...costFields.preFields, ...costFields.curFields];
|
|
|
costFields.readQueryFields = [...costFields.baseFields, ...costFields.textFields, ...costFields.preFields, ...costFields.readFields];
|
|
|
costFields.compareQueryFields = [...costFields.baseFields, ...costFields.textFields, ...costFields.preFields, ...costFields.curFields, 'calc_his'];
|
|
|
+const auditConst = require('../const/audit').costStage;
|
|
|
|
|
|
module.exports = app => {
|
|
|
class CostStageBook extends app.BaseService {
|
|
|
@@ -129,9 +130,9 @@ module.exports = app => {
|
|
|
if (d.in_tp !== undefined ) {
|
|
|
nd.in_tp = d.in_tp !== undefined ? this.ctx.helper.round(d.in_tp || 0, this.ctx.costStage.decimal.tp) : (od.in_tp ? od.in_tp : 0);
|
|
|
const divNum = this.ctx.helper.add(1, this.ctx.helper.div(cl.tax, 100));
|
|
|
- nd.in_excl_tax_tp = this.ctx.helper.div(nd.in_tp, divNum, this.ctx.costStage.decimal.tp);
|
|
|
+ nd.in_excl_tax_tp = this.ctx.helper.div(nd.in_tp, divNum, this.ctx.costStage.decimal.excl_tax_tp);
|
|
|
}
|
|
|
- if (d.in_excl_tax_tp !== undefined) nd.in_excl_tax_tp = this.ctx.helper.round(d.in_excl_tax_tp, this.ctx.costStage.decimal.tp);
|
|
|
+ if (d.in_excl_tax_tp !== undefined) nd.in_excl_tax_tp = this.ctx.helper.round(d.in_excl_tax_tp, this.ctx.costStage.decimal.excl_tax_tp);
|
|
|
if (od) {
|
|
|
updateDetail.push(nd);
|
|
|
} else {
|
|
|
@@ -198,6 +199,74 @@ module.exports = app => {
|
|
|
const result = await this.db.queryOne(`SELECT ${sumFields.join(', ')} FROM ${this.tableName} where stage_id = ?`, [stage.id]);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ async setDecimal(decimal) {
|
|
|
+ if (!this.ctx.costStage) throw '读取数据错误';
|
|
|
+ if (this.ctx.costStage.stage_order !== this.ctx.costStage.latestOrder) throw '往期不可修改小数位数';
|
|
|
+ if (this.ctx.costStage.audit_status !== auditConst.status.uncheck && this.ctx.costStage.audit_status !== auditConst.status.checkNo) throw '仅原报可修改小数位数';
|
|
|
+ const orgDecimal = this.ctx.costStage.decimal;
|
|
|
+
|
|
|
+ const calcTp = decimal.tp < orgDecimal.tp || decimal.excl_tax_tp !== orgDecimal.excl_tax_tp;
|
|
|
+ this.ctx.costStage.decimal = this.ctx.helper._.assignIn(this.ctx.costStage.typeInfo.decimal, decimal);
|
|
|
+
|
|
|
+ const updateData = [], detailUpdateData = [];
|
|
|
+ let orgLedger, orgDetail;
|
|
|
+ if (calcTp) {
|
|
|
+ const detailCalcData = await this.ctx.service.costStageBookDetail.getAllDataByCondition({ where: { stage_id: this.ctx.costStage.id }});
|
|
|
+ orgDetail = await this.ctx.service.costStageDetail.getAllDataByCondition({ where: { stage_id: this.ctx.costStage.relaStage.id }});
|
|
|
+ for (const cd of detailCalcData) {
|
|
|
+ const nd = { id: cd.id, ledger_id: cd.ledger_id, detail_id: cd.detail_id };
|
|
|
+ const cl = orgLedger.find(x => { return x.id === cd.ledger_id });
|
|
|
+ nd.in_tp = this.ctx.helper.round(cd.in_tp, decimal.tp);
|
|
|
+ const divNum = cl.tax ? this.ctx.helper.add(1, this.ctx.helper.div(cl.tax, 100)) : 1;
|
|
|
+ nd.in_excl_tax_tp = this.ctx.helper.div(nd.in_tp !== undefined ? nd.in_tp : nd.in_tp, divNum, decimal.excl_tax_tp);
|
|
|
+ detailUpdateData.push(nd);
|
|
|
+ }
|
|
|
+ const calcData = await this.getAllDataByCondition({ where: {stage_id: this.ctx.costStage.id } });
|
|
|
+ orgLedger = await this.ctx.service.costStageLedger.getAllDataByCondition({ where: { stage_id: this.ctx.costStage.relaStage.id }});
|
|
|
+ for (const cd of calcData) {
|
|
|
+ const nd = { id: cd.id, ledger_id: cd.ledger_id };
|
|
|
+ const relaDetail = detailUpdateData.filter(x => { return x.ledger_id === cd.ledger_id; });
|
|
|
+ if (relaDetail && relaDetail.length > 0) {
|
|
|
+ for (const rd of relaDetail) {
|
|
|
+ for (const prop of costFields.calcFields) {
|
|
|
+ nd[prop] = this.ctx.helper.add(nd[prop], rd[prop]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const cl = orgLedger.find(x => { return x.id === cd.ledger_id });
|
|
|
+ nd.in_tp = this.ctx.helper.round(cd.in_tp, decimal.tp);
|
|
|
+ const divNum = cl.tax ? this.ctx.helper.add(1, this.ctx.helper.div(cl.tax, 100)) : 1;
|
|
|
+ nd.in_excl_tax_tp = this.ctx.helper.div(nd.in_tp !== undefined ? nd.in_tp : nd.in_tp, divNum, decimal.excl_tax_tp);
|
|
|
+ }
|
|
|
+ updateData.push(nd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ await conn.update(this.ctx.service.costStage.tableName, { id: this.ctx.costStage.id, decimal: JSON.stringify(this.ctx.costStage.decimal)});
|
|
|
+ if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
|
|
|
+ if (detailUpdateData.length > 0) await conn.updateRows(this.ctx.service.costStageBookDetail.tableName, detailUpdateData);
|
|
|
+ await conn.commit();
|
|
|
+ } catch(err) {
|
|
|
+ await conn.rollback();
|
|
|
+ return { calc: false, bills: [], detail: [] };
|
|
|
+ }
|
|
|
+ updateData.forEach(i => {
|
|
|
+ i.book_id = i.id;
|
|
|
+ delete i.id;
|
|
|
+ const cl = orgLedger.find(x => { return x.id === i.ledger_id });
|
|
|
+ i.tree_id = cl.tree_id;
|
|
|
+ });
|
|
|
+ detailUpdateData.forEach(u => {
|
|
|
+ u.book_id = u.id;
|
|
|
+ delete u.id;
|
|
|
+ const cl = orgDetail.find(x => { return x.id === od.detail_id; });
|
|
|
+ i.id = cl.id;
|
|
|
+ });
|
|
|
+ return { calc: calcTp, bills: updateData, detail: detailUpdateData };
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return CostStageBook;
|