Example #1
0
  /**
   * Create Order from Basket
   *
   * @param wu web User
   * @param wb web basket
   * @return true if created & processed
   */
  private boolean createOrder(WebUser wu, WebBasket wb) {
    m_order = new MOrder(m_ctx, 0, null);
    log.fine(
        "AD_Client_ID="
            + m_order.getAD_Client_ID()
            + ",AD_Org_ID="
            + m_order.getAD_Org_ID()
            + " - "
            + m_order);
    //
    m_order.setC_DocTypeTarget_ID(MOrder.DocSubTypeSO_Prepay);
    m_order.setPaymentRule(MOrder.PAYMENTRULE_CreditCard);
    m_order.setDeliveryRule(MOrder.DELIVERYRULE_AfterReceipt);
    m_order.setInvoiceRule(MOrder.INVOICERULE_Immediate);
    m_order.setIsSelfService(true);
    if (wb.getM_PriceList_ID() > 0) m_order.setM_PriceList_ID(wb.getM_PriceList_ID());
    if (wb.getSalesRep_ID() != 0) m_order.setSalesRep_ID(wb.getSalesRep_ID());

    //	BPartner
    m_order.setC_BPartner_ID(wu.getC_BPartner_ID());
    m_order.setC_BPartner_Location_ID(wu.getC_BPartner_Location_ID());
    m_order.setAD_User_ID(wu.getAD_User_ID());
    //
    m_order.setSendEMail(true);
    m_order.setDocAction(MOrder.DOCACTION_Prepare);
    m_order.saveEx();
    log.fine("ID=" + m_order.getC_Order_ID() + ", DocNo=" + m_order.getDocumentNo());

    ArrayList<WebBasketLine> lines = wb.getLines();
    for (int i = 0; i < lines.size(); i++) {
      WebBasketLine wbl = lines.get(i);
      MOrderLine ol = new MOrderLine(m_order);
      ol.setM_Product_ID(wbl.getM_Product_ID(), true);
      ol.setQty(wbl.getQuantity());
      ol.setPrice();
      ol.setPrice(wbl.getPrice());
      ol.setTax();
      ol.saveEx();
    } //	for all lines
    boolean ok = m_order.processIt(MOrder.DOCACTION_Prepare);
    m_order.saveEx();

    //	Web User = Customer
    if (!wu.isCustomer()) {
      //	log.info("-------------------------------------- " + wu.isCustomer());
      wu.setIsCustomer(true);
      wu.save();
      //	log.info("-------------------------------------- " + wu.isCustomer());
    }
    BigDecimal amt = m_order.getGrandTotal();
    log.info("Amt=" + amt);
    return ok;
  } //	createOrder
  /**
   * Stream invoice
   *
   * @param request request
   * @param response response
   * @return "" or error message
   */
  private String streamInvoice(HttpServletRequest request, HttpServletResponse response) {
    int MIN_SIZE = 2000; // 	if not created size is 1015

    //	Get Invoice ID
    int C_Invoice_ID = WebUtil.getParameterAsInt(request, "Invoice_ID");
    if (C_Invoice_ID == 0) {
      log.fine("No ID)");
      return "No Invoice ID";
    }

    //	Get Invoice
    Properties ctx = JSPEnv.getCtx(request);
    MInvoice invoice = new MInvoice(ctx, C_Invoice_ID, null);
    if (invoice.getC_Invoice_ID() != C_Invoice_ID) {
      if (log.isLoggable(Level.FINE)) log.fine("Invoice not found - ID=" + C_Invoice_ID);
      return "Invoice not found";
    }
    //	Get WebUser & Compare with invoice
    HttpSession session = request.getSession(true);
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    if (wu.getC_BPartner_ID() != invoice.getC_BPartner_ID()) {
      log.warning(
          "Invoice from BPartner - C_Invoice_ID="
              + C_Invoice_ID
              + " - BP_Invoice="
              + invoice.getC_BPartner_ID()
              + " = BP_Web="
              + wu.getC_BPartner_ID());
      return "Your invoice not found";
    }

    //	Check Directory
    String dirName = ctx.getProperty("documentDir", ".");
    try {
      File dir = new File(dirName);
      if (!dir.exists()) dir.mkdir();
    } catch (Exception ex) {
      log.log(Level.SEVERE, "Could not create directory " + dirName, ex);
      return "Streaming error - directory";
    }
    //	Check if Invoice already created
    String fileName = invoice.getPDFFileName(dirName);
    File file = new File(fileName);
    if (file.exists() && file.isFile() && file.length() > MIN_SIZE) {
      if (log.isLoggable(Level.INFO))
        log.info("Existing: " + file + " - " + new Timestamp(file.lastModified()));
    } else {
      if (log.isLoggable(Level.INFO)) log.info("New: " + fileName);
      file = invoice.createPDF(file);
      if (file != null) {
        invoice.setDatePrinted(new Timestamp(System.currentTimeMillis()));
        invoice.saveEx();
      }
    }
    //	Issue Error
    if (file == null || !file.exists() || file.length() < MIN_SIZE) {
      log.warning("File does not exist - " + file);
      return "Streaming error - file";
    }

    //	Send PDF
    try {
      int bufferSize = 2048; // 	2k Buffer
      int fileLength = (int) file.length();
      //
      response.setContentType("application/pdf");
      response.setBufferSize(bufferSize);
      response.setContentLength(fileLength);
      //
      if (log.isLoggable(Level.FINE)) log.fine(file.getAbsolutePath() + ", length=" + fileLength);
      long time = System.currentTimeMillis(); // 	timer start
      //
      FileInputStream in = new FileInputStream(file);
      ServletOutputStream out = response.getOutputStream();
      byte[] buffer = new byte[bufferSize];
      double totalSize = 0;
      int count = 0;
      do {
        count = in.read(buffer, 0, bufferSize);
        if (count > 0) {
          totalSize += count;
          out.write(buffer, 0, count);
        }
      } while (count != -1);
      out.flush();
      out.close();
      //
      in.close();
      time = System.currentTimeMillis() - time;
      double speed = (totalSize / 1024) / ((double) time / 1000);
      if (log.isLoggable(Level.FINE))
        log.fine("Length=" + totalSize + " - " + time + " ms - " + speed + " kB/sec");
    } catch (IOException ex) {
      log.log(Level.SEVERE, ex.toString());
      return "Streaming error";
    }

    return null;
  } //	streamInvoice