到货单制单日期校验
This commit is contained in:
		
							parent
							
								
									509fab347d
								
							
						
					
					
						commit
						6dbe4d310f
					
				|  | @ -6,6 +6,7 @@ | |||
| package nc.impl.pu.m23.maintain.rule; | ||||
| 
 | ||||
| import nc.bs.framework.common.NCLocator; | ||||
| import nc.bs.trade.business.HYPubBO; | ||||
| import nc.impl.pubapp.pattern.rule.IRule; | ||||
| import nc.pubitf.so.m30.api.ISaleOrderQueryAPI; | ||||
| import nc.vo.pu.m21.entity.OrderItemVO; | ||||
|  | @ -18,6 +19,7 @@ import nc.vo.so.m30.entity.SaleOrderBVO; | |||
| import org.apache.commons.lang3.StringUtils; | ||||
| 
 | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class CheckBillDateRule  implements IRule<ArriveVO> { | ||||
|  | @ -30,112 +32,117 @@ public class CheckBillDateRule  implements IRule<ArriveVO> { | |||
| 
 | ||||
| 
 | ||||
| // 提取API服务实例到循环外,提升性能 | ||||
|           try{for (ArriveVO vo : vos) { | ||||
|               // 校验到货单主信息是否存在 | ||||
|               if (vo == null || vo.getHVO() == null) { | ||||
|                   throw new BusinessException("到货单主信息不能为空"); | ||||
|               } | ||||
|         try{for (ArriveVO vo : vos) { | ||||
|             // 校验到货单主信息是否存在 | ||||
|             if (vo == null || vo.getHVO() == null) { | ||||
|                 throw new BusinessException("到货单主信息不能为空"); | ||||
|             } | ||||
| //              只校验箱变 | ||||
|             HYPubBO hybo = new HYPubBO(); | ||||
|             String org = hybo.findColValue("org_adminorg", "pk_adminorg", " code = 'C030'  ") + ""; | ||||
|             if(!vo.getHVO().getPk_org().equals(org)){ | ||||
|                 continue; | ||||
|             } | ||||
|             Date arriveMakeDate = null != vo.getHVO().getDbilldate() ? vo.getHVO().getDbilldate().toDate(): new Date() ; | ||||
|             // 校验制单日期是否存在 | ||||
|             if (arriveMakeDate == null) { | ||||
|                 throw new BusinessException("到货单制单日期不能为空"); | ||||
|             } | ||||
| 
 | ||||
|               Date arriveMakeDate = null != vo.getHVO().getDmakedate() ? vo.getHVO().getDmakedate().toDate(): new Date() ; | ||||
|               // 校验制单日期是否存在 | ||||
|               if (arriveMakeDate == null) { | ||||
|                   throw new BusinessException("到货单制单日期不能为空"); | ||||
|               } | ||||
|             // 遍历到货单明细 | ||||
|             ArriveItemVO[] itemVOs = vo.getBVO(); | ||||
|             if (itemVOs == null || itemVOs.length==0) { | ||||
|                 continue; // 没有明细则跳过当前到货单处理 | ||||
|             } | ||||
| 
 | ||||
|               // 遍历到货单明细 | ||||
|               ArriveItemVO[] itemVOs = vo.getBVO(); | ||||
|               if (itemVOs == null || itemVOs.length==0) { | ||||
|                   continue; // 没有明细则跳过当前到货单处理 | ||||
|               } | ||||
|             for (ArriveItemVO itemVO : itemVOs) { | ||||
|                 // 校验明细必要字段 | ||||
|                 if (itemVO == null) { | ||||
|                     throw new BusinessException("到货单明细不能为空"); | ||||
|                 } | ||||
|                 String pkOrder = itemVO.getPk_order(); | ||||
|                 String pkOrderB = itemVO.getPk_order_b(); | ||||
|                 if (StringUtils.isEmpty(pkOrder) || StringUtils.isEmpty(pkOrderB)) { | ||||
|                     throw new BusinessException("采购订单ID或订单明细ID不能为空"); | ||||
|                 } | ||||
| 
 | ||||
|               for (ArriveItemVO itemVO : itemVOs) { | ||||
|                   // 校验明细必要字段 | ||||
|                   if (itemVO == null) { | ||||
|                       throw new BusinessException("到货单明细不能为空"); | ||||
|                   } | ||||
|                   String pkOrder = itemVO.getPk_order(); | ||||
|                   String pkOrderB = itemVO.getPk_order_b(); | ||||
|                   if (StringUtils.isEmpty(pkOrder) || StringUtils.isEmpty(pkOrderB)) { | ||||
|                       throw new BusinessException("采购订单ID或订单明细ID不能为空"); | ||||
|                   } | ||||
| 
 | ||||
|                   try { | ||||
|                       // 查询对应的销售订单 | ||||
|                       OrderVO[] orderVOs =new BillQuery<OrderVO>(OrderVO.class).query(new String[]{pkOrder}); | ||||
|                       if (orderVOs == null || orderVOs.length == 0) { | ||||
|                           return; | ||||
|                 try { | ||||
|                     // 查询对应的销售订单 | ||||
|                     OrderVO[] orderVOs =new BillQuery<OrderVO>(OrderVO.class).query(new String[]{pkOrder}); | ||||
|                     if (orderVOs == null || orderVOs.length == 0) { | ||||
|                         return; | ||||
| //                          throw new BusinessException("未找到ID为" + pkOrder + "的采购订单"); | ||||
|                       } | ||||
|                     } | ||||
| 
 | ||||
|                       // 获取订单明细 | ||||
|                       OrderVO o = orderVOs[0]; | ||||
|                       OrderItemVO[] orderItemVOs = o.getBVO(); | ||||
|                       if (orderItemVOs == null || orderItemVOs.length == 0) { | ||||
|                           return; | ||||
|                     // 获取订单明细 | ||||
|                     OrderVO o = orderVOs[0]; | ||||
|                     OrderItemVO[] orderItemVOs = o.getBVO(); | ||||
|                     if (orderItemVOs == null || orderItemVOs.length == 0) { | ||||
|                         return; | ||||
| //                          throw new BusinessException("采购订单" + pkOrder + "没有明细数据"); | ||||
|                       } | ||||
|                     } | ||||
| 
 | ||||
|                       // 查找匹配的订单明细 | ||||
|                       boolean found = false; | ||||
|                       for (OrderItemVO orderItem : orderItemVOs) { | ||||
|                           if (orderItem != null && pkOrderB.equals(orderItem.getPk_order_b())) { | ||||
|                               found = true; | ||||
|                               Date expectArriveDate = orderItem.getDplanarrvdate().toDate(); | ||||
|                               if (expectArriveDate == null) { | ||||
|                                   throw new BusinessException("采购订单明细" + pkOrderB + "的计划到货时间不能为空"); | ||||
|                               } | ||||
|                     // 查找匹配的订单明细 | ||||
|                     boolean found = false; | ||||
|                     for (OrderItemVO orderItem : orderItemVOs) { | ||||
|                         if (orderItem != null && pkOrderB.equals(orderItem.getPk_order_b())) { | ||||
|                             found = true; | ||||
|                             Date expectArriveDate = orderItem.getDplanarrvdate().toDate(); | ||||
|                             if (expectArriveDate == null) { | ||||
|                                 throw new BusinessException("采购订单明细" + pkOrderB + "的计划到货时间不能为空"); | ||||
|                             } | ||||
| 
 | ||||
|                               // 核心逻辑:只允许提前3天到货 | ||||
|                               // 1. 计算预计到货时间前3天的日期(最早允许的制单日期) | ||||
|                               Date earliestAllowedDate = addDays(expectArriveDate, -3); | ||||
|                             // 核心逻辑:只允许提前3天到货 | ||||
|                             // 1. 计算预计到货时间前3天的日期(最早允许的制单日期) | ||||
|                             Date earliestAllowedDate = addDays(expectArriveDate, -3); | ||||
| 
 | ||||
|                               // 2. 判断规则 | ||||
|                               boolean isTooEarly = arriveMakeDate.before(earliestAllowedDate); | ||||
|                               boolean isTooLate = arriveMakeDate.after(expectArriveDate); | ||||
|                             // 2. 判断规则 | ||||
|                             boolean isTooEarly =clearTime(arriveMakeDate).before(clearTime(earliestAllowedDate)); | ||||
| //                              boolean isTooLate = arriveMakeDate.after(expectArriveDate); | ||||
| 
 | ||||
|                               if (isTooEarly || isTooLate) { | ||||
|                                   String errorMsg; | ||||
|                                   if (isTooEarly) { | ||||
|                                       errorMsg = String.format( | ||||
|                                               "到货单制单日期(%s)早于采购订单预计到货时间(%s)超过3天,不允许保存。最早允许制单日期为%s。订单编号:%s,明细行号:%s", | ||||
|                                               formatDate(arriveMakeDate), | ||||
|                                               formatDate(expectArriveDate), | ||||
|                                               formatDate(earliestAllowedDate), | ||||
|                                               o.getHVO().getVbillcode(), | ||||
|                                               orderItem.getCrowno() | ||||
|                                       ); | ||||
|                                   } else { // isTooLate | ||||
|                                       errorMsg = String.format( | ||||
|                                               "到货单制单日期(%s)晚于采购订单预计到货时间(%s),不符合提前到货规则,不允许保存。订单编号:%s,明细行号:%s", | ||||
|                                               formatDate(arriveMakeDate), | ||||
|                                               formatDate(expectArriveDate), | ||||
|                                               o.getHVO().getVbillcode(), | ||||
|                                               orderItem.getCrowno() | ||||
|                                       ); | ||||
|                                   } | ||||
|                                   throw new BusinessException(errorMsg); | ||||
|                               } | ||||
|                               break; // 找到匹配明细后退出循环 | ||||
|                           } | ||||
|                       } | ||||
| //                              if (isTooEarly || isTooLate) { | ||||
|                             String errorMsg; | ||||
|                             if (isTooEarly) { | ||||
|                                 errorMsg = String.format( | ||||
|                                         "到货单制单日期(%s)早于采购订单预计到货时间(%s)超过3天,不允许保存。最早允许制单日期为%s。订单编号:%s,明细行号:%s", | ||||
|                                         formatDate(arriveMakeDate), | ||||
|                                         formatDate(expectArriveDate), | ||||
|                                         formatDate(earliestAllowedDate), | ||||
|                                         o.getHVO().getVbillcode(), | ||||
|                                         orderItem.getCrowno() | ||||
|                                 ); | ||||
| //                                  } else { // isTooLate | ||||
| //                                      errorMsg = String.format( | ||||
| //                                              "到货单制单日期(%s)晚于采购订单预计到货时间(%s),不符合提前到货规则,不允许保存。订单编号:%s,明细行号:%s", | ||||
| //                                              formatDate(arriveMakeDate), | ||||
| //                                              formatDate(expectArriveDate), | ||||
| //                                              o.getHVO().getVbillcode(), | ||||
| //                                              orderItem.getCrowno() | ||||
| //                                      ); | ||||
| //                                  } | ||||
|                                 throw new BusinessException(errorMsg); | ||||
|                             } | ||||
|                             break; // 找到匹配明细后退出循环 | ||||
|                         } | ||||
|                     } | ||||
| 
 | ||||
|                       if (!found) { | ||||
|                           throw new BusinessException("未找到采购订单" + pkOrder + "中ID为" + pkOrderB + "的明细"); | ||||
|                       } | ||||
|                     if (!found) { | ||||
|                         throw new BusinessException("未找到采购订单" + pkOrder + "中ID为" + pkOrderB + "的明细"); | ||||
|                     } | ||||
| 
 | ||||
|                   } catch (BusinessException e) { | ||||
|                       // 直接抛出业务异常,中断流程 | ||||
|                       throw e; | ||||
|                   } catch (Exception e) { | ||||
|                       // 捕获其他异常并包装为业务异常 | ||||
|                       throw new BusinessException("校验采购订单与到货单时间关系时发生错误:" + e.getMessage(), e); | ||||
|                   } | ||||
|               } | ||||
|           } | ||||
|                 } catch (BusinessException e) { | ||||
|                     // 直接抛出业务异常,中断流程 | ||||
|                     throw e; | ||||
|                 } catch (Exception e) { | ||||
|                     // 捕获其他异常并包装为业务异常 | ||||
|                     throw new BusinessException("校验采购订单与到货单时间关系时发生错误:" + e.getMessage(), e); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|           } catch (Exception e) { | ||||
|               throw new RuntimeException(e); | ||||
|           } | ||||
|         } catch (Exception e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | @ -160,6 +167,22 @@ public class CheckBillDateRule  implements IRule<ArriveVO> { | |||
|             return DATE_FORMATTER.format(date); | ||||
|         } | ||||
|     } | ||||
|     /** | ||||
|      * 清除日期中的时间部分,只保留年月日 | ||||
|      */ | ||||
|     private static Date clearTime(Date date) { | ||||
|         if (date == null) { | ||||
|             return null; | ||||
|         } | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.setTime(date); | ||||
|         // 将时间部分设置为0 | ||||
|         calendar.set(Calendar.HOUR_OF_DAY, 0); | ||||
|         calendar.set(Calendar.MINUTE, 0); | ||||
|         calendar.set(Calendar.SECOND, 0); | ||||
|         calendar.set(Calendar.MILLISECOND, 0); | ||||
|         return calendar.getTime(); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue