package com.yem.wm.cm.salescommission;

import com.grapecity.documents.excel.Q;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * @author 张立攀
 * @version 1.0
 * @description  初期销售提成
 * @date 2023-08-22 16:36
 */
public class SalesCommissionPlugin  extends AbstractBillPlugIn {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String key = e.getProperty().getName();
        int row = e.getChangeSet()[0].getRowIndex();
        switch (key){
            case "yem_contractno":
                DynamicObject yem_contractno = (DynamicObject) this.getModel().getValue("yem_contractno");
                String billno = yem_contractno.getString("billno");
                List<QFilter> list  = new ArrayList<QFilter>();
                list.add(new QFilter("billno", QCP.equals,billno));
                DynamicObject datas = BusinessDataServiceHelper.loadSingle("yem_es_salesorder","",list.toArray(new QFilter[0]));
                if(datas != null){
                    datas = BusinessDataServiceHelper.loadSingle(datas.getPkValue(),datas.getDynamicObjectType().getName());
                    DynamicObject customer = datas.getDynamicObject("yem_customer");
                    DynamicObject country = datas.getDynamicObject("yem_clientcountry");
                    DynamicObject reccondition = datas.getDynamicObject("yem_reccondition");
                    DynamicObject currency = datas.getDynamicObject("yem_currency");
                    BigDecimal yem_exrate = datas.getBigDecimal("yem_exrate");
                    if(customer != null){
                        this.getModel().setValue("yem_client",customer.getPkValue(),row);
                    }
                    if(country != null){
                        this.getModel().setValue("yem_country",country.getPkValue(),row);
                    }
                    if(reccondition != null){
                        this.getModel().setValue("yem_paymenttype",reccondition.getPkValue(),row);
                    }
                    if(currency != null){
                        this.getModel().setValue("yem_currency",currency.getPkValue(),row);
                    }
                    this.getModel().setValue("yem_contractexrate",yem_exrate,row);
                }


                break;
        }
    }
}
