Pārlūkot izejas kodu

汇总表,合同支付数据相关

MaiXinRong 5 gadi atpakaļ
vecāks
revīzija
29db0fb1c4

+ 5 - 0
app/service/report.js

@@ -130,6 +130,11 @@ module.exports = app => {
                                 customDefine.gather_select, customSelect ? customSelect.gather_select : null));
                             runnableKey.push(filter);
                             break;
+                        case 'mem_gather_stage_pay':
+                            runnableRst.push(service.rptGatherMemory.getGatherStagePay(memFieldKeys[filter],
+                                customDefine.gather_select, customSelect ? customSelect.gather_select : null));
+                            runnableKey.push(filter);
+                            break;
                         default:
                             break;
                     }

+ 173 - 30
app/service/rpt_gather_memory.js

@@ -9,8 +9,13 @@
  */
 
 const Ledger = require('../lib/ledger');
+const PayCalculator = require('../lib/pay_calc');
+
 const auditConst = require('../const/audit');
+const payConst = require('../const/deal_pay');
+
 const moment = require('moment');
+
 const indexPre = 'id_';
 
 const gatherUtils = {
@@ -125,6 +130,7 @@ module.exports = app => {
                 rootId: -1
             });
             this.resultTenderInfo = [];
+            this.resultDealpPay = [];
         }
 
         _checkSpecialTender(tender, special) {
@@ -434,7 +440,7 @@ module.exports = app => {
 
         async _gatherMonthTenderInfo(sTender, index, month, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const info = this._getBaseTenderInfo(tender);
+            const info = await this._getBaseTenderInfo(tender);
             const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
             this._getStageTenderInfo(stage, info);
             this.resultTenderInfo.push(info);
@@ -442,32 +448,8 @@ module.exports = app => {
 
         async _gatherZoneTenderInfo(sTender, index, zone) {
             const helper = this.ctx.helper;
-            /**
-             * 汇总并合并 相关数据
-             * @param {Array} index - 主数据
-             * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
-             */
-            const sumAssignRelaData = function (index, rela) {
-                const loadFields = function (datas, fields, prefix, relaId) {
-                    for (const d of datas) {
-                        const key = indexPre + d[relaId];
-                        const m = index[key];
-                        if (m) {
-                            for (const f of fields) {
-                                if (d[f] !== undefined) {
-                                    m[prefix + f] = helper.add(m[prefix + f], d[f]);
-                                }
-                            }
-                        }
-                    }
-                };
-                for (const r of rela) {
-                    loadFields(r.data, r.fields, r.prefix, r.relaId);
-                }
-            };
-
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const info = this._getBaseTenderInfo(tender);
+            const info = await this._getBaseTenderInfo(tender);
 
             const times = zone.split(' - ');
             if (times.length !== 2) throw '选择的汇总周期无效';
@@ -494,7 +476,7 @@ module.exports = app => {
 
         async _gatherFinalTenderInfo(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const info = this._getBaseTenderInfo(tender);
+            const info = await this._getBaseTenderInfo(tender);
             const stages = await this.db.select(this.ctx.service.stage.tableName, {
                 where: { tid: tender.id },
                 orders: [['order', 'desc']],
@@ -511,7 +493,7 @@ module.exports = app => {
 
         async _gatherCheckedFinalTenderInfo(sTender, index, hasPre) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const info = this._getBaseTenderInfo(tender);
+            const info = await this._getBaseTenderInfo(tender);
             const stages = await this.db.select(this.ctx.service.stage.tableName, {
                 where: { tid: tender.id },
                 orders: [['order', 'desc']],
@@ -528,14 +510,14 @@ module.exports = app => {
 
         async _gatherLedgerTenderInfo(sTender, index) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
-            const info = this._getBaseTenderInfo(tender);
+            const info = await this._getBaseTenderInfo(tender);
             this.resultTenderInfo.push(info);
         }
 
         async _gatherSpecialTenderInfo(sTender, sKey) {
             const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
 
-            const info = this._getBaseTenderInfo(tender);
+            const info = await this._getBaseTenderInfo(tender);
             info.spec = sKey;
             this.resultTenderInfo.push(info);
         }
@@ -572,6 +554,167 @@ module.exports = app => {
                     await this._gatherSpecialTenderInfo(tender, specialKey);
                 }
             }
+            return this.resultTenderInfo;
+        }
+
+        /**
+         * 合同支付
+         */
+        _gatherPayRecord(dealPay, fun) {
+            let rdp;
+            if (dealPay.ptype !== payConst.payType.normal) {
+                rdp = this.ctx.helper._.find(this.resultDealpPay, {ptype: dealPay.ptype});
+            } else {
+                rdp = this.ctx.helper._.find(this.resultDealpPay, {
+                    name: dealPay.name,
+                    minus: dealPay.minus ? true : false,
+                    is_yf: dealPay.is_yf ? true : false,
+                });
+            }
+            if (!rdp) {
+                rdp = {
+                    ptype: dealPay.ptype,
+                    name: dealPay.name,
+                    minus: dealPay.minus ? true : false,
+                    is_yf: dealPay.is_yf ? true : false,
+                };
+                this.resultDealpPay.push(rdp);
+            }
+            if (fun) fun(rdp, dealPay);
+        }
+
+        async _checkStagePayCalc(tender, stage, dealPay) {
+            if (!stage.readOnly) {
+                // 计算 本期金额
+                const payCalculator = new PayCalculator(this.ctx, stage, tender.info);
+                await payCalculator.calculateAll(dealPay);
+            }
+        }
+
+        async _gatherStagePay(index, tender, stage, hasPre) {
+            if (stage) {
+                const helper = this.ctx.helper;
+                await this.ctx.service.stage.doCheckStage(stage);
+
+                const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
+                await this._checkStagePayCalc(tender, stage, dealPay);
+                for (const dp of dealPay) {
+                    dp.end_tp = helper.add(dp.pre_tp, dp.tp);
+                    this._gatherPayRecord(dp, function (gatherData, sourceData) {
+                        const preFix = 't_' + index + '_';
+                        gatherData[preFix + '_id'] = tender.id;
+                        gatherData[preFix + '_name'] = tender.name;
+
+                        gatherData[preFix + 'tp'] = helper.add(gatherData[preFix + 'tp'], sourceData.tp);
+                        gatherData[preFix + 'pre_tp'] = helper.add(gatherData[preFix + 'pre_tp'], sourceData.pre_tp);
+                        gatherData[preFix + 'end_tp'] = helper.add(gatherData[preFix + 'end_tp'], sourceData.end_tp);
+
+                        gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
+                        gatherData['s_' + 'pre_tp'] = helper.add(gatherData['s_' + 'pre_tp'], sourceData.pre_tp);
+                        gatherData['s_' + 'end_tp'] = helper.add(gatherData['s_' + 'end_tp'], sourceData.end_tp);
+                    });
+                }
+            }
+        }
+
+        async _gatherMonthStagePay(sTender, index, month, hasPre) {
+            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
+            const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
+            await this._gatherStagePay(index, tender, stage, hasPre);
+        }
+
+        async _gatherZoneStagePay(sTender, index, zone) {
+            const helper = this.ctx.helper;
+
+            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
+
+            const times = zone.split(' - ');
+            if (times.length !== 2) throw '选择的汇总周期无效';
+            const beginTime = moment(times[0], 'YYYY-MM').date();
+            const endTime = moment(times[1], 'YYYY-MM').date();
+
+            const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
+            for (const stage of stages) {
+                const sTime = moment(stage.s_time, 'YYYY-MM').date();
+                if (sTime >= beginTime && sTime <= endTime) {
+                    await this.ctx.service.stage.doCheckStage(stage);
+
+                    const dealPay = await this.ctx.service.stagePay.getStagePays(stage);
+                    await this._checkStagePayCalc(tender, stage, dealPay);
+                    for (const dp of dealPay) {
+                        dp.end_tp = helper.add(dp.pre_tp, dp.tp);
+                        this._gatherPayRecord(dp, function (gatherData, sourceData) {
+                            const preFix = 't_' + index + '_';
+                            gatherData[preFix + '_id'] = tender.id;
+                            gatherData[preFix + '_name'] = tender.name;
+
+                            gatherData[preFix + 'tp'] = helper.add(gatherData[preFix + 'tp'], sourceData.tp);
+                            gatherData['s_' + 'tp'] = helper.add(gatherData['s_' + 'tp'], sourceData.tp);
+                        });
+                    }
+                }
+            }
+        }
+
+        async _gatherFinalStagePay(sTender, index, hasPre) {
+            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
+            const stages = await this.db.select(this.ctx.service.stage.tableName, {
+                where: { tid: tender.id },
+                orders: [['order', 'desc']],
+            });
+            if (stages.length !== 0) {
+                const lastStage = stages[0];
+                if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
+                    stages.splice(0, 1);
+                }
+            }
+            await this._gatherStagePay(index, tender, stages[0], hasPre);
+        }
+
+        async _gatherCheckedFinalStagePay(sTender, index, hasPre) {
+            const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
+            const stages = await this.db.select(this.ctx.service.stage.tableName, {
+                where: { tid: tender.id },
+                orders: [['order', 'desc']],
+            });
+            if (stages.length !== 0) {
+                const lastStage = stages[0];
+                if (lastStage.status !== auditConst.stage.status.checked) {
+                    stages.splice(0, 1);
+                }
+            }
+            await this._gatherStagePay(index, tender, stages[0], hasPre);
+        }
+
+        async getGatherStagePay(memFieldKeys, gsDefine, gsCustom) {
+            if (!gsDefine || !gsDefine.enable) return [];
+            if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
+
+            this.resultTenderInfo = [];
+            const gsSetting = JSON.parse(gsDefine.setting);
+            let commonIndex = 0;
+            for (const tender of gsCustom.tenders) {
+                const specialKey = this._checkSpecialTender(tender, gsSetting.special);
+                if (specialKey === '') {
+                    switch (gsSetting.type) {
+                        case 'month':
+                            await this._gatherMonthStagePay(tender, commonIndex, gsCustom.month, gsSetting.hasPre);
+                            break;
+                        case 'zone':
+                            await this._gatherZoneStagePay(tender, commonIndex, gsCustom.zone);
+                            break;
+                        case 'final':
+                            await this._gatherFinalStagePay(tender, commonIndex, gsSetting.hasPre);
+                            break;
+                        case 'checked-final':
+                            await this._gatherCheckedFinalStagePay(tender, commonIndex, gsSetting.hasPre);
+                            break;
+                    }
+                    commonIndex++;
+                }
+                // 合同支付,只有在每一期中有数据,故特殊标段直接不汇总合同支付
+            }
+            return this.resultDealpPay;
         }
     }
 

+ 25 - 1
builder_report_index_define.js

@@ -531,6 +531,30 @@ const gather_tender_info = {
         { name: '截止本期-应付', field: 'end_yf', type: dataType.currency },
     ],
 };
+const gather_stage_pay = {
+    name: '汇总-期-合同支付 数据表(mem_gather_stage_pay)',
+    remark: '',
+    id: 38,
+    key: 'mem_gather_stage_pay',
+    prefix: '汇总-期-合同支付',
+    cols: [
+        { name: '名称', field: 'name', type: dataType.str },
+        { name: '类型', field: 'ptype', type: dataType.int },
+        { name: '是否扣款项', field: 'minus', type: dataType.int },
+        { name: '是否参与本期应付计算', field: 'minus', type: dataType.int },
+
+        { name: '标段id', field: 't_id', type: dataType.int },
+        { name: '标段-名称', field: 't_name', type: dataType.int },
+
+        { name: '(标段)本期-金额', field: 't_tp', type: dataType.currency },
+        { name: '(标段)截止上期-金额', field: 't_pre_tp', type: dataType.currency },
+        { name: '(标段)截止本期-金额', field: 't_end_tp', type: dataType.currency },
+
+        { name: '(合计)本期-金额', field: 's_tp', type: dataType.currency },
+        { name: '(合计)截止上期-金额', field: 's_pre_tp', type: dataType.currency },
+        { name: '(合计)截止本期-金额', field: 's_end_tp', type: dataType.currency },
+    ],
+};
 
 const recursiveMkdirSync = async function(pathName) {
     if (!fs.existsSync(pathName)) {
@@ -627,7 +651,7 @@ const defines = [stage_jgcl, stage_bonus, stage_other,
     stage_pos, stage_pos_compare,
     stage_pay,
     stage_im_zl, stage_im_tz, stage_im_tz_bills,
-    gather_stage_bills, gather_tender_info,
+    gather_stage_bills, gather_tender_info, gather_stage_pay
 ];
 for (const d of defines) {
     exportTableDefine(d);

+ 114 - 19
test/app/service/rpt_gather_memory.test.js

@@ -28,37 +28,132 @@ describe('test/app/service/rpt_gather_memory.test.js', () => {
         assert(loginResult);
         mockData.session = ctx.session;
     });
-    // 期部位明细数据
+    // 台账数据
     it('test getGatherStageBills - month', function* () {
         const ctx = app.mockContext(mockData);
 
+        // const select = {
+        //     tenders: [{tid: 2256}, {tid: 2257}, {tid: 2258, gs: true}],
+        //     type: 'month',
+        //     month: '2020-01',
+        // };
         const select = {
-            tenders: [{tid: 2256}, {tid: 2257}, {tid: 2258, gs: true}],
-            type: 'month',
-            month: '2020-01',
+            tenders: [{tid: 2256}, {tid: 2257}],
+            type: 'ledger',
         };
+        // const define = {
+        //     enable: true,
+        //     setting: JSON.stringify({
+        //         title: '请选择汇总的标段',
+        //         type: 'month',
+        //         special: [
+        //             {"title": "批复概算", "key": "gs"}
+        //         ]
+        //     })
+        // };
         const define = {
             enable: true,
-            setting: {
+            setting: JSON.stringify({
                 title: '请选择汇总的标段',
-                type: 'month',
-                special: [
-                    {"title": "批复概算", "key": "gs"}
-                ]
-            }
+                type: 'ledger',
+            })
         };
 
         const mem_gather_stage_bills = yield ctx.service.rptGatherMemory.getGatherStageBills([], define, select);
         yield ctx.helper.saveBufferFile(JSON.stringify(mem_gather_stage_bills, "", "\t"), path.join(savePath, 'mem_gather_stage_bills.json'));
 
-        const reportDataAnalysis = require('../../../app/lib/rpt_data_analysis');
-        // 配合部位明细
-        const reportData = {mem_gather_stage_bills: mem_gather_stage_bills};
-        reportDataAnalysis.analysisObj.gatherSelectConverse.fun(ctx, reportData, [], {table: ["mem_gather_stage_bills"]}, {
-            cDefine: { gather_select: select },
-            tplDefine: {gather_select: define },
-        });
-        yield ctx.helper.saveBufferFile(JSON.stringify(reportData, "", "\t"), path.join(savePath, 'mem_gather_stage_bills_coverse.json'));
-        assert(reportData.mem_gather_stage_bills.length === mem_gather_stage_bills.length * 2);
+        // const reportDataAnalysis = require('../../../app/lib/rpt_data_analysis');
+        // const reportData = {mem_gather_stage_bills: mem_gather_stage_bills};
+        // reportDataAnalysis.analysisObj.gatherSelectConverse.fun(ctx, reportData, [], {table: ["mem_gather_stage_bills"]}, {
+        //     cDefine: { gather_select: select },
+        //     tplDefine: {gather_select: define },
+        // });
+        // yield ctx.helper.saveBufferFile(JSON.stringify(reportData, "", "\t"), path.join(savePath, 'mem_gather_stage_bills_coverse.json'));
+        // assert(reportData.mem_gather_stage_bills.length === mem_gather_stage_bills.length * 2);
+    });
+    // 标段信息
+    it('test getGatherTenderInfo - month', function* () {
+        const ctx = app.mockContext(mockData);
+
+        // const select = {
+        //     tenders: [{tid: 2256}, {tid: 2257}, {tid: 2258, gs: true}],
+        //     type: 'month',
+        //     month: '2020-01',
+        // };
+        const select = {
+            tenders: [{tid: 2256}, {tid: 2257}],
+            type: 'ledger',
+        };
+        // const define = {
+        //     enable: true,
+        //     setting: JSON.stringify({
+        //         title: '请选择汇总的标段',
+        //         type: 'month',
+        //         special: [
+        //             {"title": "批复概算", "key": "gs"}
+        //         ]
+        //     })
+        // };
+        const define = {
+            enable: true,
+            setting: JSON.stringify({
+                title: '请选择汇总的标段',
+                type: 'ledger',
+            })
+        };
+
+        const mem_gather_stage_bills = yield ctx.service.rptGatherMemory.getGatherTenderInfo([], define, select);
+        yield ctx.helper.saveBufferFile(JSON.stringify(mem_gather_stage_bills, "", "\t"), path.join(savePath, 'mem_gather_tender_info.json'));
+
+        // const reportDataAnalysis = require('../../../app/lib/rpt_data_analysis');
+        // const reportData = {mem_gather_stage_bills: mem_gather_stage_bills};
+        // reportDataAnalysis.analysisObj.gatherSelectConverse.fun(ctx, reportData, [], {table: ["mem_gather_stage_bills"]}, {
+        //     cDefine: { gather_select: select },
+        //     tplDefine: {gather_select: define },
+        // });
+        // yield ctx.helper.saveBufferFile(JSON.stringify(reportData, "", "\t"), path.join(savePath, 'mem_gather_stage_bills_coverse.json'));
+        // assert(reportData.mem_gather_stage_bills.length === mem_gather_stage_bills.length * 2);
+    });    // 标段信息
+    it('test getGatherDealPay - final', function* () {
+        const ctx = app.mockContext(mockData);
+
+        // const select = {
+        //     tenders: [{tid: 2256}, {tid: 2257}, {tid: 2258, gs: true}],
+        //     type: 'month',
+        //     month: '2020-01',
+        // };
+        const select = {
+            tenders: [{tid: 2256}, {tid: 2257}],
+            type: 'final',
+        };
+        // const define = {
+        //     enable: true,
+        //     setting: JSON.stringify({
+        //         title: '请选择汇总的标段',
+        //         type: 'month',
+        //         special: [
+        //             {"title": "批复概算", "key": "gs"}
+        //         ]
+        //     })
+        // };
+        const define = {
+            enable: true,
+            setting: JSON.stringify({
+                title: '请选择汇总的标段',
+                type: 'final',
+            })
+        };
+
+        const mem_gather_stage_bills = yield ctx.service.rptGatherMemory.getGatherStagePay([], define, select);
+        yield ctx.helper.saveBufferFile(JSON.stringify(mem_gather_stage_bills, "", "\t"), path.join(savePath, 'mem_gather_deal_pay.json'));
+
+        // const reportDataAnalysis = require('../../../app/lib/rpt_data_analysis');
+        // const reportData = {mem_gather_stage_bills: mem_gather_stage_bills};
+        // reportDataAnalysis.analysisObj.gatherSelectConverse.fun(ctx, reportData, [], {table: ["mem_gather_stage_bills"]}, {
+        //     cDefine: { gather_select: select },
+        //     tplDefine: {gather_select: define },
+        // });
+        // yield ctx.helper.saveBufferFile(JSON.stringify(reportData, "", "\t"), path.join(savePath, 'mem_gather_stage_bills_coverse.json'));
+        // assert(reportData.mem_gather_stage_bills.length === mem_gather_stage_bills.length * 2);
     });
 });