|  | @@ -0,0 +1,197 @@
 | 
	
		
			
				|  |  | +'use strict';
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * @author Mai
 | 
	
		
			
				|  |  | + * @date
 | 
	
		
			
				|  |  | + * @version
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const { app, assert } = require('egg-mock/bootstrap');
 | 
	
		
			
				|  |  | +const addData = {
 | 
	
		
			
				|  |  | +    name: 'test_stage_pay',
 | 
	
		
			
				|  |  | +    category: null,
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +const mockData = {};
 | 
	
		
			
				|  |  | +let testPayId;
 | 
	
		
			
				|  |  | +const _ = require('lodash');
 | 
	
		
			
				|  |  | +const Calc = require('../../../app/lib/pay_calc');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +describe('test/app/service/stage_pay.test.js', () => {
 | 
	
		
			
				|  |  | +    // 准备测试数据,新增测试标段
 | 
	
		
			
				|  |  | +    before(function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext();
 | 
	
		
			
				|  |  | +        // 模拟登录session
 | 
	
		
			
				|  |  | +        const postData = {
 | 
	
		
			
				|  |  | +            account: '734406061@qq.com',
 | 
	
		
			
				|  |  | +            project: 'T201711273363',
 | 
	
		
			
				|  |  | +            project_password: 'mai654321',
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  | +        ctx.session = {};
 | 
	
		
			
				|  |  | +        const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
 | 
	
		
			
				|  |  | +        assert(loginResult);
 | 
	
		
			
				|  |  | +        mockData.session = ctx.session;
 | 
	
		
			
				|  |  | +        // 移除旧测试数据
 | 
	
		
			
				|  |  | +        const testTender = yield ctx.service.tender.getDataByCondition({
 | 
	
		
			
				|  |  | +            name: addData.name,
 | 
	
		
			
				|  |  | +            project_id: ctx.session.sessionProject.id,
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        if (testTender) {
 | 
	
		
			
				|  |  | +            const result = yield ctx.service.tender.deleteTenderNoBackup(testTender.id);
 | 
	
		
			
				|  |  | +            assert(result);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 新增测试用标段
 | 
	
		
			
				|  |  | +        const result = yield ctx.service.tender.add(addData);
 | 
	
		
			
				|  |  | +        assert(result);
 | 
	
		
			
				|  |  | +        const tender = yield ctx.service.tender.getDataByCondition({
 | 
	
		
			
				|  |  | +            name: addData.name,
 | 
	
		
			
				|  |  | +            project_id: ctx.session.sessionProject.id,
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        const info = yield ctx.service.tenderInfo.getTenderInfo(tender.id);
 | 
	
		
			
				|  |  | +        mockData.tender = {id: tender.id, data: tender, info: info};
 | 
	
		
			
				|  |  | +        ctx.tender = mockData.tender;
 | 
	
		
			
				|  |  | +        // 添加测试清单,新增清单201-1,1-1的子节点,添加单价、数量
 | 
	
		
			
				|  |  | +        const node = yield ctx.service.ledger.getDataByCondition({
 | 
	
		
			
				|  |  | +            tender_id: ctx.tender.id,
 | 
	
		
			
				|  |  | +            code: '1-1',
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        const addResult = yield ctx.service.ledger.addNode(ctx.tender.id, node.ledger_id);
 | 
	
		
			
				|  |  | +        assert(addResult.create.length === 1);
 | 
	
		
			
				|  |  | +        let newNode = addResult.create[0];
 | 
	
		
			
				|  |  | +        yield ctx.service.ledger.updateCalc(ctx.tender.id, {
 | 
	
		
			
				|  |  | +            id: newNode.id, ledger_id: newNode.ledger_id, tender_id: newNode.tender_id,
 | 
	
		
			
				|  |  | +            b_code: '201-1', name: '清单X', unit: 'm2', sgfh_qty: 3000, unit_price: 2.5
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        yield ctx.service.ledger.downLevelNode(ctx.tender.id, newNode.ledger_id);
 | 
	
		
			
				|  |  | +        newNode = yield ctx.service.ledger.getDataById(newNode.id);
 | 
	
		
			
				|  |  | +        assert(newNode.ledger_pid === node.ledger_id);
 | 
	
		
			
				|  |  | +        assert(newNode.total_price === 7500);
 | 
	
		
			
				|  |  | +        // 新增测试期
 | 
	
		
			
				|  |  | +        const stage = yield ctx.service.stage.addStage(mockData.tender.id, '2019-06', '2019-06-10 ~ 2019-06-30');
 | 
	
		
			
				|  |  | +        assert(stage);
 | 
	
		
			
				|  |  | +        mockData.stage = stage;
 | 
	
		
			
				|  |  | +        mockData.stage.curTimes = 1;
 | 
	
		
			
				|  |  | +        mockData.stage.curOrder = 0;
 | 
	
		
			
				|  |  | +        ctx.stage = mockData.stage;
 | 
	
		
			
				|  |  | +        // 计量清单201-1
 | 
	
		
			
				|  |  | +        yield ctx.service.stageBills.updateStageData({lid: newNode.id, contract_qty: 200});
 | 
	
		
			
				|  |  | +        const stageNode = yield ctx.service.stageBills.getLastestStageData(ctx.tender.id, ctx.stage.id, newNode.id);
 | 
	
		
			
				|  |  | +        assert(stageNode.contract_tp === 500);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    // 第一期/原报
 | 
	
		
			
				|  |  | +    // 测试查询方法
 | 
	
		
			
				|  |  | +    it('test getAuditorStagePay', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        const pays = yield ctx.service.pay.getAllDataByCondition({ where: {tid: ctx.tender.id}});
 | 
	
		
			
				|  |  | +        let stagePays = yield ctx.service.stagePay.getAuditorStagePay(_.map(pays, 'id'), ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 5);
 | 
	
		
			
				|  |  | +        stagePays = yield ctx.service.stagePay.getAuditorStagePay(_.map(pays, 'id'), ctx.stage.id, ctx.stage.curTimes, 1);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 0);
 | 
	
		
			
				|  |  | +        const bqwc = _.find(pays, {name: '本期完成计量'});
 | 
	
		
			
				|  |  | +        let stagePay = yield ctx.service.stagePay.getAuditorStagePay(bqwc.id, ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
 | 
	
		
			
				|  |  | +        assert(stagePay);
 | 
	
		
			
				|  |  | +        assert(stagePay.name === '本期完成计量');
 | 
	
		
			
				|  |  | +        stagePay = yield ctx.service.stagePay.getAuditorStagePay(bqwc.id, ctx.stage.id, ctx.stage.curTimes, 1);
 | 
	
		
			
				|  |  | +        assert(!stagePay);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    it('test getAuditorStageData', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        let stagePays = yield ctx.service.stagePay.getAuditorStageData(ctx.stage.id, ctx.stage.curTimes, ctx.stage.curOrder);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 5);
 | 
	
		
			
				|  |  | +        stagePays = yield ctx.service.stagePay.getAuditorStageData(ctx.stage.id, ctx.stage.curTimes, 1);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 0);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    it('test getStagePay', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        const pays = yield ctx.service.pay.getAllDataByCondition({ where: {tid: ctx.tender.id}});
 | 
	
		
			
				|  |  | +        let stagePays = yield ctx.service.stagePay.getStagePay(ctx.stage, _.map(pays, 'id'));
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 5);
 | 
	
		
			
				|  |  | +        const bqwc = _.find(pays, {name: '本期完成计量'});
 | 
	
		
			
				|  |  | +        let stagePay = yield ctx.service.stagePay.getStagePay(ctx.stage, bqwc.id);
 | 
	
		
			
				|  |  | +        assert(stagePay);
 | 
	
		
			
				|  |  | +        assert(stagePay.name === '本期完成计量');
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    it('test getStagePays', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        let stagePays = yield ctx.service.stagePay.getStagePays(ctx.stage);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 5);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    it('test getLastestStageData', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        const stagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 5);
 | 
	
		
			
				|  |  | +        let node = _.find(stagePays, {name: '本期完成计量'});
 | 
	
		
			
				|  |  | +        assert(node);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    // syncAdd 由 pay.add 调用测试
 | 
	
		
			
				|  |  | +    it('test syncAdd', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        const orgStagePays = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        const result = yield ctx.service.pay.add();
 | 
	
		
			
				|  |  | +        assert(result);
 | 
	
		
			
				|  |  | +        testPayId = result.pid;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        const stagePays = yield ctx.service.stagePay.getAllDataByCondition({where: {
 | 
	
		
			
				|  |  | +            tid: ctx.tender.id,
 | 
	
		
			
				|  |  | +            sid: ctx.stage.id,
 | 
	
		
			
				|  |  | +            stimes: ctx.stage.curTimes,
 | 
	
		
			
				|  |  | +            sorder: ctx.stage.curOrder,
 | 
	
		
			
				|  |  | +        }});
 | 
	
		
			
				|  |  | +        assert(stagePays.length === orgStagePays.length + 1);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    it('test save', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        const data = {
 | 
	
		
			
				|  |  | +            pid: testPayId, name: '合同支付测试', expr: 'bqwc*5%', invalidField: 'invalid'
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  | +        yield ctx.service.stagePay.save(data);
 | 
	
		
			
				|  |  | +        const stagePay = yield ctx.service.stagePay.getStagePay(ctx.stage, testPayId);
 | 
	
		
			
				|  |  | +        assert(stagePay);
 | 
	
		
			
				|  |  | +        assert(stagePay.name === data.name);
 | 
	
		
			
				|  |  | +        assert(stagePay.expr === data.expr);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    it('check test Result with Calc', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        const calc = new Calc(ctx, ctx.tender.info.decimal);
 | 
	
		
			
				|  |  | +        const dealPay = yield ctx.service.stagePay.getStageLastestPays(ctx.stage.id);
 | 
	
		
			
				|  |  | +        yield calc.calculateAll(dealPay);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        const bqwc = app._.find(dealPay, {name: '本期完成计量'});
 | 
	
		
			
				|  |  | +        assert(bqwc.tp === 500);
 | 
	
		
			
				|  |  | +        const pay1 = app._.find(dealPay, {pid: testPayId});
 | 
	
		
			
				|  |  | +        assert(pay1.tp === 25);
 | 
	
		
			
				|  |  | +        const bqyf = app._.find(dealPay, {name: '本期应付'});
 | 
	
		
			
				|  |  | +        assert(bqyf.tp === 525);
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +    // calcAllStagePays, copyAuditStagePays 在 stageAudit.start/stageAudit.check 调用测试
 | 
	
		
			
				|  |  | +    // 第一期/一审
 | 
	
		
			
				|  |  | +    it('test calcAllStagePays & copyAuditStagePays', function* () {
 | 
	
		
			
				|  |  | +        const ctx = app.mockContext(mockData);
 | 
	
		
			
				|  |  | +        const result1 = yield ctx.service.stageAudit.addAuditor(ctx.stage.id, 14, ctx.stage.curTimes);
 | 
	
		
			
				|  |  | +        assert(result1);
 | 
	
		
			
				|  |  | +        const result2 = yield ctx.service.stageAudit.addAuditor(ctx.stage.id, 15, ctx.stage.curTimes);
 | 
	
		
			
				|  |  | +        assert(result2);
 | 
	
		
			
				|  |  | +        yield ctx.service.stageAudit.start(ctx.stage.id, ctx.stage.curTimes);
 | 
	
		
			
				|  |  | +        // 验证calcAllStagePays的结果
 | 
	
		
			
				|  |  | +        let stagePays, sp;
 | 
	
		
			
				|  |  | +        stagePays = yield ctx.service.stagePay.getStagePays(ctx.stage);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 6);
 | 
	
		
			
				|  |  | +        sp = _.find(stagePays, {name: '本期完成计量'});
 | 
	
		
			
				|  |  | +        assert(sp.tp === 500);
 | 
	
		
			
				|  |  | +        sp = _.find(stagePays, {pid: testPayId});
 | 
	
		
			
				|  |  | +        assert(sp.tp === 25);
 | 
	
		
			
				|  |  | +        sp = _.find(stagePays, {name: '本期应付'});
 | 
	
		
			
				|  |  | +        assert(sp.tp === 525);
 | 
	
		
			
				|  |  | +        // 验证copyAuditStagePays的结果
 | 
	
		
			
				|  |  | +        ctx.stage.curOrder = 1;
 | 
	
		
			
				|  |  | +        stagePays = yield ctx.service.stagePay.getStagePays(ctx.stage);
 | 
	
		
			
				|  |  | +        assert(stagePays.length === 6);
 | 
	
		
			
				|  |  | +        sp = _.find(stagePays, {name: '本期完成计量'});
 | 
	
		
			
				|  |  | +        assert(sp.expr === 'bqwc');
 | 
	
		
			
				|  |  | +        sp = _.find(stagePays, {pid: testPayId});
 | 
	
		
			
				|  |  | +        assert(sp.expr === 'bqwc*5%');
 | 
	
		
			
				|  |  | +    });
 | 
	
		
			
				|  |  | +});
 |