@SuppressWarnings("unchecked")
  public List<DeliveryOrder> getUnexportDeliveryOrderByPlant(Plant plant) {
    DetachedCriteria criteria = DetachedCriteria.forClass(DeliveryOrder.class);
    criteria.createAlias("plantSupplier", "ps");

    criteria.add(Restrictions.eq("ps.plant", plant));
    criteria.add(Restrictions.eq("isPrint", true));
    criteria.add(Restrictions.eq("isExport", false));
    criteria.add(Restrictions.eq("status", "Confirm"));
    criteria.add(Restrictions.eq("ps.needExportDo", true));
    criteria.addOrder(Order.asc("createDate"));

    List<DeliveryOrder> deliveryOrderList = this.findByCriteria(criteria);

    if (deliveryOrderList != null && deliveryOrderList.size() > 0) {
      for (int i = 0; i < deliveryOrderList.size(); i++) {
        DeliveryOrder deliveryOrder = deliveryOrderList.get(i);

        if (deliveryOrder.getDeliveryOrderDetailList() != null
            && deliveryOrder.getDeliveryOrderDetailList().size() > 0) {}
      }
    }

    return deliveryOrderList;
  }
  public DeliveryOrder confirm(DeliveryOrder deliveryOrder) {
    deliveryOrder.setStatus("Confirm");

    List<DeliveryOrderDetail> deliveryOrderDetailList = new ArrayList<DeliveryOrderDetail>();
    for (int i = 0; i < deliveryOrder.getDeliveryOrderDetailList().size(); i++) {
      DeliveryOrderDetail deliveryOrderDetail = deliveryOrder.getDeliveryOrderDetailList().get(i);
      if (deliveryOrderDetail.getQty() != null
          && deliveryOrderDetail.getQty().compareTo(BigDecimal.ZERO) > 0) {
        deliveryOrderDetailList.add(deliveryOrderDetail);

        if (deliveryOrderDetail.getPurchaseOrderDetail() != null) {
          PurchaseOrderDetail purchaseOrderDetail = deliveryOrderDetail.getPurchaseOrderDetail();
          if (purchaseOrderDetail.getShipQty() == null) {
            purchaseOrderDetail.setShipQty(deliveryOrderDetail.getQty());
          } else {
            BigDecimal deliverQty =
                purchaseOrderDetail.getShipQty().add(deliveryOrderDetail.getQty());
            purchaseOrderDetail.setShipQty(deliverQty);
          }

          purchaseOrderDetail = this.purchaseOrderDetailManager.save(purchaseOrderDetail);

          if (purchaseOrderDetail.getQty().compareTo(purchaseOrderDetail.getShipQty()) == 0) {
            this.purchaseOrderManager.tryClosePurchaseOrder(
                purchaseOrderDetail.getPurchaseOrder().getPoNo(), purchaseOrderDetail);
          }

        } else if (deliveryOrderDetail.getScheduleItemDetail() != null) {
          ScheduleItemDetail scheduleItemDetail = deliveryOrderDetail.getScheduleItemDetail();
          if (scheduleItemDetail.getDeliverQty() == null) {
            scheduleItemDetail.setDeliverQty(deliveryOrderDetail.getQty());
          } else {
            BigDecimal deliverQty =
                scheduleItemDetail.getDeliverQty().add(deliveryOrderDetail.getQty());
            scheduleItemDetail.setDeliverQty(deliverQty);
          }

          this.scheduleItemDetailManager.save(scheduleItemDetail);
        }
      } else {
        this.deliveryOrderDetailManager.remove(deliveryOrderDetail.getId());
      }
    }

    deliveryOrder.setDeliveryOrderDetailList(deliveryOrderDetailList);
    this.genericDao.save(deliveryOrder);

    return deliveryOrder;
  }
 public DeliveryOrder get(String doNo, boolean includeDetail) {
   DeliveryOrder deliveryOrder = this.genericDao.get(doNo);
   if (includeDetail
       && deliveryOrder.getDeliveryOrderDetailList() != null
       && deliveryOrder.getDeliveryOrderDetailList().size() > 0) {}
   return deliveryOrder;
 }
  public DeliveryOrder createScheduleDeliveryOrder(DeliveryOrder deliveryOrder) {

    PlantSupplier plantSupplier =
        this.plantSupplierManager.get(deliveryOrder.getPlantSupplier().getId());
    deliveryOrder.setPlantSupplier(plantSupplier);
    deliveryOrder.setDoNo(
        this.numberControlManager.generateNumber(plantSupplier.getDoNoPrefix(), 10));
    deliveryOrder.setExternalDoNo(deliveryOrder.getDoNo());

    List<DeliveryOrderDetail> deliveryOrderDetailList = deliveryOrder.getDeliveryOrderDetailList();

    for (int i = 0; i < deliveryOrderDetailList.size(); i++) {

      DeliveryOrderDetail deliveryOrderDetail = deliveryOrder.getDeliveryOrderDetailList().get(i);
      Item item = this.itemManager.get(deliveryOrderDetail.getItem().getId());
      deliveryOrderDetail.setDeliveryOrder(deliveryOrder);
      deliveryOrderDetail.setItem(item);
      deliveryOrderDetail.setPurchaseOrderDetail(null);
      // deliveryOrderDetail.setQty(deliveryOrderDetail.getCurrentQty());

      // ScheduleItemDetail scheduleItemDetail =
      // deliveryOrderDetail.getScheduleItemDetail();

      // BigDecimal deliverQty = scheduleItemDetail.getDeliverQty();
      // if (deliverQty == null) {
      // deliverQty = deliveryOrderDetail.getCurrentQty();
      // } else {
      // deliverQty = deliverQty.add(deliveryOrderDetail.getCurrentQty());
      // }
      // scheduleItemDetail.setDeliverQty(deliverQty);

      // this.scheduleItemDetailManager.save(scheduleItemDetail);
    }
    deliveryOrder.setStatus("Create");
    this.save(deliveryOrder);

    return this.get(deliveryOrder.getDoNo(), true);
  }
  private void AddE1EDL20List(List<DELVRY03E1EDL20> E1EDL20List, DeliveryOrder deliveryOrder) {
    DELVRY03E1EDL20 E1EDL20 = new DELVRY03E1EDL20();
    E1EDL20.setSEGMENT("1");
    E1EDL20.setVBELN(deliveryOrder.getExternalDoNo());

    List<DELVRY03E1ADRM1> E1ADRM1List = E1EDL20.getE1ADRM1();
    addE1ADRM1List(E1ADRM1List, deliveryOrder);

    List<DELVRY03E1EDT13> E1EDT13List = E1EDL20.getE1EDT13();
    addE1EDT13List(E1EDT13List, deliveryOrder);

    List<DELVRY03E1EDL24> E1EDL24List = E1EDL20.getE1EDL24();
    addE1EDL24List(E1EDL24List, deliveryOrder.getDeliveryOrderDetailList());

    E1EDL20List.add(E1EDL20);
  }