MaiXinRong 1 year ago
parent
commit
af5b2e407b
2 changed files with 197 additions and 1 deletions
  1. 195 0
      app/lib/settle.js
  2. 2 1
      app/public/js/settle_ledger.js

+ 195 - 0
app/lib/settle.js

@@ -0,0 +1,195 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+const Ledger = require('./ledger');
+
+class Settle {
+    constructor (ctx) {
+        this.ctx = ctx;
+    }
+
+    async _loadLatestStageData() {
+        this.settle.latestStage = await this.ctx.service.stage.getLastestCompleteStage(this.settle.tid);
+        this.ledgerColumn = [
+            'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
+            'code', 'b_code', 'name', 'unit', 'unit_price',
+            'quantity', 'total_price', 'memo', 'drawing_code', 'node_type'];
+        const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({ columns: this.ledgerColumn, where: { tender_id: this.settle.tid } });
+        const endLedgerData = await this.ctx.service.stageBillsFinal.getAllDataByCondition({ where: { sid: this.settle.latestStage.id } });
+        this.ctx.helper.assignRelaData(ledgerData, [
+            { data: endLedgerData, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'settle_', relaId: 'lid' },
+        ]);
+
+        this.posColumn = ['id', 'tid', 'lid', 'name', 'position', 'porder', 'quantity', 'add_stage_order', 'drawing_code'];
+        const posData = await this.ctx.service.pos.getAllDataByCondition({ columns: this.posColumn, where: { tid: this.settle.tid } });
+        const endPosData = await this.ctx.service.stagePosFinal.getAllDataByCondition({ where: { sid: this.settle.latestStage.id } });
+        this.ctx.helper.assignRelaData(posData, [
+            { data: endPosData, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'settle_', relaId: 'pid' },
+        ]);
+
+        this.stageTree = new Ledger.billsTree(this.ctx, {
+            id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1,
+            calcFields: ['total_price', 'settle_gather_tp', 'settle_contract_tp', 'settle_qc_tp']
+        });
+        this.stageTree.loadDatas(ledgerData);
+        this.stageTree.calculateAll();
+
+        this.stagePos = new Ledger.pos({ id: 'id', ledgerId: 'lid' });
+        this.stagePos.loadDatas(posData);
+    }
+
+    async _loadSettleSelect() {
+        const select = await this.ctx.service.settleSelect.getAllDataByCondition({ where: { settle_id: this.settle.id } });
+        for (const s of select) {
+            if (s.pid) {
+                const sp = this.stagePos.getPos(s.pid);
+                // console.log(s.pid, sp, this.stagePos.items);
+                if (!sp) continue;
+                sp.is_settle = true;
+                const sb = this.stageTree.nodes.find(x => { return x.id === sp.lid });
+                sb.is_settle = true;
+                const parents = this.stageTree.getAllParents(sb);
+                parents.forEach(p => { p.is_settle = true; });
+            }
+            if (s.lid) {
+                const sb = this.stageTree.nodes.find(x => { return x.id === s.lid });
+                sb.is_settle = true;
+                const parents = this.stageTree.getAllParents(sb);
+                parents.forEach(p => { p.is_settle = true; });
+                const posterity = this.stageTree.getPosterity(sb);
+                for (const p of posterity) {
+                    p.is_settle = true;
+                    const pos = this.stagePos.getLedgerPos(p.id);
+                    if (pos && pos.length > 0) pos.forEach(x => { x.is_settle = true; });
+                }
+            }
+        }
+    }
+
+    async _loadPreSettle() {
+        if (this.settle.settle_order <= 1) return;
+        const prePos = await this.ctx.service.settlePos.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
+        for (const pp of prePos) {
+            const sp = this.stagePos.getPos(pp.pid);
+            if (sp) {
+                sp.pre_settle = true;
+                sp.pre_contract_qty = sp.end_contract_qty;
+                sp.pre_qc_qty = sp.end_qc_qty;
+                sp.pre_qc_minus_qty = sp.pre_qc_minus_qty;
+            }
+        }
+        const preBills = await this.ctx.service.settleBills.getAllDataByCondition({ where: { tid: this.settle.tid, settle_order: this.settle.settle_order - 1 } });
+        for (const pb of preBills) {
+            const sb = this.stageTree.nodes.find(x => { return x.id === pb.lid });
+            if (sb) {
+                sb.pre_settle = true;
+                sb.pre_contract_qty = pb.pre_contract_qty;
+                sb.pre_contract_tp = pb.pre_contract_tp;
+                sb.pre_qc_qty = pb.pre_qc_qty;
+                sb.pre_qc_tp = pb.pre_qc_tp;
+                sb.pre_qc_minus_qty = pb.pre_qc_minus_qty;
+            }
+        }
+    }
+
+    calculateSettle() {
+        const helper = this.ctx.helper;
+        this.stagePos.calculateAll(function(p) {
+            if (p.is_settle || !p.pre_settle) {
+                p.cur_contract_qty = p.settle_contract_qty;
+                p.cur_qc_qty = p.settle_qc_qty;
+                p.cur_qc_minus_qty = p.settle_qc_minus_qty;
+            }
+            if (p.is_settle || p.pre_settle) {
+                p.end_contract_qty = helper.add(p.cur_contract_qty, p.pre_contract_qty);
+                p.end_qc_qty = helper.add(p.cur_qc_qty, p.pre_qc_qty);
+                p.end_qc_minus_qty = helper.add(p.cur_qc_minus_qty, p.pre_qc_minus_qty);
+            }
+        });
+        const self = this, decimal = this.ctx.tender.info.decimal;
+        this.stageTree.calculateAll(function(b) {
+            if (b.children && b.children.length > 0) return;
+
+            if (b.is_settle) {
+                const posRange = self.stagePos.getLedgerPos(b.id);
+                if (posRange && posRange.length > 0) {
+                    posRange.forEach(p => {
+                        b.cur_contract_qty = helper.add(b.cur_contract_qty, p.cur_contract_qty);
+                        b.cur_qc_qty = helper.add(b.cur_qc_qty, p.cur_qc_qty);
+                        b.cur_qc_minus_qty = helper.add(b.cur_qc_minus_qty, p.cur_qc_minus_qty);
+                    });
+                } else {
+                    if (!b.pre_settle) {
+                        b.cur_contract_qty = b.settle_contract_qty;
+                        b.cur_qc_qty = b.settle_qc_qty;
+                        b.cur_qc_minus_qty = b.settle_qc_minus_qty;
+                    }
+                }
+                b.cur_contract_tp = helper.mul(b.unit_price, b.cur_contract_qty, decimal.tp);
+                b.cur_qc_tp = helper.mul(b.unit_price, b.cur_qc_qty, decimal.tp);
+            }
+            if (b.is_settle || b.pre_settle) {
+                b.end_contract_qty = helper.add(b.cur_contract_qty, b.pre_contract_qty);
+                b.end_contract_tp = helper.add(b.cur_contract_tp, b.pre_contract_tp);
+                b.end_qc_qty = helper.add(b.cur_qc_qty, b.pre_qc_qty);
+                b.end_qc_tp = helper.add(b.cur_qc_tp, b.pre_qc_tp);
+                b.end_qc_minus_qty = helper.add(b.cur_qc_minus_qty, b.pre_qc_minus_qty);
+            }
+        })
+    }
+
+    getSettleData() {
+        const settleBills = [];
+        for (const node of this.stageTree.nodes) {
+            if (!node.is_settle && !node.pre_settle) continue;
+            settleBills.push({
+                tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
+                lid: node.id, tree_id: node.ledger_id, tree_pid: node.ledger_pid, tree_full_path: node.full_path,
+                tree_is_leaf: node.is_leaf, tree_level: node.level, tree_order: node.order,
+                code: node.code || '', b_code: node.b_code || '', name: node.name || '', unit: node.unit || '',
+                unit_price: node.unit_price || 0, quantity: node.quantity || 0, total_price: node.total_price || 0,
+                drawing_code: node.drawing_code || '', memo: node.memo || '', node_type: node.node_type || 0,
+                cur_contract_qty: node.cur_contract_qty || 0, cur_contract_tp: node.cur_contract_tp || 0,
+                cur_qc_qty: node.cur_qc_qty || 0, cur_qc_tp: node.cur_qc_tp || 0, cur_qc_minus_qty: node.cur_qc_minus_qty || 0,
+                pre_contract_qty: node.pre_contract_qty || 0, pre_contract_tp: node.pre_contract_tp || 0,
+                pre_qc_qty: node.pre_qc_qty || 0, pre_qc_tp: node.pre_qc_tp || 0, pre_qc_minus_qty: node.pre_qc_minus_qty || 0,
+                end_contract_qty: node.end_contract_qty || 0, end_contract_tp: node.end_contract_tp || 0,
+                end_qc_qty: node.end_qc_qty || 0, end_qc_tp: node.end_qc_tp || 0, end_qc_minus_qty: node.end_qc_minus_qty || 0,
+                is_settle: node.is_settle ? 1 : 0, pre_settle: node.pre_settle ? 1 : 0,
+            });
+        }
+        const settlePos = [];
+        for (const pos of this.stagePos.datas) {
+            if (!pos.is_settle && !pos.pre_settle) continue;
+            settlePos.push({
+                tid: this.settle.tid, settle_id: this.settle.id, settle_order: this.settle.settle_order,
+                lid: pos.lid, pid: pos.id,
+                name: pos.name || '', drawing_code: pos.drawing_code || '', position: pos.position || '',
+                cur_contract_qty: pos.cur_contract_qty || 0, cur_qc_qty: pos.cur_qc_qty || 0, cur_qc_minus_qty: pos.cur_qc_minus_qty || 0,
+                pre_contract_qty: pos.pre_contract_qty || 0, pre_qc_qty: pos.pre_qc_qty || 0, pre_qc_minus_qty: pos.pre_qc_minus_qty || 0,
+                end_contract_qty: pos.end_contract_qty || 0, end_qc_qty: pos.end_qc_qty || 0, end_qc_minus_qty: pos.end_qc_minus_qty || 0,
+                is_settle: pos.is_settle ? 1 : 0, pre_settle: pos.pre_settle ? 1 : 0,
+            });
+        }
+        return [settleBills, settlePos];
+    }
+
+    async doSettle(settle) {
+        this.settle = settle;
+
+        await this._loadLatestStageData(settle);
+        await this._loadSettleSelect();
+        await this._loadPreSettle();
+        this.calculateSettle();
+        return this.getSettleData();
+    }
+}
+
+module.exports = Settle;

+ 2 - 1
app/public/js/settle_ledger.js

@@ -248,7 +248,8 @@ $(document).ready(() => {
             readOnly: false, // todo fileUploadPermission,
             locate: function (att) {
                 if (!att) return;
-                SpreadJsObj.locateTreeNode(slSheet, att.node.ledger_id, true);
+                SpreadJsObj.locateTreeNode(slSheet, att.node.tree_id, true);
+                settleBillsObj.loadRelaAtt();
                 settlePosObj.loadCurPosData();
             }
         });