Search in sources :

Example 41 with MPayment

use of org.compiere.model.MPayment in project adempiere by adempiere.

the class FixPaymentCashLine method getOfCash.

//        doIt
/**
	 * 	Get Payment of Cash
	 *	@param ctx context
	 *	@param cashName Cash Name
	 *	@return payments of cash
	 *	@param trxName transaction
	 */
public static MPayment[] getOfCash(Properties ctx, String cashName, BigDecimal amt, int C_BankAccount_ID, int AD_Client_ID, String trxName) {
    String sql = "SELECT * FROM C_Payment p WHERE p.DocumentNo=? AND R_PnRef=? AND PayAmt=? AND C_BankAccount_ID=? AND AD_Client_ID=? " + " AND TrxType='X' AND TenderType='X'";
    ArrayList<MPayment> list = new ArrayList<MPayment>();
    PreparedStatement pstmt = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        pstmt.setString(1, cashName);
        pstmt.setString(2, cashName);
        pstmt.setBigDecimal(3, amt.negate());
        pstmt.setInt(4, C_BankAccount_ID);
        pstmt.setInt(5, AD_Client_ID);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) list.add(new MPayment(ctx, rs, trxName));
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        s_log.log(Level.SEVERE, sql, e);
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    MPayment[] retValue = new MPayment[list.size()];
    list.toArray(retValue);
    return retValue;
}
Also used : MPayment(org.compiere.model.MPayment) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 42 with MPayment

use of org.compiere.model.MPayment in project adempiere by adempiere.

the class OrderServlet method createPayment.

//	doPost
/**************************************************************************
	 * 	Create Payment, but don't save it
	 * 	@param session session
	 * 	@param ctx context
	 * 	@param wu web user
	 * 	@param wo Order
	 * 	@return Payment
	 */
private MPayment createPayment(HttpSession session, Properties ctx, WebUser wu, WebOrder wo) {
    //	See PaymentServlet.doGet
    MPayment p = new MPayment(ctx, 0, null);
    p.setAD_Org_ID(wo.getAD_Org_ID());
    p.setIsSelfService(true);
    //	for CC selection
    p.setAmount(wo.getC_Currency_ID(), wo.getGrandTotal());
    p.setIsOnline(true);
    //	Sales CC Trx
    p.setC_DocType_ID(true);
    p.setTrxType(MPayment.TRXTYPE_Sales);
    p.setTenderType(MPayment.TENDERTYPE_CreditCard);
    //	Order Info
    p.setC_Order_ID(wo.getC_Order_ID());
    //	BP Info
    p.setBP_BankAccount(wu.getBankAccount());
    //
    return p;
}
Also used : MPayment(org.compiere.model.MPayment)

Example 43 with MPayment

use of org.compiere.model.MPayment in project adempiere by adempiere.

the class PaymentServlet method doPost.

