单据插件开发案例1

时隔两年半再来写博客,天知道这些日子我经历了些啥,生活轨迹固定,未来仍然不清晰。

金蝶苍穹的插件开发,基础教程都在单据界面插件-插件基类这里了,我主要发些案例代码。

这个项目中,需要根据单据申请人的部门,设置初始成本中心,根据申请人的签约公司设置单据的核算组织,根据核算组织设置单据的本位币,当核算组织发生变化时自动调整本位币,以及在单据提交时,执行一些逻辑。

 

package zzy5.prj01.cloud01.app01;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.operate.Submit;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EventObject;

/**
 * 单据界面插件
 * 范围:费用类单据,
 * 功能:
 * 1、根据申请部门设置成本中心
 * 2、根据申请人签约公司设置单据核算组织
 * 3、根据核算组织设置单据的本位币
 * 4、当核算组织发生变化时自动调整本位币
 * 5、在单据提交时,判断当前日期是否到达行程结束日期,如果到了,则修改字段“是否到达行程确认时间”为是,否则修改字段“是否到达行程确认时间”为否
 */
public class CostCenterSetBillPlugin extends AbstractBillPlugIn implements Plugin {

    public void afterCreateNewData(EventObject e) {

        /*
        根据申请部门设置成本中心
         */
        DynamicObject costdept = (DynamicObject)this.getModel().getValue("costdept");//获取申请部门基础资料,当字段类型为基础资料时,使用DynamicObject类型
        if (null != costdept) { //如果申请部门不为空
            String costdept_number = costdept.getString("number");//获取申请部门编码
            //根据申请部门 编码,查询成本中心映射配置,获取成本中心id
            DynamicObject costcenter_costdept_map = BusinessDataServiceHelper.loadSingle(
                    "bos_costcentersourcemap", //单据体标识,成本中心映射行政组织
                    //"costcenter.id", //要查询的单据体字段,可省略
                    new QFilter[]{new QFilter("entryentity.sourcedata.number", QFilter.equals,
                            costdept_number),new QFilter("enable", QFilter.equals, 1) //仅查询生效状态的
                    }
            );
            if (null != costcenter_costdept_map) { //如果成本中心映射配置不为空
                Long costcenter_id = costcenter_costdept_map.getLong("costcenter.id");
                this.getModel().setValue("std_costcenter", costcenter_id); //设置成本中心,为基础资料字段赋值,通常使用id
            }else {
                this.getView().showTipNotification("未找到申请部门对应的成本中心,请检查【成本中心映射配置】");
            }
        }

        /*
        根据签约公司设置核算组织
         */
        DynamicObject applier= (DynamicObject)this.getModel().getValue("applier");//获取申请人,基础资料类型
        if (null != applier) { //如果申请人不为空
            DynamicObject applier_er = BusinessDataServiceHelper.loadSingle(
                    "bos_user", //单据体标识
                    //"costcenter.id", //要查询的单据体字段
                    new QFilter[]{new QFilter("id", QFilter.equals,applier.getPkValue())}
            ); //根据id查询申请人的人员资料
            DynamicObject signcompany = applier_er.getDynamicObject("zzy5_signed_co"); //获取人员的签约公司,基础资料类型
            if (null != signcompany) { //如果签约公司不为空
                this.getModel().setValue("costcompany", signcompany.getPkValue()); //getPkValue()获取id
            }else{
                this.getView().showTipNotification("未找到申请人签约公司,请检查【人员】基础资料,或者手工填写下核算组织。");
                this.getModel().setValue("costcompany", null);
            }
        }

        //根据核算组织设置本位币
        DynamicObject costcompany = (DynamicObject)this.getModel().getValue("costcompany");  //获取核算组织,基础资料类型
        if(!ObjectUtils.isEmpty(costcompany)){ //如果核算组织不为空
            DynamicObject org_currency = BusinessDataServiceHelper.loadSingle(
                    "bd_accountingsys_base", //单据体标识
                    //"costcenter.id", //要查询的单据体字段
                    new QFilter[]{new QFilter("baseacctorg.id", QFilter.equals,costcompany.getPkValue())}
            );
            if(!ObjectUtils.isEmpty(org_currency)){
                DynamicObject currency = org_currency.getDynamicObject("basecurrrency");
                this.getModel().setValue("currency",currency.getPkValue()); //设置本位币
            }
        }

        String billName = this.getView().getEntityId();
        //如果是员工报销单、员工借款单,或对公报销单,新建时,明细中第一行的币种修改为本位币
        if(billName.equals("er_dailyreimbursebill") || billName.equals("er_dailyloanbill") || billName.equals("er_publicreimbursebill")){
            DynamicObjectCollection expense_entity = this.getModel().getDataEntity().getDynamicObjectCollection("expenseentryentity");
            if(!ObjectUtils.isEmpty(expense_entity)){
                for(DynamicObject dataEntity : expense_entity){
                    dataEntity.set("entrycurrency",this.getModel().getValue("currency"));
                }
            }
        }

        super.afterCreateNewData(e);
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        if (e.getProperty().getName().equals("costcompany")) {
            //根据核算组织设置本位币
            DynamicObject costcompany = (DynamicObject)this.getModel().getValue("costcompany");
            if(!ObjectUtils.isEmpty(costcompany)){
                DynamicObject org_currency = BusinessDataServiceHelper.loadSingle(
                        "bd_accountingsys_base", //单据体标识
                        //"costcenter.id", //要查询的单据体字段
                        new QFilter[]{new QFilter("baseacctorg.id", QFilter.equals,costcompany.getPkValue())}
                );
                if(!ObjectUtils.isEmpty(org_currency)){
                    DynamicObject currency = org_currency.getDynamicObject("basecurrrency");
                    this.getModel().setValue("currency",currency.getPkValue()); //设置本位币
                }
            }
        }
    }

