226 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Java
		
	
	
	
			
		
		
	
	
			226 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Java
		
	
	
	
| package tqq9.lc123.cloud.app.plugin.operate.ap;
 | |
| 
 | |
| import com.google.gson.Gson;
 | |
| import kd.bos.dataentity.entity.DynamicObject;
 | |
| import kd.bos.dataentity.entity.DynamicObjectCollection;
 | |
| import kd.bos.dataentity.utils.StringUtils;
 | |
| import kd.bos.entity.ExtendedDataEntity;
 | |
| import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
 | |
| import kd.bos.entity.plugin.AddValidatorsEventArgs;
 | |
| import kd.bos.entity.plugin.PreparePropertysEventArgs;
 | |
| import kd.bos.entity.plugin.args.AfterOperationArgs;
 | |
| import kd.bos.entity.validate.AbstractValidator;
 | |
| import kd.bos.logging.Log;
 | |
| import kd.bos.logging.LogFactory;
 | |
| import kd.bos.orm.query.QCP;
 | |
| import kd.bos.orm.query.QFilter;
 | |
| import kd.bos.servicehelper.BusinessDataServiceHelper;
 | |
| import kd.bos.servicehelper.operation.SaveServiceHelper;
 | |
| import kd.sdk.plugin.Plugin;
 | |
| import tqq9.lc123.cloud.app.plugin.utils.HttpRequestUtils;
 | |
| 
 | |
| import java.io.IOException;
 | |
| import java.math.BigDecimal;
 | |
| import java.math.RoundingMode;
 | |
| import java.sql.*;
 | |
| import java.util.HashMap;
 | |
| 
 | |
| /**
 | |
|  * 付款单操作插件
 | |
|  */
 | |
| public class PayBillOperationPlugin extends AbstractOperationServicePlugIn implements Plugin {
 | |
|     private final static Log logger = LogFactory.getLog(PayBillOperationPlugin.class);
 | |
| 
 | |
|     private static String URL;
 | |
|     private static String USERNAME;
 | |
|     private static String PASSWORD;
 | |
|     private static String Use_URL;
 | |
|     private static String FLXT_TOKEN;
 | |
| 
 | |
| 
 | |
|     static {
 | |
|         DynamicObject url = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | |
|                 new QFilter[]{new QFilter("number", QCP.equals, "EqmDatabase_url")});
 | |
|         URL = url != null ? url.getString("name") : null;
 | |
|         DynamicObject userName = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | |
|                 new QFilter[]{new QFilter("number", QCP.equals, "EqmDatabase_username")});
 | |
|         USERNAME = userName != null ? userName.getString("name") : null;
 | |
|         DynamicObject passWord = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | |
|                 new QFilter[]{new QFilter("number", QCP.equals, "EqmDatabase_password")});
 | |
|         PASSWORD = passWord != null ? passWord.getString("name") : null;
 | |
|         url = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | |
|                 new QFilter[]{new QFilter("number", QCP.equals, "FLXT_BillCreate_Url")});
 | |
|         Use_URL = url != null ? url.getString("name") : null;
 | |
|         DynamicObject token = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | |
|                 new QFilter[]{new QFilter("number", QCP.equals, "FLXT_TOKEN")});
 | |
|         FLXT_TOKEN = token != null ? token.getString("name") : null;
 | |
| 
 | |
|     }
 | |
| 
 | |
| //    private final static HashMap<String, String> tokenMap = new HashMap<String, String>() {{
 | |
| //        put("Authorization", "Bearer b96dad1eb4f84c41bae651162aeacdd3");
 | |
| //    }};
 | |
| 
 | |
| 
 | |
|     @Override
 | |
|     public void onPreparePropertys(PreparePropertysEventArgs e) {
 | |
|         super.onPreparePropertys(e);
 | |
|         e.getFieldKeys().add("entry.e_payableamt");
 | |
|         e.getFieldKeys().add("tqq9_banktxn");
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     public void onAddValidators(AddValidatorsEventArgs e) {
 | |
|         super.onAddValidators(e);
 | |
|         e.addValidator(new PayBillOperationPlugin.BankTxnValidator());
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 付款退款校验流水号是否存在
 | |
|      */
 | |
|     class BankTxnValidator extends AbstractValidator {
 | |
|         @Override
 | |
|         public void validate() {
 | |
|             logger.info(URL);
 | |
|             logger.info(USERNAME);
 | |
|             logger.info(PASSWORD);
 | |
|             Connection conn = null;
 | |
|             Statement stmt = null;
 | |
|             ResultSet rs = null;
 | |
|             String operateKey = this.getOperateKey();
 | |
|             for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
 | |
|                 DynamicObject dataEntity1 = dataEntity.getDataEntity();
 | |
|                 DynamicObjectCollection entry = dataEntity1.getDynamicObjectCollection("entry");
 | |
|                 String corebillno = entry.get(0).getString("e_corebillno");
 | |
|                 BigDecimal actpayamt = dataEntity1.getBigDecimal("actpayamt");
 | |
|                 String billno = dataEntity1.getString("billno");//单据号
 | |
|                 if (StringUtils.isNotBlank(corebillno)) {
 | |
|                     DynamicObject pm_purorderbill = BusinessDataServiceHelper.loadSingle("pm_purorderbill", new QFilter[]{new QFilter("billno", QCP.equals, corebillno)});
 | |
|                     Boolean tqq9_isrebatecalculate = pm_purorderbill.getBoolean("tqq9_isrebatecalculate");//是否已计算返利
 | |
|                     if (pm_purorderbill != null && !tqq9_isrebatecalculate) {
 | |
|                         BigDecimal tqq9_hshfsyje = pm_purorderbill.getBigDecimal("tqq9_hshfsyje");//含税货返使用金额
 | |
|                         BigDecimal tqq9_hsxfsyje = pm_purorderbill.getBigDecimal("tqq9_hsxfsyje");//含税现返使用金额
 | |
|                         if (actpayamt.compareTo(tqq9_hshfsyje.add(tqq9_hsxfsyje)) < 0) {
 | |
|                             this.addErrorMessage(dataEntity, "单据号:" + billno + ",付款金额小于采购订单返利金额,不允许付款");
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
|                 for (DynamicObject dynamicObject : entry) {
 | |
|                     BigDecimal e_payableamt = dynamicObject.getBigDecimal("e_payableamt");//应付金额
 | |
|                     if (e_payableamt.compareTo(BigDecimal.ZERO) < 0) {
 | |
|                         String banktxn = dataEntity1.getString("tqq9_banktxn");//银行流水号
 | |
|                         if (StringUtils.isBlank(banktxn)) {
 | |
|                             this.addErrorMessage(dataEntity, "单据号:" + billno + ",银行流水为空,无法审核");
 | |
|                             break;
 | |
|                         }
 | |
|                         try {
 | |
|                             Class.forName("com.mysql.cj.jdbc.Driver");
 | |
|                             conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
 | |
|                             // 创建Statement对象
 | |
|                             stmt = conn.createStatement();
 | |
|                             String sql = "/*dialect*/SELECT * FROM v_c1cm_bank_serial where bank_voucher='" + banktxn + "'";
 | |
|                             logger.info("sql-" + sql);
 | |
| 
 | |
|                             rs = stmt.executeQuery(sql);
 | |
| 
 | |
|                             if (!rs.next()) {
 | |
|                                 this.addErrorMessage(dataEntity, "单据号:" + billno + ",银行流水:" + banktxn + ",在E企明系统中不存在,无法审核");
 | |
|                             }
 | |
|                             break;
 | |
|                         } catch (SQLException | ClassNotFoundException e) {
 | |
|                             logger.info("同步oa中间表出现异常-" + e.getMessage());
 | |
|                             e.printStackTrace();
 | |
|                         } finally {
 | |
|                             // 关闭资源
 | |
|                             try {
 | |
|                                 if (rs != null) {
 | |
|                                     rs.close();
 | |
|                                 }
 | |
|                                 if (stmt != null) {
 | |
|                                     stmt.close();
 | |
|                                 }
 | |
|                                 if (conn != null) {
 | |
|                                     conn.close();
 | |
|                                 }
 | |
|                             } catch (Exception e) {
 | |
|                                 e.printStackTrace();
 | |
|                             }
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
| 
 | |
| 
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     public void afterExecuteOperationTransaction(AfterOperationArgs e) {
 | |
|         super.afterExecuteOperationTransaction(e);
 | |
|         for (DynamicObject dataEntity : e.getDataEntities()) {
 | |
|             HashMap<String, String> tokenMap = new HashMap<String, String>();
 | |
|             tokenMap.put("Authorization", FLXT_TOKEN);
 | |
|             String billno = dataEntity.getString("billno");
 | |
|             BigDecimal actpayamt = dataEntity.getBigDecimal("actpayamt");
 | |
|             DynamicObjectCollection entry = dataEntity.getDynamicObjectCollection("entry");
 | |
|             String corebillno = entry.get(0).getString("e_corebillno");
 | |
|             BigDecimal oneHundred = new BigDecimal(100);
 | |
|             //采购订单
 | |
|             DynamicObject pm_purorderbill = BusinessDataServiceHelper.loadSingle("pm_purorderbill", new QFilter[]{new QFilter("billno", QCP.equals, corebillno)});
 | |
|             Boolean tqq9_isrebatecalculate = pm_purorderbill.getBoolean("tqq9_isrebatecalculate");//是否已计算返利
 | |
|             String operationKey = e.getOperationKey();
 | |
|             if (pm_purorderbill != null &&tqq9_isrebatecalculate&&StringUtils.equals("pay",operationKey)) {
 | |
|                 BigDecimal tqq9_hshfsyje = pm_purorderbill.getBigDecimal("tqq9_hshfsyje");//含税货返使用金额
 | |
|                 BigDecimal tqq9_hsxfsyje = pm_purorderbill.getBigDecimal("tqq9_hsxfsyje");//含税现返使用金额
 | |
|                 String tqq9_hfid = pm_purorderbill.getString("tqq9_hfid");//货返id
 | |
|                 String tqq9_xfid = pm_purorderbill.getString("tqq9_xfid");//现返id
 | |
|                 if (tqq9_hshfsyje.compareTo(BigDecimal.ZERO) > 0 && actpayamt.compareTo(BigDecimal.ZERO) > 0) {
 | |
|                     BigDecimal notaxamount = tqq9_hshfsyje.multiply(oneHundred.divide(oneHundred.add(new BigDecimal(13)), 10, RoundingMode.HALF_UP)).setScale(2, RoundingMode.HALF_UP);
 | |
|                     BigDecimal taxamount = tqq9_hshfsyje.subtract(notaxamount);//税额
 | |
|                     HashMap<String, Object> bodyMap = new HashMap<>();
 | |
|                     bodyMap.put("id", tqq9_hfid);
 | |
|                     bodyMap.put("lockMoney", tqq9_hshfsyje);
 | |
|                     bodyMap.put("lockTaxAmount", taxamount);
 | |
|                     Gson gson = new Gson();
 | |
|                     try {
 | |
|                         String bodyString = HttpRequestUtils.doGet(Use_URL, bodyMap, tokenMap);
 | |
|                         HashMap map = gson.fromJson(bodyString, HashMap.class);
 | |
|                         Boolean data = (Boolean) map.get("data");
 | |
|                         if (data == null || !data) {
 | |
|                             this.getOperationResult().setMessage("付款单:" + billno + ",审核时扣除订单金额失败");
 | |
|                         } else {
 | |
|                             pm_purorderbill.set("tqq9_isrebatecalculate", true);
 | |
|                             SaveServiceHelper.save(new DynamicObject[]{pm_purorderbill});
 | |
|                         }
 | |
|                     } catch (IOException ex) {
 | |
|                         throw new RuntimeException(ex);
 | |
|                     }
 | |
|                     //现返审核
 | |
|                 } else if (tqq9_hsxfsyje.compareTo(BigDecimal.ZERO) > 0 && actpayamt.compareTo(BigDecimal.ZERO) > 0) {
 | |
|                     BigDecimal notaxamount = tqq9_hsxfsyje.multiply(oneHundred.divide(oneHundred.add(new BigDecimal(13)), 10, RoundingMode.HALF_UP)).setScale(2, RoundingMode.HALF_UP);
 | |
|                     BigDecimal taxamount = tqq9_hsxfsyje.subtract(notaxamount);//税额
 | |
|                     HashMap<String, Object> bodyMap = new HashMap<>();
 | |
|                     bodyMap.put("id", tqq9_xfid);
 | |
|                     bodyMap.put("lockMoney", tqq9_hsxfsyje);
 | |
|                     bodyMap.put("lockTaxAmount", taxamount);
 | |
|                     Gson gson = new Gson();
 | |
|                     try {
 | |
|                         String bodyString = HttpRequestUtils.doGet(Use_URL, bodyMap, tokenMap);
 | |
|                         HashMap map = gson.fromJson(bodyString, HashMap.class);
 | |
|                         Boolean data = (Boolean) map.get("data");
 | |
|                         if (data == null || !data) {
 | |
|                             this.getOperationResult().setMessage("付款单:" + billno + ",审核时扣除订单金额失败");
 | |
|                         } else {
 | |
|                             pm_purorderbill.set("tqq9_isrebatecalculate", true);
 | |
|                             SaveServiceHelper.save(new DynamicObject[]{pm_purorderbill});
 | |
|                         }
 | |
|                     } catch (IOException ex) {
 | |
|                         throw new RuntimeException(ex);
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|     }
 | |
| } |