//  doGet
/**
	 *  Process the HTTP Post request.
	 * 	The actual payment processing
	 *
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.info("Post from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
    Properties ctx = JSPEnv.getCtx(request);
    HttpSession session = request.getSession(true);
    //	WEnv.dump(session);
    //	WEnv.dump(request);
    //	Web User/Payment
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    MPayment p = (MPayment) session.getAttribute(ATTR_PAYMENT);
    WebOrder wo = (WebOrder) session.getAttribute(WebOrder.NAME);
    String url = null;
    if (wu == null || p == null)
        url = "/index.jsp";
    else if (processPayment(request, ctx, p, wu, wo))
        url = "/confirm.jsp";
    else
        url = "/paymentInfo.jsp";
    log.info("Forward to " + url);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);
}
Also used : HttpSession(javax.servlet.http.HttpSession) MPayment(org.compiere.model.MPayment) WebUser(org.compiere.util.WebUser) Properties(java.util.Properties) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 44 with MPayment

use of org.compiere.model.MPayment in project adempiere by adempiere.

the class PaymentServlet method doGet.

//  destroy
/**************************************************************************
	 *  Process the initial HTTP Get request.
	 *  Reads the Parameter Amt and optional C_Invoice_ID
	 *
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.info("Get from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
    Properties ctx = JSPEnv.getCtx(request);
    HttpSession session = request.getSession(true);
    session.removeAttribute(WebSessionCtx.HDR_MESSAGE);
    //	WEnv.dump(session);
    //	WEnv.dump(request);
    //	Non existing user or Existing Web Payment
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    MPayment p = (MPayment) session.getAttribute(ATTR_PAYMENT);
    if (wu == null) {
        log.info("No User");
        String url = "/index.jsp";
        log.info("Forward to " + url);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }
    //	Remove any open Order
    session.removeAttribute(WebOrder.NAME);
    //	Payment Amount
    String amtParam = WebUtil.getParameter(request, "Amt");
    if (amtParam == null || amtParam.length() == 0) {
        log.info("No Payment Amount (" + amtParam + ")");
        doPost(request, response);
        return;
    }
    char[] chars = amtParam.toCharArray();
    StringBuffer sb = new StringBuffer();
    boolean decimal = false;
    for (int i = chars.length - 1; i >= 0; i--) {
        char c = chars[i];
        if (c == ',' || c == '.') {
            if (!decimal) {
                sb.insert(0, '.');
                decimal = true;
            }
        } else if (Character.isDigit(c))
            sb.insert(0, c);
    }
    BigDecimal amt = null;
    try {
        if (sb.length() > 0) {
            amt = new BigDecimal(sb.toString());
            //	make it positive
            amt = amt.abs();
        }
    } catch (Exception ex) {
        log.warning("Parsing Amount=" + amtParam + " (" + sb + ") - " + ex.toString());
    }
    //	Need to be positive amount
    if (amt == null || amt.compareTo(Env.ZERO) < 0) {
        log.info("No valid Payment Amount (" + amtParam + ") - " + amt);
        doPost(request, response);
        return;
    }
    String invoiceParam = WebUtil.getParameter(request, "C_Invoice_ID");
    int C_Invoice_ID = 0;
    try {
        if (invoiceParam != null)
            C_Invoice_ID = Integer.parseInt(invoiceParam);
    } catch (NumberFormatException ex) {
        log.warning("Parsing C_Invoice_ID=" + invoiceParam + " - " + ex.toString());
    }
    log.info("Amt=" + amt + ", C_Invoice_ID=" + C_Invoice_ID);
    //	Create New Payment for Amt & optional Invoice
    //	see OrderServlet.createPayment
    p = new MPayment(ctx, 0, null);
    //	p.setAD_Org_ID(..);
    p.setIsSelfService(true);
    //	for CC selection ges default from Acct Currency
    p.setAmount(0, amt);
    p.setIsOnline(true);
    //	Sales CC Trx
    p.setC_DocType_ID(true);
    p.setTrxType(MPayment.TRXTYPE_Sales);
    p.setTenderType(MPayment.TENDERTYPE_CreditCard);
    //	Payment Info
    p.setC_Invoice_ID(C_Invoice_ID);
    //	BP Info
    p.setBP_BankAccount(wu.getBankAccount());
    //
    //	p.saveEx();
    session.setAttribute(ATTR_PAYMENT, p);
    String url = "/paymentInfo.jsp";
    log.info("Forward to " + url);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);
}
Also used : HttpSession(javax.servlet.http.HttpSession) Properties(java.util.Properties) RequestDispatcher(javax.servlet.RequestDispatcher) BigDecimal(java.math.BigDecimal) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) MPayment(org.compiere.model.MPayment) WebUser(org.compiere.util.WebUser)

Example 45 with MPayment

use of org.compiere.model.MPayment in project adempiere by adempiere.

the class InvoiceWriteOff method writeOff.

//	doIt
/**
	 * 	Write Off
	 *	@param invoiceId invoice
	 *	@param documentNo doc no
	 *	@param dateInvoiced date
	 *	@param currencyId currency
	 *	@param openAmt open amt
	 *	@return true if written off
	 */
