Index: ssts-returngoods/src/main/java/com/forgon/disinfectsystem/returngoodsrecord/service/ReturnGoodsRecordManagerImpl.java =================================================================== diff -u -r17715 -r17725 --- ssts-returngoods/src/main/java/com/forgon/disinfectsystem/returngoodsrecord/service/ReturnGoodsRecordManagerImpl.java (.../ReturnGoodsRecordManagerImpl.java) (revision 17715) +++ ssts-returngoods/src/main/java/com/forgon/disinfectsystem/returngoodsrecord/service/ReturnGoodsRecordManagerImpl.java (.../ReturnGoodsRecordManagerImpl.java) (revision 17725) @@ -16,6 +16,7 @@ import java.util.Set; import java.util.stream.Collectors; +import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.collections4.CollectionUtils; @@ -32,6 +33,7 @@ import com.forgon.disinfectsystem.common.CssdUtils; import com.forgon.disinfectsystem.diposablegoods.service.DiposableGoodBatchStockManager; import com.forgon.disinfectsystem.diposablegoods.service.DiposableGoodsManager; +import com.forgon.disinfectsystem.diposablegoods.util.DisposableGoodsUtils; import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoods; import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoodsBatch; import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoodsBatchStock; @@ -72,6 +74,7 @@ import com.forgon.tools.db.InitDbConnection; import com.forgon.tools.hibernate.ObjectDao; import com.forgon.tools.json.JSONUtil; +import com.forgon.tools.util.ForgonDateUtils; import com.forgon.tools.util.SqlUtils; import com.google.gson.JsonObject; @@ -315,6 +318,23 @@ batchCount); return list; } + private List getDiposableGoodsItemCanReturned2(Long warehouseID,Long invoicePlanId,Long identificationID){ + final int batchCount = 10; + List list = null; + // 按发货日期时间降序,即优先从最近的发货物品里面退货 + String isNullFunction = DatabaseUtil.getSqlIsNullFunctionName(dbConnection); + + String sql = String +// .format(" select d from %s i,%s d where i.id = d.invoice.id and i.depart = '%s' and d.name = '%s' and d.batch = '%s' and (ABS(d.price-%s)<%s) and (d.amount > %s(d.returnedAmount,0)) and d.identificationID is not null and d.invoiceItemID is not null order by i.applicationTime desc", + .format(" select d from %s i,%s d where i.id = d.invoice.id and i.sourceWarehouseId=%s and i.invoicePlan.id = %s and d.identificationID=%s and (d.amount > %s(d.returnedAmount,0)) and d.identificationID is not null and d.invoiceItemID is not null order by i.applicationTime desc", + Invoice.class.getSimpleName(), + DiposableGoodsItem.class.getSimpleName(), warehouseID,invoicePlanId, + identificationID, + isNullFunction); + list = (List) objectDao.findByHql(sql, 0, + batchCount); + return list; + } /* 退货某一项物品(批次+价格),因为发货明细可能会很多,所以每次只取10个来退货,一直到所有物品退完 * */ @@ -325,14 +345,18 @@ Map idToDisposableGoodsBatchMap, Map idToDisposableGoodsBatchStockMap, List returnDiposableGoodsItems){ - + Long invoicePlanId = returnGoodsItem.getReturnGoodsRecord().getInvoicePlanId(); + Long identificationID = returnGoodsItem.getIdentificationID(); Long disposableGoodsID = returnGoodsItem.getDisposableGoodsID(); DisposableGoods disposableGoods = idToDisposableGoodsMap.get(disposableGoodsID); Long disposableGoodsBatchStockID = returnGoodsItem.getDisposableGoodsBatchStockID(); Double price = returnGoodsItem.getPrice(); if(price == null){ throw new RuntimeException("金额不能为空!"); } + if(DatabaseUtil.isPoIdValid(invoicePlanId) && !DatabaseUtil.isPoIdValid(identificationID)){ + throw new RuntimeException("标识号id不能为空!"); + } String batchNumber = returnGoodsItem.getBatchNumber(); DisposableGoodsStock disposableGoodsStock = idToDisposableGoodsStockMap.get(disposableGoodsBatchStockID); String diposableGoodsShowName = disposableGoods.getShowName(); @@ -342,8 +366,14 @@ } BigDecimal settlementPrice = new BigDecimal(0.00); while(true){ - // 一次取10个发货明细用于退货 - List list = getDiposableGoodsItemCanReturned(warehouseID,depart, diposableGoodsShowName, disposableGoodsBatchStockID,price); + + List list = null; + if(DatabaseUtil.isPoIdValid(invoicePlanId)){ + list = getDiposableGoodsItemCanReturned2(warehouseID,invoicePlanId, identificationID); + }else{ + // 一次取10个发货明细用于退货 + list = getDiposableGoodsItemCanReturned(warehouseID,depart, diposableGoodsShowName, disposableGoodsBatchStockID,price); + } if(list == null || list.size() == 0){ String err = String.format("物品(%s)批次(%s)退货数量大于发货数量!", diposableGoodsShowName,batchNumber); throw new RuntimeException(err); @@ -361,12 +391,12 @@ Long sendOutItemAmountCanReturn = amount - returnedAmount;// 发货项可退货的数量 Long curAmountCanReturn = Math.min(sendOutItemAmountCanReturn, amountToReturn);// 当前可退货数量 - String identificationID = sendOutDiposableGoodsItem.getIdentificationID().toString(); + String identificationIDStr = sendOutDiposableGoodsItem.getIdentificationID().toString(); sendOutDiposableGoodsItem.setReturnedAmount(returnedAmount + curAmountCanReturn);// 设置发货明细的已退货数量 returnGoodsItem.setReturnedAmount(returnGoodsItem.getReturnedAmount() + curAmountCanReturn);// 设置退货汇总项的已退货数量 // 一次性物品库存修改 - DisposableGoodsIdentification identification = (DisposableGoodsIdentification)objectDao.getByProperty(DisposableGoodsIdentification.class.getSimpleName(), "id", Long.valueOf(identificationID)); + DisposableGoodsIdentification identification = (DisposableGoodsIdentification)objectDao.getByProperty(DisposableGoodsIdentification.class.getSimpleName(), "id", Long.valueOf(identificationIDStr)); if(identification == null){ throw new RuntimeException("标识号已被删除!"); } @@ -418,6 +448,17 @@ // 设置金额汇总 returnGoodsItem.setSettlementPrice(settlementPrice); } + private void updateInvoicePlan(Long invoicePlanId){ + if(!DatabaseUtil.isPoIdValid(invoicePlanId)){ + return; + } + InvoicePlan invoicePlan = (InvoicePlan)objectDao.getByProperty_ForUpdate( + InvoicePlan.class.getSimpleName(), "id", invoicePlanId); + if(invoicePlan != null){ + invoicePlan.setReturnedGoods(Constants.STR_YES); + objectDao.update(invoicePlan); + } + } // 高值耗材退货 private void returnExpensiveDisposablegoods(Long warehouseID,String depart,ReturnGoodsItem returnGoodsItem, Map idToDisposableGoodsMap, @@ -713,7 +754,7 @@ Map returnMsg = new HashMap(); returnMsg.put("success", false); returnMsg.put("errMsg", ""); - + Long invoicePlanId = returnGoodsRecord.getInvoicePlanId(); // 汇总退货信息 Set disposableGoodsBarcodesSet = new HashSet(); Set disposableGoodsIDsSet = new HashSet();// 一次性物品id集合 @@ -767,6 +808,8 @@ // 创建发货单 createInvoicePlan(returnGoodsRecord); + updateInvoicePlan(invoicePlanId); + // 保存退货明细项 for (DiposableGoodsItem diposableGoodsItem : returnDiposableGoodsItems) { diposableGoodsItem.setReturnGoodsRecordID(returnGoodsRecord.getId()); @@ -814,7 +857,9 @@ } } returnMsg.put("success", true); - + if(true){ +// throw new RuntimeException("速度测试!"); + } return returnMsg; } @Override @@ -823,6 +868,7 @@ if (returnGoodsRecord.getId() != null) { throw new RuntimeException("目前不支持退货记录的修改!"); } + Long invoicePlanId = returnGoodsRecord.getInvoicePlanId(); // 先保存一次,若重装,装配任务需记下退货记录Id saveOrUpdate(returnGoodsRecord); @@ -886,16 +932,17 @@ // 创建发货计划单 createInvoicePlan(returnGoodsRecord); + updateInvoicePlan(invoicePlanId); saveOrUpdate(returnGoodsRecord); // 回写物资系统 if (writeBackInventoryHelper != null) { writeBackInventoryHelper.writeBackForReturnGoods( Collections.singleton(returnGoodsRecord.getId()), true); } -// if(true){ -// throw new RuntimeException(String.format("test")); -// } + if(true){ +// throw new RuntimeException("速度测试!"); + } return buildSuccessMsgJsonResult(); } @@ -1594,4 +1641,141 @@ } return batchs; } + /// + public JSONArray loadDiposableGoodsReturnGoodsInfoByInvoicePlanId(String warehouseID,Long invoicePlanId){ + JSONArray jsonArray = new JSONArray(); + StringBuilder sql = new StringBuilder(); + + String isNullFunction = DatabaseUtil.getSqlIsNullFunctionName(dbConnection); + + sql.append(String + .format("select dg.name,dg.specification,dg.unit, dbs.barcode,dbs.batchNumber,dbs.expDate,dbs.disposableGoodsId,dbs.diposablegoods_id as disposableGoodsStockId,dbs.disposableGoodsBatchId,dbs.id as disposableGoodsBatchStockId,d.identificationID, d.price as price,d.fluctuationPrice as fluctuationPrice, %s(d.amount,0) as sumAmount, %s(d.returnedAmount,0) as sumReturnedAmount from invoice i inner join DiposableGoodsItem d" + + " on i.id = d.invoice_id inner join DisposableGoods dg on d.disposableGoodsID=dg.id inner join DisposableGoodsBatchStock dbs on d.disposableGoodsBatchStockID=dbs.id where i.invoicePlan_ID=%s and %s(d.amount,0)>%s(d.returnedAmount,0) and d.identificationID is not null ", isNullFunction, + isNullFunction,invoicePlanId,isNullFunction,isNullFunction)); + sql.append(" and i.sourceWarehouseId = " + warehouseID + "");// 限定仓库 +// sql.append(" group by d.price, d.fluctuationPrice"); + ResultSet resultSet = objectDao.executeSql(sql.toString()); + try { + while (resultSet.next()) { + Long sumAmount = resultSet.getLong("sumAmount"); + Long sumReturnedAmount = resultSet.getLong("sumReturnedAmount"); + Double price = resultSet.getDouble("price"); + Double fluctuationPrice = resultSet.getDouble("fluctuationPrice"); + long amountCanReturn = sumAmount - sumReturnedAmount; + if(sumAmount <= sumReturnedAmount){ + continue; + } + String name = resultSet.getString("name"); + String specification = resultSet.getString("specification"); + String unit = resultSet.getString("unit"); + String showName = DisposableGoodsUtils.getDisposableGoodsFullName(name, specification); + String barcode = resultSet.getString("barcode"); + String batchNumber = resultSet.getString("batchNumber"); + Date expDate = resultSet.getTimestamp("expDate"); + Long disposableGoodsId = resultSet.getLong("disposableGoodsId"); + Long disposableGoodsStockId = resultSet.getLong("disposableGoodsStockId"); + Long disposableGoodsBatchId = resultSet.getLong("disposableGoodsBatchId"); + Long disposableGoodsBatchStockId = resultSet.getLong("disposableGoodsBatchStockId"); + Long identificationID = resultSet.getLong("identificationID"); + + JSONObject obj = new JSONObject(); + + obj.put("tousseName", showName); + obj.put("unit", unit); + + obj.put("barcode", barcode); + obj.put("batchNumber", batchNumber); + obj.put("expDate", ForgonDateUtils.safelyFormatDate(expDate, Constants.SIMPLEDATEFORMAT_YYYYMMDDHHMMSS, null)); + obj.put("disposableGoodsID", disposableGoodsId); + obj.put("disposableGoodsStockID", disposableGoodsStockId); + obj.put("disposableGoodsBatchID", disposableGoodsBatchId); + obj.put("disposableGoodsBatchStockID", disposableGoodsBatchStockId); + obj.put("identificationID", identificationID); + + obj.put("price", price); + obj.put("fluctuationPrice", fluctuationPrice); + obj.put("returnedAmount", sumReturnedAmount); + obj.put("sentAmount", sumAmount); + obj.put("amount", amountCanReturn); + obj.put("amountCanReturn", amountCanReturn); + obj.put("isDelivery", "否"); + jsonArray.add(obj); + } + } catch (SQLException e) { + e.printStackTrace(); + }finally { + DatabaseUtil.closeResultSetAndStatement(resultSet); + } + + return jsonArray; + } + + public JSONArray loadReturnedTousseItemsByInvoicePlanId(Long invoicePlanId){ + + JSONArray tousses = new JSONArray(); + InvoicePlan invoicePlan = (InvoicePlan) objectDao.getByProperty( + InvoicePlan.class.getSimpleName(), "id", Long.valueOf(invoicePlanId)); + if(invoicePlan != null){ + Map> nameToTousseListMap = new HashMap>(); + List list = objectDao.findBySql(TousseInstance.class.getSimpleName(), String.format(" where po.invoice_id in(select v.id from %s v where v.invoicePlan.id=%s) and po.status in('%s','%s')",Invoice.class.getSimpleName(), invoicePlan.getId(),TousseInstance.STATUS_SHIPPED,TousseInstance.STATUS_SIGNED)); + if(list != null){ + for (TousseInstance tousseInstance : list) { + String name = tousseInstance.getShowTousseName(); + List list2 = nameToTousseListMap.get(name); + if(list2 == null){ + list2 = new LinkedList(); + nameToTousseListMap.put(name, list2); + } + list2.add(tousseInstance); + + } + } + for (Map.Entry> entry : nameToTousseListMap.entrySet()) { + String name = entry.getKey(); + List list3 = entry.getValue(); + if(list3.size() == 0){ + continue; + } + JSONObject folder = new JSONObject(); + folder.put("tousseName", name); + folder.put("amount", list3.size()); + folder.put("barcode", ""); + folder.put("price", list3.get(0).getFluctuationPrice()); + folder.put("isDelivery", "否"); + folder.put("isPacking", "否"); + folder.put("uiProvider", "col"); + folder.put("cls", "master-task"); + folder.put("leaf", false); + folder.put("iconCls", "task-folder"); + + JSONArray children = new JSONArray(); + for (TousseInstance tousseInstance : list3) { + JSONObject tousse = new JSONObject(); + tousse.put("tousseName", name); + tousse.put("amount", 1); + tousse.put("barcode", tousseInstance.getBarcode()); + tousse.put("price", tousseInstance.getFluctuationPrice()); + + tousse.put("departCoding", tousseInstance.getLocation()); + tousse.put("location", tousseInstance.getLocation()); + tousse.put("settleAccountsDepartCode", tousseInstance.getSettleAccountsDepartCode()); + tousse.put("settleAccountsDepart", tousseInstance.getSettleAccountsDepart()); + tousse.put("tousseInstance", tousse); + + tousse.put("isDelivery", ""); + tousse.put("isPacking", ""); + tousse.put("uiProvider", "col"); + tousse.put("leaf", true); + tousse.put("iconCls", "task"); + + children.add(tousse); + } + folder.put("children", children); + + tousses.add(folder); + } + } + + return tousses; + } } Index: ssts-returngoods/src/main/java/com/forgon/disinfectsystem/returngoodsrecord/service/ReturnGoodsRecordManager.java =================================================================== diff -u -r12777 -r17725 --- ssts-returngoods/src/main/java/com/forgon/disinfectsystem/returngoodsrecord/service/ReturnGoodsRecordManager.java (.../ReturnGoodsRecordManager.java) (revision 12777) +++ ssts-returngoods/src/main/java/com/forgon/disinfectsystem/returngoodsrecord/service/ReturnGoodsRecordManager.java (.../ReturnGoodsRecordManager.java) (revision 17725) @@ -6,6 +6,8 @@ import java.util.List; import java.util.Map; +import net.sf.json.JSONArray; + import org.apache.commons.lang.StringUtils; import com.forgon.disinfectsystem.entity.recyclingrecord.RecyclingRecord; @@ -52,4 +54,6 @@ // 根据一次性物品ID和部门,找出可以退货的批次 public String getDisposableGoodsBatchStocksByDisposableGoodsStockIDAndWarehouseDepart(String diposableGoodsID, String warehouseID,String depart); + public JSONArray loadDiposableGoodsReturnGoodsInfoByInvoicePlanId(String warehouseID,Long invoicePlanId); + public JSONArray loadReturnedTousseItemsByInvoicePlanId(Long invoicePlanId); }