package tqq9.lc123.cloud.app.plugin.form.pm; import kd.bos.bill.AbstractBillPlugIn; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.metadata.IDataEntityProperty; import kd.bos.dataentity.utils.StringUtils; import kd.bos.entity.datamodel.IDataModel; import kd.bos.entity.datamodel.RowDataEntity; import kd.bos.entity.datamodel.events.AfterAddRowEventArgs; import kd.bos.entity.datamodel.events.BeforeAddRowEventArgs; import kd.bos.entity.datamodel.events.ChangeData; import kd.bos.entity.datamodel.events.PropertyChangedArgs; import kd.bos.logging.Log; import kd.bos.logging.LogFactory; import kd.bos.servicehelper.BusinessDataServiceHelper; import java.math.BigDecimal; import java.math.RoundingMode; /** * 采购订单 * 值改变事件:现返、货返金额分摊;付款比例不超过100% */ public class PuroderBillShareRefundPlugin extends AbstractBillPlugIn { private final static Log logger = LogFactory.getLog(PuroderBillShareRefundPlugin.class); private final static String ENTRYENTITY = "billentry"; @Override public void afterAddRow(AfterAddRowEventArgs e) { super.afterAddRow(e); if (StringUtils.equals(e.getEntryProp().getName(), ENTRYENTITY)) { BigDecimal tqq9_discountamount = (BigDecimal) this.getModel().getValue("tqq9_discountamount"); if (tqq9_discountamount.compareTo(BigDecimal.ZERO) > 0) { RowDataEntity[] rowDataEntities = e.getRowDataEntities(); for (RowDataEntity rowDataEntity : rowDataEntities) { int rowIndex = rowDataEntity.getRowIndex(); this.getModel().setValue("discounttype","C",rowIndex); } } } } @Override public void propertyChanged(PropertyChangedArgs e) { super.propertyChanged(e); String name = e.getProperty().getName(); DynamicObject dataEntity = this.getModel().getDataEntity(true); if ("tqq9_hsxfsyje".equals(name)) { //含税现返使用金额 BigDecimal tqq9_hsxfsyje = dataEntity.getBigDecimal(name);//单头含税现返使用金额 if (tqq9_hsxfsyje.compareTo(BigDecimal.ZERO) > 0) { BigDecimal totalallamount = dataEntity.getBigDecimal("totalallamount");//单头价税合计 DynamicObjectCollection billentries = dataEntity.getDynamicObjectCollection("billentry"); if (billentries != null && billentries.size() > 0) { BigDecimal remainRefundAmt = tqq9_hsxfsyje;//拆分后剩余的金额 for (int i = 0; i <= billentries.size() - 1; i++) { DynamicObject billentry = billentries.get(i); if (i == billentries.size() - 1) { //如果是最后一行 this.getModel().setValue("tqq9_xfamount", remainRefundAmt, i);//明细现返使用金额 } else { //如果不是最后一行 BigDecimal amountandtax = billentry.getBigDecimal("amountandtax");//明细价税合计 BigDecimal tqq9_xfamount = tqq9_hsxfsyje.multiply(amountandtax).divide(totalallamount, 2, RoundingMode.HALF_UP);//分录行含税金额比例 this.getModel().setValue("tqq9_xfamount", tqq9_xfamount, i);//明细现返使用金额 remainRefundAmt = remainRefundAmt.subtract(tqq9_xfamount); } } } } this.getView().updateView("billentry"); } if ("tqq9_hshfsyje".equals(name)) { //含税货返使用金额 BigDecimal tqq9_hshfsyje = dataEntity.getBigDecimal(name);//单头含税现返使用金额 if (tqq9_hshfsyje.compareTo(BigDecimal.ZERO) > 0) { BigDecimal totalallamount = dataEntity.getBigDecimal("totalallamount");//单头价税合计 DynamicObjectCollection billentries = dataEntity.getDynamicObjectCollection("billentry"); if (billentries != null && billentries.size() > 0) { BigDecimal remainRefundAmt = tqq9_hshfsyje;//拆分后剩余的金额 for (int i = 0; i <= billentries.size() - 1; i++) { DynamicObject billentry = billentries.get(i); if (i == billentries.size() - 1) { //如果是最后一行 this.getModel().setValue("tqq9_hfamount", remainRefundAmt, i);//明细货返使用金额 } else { //如果不是最后一行 BigDecimal amountandtax = billentry.getBigDecimal("amountandtax");//明细价税合计 BigDecimal tqq9_hfamount = tqq9_hshfsyje.multiply(amountandtax).divide(totalallamount, 2, RoundingMode.HALF_UP);//分录行含税金额比例 this.getModel().setValue("tqq9_hfamount", tqq9_hfamount, i);//明细货返使用金额 remainRefundAmt = remainRefundAmt.subtract(tqq9_hfamount); } } } } this.getView().updateView("billentry"); } if ("tqq9_discountamount".equals(name)) { //优惠金额 BigDecimal tqq9_discountamount = dataEntity.getBigDecimal(name);//单头优惠金额 if (tqq9_discountamount.compareTo(BigDecimal.ZERO) > 0) { BigDecimal totalallamount = dataEntity.getBigDecimal("totalallamount");//单头价税合计 DynamicObjectCollection billentries = dataEntity.getDynamicObjectCollection("billentry"); if (billentries != null && billentries.size() > 0) { BigDecimal remainRefundAmt = tqq9_discountamount;//拆分后剩余的金额 for (int i = 0; i <= billentries.size() - 1; i++) { DynamicObject billentry = billentries.get(i); if (i == billentries.size() - 1) { //如果是最后一行 this.getModel().setValue("tqq9_disamount", remainRefundAmt, i);//明细优惠金额 } else { //如果不是最后一行 BigDecimal amountandtax = billentry.getBigDecimal("amountandtax");//明细价税合计 BigDecimal tqq9_disamount = tqq9_discountamount.multiply(amountandtax).divide(totalallamount, 2, RoundingMode.HALF_UP);//分录行含税金额比例 this.getModel().setValue("tqq9_disamount", tqq9_disamount, i);//明细优惠金额 remainRefundAmt = remainRefundAmt.subtract(tqq9_disamount); } } } } this.getView().updateView("billentry"); } if (StringUtils.equals("payrate", name)) { //校验付款比例不能大于100% IDataModel model = this.getModel(); DynamicObject pm_purorderbill = model.getDataEntity(true);//采购订单 DynamicObjectCollection entries = pm_purorderbill.getDynamicObjectCollection("purbillentry_pay");//付款计划分录 BigDecimal zero = BigDecimal.ZERO; for (DynamicObject entry : entries) { BigDecimal payrate = entry.getBigDecimal("payrate"); zero = zero.add(payrate); } if (zero.compareTo(new BigDecimal(100)) > 0) { this.getView().showErrorNotification("付款计划付款比例总计大于100%,请修改"); } } if (StringUtils.equals("tqq9_amount", name)) { //单头金额下推 BigDecimal tqq9_hsxfsyje = dataEntity.getBigDecimal("tqq9_hsxfsyje");//单头含税现返使用金额 BigDecimal tqq9_hshfsyje = dataEntity.getBigDecimal("tqq9_hshfsyje");//单头含税货返使用金额 BigDecimal tqq9_discountamount = dataEntity.getBigDecimal("tqq9_discountamount");//单头优惠金额 BigDecimal totalallamount = dataEntity.getBigDecimal("totalallamount");//单头价税合计 DynamicObjectCollection billentries = dataEntity.getDynamicObjectCollection("billentry"); BigDecimal remainRefundAmt_xf = tqq9_hsxfsyje;//拆分后剩余的金额-现返 BigDecimal remainRefundAmt_hf = tqq9_hshfsyje;//拆分后剩余的金额-货返 BigDecimal remainRefundAmt_yh = tqq9_discountamount;//拆分后剩余的金额-优惠 if (billentries != null && billentries.size() > 0) { for (int i = 0; i <= billentries.size() - 1; i++) { DynamicObject billentry = billentries.get(i); if (tqq9_hsxfsyje.compareTo(BigDecimal.ZERO) > 0) { if (i == billentries.size() - 1) { //如果是最后一行 this.getModel().setValue("tqq9_xfamount", remainRefundAmt_xf, i);//明细现返使用金额 } else { //如果不是最后一行 BigDecimal amountandtax = billentry.getBigDecimal("amountandtax");//明细价税合计 BigDecimal tqq9_xfamount = tqq9_hsxfsyje.multiply(amountandtax).divide(totalallamount.add(amountandtax), 2, RoundingMode.HALF_UP);//分录行含税金额比例 this.getModel().setValue("tqq9_xfamount", tqq9_xfamount, i);//明细现返使用金额 remainRefundAmt_xf = remainRefundAmt_xf.subtract(tqq9_xfamount); } } if (tqq9_hshfsyje.compareTo(BigDecimal.ZERO) > 0) { if (i == billentries.size() - 1) { //如果是最后一行 this.getModel().setValue("tqq9_hfamount", remainRefundAmt_hf, i);//明细货返使用金额 } else { //如果不是最后一行 BigDecimal amountandtax = billentry.getBigDecimal("amountandtax");//明细价税合计 BigDecimal tqq9_hfamount = tqq9_hshfsyje.multiply(amountandtax).divide(totalallamount.add(amountandtax), 2, RoundingMode.HALF_UP);//分录行含税金额比例 this.getModel().setValue("tqq9_hfamount", tqq9_hfamount, i);//明细货返使用金额 remainRefundAmt_hf = remainRefundAmt_hf.subtract(tqq9_hfamount); } } if (tqq9_discountamount.compareTo(BigDecimal.ZERO) > 0) { if (i == billentries.size() - 1) { //如果是最后一行 this.getModel().setValue("tqq9_disamount", remainRefundAmt_yh, i);//明细优惠金额 this.getModel().setValue("discountamount", remainRefundAmt_yh, i);//明细折扣额 } else { //如果不是最后一行 BigDecimal amountandtax = billentry.getBigDecimal("amountandtax");//明细价税合计 BigDecimal tqq9_disamount = tqq9_discountamount.multiply(amountandtax).divide(totalallamount.add(amountandtax), 2, RoundingMode.HALF_UP);//分录行含税金额比例 this.getModel().setValue("tqq9_disamount", tqq9_disamount, i);//明细优惠金额 this.getModel().setValue("discountamount", tqq9_disamount, i);// remainRefundAmt_yh = remainRefundAmt_yh.subtract(tqq9_disamount); } } } } this.getView().updateView("billentry"); } if(StringUtils.equals("material",name)){ ChangeData changeData = e.getChangeSet()[0]; int rowIndex = changeData.getRowIndex(); DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection(ENTRYENTITY); DynamicObject dynamicObject = dynamicObjectCollection.get(rowIndex); DynamicObject material = dynamicObject.getDynamicObject("material"); DynamicObject masterid = material.getDynamicObject("masterid"); DynamicObject bd_material = BusinessDataServiceHelper.loadSingle(masterid.getLong("id"), "bd_material"); DynamicObject org = dataEntity.getDynamicObject("org"); //上海 if(StringUtils.equals("Org-00001",org.getString("number"))){ BigDecimal tqq9_maxprice_sh = bd_material.getBigDecimal("tqq9_maxprice_sh"); if(tqq9_maxprice_sh.compareTo(BigDecimal.ZERO)>0){ getModel().setValue("priceandtax",tqq9_maxprice_sh,rowIndex); getModel().setValue("tqq9_zgcgxj",tqq9_maxprice_sh,rowIndex); } //北京 }else if(StringUtils.equals("Org-00002",org.getString("number"))){ BigDecimal tqq9_maxprice_bj = bd_material.getBigDecimal("tqq9_maxprice_bj"); if(tqq9_maxprice_bj.compareTo(BigDecimal.ZERO)>0) { getModel().setValue("priceandtax", tqq9_maxprice_bj, rowIndex); getModel().setValue("tqq9_zgcgxj", tqq9_maxprice_bj, rowIndex); } //广州 } else if(StringUtils.equals("Org-00003",org.getString("number"))){ BigDecimal tqq9_maxprice_gz = bd_material.getBigDecimal("tqq9_maxprice_gz"); if(tqq9_maxprice_gz.compareTo(BigDecimal.ZERO)>0) { getModel().setValue("priceandtax", tqq9_maxprice_gz, rowIndex); getModel().setValue("tqq9_zgcgxj", tqq9_maxprice_gz, rowIndex); } } } if(StringUtils.equals("priceandtax",name)){ ChangeData changeData = e.getChangeSet()[0]; int rowIndex = changeData.getRowIndex(); DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection(ENTRYENTITY); BigDecimal oldValue = (BigDecimal)changeData.getOldValue(); BigDecimal tqq9_zgcgxj = (BigDecimal)getModel().getValue("tqq9_zgcgxj"); if (oldValue.compareTo(tqq9_zgcgxj)>0&&tqq9_zgcgxj.compareTo(BigDecimal.ZERO)>0) { getModel().setValue("priceandtax",0); this.getView().showErrorNotification("物料明细分录行"+rowIndex+1+"含税单价超过最高采购限价,请修改"); } } } }