Exemplo n.º 1
0
  /**
   * The doPost method of the servlet. <br>
   * This method is called when a form has its tag value method equals to post.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    Connection conn = null;
    try {
      // 使用連接池獲取連接
      DataSource ds = (DataSource) getServletConfig().getServletContext().getAttribute("dbpool");
      conn = ds.getConnection();
      conn.setAutoCommit(false);

      String userId = request.getSession().getAttribute("userId").toString();
      String curIntake = request.getSession().getAttribute("curIntake").toString();

      String isbn = null;
      if (request.getParameter("isbn") != null && !request.getParameter("isbn").equals("")) {
        isbn = request.getParameter("isbn").trim();
      }

      String fromDate = null;
      if (request.getParameter("fromDate") != null
          && !request.getParameter("fromDate").equals("")) {
        fromDate = request.getParameter("fromDate");
      }

      String toDate = null;
      if (request.getParameter("toDate") != null && !request.getParameter("toDate").equals("")) {
        toDate = request.getParameter("toDate");
      }

      BookDAOImpl bookDAOImpl = new BookDAOImpl();
      OrderDetailDAOImpl orderDetailDAOImpl = new OrderDetailDAOImpl();
      BookPurchasingDAOImpl bookPurchasingDAOImpl = new BookPurchasingDAOImpl();

      String type = null;
      if (request.getParameter("type") != null && !request.getParameter("type").equals("")) {
        type = request.getParameter("type");
      }
      // add
      String changetype = null;
      changetype = request.getParameter("changetype");

      // add
      if (type != null && type.equals("changeBook")) {
        List list = null;
        if (request.getSession().getAttribute("list") != null) {
          list = (List) request.getSession().getAttribute("list");
        }
        String sId = request.getParameter("sId");
        String[] id = sId.split(";");

        String remarks = request.getParameter("remarks");
        if (!ToolsOfString.isNull(remarks)) {
          remarks = new String(remarks.getBytes("ISO-8859-1"), "UTF-8");
        }
        if (changetype.equals("S")) {
          int confrimQty = 0;
          // 該Isbn值為要更新的Isbn
          // List<OrDetail> changeIsbnlist = orderDetailDAOImpl.getChangeBookList(conn, curIntake,
          // isbn, confrimQty);
          List<OrDetail> changeIsbnlist =
              orderDetailDAOImpl.getChangeNoActindBookList(conn, curIntake, isbn, confrimQty);
          List<OrDetail> updateList = new ArrayList();
          List<OrDetail> unUpdateList = new ArrayList<OrDetail>();
          boolean updateFlag = true;

          if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
              for (int j = 0; j < id.length; j++) {
                Integer doId = Integer.parseInt(id[j]);
                if (doId == i) {
                  OrDetail od = (OrDetail) list.get(i);
                  boolean flag = true;
                  if (changeIsbnlist != null && !changeIsbnlist.isEmpty()) {
                    for (OrDetail orDetail : changeIsbnlist) {
                      // 若同一個訂單內含有要更換的isbn就不再更新 remove  by xfke
                      /*if (od.getOrderSeqNo() == orDetail .getOrderSeqNo()) {
                      	unUpdateList.add(orDetail);
                      	flag = false;
                      	break;
                      }*/

                      // 若同一個訂單內含有要更換的ISBN,則被更換的ISBN改為 actind='N',要更換的ISBN改為actind='Y'
                      if (od.getOrderSeqNo() == orDetail.getOrderSeqNo()) {
                        od.setActInd("N");
                        od.setUpdUid(userId);
                        orDetail.setActInd("Y");
                        orDetail.setUpdUid(userId);
                        if (orderDetailDAOImpl.updateOrderDetailInfo(conn, od)
                            && orderDetailDAOImpl.updateOrderDetailInfo(conn, orDetail)) {

                          updateFlag = true;
                          // 并不是真正的置換,只是修改兩個記錄的actind
                          unUpdateList.add(orDetail);
                        } else {
                          updateFlag = false;
                        }
                        flag = false;
                        break;
                      }
                    }
                  }
                  if (flag) {
                    od.setUpdDate(new Date());
                    od.setUpdUid(userId);
                    od.setActInd("Y");
                    updateList.add(od);
                  }
                }
              }
            }
          }
          request.setAttribute("unUpdateList", unUpdateList);
          if (updateList != null && !updateList.isEmpty()) {
            for (OrDetail od : updateList) {
              try {
                if (orderDetailDAOImpl.changeOrDetailIsbn(conn, od, isbn, remarks) == false) {
                  updateFlag = false;
                  break;
                }
              } catch (Exception e) {
                conn.rollback();
                request.setAttribute("msg", e.getMessage());
                request.setAttribute("type", "changeBook");
                request.setAttribute("changetype", changetype);
                request.getRequestDispatcher("msg.jsp").forward(request, response);
              }
            }
          } else if (unUpdateList == null || unUpdateList.isEmpty()) {
            request.setAttribute("msg", "沒有可以置換的圖書記錄!");
            request.setAttribute("type", "changeBook");
            request.setAttribute("changetype", changetype);
            request.getRequestDispatcher("msg.jsp").forward(request, response);
          }

          if (updateFlag) {
            conn.commit();
            request.setAttribute("msg", "圖書置換成功!");
            request.setAttribute("type", "changeBook");
            request.setAttribute("changetype", changetype);
            request.getRequestDispatcher("msg.jsp").forward(request, response);
          } else {
            conn.rollback();
            request.setAttribute("msg", "圖書置換失敗!");
            request.setAttribute("type", "changeBook");
            request.setAttribute("changetype", changetype);
            request.getRequestDispatcher("msg.jsp").forward(request, response);
          }
        } else if (changetype.equals("P")) {
          // List<BookPurchasingBean> changeIsbnlist =
          // bookPurchasingDAOImpl.getBookPurchasingBeanByIsbn(conn, isbn, curIntake, null, null,
          // true);
          List<BookPurchasingBean> updateList = new ArrayList<BookPurchasingBean>();
          List<BookPurchasingBean> unUpdateList = new ArrayList<BookPurchasingBean>();

          if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
              for (int j = 0; j < id.length; j++) {
                Integer doId = Integer.parseInt(id[j]);
                if (doId == i) {
                  BookPurchasingBean od = (BookPurchasingBean) list.get(i);
                  if (od.getBookPurchase().getQuantity() == od.getBookPurchase().getLeave()) {
                    // NEW ISBN
                    od.getBook().setIsbn(isbn);
                    od.setChangeRemarks(remarks);
                    od.getBookPurchase().setUpdDate(new Date());
                    od.getBookPurchase().setUpdUid(userId);
                    updateList.add(od);
                  } else {
                    unUpdateList.add(od);
                  }
                }
              }
            }
          }
          request.setAttribute("unUpdateList", unUpdateList);
          if (updateList != null && !updateList.isEmpty()) {
            boolean updateFlag = true;
            for (BookPurchasingBean bPurchasingBean : updateList) {
              try {
                if (bookPurchasingDAOImpl.changeIsbnList(conn, bPurchasingBean) == false) {
                  updateFlag = false;
                  break;
                }
              } catch (Exception e) {
                conn.rollback();
                request.setAttribute("msg", e.getMessage());
                request.setAttribute("type", "changeBook");
                request.setAttribute("changetype", changetype);
                request.getRequestDispatcher("msg.jsp").forward(request, response);
              }
            }
            if (updateFlag) {
              conn.commit();
              request.setAttribute("msg", "圖書置換成功!");
              request.setAttribute("type", "changeBook");
              request.setAttribute("changetype", changetype);
              request.getRequestDispatcher("msg.jsp").forward(request, response);
            } else {
              conn.rollback();
              request.setAttribute("msg", "圖書置換失敗!");
              request.setAttribute("type", "changeBook");
              request.setAttribute("changetype", changetype);
              request.getRequestDispatcher("msg.jsp").forward(request, response);
            }
          } else {
            request.setAttribute("msg", "沒有可以置換的圖書記錄!");
            request.setAttribute("type", "changeBook");
            request.setAttribute("changetype", changetype);
            request.getRequestDispatcher("msg.jsp").forward(request, response);
          }
        }
      } else if (type != null && type.equals("checkChangeIsbn")) {
        Book book = bookDAOImpl.getBookByPK(conn, isbn);
        if (book != null && book.getIsbn() != null) {
          out.print("Y");
        } else {
          out.print("N");
        }
      } else {
        request.getSession().removeAttribute("list");
        int confrimQty = 1;

        // add rainbow
        request.setAttribute("changetype", changetype);
        if (changetype.equals("S")) {
          List list =
              orderDetailDAOImpl.getChangeBookList(
                  conn, curIntake, isbn, fromDate, toDate, confrimQty);
          request.setAttribute("list", list);
          request.getSession().setAttribute("list", list);
        } else {
          List list =
              bookPurchasingDAOImpl.getBookPurchasingBeanByIsbn(
                  conn, isbn, curIntake, fromDate, toDate, true);
          request.setAttribute("list", list);
          request.getSession().setAttribute("list", list);
        }
        // add rainbow
        request.setAttribute("isbn", isbn);
        request.setAttribute("fromDate", fromDate);
        request.setAttribute("toDate", toDate);
        request.getRequestDispatcher("changeBookResult.jsp").forward(request, response);
      }
    } catch (Exception e) {
      try {
        conn.rollback();
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
      // throw new ServletException (e);
    } finally {
      try {
        if (conn != null && !conn.isClosed()) conn.close();
      } catch (SQLException sqle) {
        sqle.printStackTrace();
      }
    }
  }
