diff --git a/ic/src/public/nccloud/resources/ic/ic/saleout/saveBeforeCheck.java b/ic/src/public/nccloud/resources/ic/ic/saleout/saveBeforeCheck.java new file mode 100644 index 0000000..d748044 --- /dev/null +++ b/ic/src/public/nccloud/resources/ic/ic/saleout/saveBeforeCheck.java @@ -0,0 +1,187 @@ +package nccloud.resources.ic.ic.saleout; + +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import nc.bs.businessevent.BdUpdateEvent; +import nc.bs.businessevent.BusinessEvent; +import nc.bs.businessevent.IBusinessEvent; +import nc.bs.businessevent.IBusinessListener; +import nc.bs.framework.common.NCLocator; +import nc.bs.ic.general.businessevent.ICGeneralCommonEvent; +import nc.itf.uap.IUAPQueryBS; +import nc.jdbc.framework.processor.ColumnProcessor; +import nc.jdbc.framework.processor.MapProcessor; +import nc.vo.ic.general.define.ICBillBodyVO; +import nc.vo.ic.m4c.entity.SaleOutHeadVO; +import nc.vo.ic.m4c.entity.SaleOutVO; +import nc.vo.pub.BusinessException; +import nc.vo.pub.lang.UFDouble; +import nc.vo.pubapp.AppContext; +import nc.vo.tmpub.util.ModuleEnum; +import nc.vo.tmpub.util.ModuleUtil; +import java.time.LocalDateTime; + + +/** + * 发货时保存时验证销售订单累计发货数量,并校验销售发票标识,并验证发货款比例 + * zhangxinah + * 2005适配2312 + */ +public class saveBeforeCheck implements IBusinessListener { + + @Override + public void doAction(IBusinessEvent event) throws BusinessException { + + String pk_group = AppContext.getInstance().getPkGroup(); + if (ModuleUtil.isEnableByGroup(pk_group, ModuleEnum.SSCRP, false)) { + if (event != null) { + Map map = this.getBills(event); + SaleOutVO[] obills = map.get("obj"); + // 判断是否是需要控制的业务单元 + SaleOutHeadVO parentVO = obills[0].getHead(); + String pkstockorgStr = getPk_stockorg(parentVO.getPk_org()); + if (pkstockorgStr == null || pkstockorgStr.equals("")) { + return; + } + ICBillBodyVO[] childrenVO = obills[0].getChildrenVO(); + for (int i = 0; i < childrenVO.length; ++i) { + ICBillBodyVO vo = childrenVO[i]; + Map valMap = getSaleorderPK(vo.getCsourcebillbid()); + if (valMap == null || valMap.isEmpty()) { + return; + } + // 历史数据不考虑 + String storedDateStr = (String) valMap.get("creationtime"); + String targetDateStr = "2024-12-31 00:00:00"; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime storedDate = LocalDateTime.parse(storedDateStr, formatter); + LocalDateTime targetDate = LocalDateTime.parse(targetDateStr, formatter); + if (storedDate.isBefore(targetDate)) { + return; + } + // 销售发票表头pk_billtypecode=30-Cxx-12 + if (valMap.get("pk_billtypecode").equals("30-Cxx-12") + || valMap.get("pk_billtypecode").equals("30-Cxx-02") + || valMap.get("pk_billtypecode").equals("30-Cxx-08") + || valMap.get("pk_billtypecode").equals("30-Cxx-13")) { + return; + } + // 销售发票表头vdef21=是 + if (valMap.get("vdef20") == null || !valMap.get("vdef20").equals("Y")) { + throw new BusinessException("销售出库单明细" + (i + 1) + ",销售发票验证不通过,无法保存!"); + } + if (valMap.get("sqty") == null) { + throw new BusinessException("销售出库单明细" + (i + 1) + ",累计发货申请数量为0,无法保存!"); + } else { + try { + double sQty = Double.parseDouble((String) valMap.get("sqty"));// 累计发货申请数量 + UFDouble bdnumUF = vo.getNshouldassistnum();// 本单应发数量 + double bdnum = 0; + if (bdnumUF != null) { + bdnum = bdnumUF.getDouble(); + } + // 应发数量判断 + Object ntotaloutnumsObj = valMap.get("ntotaloutnums"); + String ntotaloutnums = (ntotaloutnumsObj != null) ? ntotaloutnumsObj.toString() : ""; + double ntotaloutnum = (ntotaloutnums.isEmpty()) ? 0 : Double.parseDouble(ntotaloutnums);// 累计出库主数量 + + if (sQty < (ntotaloutnum + bdnum)) { + throw new BusinessException("销售出库单明细" + (i + 1) + ",累计出库应发数量'" + (ntotaloutnum + bdnum) + + "'大于销售订单累计发货申请数量'" + sQty + "',无法保存!"); + } + // 实发数量判断 + UFDouble bdsfnumUF = vo.getNassistnum();// 本单实发数量 + double bdsfnum = 0; + if (bdsfnumUF != null) { + bdsfnum = bdsfnumUF.getDouble(); + } + Object nassistnumObj = valMap.get("nassistnum"); + String nassistnums = (nassistnumObj != null) ? nassistnumObj.toString() : ""; + double nassistnum = (nassistnums.isEmpty()) ? 0 : Double.parseDouble(nassistnums);// 累计出库主数量 + if (sQty < (nassistnum + bdsfnum)) { + throw new BusinessException("销售出库单明细" + (i + 1) + ",累计出库实发数量'" + (nassistnum + bdsfnum) + + "'大于销售订单累计发货申请数量'" + sQty + "',无法保存!"); + } + } catch (NumberFormatException e) { + throw new BusinessException("累计发货申请数量、累计出库数量转化数值失败" + e); + } + } + + } + + } + + } + + } + + private Map getSaleorderPK(String csourcebillbidStr) throws BusinessException { + IUAPQueryBS queryBS = NCLocator.getInstance().lookup(IUAPQueryBS.class); + String sql = " SELECT s.creationtime,bt.pk_billtypecode,sb.csaleorderbid,sb.vbdef2 AS sQty,\n" + "s.vdef11,\n" + + "si.vdef20,\n" + "sbv.nassistnum,\n" + "sbv.ntotaloutnums\n" + "FROM so_saleinvoice_b sib\n" + + "INNER JOIN so_saleinvoice si ON si.csaleinvoiceid = sib.csaleinvoiceid\n" + + "INNER JOIN so_saleorder_b sb ON sb.csaleorderbid = sib.csrcbid\n" + + "left join (SELECT SUM(b.nshouldassistnum) AS ntotaloutnums,sum(nassistnum) as nassistnum,c.csaleorderbid\n" + + "FROM so_saleorder_b c\n" + "INNER JOIN so_saleinvoice_b a ON c.csaleorderbid = a.csrcbid\n" + + "INNER JOIN ic_saleout_b b ON b.csourcebillbid = a.csaleinvoicebid\n" + "where a.dr=0 and b.dr=0 \n" + + "GROUP BY c.csaleorderbid) sbv on sbv.csaleorderbid=sb.csaleorderbid\n" + + "INNER JOIN so_saleorder s ON s.csaleorderid = sb.csaleorderid\n" + + "INNER JOIN bd_billtype bt on bt.pk_billtypeid=s.ctrantypeid\n" + + "INNER JOIN so_saleorder_exe se ON sb.csaleorderbid = se.csaleorderbid\n" + + "WHERE sib.csaleinvoicebid = '" + csourcebillbidStr + "' "; + Map valList = (Map) queryBS.executeQuery(sql, new MapProcessor()); + return valList; + } + + public IUAPQueryBS getQueryService() { + return NCLocator.getInstance().lookup(IUAPQueryBS.class); + } + + private Map getBills(IBusinessEvent event) throws BusinessException { + Object object = null; + Object old = null; + Map retMap = new HashMap<>(); + // 类型判断和赋值 + if (event instanceof BusinessEvent) { + BusinessEvent bills = (BusinessEvent) event; + object = bills.getObject(); + } else if (event instanceof BdUpdateEvent) { + BdUpdateEvent e = (BdUpdateEvent) event; + object = e.getNewObject(); + old = e.getOldObject(); + } else if (event instanceof ICGeneralCommonEvent) { + ICGeneralCommonEvent e = (ICGeneralCommonEvent) event; + object = e.getNewObjs(); + old = e.getOldObjs(); + } else { + throw new BusinessException("未找到单据类型"); + } + // 将 object 和 old 转换为 SaleOutVO[] 并添加到 retMap + retMap.put("new", toSaleOutVOArray(object)); + retMap.put("old", toSaleOutVOArray(old)); + // 根据 new 或 old 来设置 "obj" + retMap.put("obj", retMap.get("new") != null ? retMap.get("new") : retMap.get("old")); + return retMap; + } + + // 辅助方法:将对象转换为 SaleOutVO 数组 + private SaleOutVO[] toSaleOutVOArray(Object object) { + if (object == null) { + return null; + } + if (object.getClass().isArray()) { + return (SaleOutVO[]) object; + } else { + return new SaleOutVO[] { (SaleOutVO) object }; + } + } + + private String getPk_stockorg(String pk_stockorg) throws BusinessException { + String sql = " select pk_stockorg from org_stockorg where code in ('C018','C029','C033','C039','C020','C019') and pk_stockorg='" + + pk_stockorg + "' "; + String saleorderPK = (String) getQueryService().executeQuery(sql, new ColumnProcessor()); + return saleorderPK; + } + +}