Search in sources :

Example 1 with MPaySelectionLine

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

the class PSCreateFromOrder method doIt.

@Override
protected String doIt() throws Exception {
    //	Instance current Payment Selection
    MPaySelection paySelection = new MPaySelection(getCtx(), getRecord_ID(), get_TrxName());
    m_SeqNo = paySelection.getLastLineNo();
    //	Loop for keys
    for (Integer key : getSelectionKeys()) {
        //	get values from result set
        int C_Order_ID = key;
        String PaymentRule = getSelectionAsString(key, "ORD_PaymentRule");
        BigDecimal GrandTotal = getSelectionAsBigDecimal(key, "ORD_GrandTotal");
        BigDecimal ConvertedAmt = getSelectionAsBigDecimal(key, "ORD_ConvertedAmt");
        BigDecimal PayAmt = getSelectionAsBigDecimal(key, "ORD_PayAmt");
        m_SeqNo += 10;
        MPaySelectionLine line = new MPaySelectionLine(paySelection, m_SeqNo, PaymentRule);
        //	Add Order
        line.setOrder(C_Order_ID, GrandTotal, ConvertedAmt, PayAmt, Env.ZERO);
        //	Save
        line.saveEx();
    }
    //	Default Ok
    return "@OK@";
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelection(org.compiere.model.MPaySelection) BigDecimal(java.math.BigDecimal)

Example 2 with MPaySelectionLine

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

the class PSCreateFromPaySelection method doIt.

@Override
protected String doIt() throws Exception {
    //	Instance current Payment Selection
    MPaySelection paySelection = new MPaySelection(getCtx(), getRecord_ID(), get_TrxName());
    m_SeqNo = paySelection.getLastLineNo();
    //	Loop for keys
    for (Integer key : getSelectionKeys()) {
        //	get values from result set
        int C_PaySelectionLine_ID = key;
        String PaymentRule = getSelectionAsString(key, "PSL_PaymentRule");
        BigDecimal PayAmt = getSelectionAsBigDecimal(key, "PSL_PayAmt");
        BigDecimal DiscountAmt = getSelectionAsBigDecimal(key, "PSL_DiscountAmt");
        m_SeqNo += 10;
        MPaySelectionLine line = new MPaySelectionLine(paySelection, m_SeqNo, PaymentRule);
        //	Add Order
        line.setPaySelectionLineParent(C_PaySelectionLine_ID, PayAmt, DiscountAmt);
        //	Save
        line.saveEx();
    }
    //	Default Ok
    return "@OK@";
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelection(org.compiere.model.MPaySelection) BigDecimal(java.math.BigDecimal)

Example 3 with MPaySelectionLine

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

the class PaySelectionCreateCheck method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message (clear text)
	 *  @throws Exception if not successful
	 */
protected String doIt() throws Exception {
    log.info("C_PaySelection_ID=" + p_C_PaySelection_ID + ", PaymentRule=" + p_PaymentRule);
    MPaySelection paySelection = new MPaySelection(getCtx(), p_C_PaySelection_ID, get_TrxName());
    if (paySelection.get_ID() == 0)
        throw new IllegalArgumentException("Not found C_PaySelection_ID=" + p_C_PaySelection_ID);
    if (paySelection.isProcessed())
        throw new IllegalArgumentException("@Processed@");
    for (MPaySelectionLine paySelectionLine : paySelection.getLines(false)) {
        if (!paySelectionLine.isActive() || paySelectionLine.isProcessed())
            continue;
        createCheck(paySelectionLine);
    }
    //
    paySelection.setProcessed(true);
    paySelection.saveEx();
    return "@C_PaySelectionCheck_ID@ - #" + paySelectionChecks.size();
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelection(org.compiere.model.MPaySelection)

Example 4 with MPaySelectionLine

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

the class PaySelectionCreateFrom method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message 
	 *  @throws Exception if not successful
	 */
protected String doIt() throws Exception {
    log.info("C_PaySelection_ID=" + p_C_PaySelection_ID + ", OnlyDiscount=" + p_OnlyDiscount + ", OnlyDue=" + p_OnlyDue + ", IncludeInDispute=" + p_IncludeInDispute + ", MatchRequirement=" + p_MatchRequirement + ", PaymentRule=" + p_PaymentRule + ", C_BP_Group_ID=" + p_C_BP_Group_ID + ", C_BPartner_ID=" + p_C_BPartner_ID);
    MPaySelection psel = new MPaySelection(getCtx(), p_C_PaySelection_ID, get_TrxName());
    if (psel.get_ID() == 0)
        throw new IllegalArgumentException("Not found C_PaySelection_ID=" + p_C_PaySelection_ID);
    if (psel.isProcessed())
        throw new IllegalArgumentException("@Processed@");
    if (p_DueDate == null)
        p_DueDate = psel.getPayDate();
    //	psel.getPayDate();
    String sql = "SELECT i.C_Invoice_ID, i.C_InvoicePaySchedule_ID," + //	Open
    " currencyConvert(invoiceOpen(i.C_Invoice_ID, i.C_InvoicePaySchedule_ID)" + //	##1/2 Currency_To,PayDate
    ",i.C_Currency_ID, ?,?, i.C_ConversionType_ID,i.AD_Client_ID,i.AD_Org_ID)," + //	Discount
    " currencyConvert(paymentTermDiscount(i.GrandTotal,i.C_Currency_ID,i.C_PaymentTerm_ID,i.DateInvoiced, ?)" + //	##3 PayDate
    ",i.C_Currency_ID, ?,?,i.C_ConversionType_ID,i.AD_Client_ID,i.AD_Org_ID)," + //	##4/5 Currency_To,PayDate
    " PaymentRule, IsSOTrx " + //	4..6
    "FROM C_Invoice_v i " + "LEFT OUTER JOIN C_InvoicePaySchedule ips ON (i.C_InvoicePaySchedule_ID = ips.C_InvoicePaySchedule_ID) " + "WHERE IsSOTrx='N' AND IsPaid='N' AND DocStatus IN ('CO','CL')" + //	##6
    " AND i.AD_Client_ID=?" + // And a payment is needed  ##7
    " AND invoiceOpen(i.C_Invoice_ID,i.C_InvoicePaySchedule_ID)-paymentTermDiscount(i.GrandTotal,i.C_Currency_ID,i.C_PaymentTerm_ID,i.DateInvoiced, ?) != 0.0" + //  BR3450248 - Partially paid invoice does not appear in payment selection
    " AND NOT EXISTS (SELECT * FROM C_PaySelectionLine psl" + " INNER JOIN C_PaySelectionCheck psc ON (psl.C_PaySelectionCheck_ID=psc.C_PaySelectionCheck_ID)" + " LEFT OUTER JOIN C_Payment pmt ON (pmt.C_Payment_ID=psc.C_Payment_ID)" + " WHERE i.C_Invoice_ID=psl.C_Invoice_ID" + // include all InvoicePaySchedules - these are handled by invoiceOpen()
    " AND i.C_InvoicePaySchedule_ID is null" + " AND psl.IsActive='Y'" + " AND (pmt.DocStatus IS NULL OR pmt.DocStatus NOT IN ('VO','RE'))" + " AND psl.differenceamt = 0.0) " + // Avoid repeats in current PaySelection which can happen if the button is clicked more than once
    " AND NOT EXISTS (SELECT * FROM C_PaySelectionLine psl" + " WHERE i.C_Invoice_ID=psl.C_Invoice_ID AND psl.IsActive='Y'" + // ##8 Current selection
    " AND psl.C_PaySelection_ID = ?)";
    //	Disputed
    if (!p_IncludeInDispute)
        sql += " AND i.IsInDispute='N'";
    //	PaymentRule (optional)
    if (p_PaymentRule != null)
        //	##
        sql += " AND PaymentRule=?";
    //	OnlyDiscount
    if (p_OnlyDiscount) {
        if (p_OnlyDue)
            sql += " AND (";
        else
            sql += " AND ";
        //	##
        sql += "paymentTermDiscount(invoiceOpen(i.C_Invoice_ID, i.C_InvoicePaySchedule_ID), C_Currency_ID, C_PaymentTerm_ID, DateInvoiced, ?) > 0";
    }
    //	OnlyDue
    if (p_OnlyDue) {
        if (p_OnlyDiscount)
            sql += " OR ";
        else
            sql += " AND ";
        //	##
        sql += "COALESCE(ips.duedate,paymentTermDueDate(i.C_PaymentTerm_ID, i.DateInvoiced)) <= ?";
        if (p_OnlyDiscount)
            sql += ")";
    }
    //	Business Partner
    if (p_C_BPartner_ID != 0)
        //	##
        sql += " AND C_BPartner_ID=?";
    else //	Business Partner Group
    if (p_C_BP_Group_ID != 0)
        sql += " AND EXISTS (SELECT * FROM C_BPartner bp " + //	##
        "WHERE bp.C_BPartner_ID=i.C_BPartner_ID AND bp.C_BP_Group_ID=?)";
    //	PO Matching Requirement
    if (p_MatchRequirement.equals("P") || p_MatchRequirement.equals("B")) {
        sql += " AND EXISTS (SELECT * FROM C_InvoiceLine il " + "WHERE i.C_Invoice_ID=il.C_Invoice_ID" + " AND QtyInvoiced=(SELECT SUM(Qty) FROM M_MatchPO m " + "WHERE il.C_InvoiceLine_ID=m.C_InvoiceLine_ID))";
    }
    //	Receipt Matching Requirement
    if (p_MatchRequirement.equals("R") || p_MatchRequirement.equals("B")) {
        sql += " AND EXISTS (SELECT * FROM C_InvoiceLine il " + "WHERE i.C_Invoice_ID=il.C_Invoice_ID" + " AND QtyInvoiced=(SELECT SUM(Qty) FROM M_MatchInv m " + "WHERE il.C_InvoiceLine_ID=m.C_InvoiceLine_ID))";
    }
    //
    int lines = 0;
    int C_CurrencyTo_ID = psel.getC_Currency_ID();
    PreparedStatement pstmt = null;
    try {
        pstmt = DB.prepareStatement(sql, get_TrxName());
        int index = 1;
        pstmt.setInt(index++, C_CurrencyTo_ID);
        pstmt.setTimestamp(index++, psel.getPayDate());
        //
        pstmt.setTimestamp(index++, psel.getPayDate());
        pstmt.setInt(index++, C_CurrencyTo_ID);
        pstmt.setTimestamp(index++, psel.getPayDate());
        //
        pstmt.setInt(index++, psel.getAD_Client_ID());
        pstmt.setTimestamp(index++, psel.getPayDate());
        pstmt.setInt(index++, psel.getC_PaySelection_ID());
        if (p_PaymentRule != null)
            pstmt.setString(index++, p_PaymentRule);
        if (p_OnlyDiscount)
            pstmt.setTimestamp(index++, psel.getPayDate());
        if (p_OnlyDue)
            pstmt.setTimestamp(index++, p_DueDate);
        if (p_C_BPartner_ID != 0)
            pstmt.setInt(index++, p_C_BPartner_ID);
        else if (p_C_BP_Group_ID != 0)
            pstmt.setInt(index++, p_C_BP_Group_ID);
        //
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int C_Invoice_ID = rs.getInt(1);
            int C_InvoicePaySchedule_ID = rs.getInt(2);
            BigDecimal PayAmt = rs.getBigDecimal(3);
            if (C_Invoice_ID == 0 || Env.ZERO.compareTo(PayAmt) == 0)
                continue;
            BigDecimal DiscountAmt = rs.getBigDecimal(4);
            String PaymentRule = rs.getString(5);
            boolean isSOTrx = "Y".equals(rs.getString(6));
            //
            lines++;
            MPaySelectionLine pselLine = new MPaySelectionLine(psel, lines * 10, PaymentRule);
            //	
            pselLine.setInvoice(C_Invoice_ID, C_InvoicePaySchedule_ID, isSOTrx, PayAmt, PayAmt.subtract(DiscountAmt), DiscountAmt);
            if (!pselLine.save()) {
                pstmt.close();
                throw new IllegalStateException("Cannot save MPaySelectionLine");
            }
        }
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    return "@C_PaySelectionLine_ID@  - #" + lines;
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelection(org.compiere.model.MPaySelection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 5 with MPaySelectionLine

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

the class GenericPaymentExport method exportToFile.

/**************************************************************************
	 *  Export to File
	 *  @param paySelectionChecks array of checks
	 *  @param file file to export checks
	 *  @return number of lines
	 */
public int exportToFile(List<MPaySelectionCheck> paySelectionChecks, File file, StringBuffer error) {
    if (paySelectionChecks == null || paySelectionChecks.size() == 0)
        return 0;
    //  Must be a file
    if (file.isDirectory()) {
        error.append("No se puede escribir, el archivo seleccionado es un directorio - " + file.getAbsolutePath());
        s_log.log(Level.SEVERE, error.toString());
        return -1;
    }
    //  delete if exists
    try {
        if (file.exists())
            file.delete();
    } catch (Exception e) {
        s_log.log(Level.WARNING, "Could not delete - " + file.getAbsolutePath(), e);
    }
    //  ease
    char x = '"';
    int noLines = 0;
    StringBuffer line = null;
    try {
        FileWriter fw = new FileWriter(file);
        //  write header
        line = new StringBuffer();
        line.append(x).append("Value").append(x).append(",").append(x).append("Name").append(x).append(",").append(x).append("Contact").append(x).append(",").append(x).append("Addr1").append(x).append(",").append(x).append("Addr2").append(x).append(",").append(x).append("City").append(x).append(",").append(x).append("State").append(x).append(",").append(x).append("ZIP").append(x).append(",").append(x).append("Country").append(x).append(",").append(x).append("ReferenceNo").append(x).append(",").append(x).append("DocumentNo").append(x).append(",").append(x).append("PayDate").append(x).append(",").append(x).append("Currency").append(x).append(",").append(x).append("PayAmount").append(x).append(",").append(x).append("Comment").append(x).append(Env.NL);
        fw.write(line.toString());
        noLines++;
        //  write lines
        for (MPaySelectionCheck paySelectionCheck : paySelectionChecks) {
            if (paySelectionCheck == null)
                continue;
            //  BPartner Info
            String[] bp = getBPartnerInfo(paySelectionCheck.getC_BPartner_ID());
            //  Comment - list of invoice document no
            StringBuffer comment = new StringBuffer();
            List<MPaySelectionLine> paySelectionLines = paySelectionCheck.getPaySelectionLinesAsList(false);
            for (MPaySelectionLine paySelectionLine : paySelectionLines) {
                if (paySelectionLine.get_ID() == paySelectionLines.get(0).get_ID())
                    comment.append(", ");
                comment.append(paySelectionLine.getInvoice().getDocumentNo());
            }
            line = new StringBuffer();
            // Value
            line.append(x).append(bp[BP_VALUE]).append(x).append(",").append(x).append(bp[BP_NAME]).append(x).append(// Name
            ",").append(x).append(bp[BP_CONTACT]).append(x).append(// Contact
            ",").append(x).append(bp[BP_ADDR1]).append(x).append(// Addr1
            ",").append(x).append(bp[BP_ADDR2]).append(x).append(// Addr2
            ",").append(x).append(bp[BP_CITY]).append(x).append(// City
            ",").append(x).append(bp[BP_REGION]).append(x).append(// State
            ",").append(x).append(bp[BP_POSTAL]).append(x).append(// ZIP
            ",").append(x).append(bp[BP_COUNTRY]).append(x).append(// Country
            ",").append(x).append(bp[BP_REFNO]).append(x).append(// ReferenceNo
            ",").append(x).append(paySelectionCheck.getDocumentNo()).append(x).append(// DocumentNo
            ",").append(paySelectionCheck.getParent().getPayDate()).append(// PayDate
            ",").append(x).append(MCurrency.getISO_Code(Env.getCtx(), paySelectionCheck.getParent().getC_Currency_ID())).append(x).append(// Currency
            ",").append(paySelectionCheck.getPayAmt()).append(// PayAmount
            ",").append(x).append(comment.toString()).append(// Comment
            x).append(Env.NL);
            fw.write(line.toString());
            noLines++;
        }
        //  write line
        fw.flush();
        fw.close();
    } catch (Exception e) {
        error.append(e.toString());
        s_log.log(Level.SEVERE, "", e);
        return -1;
    }
    return noLines;
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelectionCheck(org.compiere.model.MPaySelectionCheck) FileWriter(java.io.FileWriter) SQLException(java.sql.SQLException)

Aggregations

MPaySelectionLine (org.compiere.model.MPaySelectionLine)10 MPaySelection (org.compiere.model.MPaySelection)8 BigDecimal (java.math.BigDecimal)7 MPaySelectionCheck (org.compiere.model.MPaySelectionCheck)2 FileWriter (java.io.FileWriter)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 IDColumn (org.compiere.minigrid.IDColumn)1 MInvoice (org.compiere.model.MInvoice)1 MPayment (org.compiere.model.MPayment)1 MUser (org.compiere.model.MUser)1