|
@@ -8,6 +8,8 @@
|
|
|
* @version
|
|
|
*/
|
|
|
|
|
|
+const changeConst = require('../const/change');
|
|
|
+
|
|
|
module.exports = app => {
|
|
|
|
|
|
class StageImportChange extends app.BaseService {
|
|
@@ -48,13 +50,35 @@ module.exports = app => {
|
|
|
return await this.db.query(sql, [sid]);
|
|
|
}
|
|
|
|
|
|
- async getEndStageImportChange(stage) {
|
|
|
- const sql = 'SELECT scf.* ' +
|
|
|
+ async getStageImportData(stage) {
|
|
|
+ const sql = 'SELECT scf.*, ' +
|
|
|
+ ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
|
|
|
+ ' FROM ' + this.tableName + ' scf ' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.change.tableName + ' oc ON scf.rela_cid = oc.cid ' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' ocb ON scf.rela_cbid = ocb.id ' +
|
|
|
+ ' WHERE scf.sid = ?';
|
|
|
+ return await this.db.query(sql, [stage.id]);
|
|
|
+ }
|
|
|
+
|
|
|
+ async getEndStageImportData(stage) {
|
|
|
+ const sql = 'SELECT scf.*, ' +
|
|
|
+ ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
|
|
|
' FROM ' + this.tableName + ' scf ' +
|
|
|
' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.change.tableName + ' oc ON scf.rela_cid = oc.cid ' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' ocb ON scf.rela_cbid = ocb.id ' +
|
|
|
' WHERE scf.tid = ? And s.order <= ?';
|
|
|
- const result = await this.db.query(sql, [stage.tid, stage.order]);
|
|
|
- return result;
|
|
|
+ return await this.db.query(sql, [stage.tid, stage.order]);
|
|
|
+ }
|
|
|
+
|
|
|
+ async getLeafXmjStageImportData(stage, lid) {
|
|
|
+ const sql = 'SELECT scf.*, ' +
|
|
|
+ ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
|
|
|
+ ' FROM ' + this.tableName + ' scf ' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.change.tableName + ' oc ON scf.rela_cid = oc.cid ' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' ocb ON scf.rela_cbid = ocb.id ' +
|
|
|
+ ' WHERE scf.sid = ? and import_lid = ?';
|
|
|
+ return await this.db.query(sql, [stage.id, lid]);
|
|
|
}
|
|
|
|
|
|
async getLeafXmjImportChange(sid, importLid) {
|
|
@@ -72,43 +96,44 @@ module.exports = app => {
|
|
|
: await this.getImportChange(stage.id);
|
|
|
if (change.length === 0) return [];
|
|
|
|
|
|
- const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
|
|
|
- where: { cid: change.map(x => { return x.cid; }) }
|
|
|
- });
|
|
|
- const changeBillsIndex = {}, changeBillsPart = {};
|
|
|
- for (const cb of changeBills) {
|
|
|
- changeBillsIndex[cb.id] = cb;
|
|
|
- if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
|
|
|
- changeBillsPart[cb.cid].push(cb);
|
|
|
- }
|
|
|
- const stageChangeBills = await this.getEndStageImportChange(stage);
|
|
|
- for (const scb of stageChangeBills) {
|
|
|
- if (!scb.rela_qty) continue;
|
|
|
-
|
|
|
- const cb = changeBillsIndex[scb.rela_cbid];
|
|
|
- if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.rela_qty);
|
|
|
- }
|
|
|
- for (const cid in changeBillsPart) {
|
|
|
- const c = change.find(x => { return x.cid === cid });
|
|
|
- if (!c) continue;
|
|
|
-
|
|
|
- for (const cb of changeBillsPart[cid]) {
|
|
|
- cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
|
|
|
- cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
|
|
|
-
|
|
|
- c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
|
|
|
- if (cb.spamount > 0) {
|
|
|
- c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
|
|
|
- c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
|
|
|
- } else if (cb.spamount < 0){
|
|
|
- c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
|
|
|
- c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
|
|
|
- }
|
|
|
- }
|
|
|
- c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
|
|
|
- c.p_used_pt = c.p_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.p_used_tp, c.p_tp, 4), 100) : 0;
|
|
|
- c.n_used_pt = c.n_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.n_used_tp, c.n_tp, 4), 100) : 0;
|
|
|
- }
|
|
|
+ // 引入变更,不计算已执行等????
|
|
|
+ // const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
|
|
|
+ // where: { cid: change.map(x => { return x.cid; }) }
|
|
|
+ // });
|
|
|
+ // const changeBillsIndex = {}, changeBillsPart = {};
|
|
|
+ // for (const cb of changeBills) {
|
|
|
+ // changeBillsIndex[cb.id] = cb;
|
|
|
+ // if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
|
|
|
+ // changeBillsPart[cb.cid].push(cb);
|
|
|
+ // }
|
|
|
+ // const stageChangeBills = await this.getEndStageImportChange(stage);
|
|
|
+ // for (const scb of stageChangeBills) {
|
|
|
+ // if (!scb.rela_qty) continue;
|
|
|
+ //
|
|
|
+ // const cb = changeBillsIndex[scb.rela_cbid];
|
|
|
+ // if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.rela_qty);
|
|
|
+ // }
|
|
|
+ // for (const cid in changeBillsPart) {
|
|
|
+ // const c = change.find(x => { return x.cid === cid });
|
|
|
+ // if (!c) continue;
|
|
|
+ //
|
|
|
+ // for (const cb of changeBillsPart[cid]) {
|
|
|
+ // cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
|
|
|
+ // cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
|
|
|
+ //
|
|
|
+ // c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
|
|
|
+ // if (cb.spamount > 0) {
|
|
|
+ // c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
|
|
|
+ // c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
|
|
|
+ // } else if (cb.spamount < 0){
|
|
|
+ // c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
|
|
|
+ // c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
|
|
|
+ // c.p_used_pt = c.p_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.p_used_tp, c.p_tp, 4), 100) : 0;
|
|
|
+ // c.n_used_pt = c.n_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.n_used_tp, c.n_tp, 4), 100) : 0;
|
|
|
+ // }
|
|
|
return change;
|
|
|
}
|
|
|
|
|
@@ -152,6 +177,34 @@ module.exports = app => {
|
|
|
const cur = await this.db.query(sql, [sid]);
|
|
|
return this._.map(cur, 'rela_cid');
|
|
|
}
|
|
|
+
|
|
|
+ // 分类汇总变更金额
|
|
|
+ async getSubtotal(stage) {
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+ const tender = this.ctx.tender && this.ctx.tender.id === stage.tid ? this.ctx.tender : await this._getTender(stage);
|
|
|
+ const sql = 'SELECT sc.*, c.quality FROM ' + this.tableName + ' sc' +
|
|
|
+ ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.rela_cid = c.cid' +
|
|
|
+ ' WHERE sid = ?';
|
|
|
+ const data = await this.db.query(sql, [stage.id]);
|
|
|
+ const bqData = [];
|
|
|
+ for (const d of data) {
|
|
|
+ if (!d.rela_qty) continue;
|
|
|
+ let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
|
|
|
+ if (!bd) {
|
|
|
+ const bills = await this.db.get(this.ctx.service.ledger.departTableName(tender.id), { id: d.lid });
|
|
|
+ if (!bills) continue;
|
|
|
+ bd = { lid: d.lid, quality: d.quality, unit_price: bills.unit_price };
|
|
|
+ bqData.push(bd);
|
|
|
+ }
|
|
|
+ const tp = this.ctx.helper.mul(d.rela_qty, bd.unit_price, tender.info.decimal.tp);
|
|
|
+ bd.tp = this.ctx.helper.add(bd.tp, tp);
|
|
|
+ }
|
|
|
+ const result = {};
|
|
|
+ result.common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
|
|
|
+ result.more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
|
|
|
+ result.great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return StageImportChange;
|