    @Override
    /**
     * 重写父类方法,在执行操作前进行预处理
     * 如果是出差申请单,则在提交前,先提示保存
     * @param args 操作事件参数,包含操作相关信息
     */
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {

        // 获取当前视图的实体ID,即单据名称
        String billName = this.getView().getEntityId();
        // 判断事件源是否为Submit类型
        if(args.getSource() instanceof Submit) {
            // 将事件源强制转换为Submit类型
            Submit opp = (Submit) args.getSource();
            // 判断操作是否为提交操作,且单据名称是否为"er_tripreqbill"
            if (opp.getOperateKey().equals("submit") && billName.equals("er_tripreqbill")) {

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date edate = null;
                try {
                    edate = sdf.parse("2000-01-01");
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                //this.getView().showTipNotification("edate" + edate.toString());
                DynamicObjectCollection doc = this.getModel().getDataEntity().getDynamicObjectCollection("tripentry");
                for(int i = 0; i < doc.size(); i++){
                    DynamicObject docEntry = doc.get(i);
                    Date end_date = docEntry.getDate("enddate");
                    //this.getView().showTipNotification("end_date" + end_date.toString());
                    if(end_date.compareTo(edate) > 0 ) {
                        edate = end_date;
                    }
                }

                this.getModel().setValue("renddate",edate);
                Date td = new Date();
                if(edate.compareTo(td)<0){
                    this.getModel().setValue("zzy5_xcqrsj","1");
                }else{
                    this.getModel().setValue("zzy5_xcqrsj","2");
                }
                /*String bNo = this.getModel().getDataEntity().getString("billno");
                if(bNo == null || bNo.isEmpty()){
                    this.getView().showErrorNotification("请先保存单据,再提交。");
                    args.setCancel(true);
                }*/
                // 如果条件满足,则执行保存操作
                //this.getView().invokeOperation("save");
                /*OperateOption saveOp = OperateOption.create();
                saveOp.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");
                OperationResult opReslut = this.getView().invokeOperation("save", saveOp);

                if (opReslut.isSuccess()) {
                    //this.getView().showSuccessNotification("操作成功");
                    ApprovalPluginUtil.executeClosePageAfterSubmitTask(false, this.getView());
                }*/

                //ApprovalPluginUtil.executeClosePageAfterSubmitTask(true, this.getView());
                //HashMap<String, String> map = new HashMap<>();
                //map.put("method", "closeWebView");
                //this.getView().executeClientCommand("callAPPApi", map);
                //this.getView().invokeOperation("submit");

                /*OperateOption saveOp = OperateOption.create();
                saveOp.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");
                OperationResult opReslut = this.getView().invokeOperation("save", saveOp);
                if (opReslut.isSuccess()) {
                    //this.getView().showSuccessNotification("操作成功");
                    ApprovalPluginUtil.executeClosePageAfterSubmitTask(false, this.getView());
                }*/
            }
        }

        // 调用父类的beforeDoOperation方法
        super.beforeDoOperation(args);
    }
}

 

posted @ 2026-05-12 18:56  Levice  阅读(57)  评论(0)    收藏  举报