|
@@ -24,9 +24,11 @@ class autoUseChange {
|
|
|
this.updateBills = [];
|
|
|
this.updatePos = [];
|
|
|
this.insertChange = [];
|
|
|
+ this.updateChange = [];
|
|
|
|
|
|
this.changeBills = {};
|
|
|
this.changePos = {};
|
|
|
+ this.changeDetail = [];
|
|
|
}
|
|
|
|
|
|
init(source) {
|
|
@@ -42,11 +44,12 @@ class autoUseChange {
|
|
|
|
|
|
this.stageBills = source.stageBills;
|
|
|
this.stagePos = source.stagePos;
|
|
|
+ this.stageChange = source.stageChange || [];
|
|
|
}
|
|
|
|
|
|
findBillsPos(changeBills){
|
|
|
if (changeBills.gcl_id) {
|
|
|
- const node = this.ledgerTree.nodes.find(x => {return x.id === changeBills.gcl_id});
|
|
|
+ const node = this.ledgerTree.datas.find(x => {return x.id === changeBills.gcl_id});
|
|
|
const posData = this.pos.getLedgerPos(node.id) || [];
|
|
|
const changePos = posData.find(x => { return x.name === changeBills.bwmx; });
|
|
|
return { bills: node, lid: node.id, pid: changePos ? changePos.id : (posData.length > 0 ? posData[0].id : '-1') };
|
|
@@ -78,52 +81,107 @@ class autoUseChange {
|
|
|
}
|
|
|
|
|
|
useBills(bills) {
|
|
|
- const billsPos = this.findBillsPos(bills);
|
|
|
+ if (bills.billsPos) bills.billsPos.bills = this.ledgerTree.datas.find(x => {return x.id === bills.billsPos.lid; });
|
|
|
+ const billsPos = bills.billsPos || this.findBillsPos(bills);
|
|
|
if (!billsPos) return;
|
|
|
- this.insertChange.push({
|
|
|
+ const bamount = parseFloat(bills.samount);
|
|
|
+ const minus = bamount < 0;
|
|
|
+ this.changeDetail.push({
|
|
|
tid: this.default.tid, sid: this.default.sid,
|
|
|
- lid: billsPos.lid, pid: billsPos.pid, cid: bills.cid, cbid: bills.id,
|
|
|
- qty: bills.valid_qty, stimes: 1, sorder: 0,
|
|
|
+ lid: billsPos.lid, pid: billsPos.pid + '', cid: bills.cid, cbid: bills.id,
|
|
|
+ qty: bills.valid_qty, stimes: 1, sorder: 0, unit_price: bills.unit_price, minus, no_value: false,
|
|
|
});
|
|
|
|
|
|
if (billsPos.pid !== '-1') {
|
|
|
- const cp = this.changePos[billsPos.pid];
|
|
|
+ let cp = this.changePos[billsPos.pid];
|
|
|
if (!cp) {
|
|
|
- this.changePos[billsPos.pid] = { lid: billsPos.lid, pid: billsPos.pid, qty: bills.valid_qty, bills: billsPos.bills };
|
|
|
+ cp = { lid: billsPos.lid, pid: billsPos.pid, qty: 0, bills: billsPos.bills };
|
|
|
+ this.changePos[billsPos.pid] = cp
|
|
|
+ }
|
|
|
+ cp.qty = this.helper.add(cp.qty, bills.valid_qty);
|
|
|
+ if (minus) {
|
|
|
+ cp.negative_qc_qty = cp.qty;
|
|
|
} else {
|
|
|
- cp.qty = this.helper.add(cp.qty, bills.valid_qty);
|
|
|
+ cp.positive_qc_qty = cp.qty;
|
|
|
}
|
|
|
} else {
|
|
|
- const cb = this.changeBills[billsPos.lid];
|
|
|
+ let cb = this.changeBills[billsPos.lid];
|
|
|
if (!cb) {
|
|
|
- this.changeBills[billsPos.lid] = { lid: billsPos.lid, qty: bills.valid_qty, bills: billsPos.bills };
|
|
|
+ cb = { lid: billsPos.lid, qty: 0, bills: billsPos.bills };
|
|
|
+ this.changeBills[billsPos.lid] = cb;
|
|
|
+ }
|
|
|
+ cb.qty = this.helper.add(cb.qty, bills.valid_qty);
|
|
|
+ if (minus) {
|
|
|
+ cb.negative_qc_qty = this.helper.add(cb.negative_qc_qty, bills.valid_qty);
|
|
|
} else {
|
|
|
- cb.qty = this.helper.add(cb.qty, bills.valid_qty);
|
|
|
+ cb.positive_qc_qty = this.helper.add(cb.positive_qc_qty, bills.valid_qty);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
calculateAll() {
|
|
|
+ for (const cd of this.changeDetail) {
|
|
|
+ const sci = this.stageChange.findIndex(x => {
|
|
|
+ return x.lid === cd.lid && x.pid === cd.pid && x.cbid === cd.cbid;
|
|
|
+ });
|
|
|
+ const sc = this.stageChange[sci];
|
|
|
+ if (sc) {
|
|
|
+ this.updateChange.push({ id: sc.id, qty: cd.qty });
|
|
|
+ this.stageChange.splice(sci, 1);
|
|
|
+ } else {
|
|
|
+ this.insertChange.push(cd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (const sc of this.stageChange) {
|
|
|
+ if (sc.no_value) continue;
|
|
|
+ const cp = this.changePos[sc.pid];
|
|
|
+ if (cp) {
|
|
|
+ cp.qty = this.helper.add(cp.qty, sc.qty);
|
|
|
+ if (sc.minus) {
|
|
|
+ cp.negative_qc_qty = this.helper.add(cp.negative_qc_qty, sc.qty);
|
|
|
+ } else {
|
|
|
+ cp.positive_qc_qty = this.helper.add(cp.positive_qc_qty, sc.qty);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
for (const pid in this.changePos) {
|
|
|
const cp = this.changePos[pid];
|
|
|
if (!cp) continue;
|
|
|
|
|
|
const precision = this.helper.findPrecision(this.precision, cp.bills.unit);
|
|
|
const qc_qty = this.helper.round(cp.qty, precision.value);
|
|
|
+ const positive_qc_qty = this.helper.round(cp.positive_qc_qty || 0, precision.value);
|
|
|
+ const negative_qc_qty = this.helper.round(cp.negative_qc_qty || 0, precision.value);
|
|
|
const sp = this.stagePos.find(x => {return x.pid === pid});
|
|
|
if (sp) {
|
|
|
- this.updatePos.push({ id: sp.id, qc_qty });
|
|
|
+ this.updatePos.push({ id: sp.id, qc_qty, positive_qc_qty, negative_qc_qty });
|
|
|
} else {
|
|
|
this.insertPos.push({
|
|
|
tid: this.default.tid, sid: this.default.sid, said: this.default.said,
|
|
|
- lid: cp.lid, pid, qc_qty, times: 1, order: 0
|
|
|
+ lid: cp.lid, pid, qc_qty, positive_qc_qty, negative_qc_qty, times: 1, order: 0
|
|
|
});
|
|
|
}
|
|
|
const cb = this.changeBills[cp.lid];
|
|
|
if (!cb) {
|
|
|
- this.changeBills[cp.lid] = { lid: cp.lid, qty: cp.qty, bills: cp.bills };
|
|
|
+ this.changeBills[cp.lid] = { lid: cp.lid, qty: qc_qty, positive_qc_qty, negative_qc_qty, bills: cp.bills };
|
|
|
} else {
|
|
|
- cb.qty = this.helper.add(cb.qty, cp.qty);
|
|
|
+ cb.qty = this.helper.add(cb.qty, qc_qty);
|
|
|
+ cb.positive_qc_qty = this.helper.add(cb.positive_qc_qty, positive_qc_qty);
|
|
|
+ cb.negative_qc_qty = this.helper.add(cb.negative_qc_qty, negative_qc_qty);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (const sc of this.stageChange) {
|
|
|
+ if (sc.no_value) continue;
|
|
|
+ const cp = this.changePos[sc.pid];
|
|
|
+ if (cp) continue;
|
|
|
+ const cb = this.changeBills[sc.lid];
|
|
|
+ if (cb) {
|
|
|
+ cb.qty = this.helper.add(cb.qty, sc.qty);
|
|
|
+ if (sc.minus) {
|
|
|
+ cb.negative_qc_qty = this.helper.add(cb.negative_qc_qty, sc.qty);
|
|
|
+ } else {
|
|
|
+ cb.positive_qc_qty = this.helper.add(cb.positive_qc_qty, sc.qty);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
for (const lid in this.changeBills) {
|
|
@@ -132,13 +190,17 @@ class autoUseChange {
|
|
|
const precision = this.helper.findPrecision(this.precision, cb.bills.unit);
|
|
|
const qc_qty = this.helper.round(cb.qty, precision.value);
|
|
|
const qc_tp = this.helper.mul(cb.qty, cb.bills.unit_price, this.decimal.tp);
|
|
|
+ const positive_qc_qty = this.helper.round(cb.positive_qc_qty || 0, precision.value);
|
|
|
+ const positive_qc_tp = this.helper.mul(positive_qc_qty, cb.bills.unit_price, this.decimal.tp);
|
|
|
+ const negative_qc_qty = this.helper.round(cb.negative_qc_qty || 0, precision.value);
|
|
|
+ const negative_qc_tp = this.helper.mul(negative_qc_qty, cb.bills.unit_price, this.decimal.tp);
|
|
|
const sb = this.stageBills.find(x => {return x.lid === lid});
|
|
|
if (sb) {
|
|
|
- this.updateBills.push({ id: sb.id, qc_qty, qc_tp });
|
|
|
+ this.updateBills.push({ id: sb.id, qc_qty, qc_tp, positive_qc_qty, positive_qc_tp, negative_qc_qty, negative_qc_tp });
|
|
|
} else {
|
|
|
this.insertBills.push({
|
|
|
tid: this.default.tid, sid: this.default.sid, said: this.default.said,
|
|
|
- lid, qc_qty, qc_tp, times: 1, order: 0
|
|
|
+ lid, qc_qty, qc_tp, positive_qc_qty, positive_qc_tp, negative_qc_qty, negative_qc_tp, times: 1, order: 0
|
|
|
});
|
|
|
}
|
|
|
}
|
|
@@ -676,21 +738,53 @@ module.exports = app => {
|
|
|
};
|
|
|
|
|
|
async autoUseChangeBills(tender, stage, data) {
|
|
|
- for (const d of data) {
|
|
|
- const changeBills = await this.ctx.service.changeAuditList.getDataById(d.cbid);
|
|
|
- const bills = await this.ctx.service.ledger.getDataById(data.lid);
|
|
|
- const pos = await this.ctx.service.pos.getDataById(data.pid);
|
|
|
- if (pos && pos.lid !== bills.id) throw '数据错误';
|
|
|
-
|
|
|
- const allSc = await this.getAllDataByCondition({ where: { cbid: d.cbid, lid: data.lid, pid: data.pid } });
|
|
|
- const curSc = this.ctx.helper.filterLastestData(allSc, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
|
|
|
- }
|
|
|
- const changeBills = await this.ctx.service.changeAuditList.getDataById(cbid);
|
|
|
- const source = changeBills.gcl_id
|
|
|
- ? await this.ctx.service.ledger.getDataByCondition({ where: { id: [changeBills.gcl_id] } })
|
|
|
- : await this.ctx.service.ledger.getDataByCondition({ where: { tid: tender.id, b_code: changeBills.code, name: changeBills.name, unit: changeBills.unit, unit_price: changeBills.unit_price, is_leaf: true }});
|
|
|
- if (source.length < 1) then
|
|
|
- const pos = this.ctx.service.pos.getDataByCondition({ where: { lid: bills.id, name: changeBills.bwmx} });
|
|
|
+ const lid = [], cbid = [];
|
|
|
+ data.forEach(x => {
|
|
|
+ lid.push(x.lid);
|
|
|
+ cbid.push(x.cbid);
|
|
|
+ });
|
|
|
+ const validChangeBills = await this.ctx.service.stageChangeFinal.getListChangeBillsValidQty(tender.id, cbid);
|
|
|
+ validChangeBills.forEach(x => {
|
|
|
+ x.billsPos = data.find(y => { return x.id === y.cbid; });
|
|
|
+ });
|
|
|
+ const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({
|
|
|
+ columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price'],
|
|
|
+ where: { id: lid },
|
|
|
+ });
|
|
|
+ const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
|
|
|
+ this.ctx.helper.assignRelaData(ledgerData, [
|
|
|
+ { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
|
|
|
+ ]);
|
|
|
+ const posData = await this.ctx.service.pos.getAllDataByCondition({
|
|
|
+ columns: ['id', 'lid', 'name', 'porder'],
|
|
|
+ where: { lid },
|
|
|
+ });
|
|
|
+ const stageBills = await this.ctx.service.stageBills.getAllDataByCondition({ where: { sid: stage.id } });
|
|
|
+ const stagePos = await this.ctx.service.stagePos.getAllDataByCondition({ where: { sid: stage.id } });
|
|
|
+ const stageChange = await this.ctx.service.stageChange.getAllDataByCondition({ where: { sid: stage.id, lid }});
|
|
|
+
|
|
|
+ const useModal = new autoUseChange(this.ctx.helper, tender.info);
|
|
|
+ useModal.use({ledgerData, posData, stageBills, stagePos, stageChange, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } }, validChangeBills);
|
|
|
+
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ if (useModal.insertBills.length > 0) await conn.insert(this.ctx.service.stageBills.tableName, useModal.insertBills);
|
|
|
+ if (useModal.updateBills.length > 0) await conn.updateRows(this.ctx.service.stageBills.tableName, useModal.updateBills);
|
|
|
+ if (useModal.insertPos.length > 0) await conn.insert(this.ctx.service.stagePos.tableName, useModal.insertPos);
|
|
|
+ if (useModal.updatePos.length > 0) await conn.updateRows(this.ctx.service.stagePos.tableName, useModal.updatePos);
|
|
|
+ if (useModal.insertChange.length > 0) await conn.insert(this.tableName, useModal.insertChange);
|
|
|
+ if (useModal.updateChange.length > 0) await conn.updateRows(this.tableName, useModal.updateChange);
|
|
|
+ await conn.commit();
|
|
|
+ } catch (err) {
|
|
|
+ await conn.rollback();
|
|
|
+ this.ctx.log(err);
|
|
|
+ throw '保存导入数据失败';
|
|
|
+ }
|
|
|
+
|
|
|
+ const rst = { bills: {}, pos: {} };
|
|
|
+ rst.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, lid);
|
|
|
+ rst.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, { lid });
|
|
|
+ return rst;
|
|
|
};
|
|
|
|
|
|
async autoUseAllChange(tender, stage) {
|
|
@@ -713,14 +807,16 @@ module.exports = app => {
|
|
|
const useModal = new autoUseChange(this.ctx.helper, tender.info);
|
|
|
useModal.use({ledgerData, posData, stageBills, stagePos, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } }, validChangeBills);
|
|
|
|
|
|
+ // if (useModal.insertChange.length === 0) return '无可调用的清单或计量单元';
|
|
|
+
|
|
|
const conn = await this.db.beginTransaction();
|
|
|
try {
|
|
|
- if (useModal.insertBills.length > 0) conn.insert(this.ctx.service.stageBills.tableName, useModal.insertBills);
|
|
|
- if (useModal.updateBills.length > 0) conn.updateRows(this.ctx.service.stageBills.tableName, useModal.updateBills);
|
|
|
- if (useModal.insertPos.length > 0) conn.insert(this.ctx.service.stagePos.tableName, useModal.insertPos);
|
|
|
- if (useModal.updatePos.length > 0) conn.updateRows(this.ctx.service.stagePos.tableName, useModal.updatePos);
|
|
|
+ if (useModal.insertBills.length > 0) await conn.insert(this.ctx.service.stageBills.tableName, useModal.insertBills);
|
|
|
+ if (useModal.updateBills.length > 0) await conn.updateRows(this.ctx.service.stageBills.tableName, useModal.updateBills);
|
|
|
+ if (useModal.insertPos.length > 0) await conn.insert(this.ctx.service.stagePos.tableName, useModal.insertPos);
|
|
|
+ if (useModal.updatePos.length > 0) await conn.updateRows(this.ctx.service.stagePos.tableName, useModal.updatePos);
|
|
|
await conn.delete(this.tableName, { sid: stage.id });
|
|
|
- await conn.insert(this.tableName, useModal.insertChange);
|
|
|
+ if (useModal.insertChange.length > 0) await conn.insert(this.tableName, useModal.insertChange);
|
|
|
await conn.commit();
|
|
|
} catch (err) {
|
|
|
await conn.rollback();
|