Exemplo n.º 2
0
  public static List getDifferAmount(
      Connection conn, String studValue, String orderIntake, double paidAmount) {
    List differAmountList = new ArrayList();
    OrderDAOImpl orderDAOImpl = new OrderDAOImpl();
    OrderDetailDAOImpl orderDetailDAOImpl = new OrderDetailDAOImpl();
    SysConfigDAOImpl sysconfigImpl = new SysConfigDAOImpl();
    try {
      List<Order> inactiveOrderList =
          orderDAOImpl.getAllOrderSeqNoInfo(conn, studValue, orderIntake, "N");
      for (Order o : inactiveOrderList) {
        double amount = o.getPaidAmount();
        String currency = o.getPaidCurrency();
        if (o.getPaidCurrency() != null) {
          if (o.getPaidStatus().equals("Y")) {
            OrDetail od = new OrDetail();
            od.setActInd("N");
            od.setOrderSeqNo(o.getOrderSeqNo());
            List onlyList = new ArrayList();
            List<OrDetail> inactiveOrdetailList = orderDetailDAOImpl.getOrDetailInfo(conn, od);
            if (inactiveOrdetailList != null) {
              for (OrDetail ordetail : inactiveOrdetailList) {
                if (!"STUDENT".equals(ordetail.getUpdUid())) onlyList.add(ordetail);
              }
            }
            if (onlyList.size() == 1) {
              od = (OrDetail) inactiveOrdetailList.get(0);
              Difference value = new Difference();
              value.setPaidStatus(o.getPaidStatus());
              value.setOrderSeqNo(o.getOrderSeqNo());
              value.setPaidAmount(amount); // 已付金額總數
              value.setPaidcurrency(currency);
              value.setAmercemount(0); // 滯留金
              value.setFineforlatepay(0); // 逾期付款罰款金額
              BigDecimal differ =
                  new BigDecimal(Double.toString(value.getShoulePayAmount()))
                      .subtract(new BigDecimal(Double.toString(value.getPaidAmount())));
              value.setDifference(differ.doubleValue());
              value.setDifferenceMop(-o.getNetpaidamount());
              List isbnList = new ArrayList();
              isbnList.add(od.getIsbn());
              value.setIsbnList(isbnList);
              List bookPriceList = new ArrayList();
              bookPriceList.add(od.getPaidAmount());
              value.setBookPriceList(bookPriceList);
              value.setStudentNo(o.getStudentNo());
              differAmountList.add(value);
            }
          }
        }
      }

      List<Order> orderList = orderDAOImpl.getAllOrderSeqNoInfo(conn, studValue, orderIntake, "Y");
      for (Order o : orderList) {
        int orderSeqNo = o.getOrderSeqNo();
        // 實際所收取的價錢
        double amount = o.getPaidAmount();
        // 葡紙價錢
        double mopAmount = o.getNetpaidamount();
        String currency = o.getPaidCurrency();
        // 記錄的匯率
        Double rate = o.getCurrate();
        List isbnList = new ArrayList();
        List bookPriceList = new ArrayList();

        if (!o.getPaidStatus().equals("N")
            || (o.getPaidStatus().equals("N") && o.getPaidCurrency() != null)) {
          Difference value = new Difference();
          value.setPaidStatus(o.getPaidStatus());
          if (o.getPaidStatus().equals("N") && o.getPaidCurrency() != null) {
            amount = paidAmount;
          }
          double latePay = o.getFineforlatepay();
          double amerceMount = o.getAmerceMount();
          // 獲取原補訂書費百分比
          double shippingFee = o.getShippingFee();
          String studentNo = o.getStudentNo();

          // List<StudReceipt> receiptList = orderDetailImpl.getStudReceipt(conn, studentNo + "," +
          // studentNo, orderIntake, orderSeqNo);
          List<StudReceipt> receiptList =
              StudReceiptInfo.getStudReceiptList(conn, orderIntake, studValue, orderSeqNo, null);

          SysConfig config = new SysConfig();
          config.setScType("FINERATE");
          config.setScKey("SHIPPING");
          config.setActInd("Y");
          SysConfig withdrawForCarry = sysconfigImpl.getSysConfig(conn, config);
          double feePercent = 0;
          if (withdrawForCarry != null && withdrawForCarry.getScValue1() != null) {
            feePercent = Double.parseDouble(withdrawForCarry.getScValue1());
          }

          double sumMop = 0, sumRmb = 0;
          double bookMopPrice = 0, bookRmbPrice = 0;
          double bookMopWithPrice = 0, bookRmbWithPrice = 0;

          for (StudReceipt receipt : receiptList) {
            double mopPrice = 0, rmbPrice = 0;
            if (receipt.getMopNetPrice() == 0) {
              mopPrice = receipt.getMopFuturePrice();
            } else {
              mopPrice = receipt.getMopNetPrice();
            }

            if (receipt.getConfirmQty() > 0) {
              isbnList.add(receipt.getIsbn());
              bookPriceList.add(mopPrice);
            }
            // 退書費
            BigDecimal withdrawFee =
                new BigDecimal(Integer.toString(receipt.getWithDrawQty()))
                    .multiply(new BigDecimal(Double.toString(receipt.getMopWithPrice())));
            // 退運費
            BigDecimal withdrawCarryFeeMop =
                new BigDecimal(Integer.toString(receipt.getWithDrawQty2()))
                    .multiply(new BigDecimal(Double.toString(mopPrice)))
                    .multiply(new BigDecimal(Double.toString(feePercent)))
                    .multiply(new BigDecimal(Double.toString(0.01)));
            if (0 < withdrawCarryFeeMop.doubleValue() && withdrawCarryFeeMop.doubleValue() < 1) {
              withdrawCarryFeeMop = new BigDecimal(1);
            } else {
              withdrawCarryFeeMop = withdrawCarryFeeMop.setScale(0, BigDecimal.ROUND_HALF_UP);
            }
            // 分別計算各個幣種的總數
            sumMop =
                ((new BigDecimal(Double.toString(mopPrice))
                            .multiply(new BigDecimal(Integer.toString(receipt.getConfirmQty()))))
                        .add(withdrawFee)
                        .add(withdrawCarryFeeMop))
                    .add(new BigDecimal(Double.toString(sumMop)))
                    .doubleValue();

            bookMopPrice =
                (new BigDecimal(Double.toString(mopPrice))
                        .multiply(new BigDecimal(Integer.toString(receipt.getConfirmQty()))))
                    .add(new BigDecimal(Double.toString(bookMopPrice)))
                    .doubleValue();
            bookMopWithPrice =
                (new BigDecimal(Double.toString(mopPrice))
                        .multiply(new BigDecimal(Integer.toString(receipt.getWithDrawQty()))))
                    .add(new BigDecimal(Double.toString(bookMopWithPrice)))
                    .doubleValue();
          }

          // 補訂運費計算=(確定數 + 退書數) * 書價
          bookMopPrice =
              (new BigDecimal(Double.toString(bookMopPrice))
                      .add(new BigDecimal(Double.toString(bookMopWithPrice))))
                  .multiply(new BigDecimal(Double.toString(shippingFee)))
                  .doubleValue();
          if (0 < bookMopPrice && bookMopPrice < 1) {
            bookMopPrice = 1;
          } else {
            bookMopPrice =
                new BigDecimal(Double.toString(bookMopPrice))
                    .setScale(0, BigDecimal.ROUND_HALF_UP)
                    .doubleValue();
          }

          value.setOrderSeqNo(orderSeqNo);
          value.setPaidAmount(amount); // 已付金額總數
          value.setPaidcurrency(currency);

          double finalMopAmount =
              new BigDecimal(sumMop)
                  .add(new BigDecimal(latePay))
                  .add(new BigDecimal(amerceMount))
                  .add(new BigDecimal(bookMopPrice))
                  .doubleValue();
          value.setShouldPayMopAmount(finalMopAmount);
          double finalAmount =
              new BigDecimal(finalMopAmount)
                  .multiply(new BigDecimal(rate))
                  .setScale(0, BigDecimal.ROUND_HALF_UP)
                  .doubleValue();
          value.setShoulePayAmount(finalAmount); // 需實收金額總數

          value.setAmercemount(0); // 滯留金
          value.setFineforlatepay(0); // 逾期付款罰款金額
          BigDecimal differ =
              new BigDecimal(Double.toString(value.getShoulePayAmount()))
                  .subtract(new BigDecimal(Double.toString(value.getPaidAmount())));
          BigDecimal differMop =
              new BigDecimal(value.getShouldPayMopAmount()).subtract(new BigDecimal(mopAmount));
          value.setDifference(differ.doubleValue());
          value.setDifferenceMop(differMop.doubleValue());
          value.setIsbnList(isbnList);
          value.setBookPriceList(bookPriceList);
          value.setStudentNo(studentNo);
          differAmountList.add(value);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      differAmountList = null;
    }
    return differAmountList;
  }