|
@@ -13,6 +13,139 @@ const audit = require('../const/audit');
|
|
|
const timesLen = audit.stage.timesLen;
|
|
|
const changeConst = require('../const/change');
|
|
|
|
|
|
+class autoUseChange {
|
|
|
+ constructor(helper, info) {
|
|
|
+ this.helper = helper;
|
|
|
+ this.precision = info.precision;
|
|
|
+ this.decimal = info.decimal;
|
|
|
+
|
|
|
+ this.insertBills = [];
|
|
|
+ this.insertPos = [];
|
|
|
+ this.updateBills = [];
|
|
|
+ this.updatePos = [];
|
|
|
+ this.insertChange = [];
|
|
|
+
|
|
|
+ this.changeBills = {};
|
|
|
+ this.changePos = {};
|
|
|
+ }
|
|
|
+
|
|
|
+ init(source) {
|
|
|
+ this.default = source.default;
|
|
|
+
|
|
|
+ const LedgerModel = require('../lib/ledger');
|
|
|
+ this.ledgerTree = new LedgerModel.billsTree(this.ctx, {
|
|
|
+ id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [],
|
|
|
+ });
|
|
|
+ this.pos = new LedgerModel.pos({ id: 'id', ledgerId: 'lid' });
|
|
|
+ this.ledgerTree.loadDatas(source.ledgerData);
|
|
|
+ this.pos.loadDatas(source.posData);
|
|
|
+
|
|
|
+ this.stageBills = source.stageBills;
|
|
|
+ this.stagePos = source.stagePos;
|
|
|
+ }
|
|
|
+
|
|
|
+ findBillsPos(changeBills){
|
|
|
+ if (changeBills.gcl_id) {
|
|
|
+ const node = this.ledgerTree.nodes.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') };
|
|
|
+ } else {
|
|
|
+ const cb = {
|
|
|
+ b_code: changeBills.code || '',
|
|
|
+ name: changeBills.name || '',
|
|
|
+ unit: changeBills.unit || '',
|
|
|
+ unit_price: changeBills.unit_price || 0,
|
|
|
+ is_tp: false,
|
|
|
+ };
|
|
|
+ for (const node of this.ledgerTree.nodes) {
|
|
|
+ if (node.children && node.children.length > 0) continue;
|
|
|
+
|
|
|
+ const b = {
|
|
|
+ b_code: node.b_code || '',
|
|
|
+ name: node.name || '',
|
|
|
+ unit: node.unit || '',
|
|
|
+ unit_price: node.unit_price || 0,
|
|
|
+ is_tp: !!node.is_tp,
|
|
|
+ };
|
|
|
+ if (_.isMatch(cb, b)) {
|
|
|
+ posData = this.pos.getLedgerPos(node.id) || [];
|
|
|
+ return { bills: node, lid: node.id, pid: posData.length > 0 ? posData[0].id : '-1' };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ useBills(bills) {
|
|
|
+ const billsPos = this.findBillsPos(bills);
|
|
|
+ if (!billsPos) return;
|
|
|
+ this.insertChange.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,
|
|
|
+ });
|
|
|
+
|
|
|
+ if (billsPos.pid !== '-1') {
|
|
|
+ const cp = this.changePos[billsPos.pid];
|
|
|
+ if (!cp) {
|
|
|
+ this.changePos[billsPos.pid] = { lid: billsPos.lid, pid: billsPos.pid, qty: bills.valid_qty, bills: billsPos.bills };
|
|
|
+ } else {
|
|
|
+ cp.qty = this.helper.add(cp.qty, bills.valid_qty);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const cb = this.changeBills[billsPos.lid];
|
|
|
+ if (!cb) {
|
|
|
+ this.changeBills[billsPos.lid] = { lid: billsPos.lid, qty: bills.valid_qty, bills: billsPos.bills };
|
|
|
+ } else {
|
|
|
+ cb.qty = this.helper.add(cb.qty, bills.valid_qty);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ calculateAll() {
|
|
|
+ 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 sp = this.stagePos.find(x => {return x.pid === pid});
|
|
|
+ if (sp) {
|
|
|
+ this.updatePos.push({ id: sp.id, 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 });
|
|
|
+ }
|
|
|
+ const cb = this.changeBills[cp.lid];
|
|
|
+ if (!cb) {
|
|
|
+ this.changeBills[cp.lid] = { lid: cp.lid, qty: cp.qty, bills: cp.bills };
|
|
|
+ } else {
|
|
|
+ cb.qty = this.helper.add(cb.qty, cp.qty);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (const lid in this.changeBills) {
|
|
|
+ const cb = this.changeBills[lid];
|
|
|
+ if (!cb) continue;
|
|
|
+ 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 sb = this.stageBills.find(x => {return x.lid === lid});
|
|
|
+ if (sb) {
|
|
|
+ this.updateBills.push({ id: sb.id, qc_qty, 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 });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ use(source, validChangeBills) {
|
|
|
+ this.init(source);
|
|
|
+
|
|
|
+ for (const bills of validChangeBills) {
|
|
|
+ this.useBills(bills);
|
|
|
+ }
|
|
|
+ this.calculateAll();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
module.exports = app => {
|
|
|
|
|
|
class StageChange extends app.BaseService {
|
|
@@ -498,12 +631,65 @@ module.exports = app => {
|
|
|
return { lid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
|
|
|
};
|
|
|
|
|
|
-
|
|
|
async getPosMinusQty(stage, pid) {
|
|
|
const data = await this.getAllDataByCondition({ where: { sid: stage.id, pid, minus: 1 } });
|
|
|
const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
|
|
|
return { pid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
|
|
|
};
|
|
|
+
|
|
|
+ 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} });
|
|
|
+ };
|
|
|
+
|
|
|
+ async autoUseAllChange(tender, stage) {
|
|
|
+ const validChangeBills = await this.ctx.service.stageChangeFinal.getAllChangeBillsValidQty(tender.id);
|
|
|
+ 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: { tender_id: stage.tid },
|
|
|
+ });
|
|
|
+ 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: { tid: stage.tid },
|
|
|
+ });
|
|
|
+ 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 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);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ await conn.delete(this.tableName, { sid: stage.id });
|
|
|
+ await conn.insert(this.tableName, useModal.insertChange);
|
|
|
+ await conn.commit();
|
|
|
+ } catch (err) {
|
|
|
+ await conn.rollback();
|
|
|
+ this.ctx.log(err);
|
|
|
+ throw '保存导入数据失败';
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return StageChange;
|