/** * Create actual Payment * * @param C_Invoice_ID invoice * @param C_BPartner_ID partner ignored when invoice exists * @param C_Currency_ID currency * @param StmtAmt statement amount * @param TrxAmt transaction amt * @param C_BP_BankAccount_ID bank account * @param DateTrx transaction date * @param DateAcct accounting date * @param Description description * @param AD_Org_ID org * @return payment */ private MPayment createPayment( int C_Invoice_ID, int C_BPartner_ID, int C_Currency_ID, BigDecimal StmtAmt, BigDecimal TrxAmt, int C_BP_BankAccount_ID, Timestamp DateTrx, Timestamp DateAcct, String Description, int AD_Org_ID) { // Trx Amount = Payment overwrites Statement Amount if defined BigDecimal PayAmt = TrxAmt; if (PayAmt == null || Env.ZERO.compareTo(PayAmt) == 0) PayAmt = StmtAmt; if (C_Invoice_ID == 0 && (PayAmt == null || Env.ZERO.compareTo(PayAmt) == 0)) throw new IllegalStateException("@PayAmt@ = 0"); if (PayAmt == null) PayAmt = Env.ZERO; // MPayment payment = new MPayment(getCtx(), 0, get_TrxName()); payment.setAD_Org_ID(AD_Org_ID); payment.setC_BP_BankAccount_ID(C_BP_BankAccount_ID); payment.setTenderType(MPayment.TENDERTYPE_Check); if (DateTrx != null) payment.setDateTrx(DateTrx); else if (DateAcct != null) payment.setDateTrx(DateAcct); if (DateAcct != null) payment.setDateAcct(DateAcct); else payment.setDateAcct(payment.getDateTrx()); payment.setDescription(Description); // if (C_Invoice_ID != 0) { MInvoice invoice = new MInvoice(getCtx(), C_Invoice_ID, null); payment.setC_DocType_ID(invoice.isSOTrx()); // Receipt payment.setC_Invoice_ID(invoice.getC_Invoice_ID()); payment.setC_BPartner_ID(invoice.getC_BPartner_ID()); if (PayAmt.signum() != 0) // explicit Amount { payment.setC_Currency_ID(C_Currency_ID); if (invoice.isSOTrx()) payment.setPayAmt(PayAmt); else // payment is likely to be negative payment.setPayAmt(PayAmt.negate()); payment.setOverUnderAmt(invoice.getGrandTotal(true).subtract(payment.getPayAmt())); } else // set Pay Amout from Invoice { payment.setC_Currency_ID(invoice.getC_Currency_ID()); payment.setPayAmt(invoice.getGrandTotal(true)); } } else if (C_BPartner_ID != 0) { payment.setC_BPartner_ID(C_BPartner_ID); payment.setC_Currency_ID(C_Currency_ID); if (PayAmt.signum() < 0) // Payment { payment.setPayAmt(PayAmt.abs()); payment.setC_DocType_ID(false); } else // Receipt { payment.setPayAmt(PayAmt); payment.setC_DocType_ID(true); } } else return null; payment.save(); // payment.processIt(MPayment.DOCACTION_Complete); payment.save(); return payment; } // createPayment
/** * 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