private boolean writeOff(int invoiceId, String documentNo, Timestamp dateInvoiced, int currencyId, BigDecimal openAmt) {
    //	Nothing to do
    if (openAmt == null || openAmt.signum() == 0)
        return false;
    if (openAmt.abs().compareTo(getMaximumWriteOffperInvoice()) >= 0)
        return false;
    //
    if (isSimulation()) {
        addLog("@IsSimulation@");
        addLog(invoiceId, dateInvoiced, openAmt, documentNo);
        return true;
    }
    //	Invoice
    MInvoice invoice = new MInvoice(getCtx(), invoiceId, get_TrxName());
    if (!invoice.isSOTrx())
        openAmt = openAmt.negate();
    //	Allocation
    if (allocation == null || currencyId != allocation.getC_Currency_ID()) {
        processAllocation();
        allocation = new MAllocationHdr(getCtx(), true, getAccountDate(), currencyId, getProcessInfo().getTitle() + " #" + getAD_PInstance_ID(), get_TrxName());
        allocation.setAD_Org_ID(invoice.getAD_Org_ID());
        if (!allocation.save()) {
            log.log(Level.SEVERE, "Cannot create allocation header");
            return false;
        }
    }
    //	Payment
    if (isCreatePayment() && (payment == null || invoice.getC_BPartner_ID() != payment.getC_BPartner_ID() || currencyId != payment.getC_Currency_ID())) {
        processPayment();
        payment = new MPayment(getCtx(), 0, get_TrxName());
        payment.setAD_Org_ID(invoice.getAD_Org_ID());
        payment.setC_BankAccount_ID(getBankAccountId());
        payment.setTenderType(MPayment.TENDERTYPE_Check);
        payment.setDateTrx(getAccountDate());
        payment.setDateAcct(getAccountDate());
        payment.setDescription(getProcessInfo().getTitle() + " #" + getAD_PInstance_ID());
        payment.setC_BPartner_ID(invoice.getC_BPartner_ID());
        //	payments are negative
        payment.setIsReceipt(true);
        payment.setC_Currency_ID(currencyId);
        if (!payment.save()) {
            log.log(Level.SEVERE, "Cannot create payment");
            return false;
        }
    }
    //	Line
    MAllocationLine allocationLine = null;
    if (isCreatePayment()) {
        allocationLine = new MAllocationLine(allocation, openAmt, Env.ZERO, Env.ZERO, Env.ZERO);
        payment.setPayAmt(payment.getPayAmt().add(openAmt));
        allocationLine.setC_Payment_ID(payment.getC_Payment_ID());
    } else
        allocationLine = new MAllocationLine(allocation, Env.ZERO, Env.ZERO, openAmt, Env.ZERO);
    allocationLine.setC_Invoice_ID(invoiceId);
    if (allocationLine.save()) {
        addLog(invoiceId, dateInvoiced, openAmt, documentNo);
        return true;
    }
    //	Error
    log.log(Level.SEVERE, "Cannot create allocation line for C_Invoice_ID=" + invoiceId);
    return false;
}
Also used : MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) MAllocationLine(org.compiere.model.MAllocationLine) MAllocationHdr(org.compiere.model.MAllocationHdr)

Aggregations

MPayment (org.compiere.model.MPayment)47 BigDecimal (java.math.BigDecimal)14 MInvoice (org.compiere.model.MInvoice)12 Timestamp (java.sql.Timestamp)8 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 ArrayList (java.util.ArrayList)7 MCashLine (org.compiere.model.MCashLine)5 AdempiereSystemError (org.compiere.util.AdempiereSystemError)5 MAllocationLine (org.compiere.model.MAllocationLine)4 MBankStatement (org.compiere.model.MBankStatement)4 MOrder (org.compiere.model.MOrder)4 Properties (java.util.Properties)3 RequestDispatcher (javax.servlet.RequestDispatcher)3 HttpSession (javax.servlet.http.HttpSession)3 MAllocationHdr (org.compiere.model.MAllocationHdr)3 MBankAccount (org.compiere.model.MBankAccount)3 MBankStatementLine (org.compiere.model.MBankStatementLine)3 AdempiereUserError (org.compiere.util.AdempiereUserError)3 KeyNamePair (org.compiere.util.KeyNamePair)3