|  | @@ -0,0 +1,120 @@
 | 
												
													
														
															|  | 
 |  | +'use strict';
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +/**
 | 
												
													
														
															|  | 
 |  | + *
 | 
												
													
														
															|  | 
 |  | + *
 | 
												
													
														
															|  | 
 |  | + * @author Mai
 | 
												
													
														
															|  | 
 |  | + * @date
 | 
												
													
														
															|  | 
 |  | + * @version
 | 
												
													
														
															|  | 
 |  | + */
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +const RptMemBase = require('./base');
 | 
												
													
														
															|  | 
 |  | +const bindData = {};
 | 
												
													
														
															|  | 
 |  | +const Ledger = require('../ledger');
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +class rptMemPaymentSafe extends RptMemBase {
 | 
												
													
														
															|  | 
 |  | +    constructor(ctx) {
 | 
												
													
														
															|  | 
 |  | +        super(ctx, bindData);
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +    async doCheckTender(tenderId) {
 | 
												
													
														
															|  | 
 |  | +        if (this.ctx.paymentTender) return;
 | 
												
													
														
															|  | 
 |  | +        this.ctx.paymentTender = await this.ctx.service.paymentTender.doCheckTender(tenderId);
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +    async doCheckDetail(detailId) {
 | 
												
													
														
															|  | 
 |  | +        if (this.ctx.detail) return;
 | 
												
													
														
															|  | 
 |  | +        this.ctx.detail = await this.ctx.service.paymentDetail.doCheckDetail(detailId);
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +    async doBeforeLoadReport(params) {
 | 
												
													
														
															|  | 
 |  | +        await this.doCheckTender(params.tender_id);
 | 
												
													
														
															|  | 
 |  | +        await this.doCheckDetail(params.detail_id);
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +    async getSafeBills() {
 | 
												
													
														
															|  | 
 |  | +        const bills = this.ctx.detail.readOnly
 | 
												
													
														
															|  | 
 |  | +            ? await ctx.service.paymentSafeBills.getReadData(ctx.detail)
 | 
												
													
														
															|  | 
 |  | +            : await ctx.service.paymentSafeBills.getEditData(ctx.detail);
 | 
												
													
														
															|  | 
 |  | +        const setting = {
 | 
												
													
														
															|  | 
 |  | +            id: 'tree_id',
 | 
												
													
														
															|  | 
 |  | +            pid: 'tree_pid',
 | 
												
													
														
															|  | 
 |  | +            order: 'tree_order',
 | 
												
													
														
															|  | 
 |  | +            level: 'tree_level',
 | 
												
													
														
															|  | 
 |  | +            isLeaf: 'tree_is_leaf',
 | 
												
													
														
															|  | 
 |  | +            fullPath: 'tree_full_path',
 | 
												
													
														
															|  | 
 |  | +            rootId: -1,
 | 
												
													
														
															|  | 
 |  | +            calcFields: ['pre_tp', 'cur_tp', 'end_tp'],
 | 
												
													
														
															|  | 
 |  | +        };
 | 
												
													
														
															|  | 
 |  | +        const billsTree = new Ledger.billsTree(this.ctx, setting);
 | 
												
													
														
															|  | 
 |  | +        billsTree.loadDatas(bills);
 | 
												
													
														
															|  | 
 |  | +        billsTree.calculateAll();
 | 
												
													
														
															|  | 
 |  | +        return billsTree.getDefaultData();
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +    _analysisCompareData(datas, roles){
 | 
												
													
														
															|  | 
 |  | +        const findHis = function (role, history) {
 | 
												
													
														
															|  | 
 |  | +            let his = null;
 | 
												
													
														
															|  | 
 |  | +            for (const h of history) {
 | 
												
													
														
															|  | 
 |  | +                if (h.times < role.times || (h.times === role.times && h.order <= role.order)) {
 | 
												
													
														
															|  | 
 |  | +                    his = h;
 | 
												
													
														
															|  | 
 |  | +                } else {
 | 
												
													
														
															|  | 
 |  | +                    break;
 | 
												
													
														
															|  | 
 |  | +                }
 | 
												
													
														
															|  | 
 |  | +            }
 | 
												
													
														
															|  | 
 |  | +            return his;
 | 
												
													
														
															|  | 
 |  | +        };
 | 
												
													
														
															|  | 
 |  | +        for (const d of datas) {
 | 
												
													
														
															|  | 
 |  | +            if (!d.tree_is_leaf) continue;
 | 
												
													
														
															|  | 
 |  | +            d.cur_his.sort((x, y) => { return x.times === y.times ? x.order - y.order : x.times - y.times; });
 | 
												
													
														
															|  | 
 |  | +            for (const r of roles) {
 | 
												
													
														
															|  | 
 |  | +                if (r.latest) {
 | 
												
													
														
															|  | 
 |  | +                    d[`qty_${r.order}`] = d.cur_qty;
 | 
												
													
														
															|  | 
 |  | +                    d[`tp_${r.order}`] = d.cur_tp;
 | 
												
													
														
															|  | 
 |  | +                } else {
 | 
												
													
														
															|  | 
 |  | +                    const rHis = findHis(r, d.cur_his);
 | 
												
													
														
															|  | 
 |  | +                    if (rHis) {
 | 
												
													
														
															|  | 
 |  | +                        d[`qty_${r.order}`] = rHis.qty;
 | 
												
													
														
															|  | 
 |  | +                        d[`tp_${r.order}`] = rHis.tp;
 | 
												
													
														
															|  | 
 |  | +                    }
 | 
												
													
														
															|  | 
 |  | +                }
 | 
												
													
														
															|  | 
 |  | +            }
 | 
												
													
														
															|  | 
 |  | +        }
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +    async getSafeBillsCompare() {
 | 
												
													
														
															|  | 
 |  | +        const bills = await ctx.service.paymentSafeBills.getCompareData(ctx.detail);
 | 
												
													
														
															|  | 
 |  | +        const roles = await ctx.service.paymentDetailAudit.getViewFlow(ctx.detail);
 | 
												
													
														
															|  | 
 |  | +        this._analysisCompareData(bills, roles);
 | 
												
													
														
															|  | 
 |  | +        const setting = {
 | 
												
													
														
															|  | 
 |  | +            id: 'tree_id',
 | 
												
													
														
															|  | 
 |  | +            pid: 'tree_pid',
 | 
												
													
														
															|  | 
 |  | +            order: 'tree_order',
 | 
												
													
														
															|  | 
 |  | +            level: 'tree_level',
 | 
												
													
														
															|  | 
 |  | +            isLeaf: 'tree_is_leaf',
 | 
												
													
														
															|  | 
 |  | +            fullPath: 'tree_full_path',
 | 
												
													
														
															|  | 
 |  | +            rootId: -1,
 | 
												
													
														
															|  | 
 |  | +        };
 | 
												
													
														
															|  | 
 |  | +        setting.calcFields = roles.map(x => { return `tp_${x.order}`});
 | 
												
													
														
															|  | 
 |  | +        setting.calcFields.push('pre_tp');
 | 
												
													
														
															|  | 
 |  | +        const compareTree = new Ledger.billsTree(this.ctx, setting);
 | 
												
													
														
															|  | 
 |  | +        compareTree.loadDatas(bills);
 | 
												
													
														
															|  | 
 |  | +        compareTree.calculateAll();
 | 
												
													
														
															|  | 
 |  | +        return compareTree.getDefaultData();
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +    getCommonData(params, tableName, fields) {
 | 
												
													
														
															|  | 
 |  | +        switch (tableName) {
 | 
												
													
														
															|  | 
 |  | +            case 'mem_payment_tender':
 | 
												
													
														
															|  | 
 |  | +                return this.ctx.paymentTender;
 | 
												
													
														
															|  | 
 |  | +            case 'mem_payment_detail':
 | 
												
													
														
															|  | 
 |  | +                return this.ctx.detail;
 | 
												
													
														
															|  | 
 |  | +            case 'mem_payment_safe_bills':
 | 
												
													
														
															|  | 
 |  | +                return this.getSafeBills();
 | 
												
													
														
															|  | 
 |  | +            case 'mem_payment_safe_bills_compare':
 | 
												
													
														
															|  | 
 |  | +                return this.getSafeBillsCompare();
 | 
												
													
														
															|  | 
 |  | +            default:
 | 
												
													
														
															|  | 
 |  | +                return [];
 | 
												
													
														
															|  | 
 |  | +        }
 | 
												
													
														
															|  | 
 |  | +    }
 | 
												
													
														
															|  | 
 |  | +}
 | 
												
													
														
															|  | 
 |  | +
 | 
												
													
														
															|  | 
 |  | +module.exports = rptMemPaymentSafe;
 |