public DeliveryOrder createDeliveryOrder(List<PurchaseOrderDetail> purchaseOrderDetailList)
      throws IllegalAccessException, InvocationTargetException {

    DeliveryOrder deliveryOrder = null;
    PurchaseOrder purchaseOrder = null;
    for (int i = 0; i < purchaseOrderDetailList.size(); i++) {
      PurchaseOrderDetail purchaseOrderDetail = purchaseOrderDetailList.get(i);

      if (deliveryOrder == null) {
        purchaseOrder = purchaseOrderDetail.getPurchaseOrder();
        deliveryOrder = new DeliveryOrder();
        BeanUtils.copyProperties(deliveryOrder, purchaseOrder);

        deliveryOrder.setDoNo(
            this.numberControlManager.generateNumber(
                purchaseOrder.getPlantSupplier().getDoNoPrefix(), 10));
        deliveryOrder.setExternalDoNo(deliveryOrder.getDoNo());
        deliveryOrder.setCreateDate(new Date());
        deliveryOrder.setIsExport(false);
        deliveryOrder.setIsPrint(false);
        deliveryOrder.setIsRead(false);
        deliveryOrder.setStatus("Create");
      }

      DeliveryOrderDetail deliveryOrderDetail = new DeliveryOrderDetail();
      BeanUtils.copyProperties(deliveryOrderDetail, purchaseOrderDetail);

      deliveryOrderDetail.setDeliveryOrder(deliveryOrder);
      deliveryOrderDetail.setUnitCount(purchaseOrderDetail.getItem().getUnitCount());
      deliveryOrderDetail.setQty(purchaseOrderDetail.getCurrentShipQty());
      // deliveryOrderDetail.setOrderQty(purchaseOrderDetail.getQty());
      deliveryOrderDetail.setReferenceOrderNo(purchaseOrderDetail.getPurchaseOrder().getPoNo());
      deliveryOrderDetail.setReferenceSequence(purchaseOrderDetail.getSequence());
      deliveryOrderDetail.setPurchaseOrderDetail(purchaseOrderDetail);
      deliveryOrder.addDeliveryOrderDetail(deliveryOrderDetail);

      // if (purchaseOrderDetail.getShipQty() == null) {
      // purchaseOrderDetail.setShipQty(BigDecimal.ZERO);
      // }
      //
      // purchaseOrderDetail.setShipQty(purchaseOrderDetail.getShipQty().add(purchaseOrderDetail.getCurrentShipQty()));

      this.purchaseOrderDetailManager.save(purchaseOrderDetail);
    }

    // this.purchaseOrderManager.tryClosePurchaseOrder(purchaseOrder.getPoNo());
    this.save(deliveryOrder);

    return deliveryOrder;
  }
  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;
  }