/**
   * action method for editing PurchaseOrderItemReceipt
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ActionForward edit(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    PurchaseOrderItemReceipt poir = getPurchaseOrderItemReceiptFromRequest(request);
    this.checkEditPower(poir, request);
    this.putReceiverNoToRequest(poir.getPurchaseOrderItem(), request);
    this.putMaxQtyToRequest(poir.getPurchaseOrderItem(), poir, request);

    if (!isBack(request)) {
      BeanForm purchaseOrderItemReceiptForm =
          (BeanForm) getForm("/updatePurchaseOrderItemReceipt", request);
      purchaseOrderItemReceiptForm.populateToForm(poir);
    }

    this.setAdding(false, request);

    if (this.isRequestor(poir.getPurchaseOrderItem(), request)) {
      if (poir.getReceiveDate1() != null)
        request.setAttribute("x_receiveDate", poir.getReceiveDate1());
      else request.setAttribute("x_receiveDate", new Date());
    } else {
      if (poir.getReceiveDate2() != null)
        request.setAttribute("x_receiveDate", poir.getReceiveDate2());
      else request.setAttribute("x_receiveDate", new Date());
    }

    return mapping.findForward("page");
  }
  /**
   * action method for inserting PurchaseOrderItemReceipt
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ActionForward insert(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    PurchaseOrderItem poi = this.getPurchaseOrderItemFromRequest(request);
    this.checkEditPower(poi, request);

    BeanForm purchaseOrderItemReceiptForm = (BeanForm) form;
    PurchaseOrderItemReceipt poir = new PurchaseOrderItemReceipt();
    purchaseOrderItemReceiptForm.populate(poir, BeanForm.TO_BEAN);
    poir.setPurchaseOrderItem(poi);

    PurchaseOrderItemReceiptManager pm = ServiceLocator.getPurchaseOrderItemReceiptManager(request);
    if (!pm.checkQty(poir))
      throw new BackToInputActionException("purchaseOrderItemReceipt.qtyExceeds");

    poir = pm.insertPurchaseOrderItemReceipt(poir, this.getCurrentUser(request));
    request.setAttribute("X_OBJECT", poir);
    request.setAttribute("X_ROWPAGE", "purchaseOrderItemReceipt/row.jsp");

    EmailManager em = ServiceLocator.getEmailManager(request);

    Map context = new HashMap();
    User emailToUser = null;
    if (this.getCurrentUser(request).equals(poir.getReceiver1())) {
      emailToUser = poir.getReceiver2();
      context.put("x_receiveQty", poir.getReceiveQty1());
      context.put("x_receiveDate", poir.getReceiveDate1());
    } else {
      emailToUser = poir.getReceiver1();
      context.put("x_receiveQty", poir.getReceiveQty2());
      context.put("x_receiveDate", poir.getReceiveDate2());
    }
    context.put("x_emailToUser", emailToUser);
    context.put("x_receiver", this.getCurrentUser(request));
    context.put("x_poir", poir);
    context.put("role", EmailManager.EMAIL_ROLE_RECEIVER);
    em.insertEmail(poir.getLogSite(), emailToUser.getEmail(), "POItemReceive.vm", context);

    return mapping.findForward("success");
  }
  /**
   * action method for creating PurchaseOrderItemReceipt
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ActionForward newObject(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    PurchaseOrderItem poi = this.getPurchaseOrderItemFromRequest(request);
    this.checkEditPower(poi, request);
    this.putReceiverNoToRequest(poi, request);
    this.putMaxQtyToRequest(poi, null, request);

    if (!isBack(request)) {
      PurchaseOrderItemReceipt purchaseOrderItemReceipt = new PurchaseOrderItemReceipt();
      purchaseOrderItemReceipt.setPurchaseOrderItem(poi);
      BeanForm purchaseOrderItemReceiptForm =
          (BeanForm) getForm("/insertPurchaseOrderItemReceipt", request);
      purchaseOrderItemReceiptForm.populateToForm(purchaseOrderItemReceipt);
    }

    this.setAdding(true, request);
    request.setAttribute("x_receiveDate", new Date());
    return mapping.findForward("page");
  }
 private void putMaxQtyToRequest(
     PurchaseOrderItem poi, PurchaseOrderItemReceipt poir, HttpServletRequest request)
     throws Exception {
   PurchaseOrderItemReceiptManager pm = ServiceLocator.getPurchaseOrderItemReceiptManager(request);
   int receiveQty = pm.getRecevieSum(poi, this.getCurrentUser(request));
   int maxQty = poi.getQuantity() - receiveQty;
   if (poir != null) {
     User currentUser = this.getCurrentUser(request);
     if (poir.getReceiver1().equals(currentUser) && poir.getReceiveQty1() != null) {
       maxQty += poir.getReceiveQty1().intValue();
     } else if (poir.getReceiver2().equals(currentUser) && poir.getReceiveQty2() != null) {
       maxQty += poir.getReceiveQty2().intValue();
     }
   }
   if (maxQty <= 0)
     throw new ActionException("purchaseOrderItemReceipt.edit.purchaseOrderItem.alreadyReceived");
   request.setAttribute("x_maxQty", new Integer(maxQty));
 }
 private void checkDeletePower(PurchaseOrderItemReceipt poir, HttpServletRequest request) {
   this.checkViewPower(poir.getPurchaseOrderItem(), request);
   if (poir.isFinished()) throw new ActionException("purchaseOrderItemReceipt.delete.finished");
 }