Search in sources :

Example 6 with MPaySelection

use of org.compiere.model.MPaySelection 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 7 with MPaySelection

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

the class PSCreateFromHRMovement 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 HR_Movement_ID = key;
        String PaymentRule = getSelectionAsString(key, "HRM_PaymentRule");
        BigDecimal Amount = getSelectionAsBigDecimal(key, "HRM_Amount");
        m_SeqNo += 10;
        MPaySelectionLine line = new MPaySelectionLine(paySelection, m_SeqNo, PaymentRule);
        //	Add Order
        line.setHRMovement(HR_Movement_ID, Amount);
        //	Save
        line.saveEx();
    }
    //	Default Ok
    return "@OK@";
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelection(org.compiere.model.MPaySelection) BigDecimal(java.math.BigDecimal)

Example 8 with MPaySelection

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

the class PSCreateFromInvoice method doIt.

@Override
protected String doIt() throws Exception {
    //	Instance current Payment Selection
    if (getRecord_ID() > 0) {
        //	Already exists
        paymentSelection = new MPaySelection(getCtx(), getRecord_ID(), get_TrxName());
        seqNo = paymentSelection.getLastLineNo();
    } else {
        //	Is a new Payment Selection
        paymentSelection = new MPaySelection(getCtx(), 0, get_TrxName());
        paymentSelection.setC_BankAccount_ID(getBankAccountId());
        paymentSelection.setDateDoc(getDocumentDate());
        paymentSelection.setPayDate(getPaymentdate());
        if (getTargetDocumentTypeId() > 0)
            paymentSelection.setC_DocType_ID(getTargetDocumentTypeId());
        MUser user = MUser.get(getCtx(), getAD_User_ID());
        String userName = "";
        if (user != null)
            userName = user.getName();
        //	Set description
        paymentSelection.setDescription(Msg.getMsg(Env.getCtx(), "VPaySelect") + " - " + userName + " - " + DisplayType.getDateFormat(DisplayType.Date).format(getPaymentdate()));
        //	Save
        paymentSelection.saveEx();
        isNew = true;
    }
    //	Loop for keys
    for (Integer key : getSelectionKeys()) {
        //	get values from result set
        int C_Invoice_ID = key;
        int C_InvoicePaySchedule_ID = getSelectionAsInt(key, "INV_C_InvoicePaySchedule_ID");
        String PaymentRule = getSelectionAsString(key, "INV_PaymentRule");
        BigDecimal AmtSource = getSelectionAsBigDecimal(key, "INV_AmtSource");
        BigDecimal OpenAmt = getSelectionAsBigDecimal(key, "INV_OpenAmt");
        BigDecimal PayAmt = getSelectionAsBigDecimal(key, "INV_PayAmt");
        BigDecimal DiscountAmt = getSelectionAsBigDecimal(key, "INV_DiscountAmt");
        seqNo += 10;
        MPaySelectionLine line = new MPaySelectionLine(paymentSelection, seqNo, PaymentRule);
        //	Add Order
        line.setInvoice(C_Invoice_ID, C_InvoicePaySchedule_ID, AmtSource, OpenAmt, PayAmt, DiscountAmt);
        //	Save
        line.saveEx();
    }
    //	For new
    if (isNew) {
        //	Load Record
        paymentSelection.load(get_TrxName());
        //	Process Selection
        if (!paymentSelection.processIt(MPaySelection.DOCACTION_Complete)) {
            throw new AdempiereException("@Error@ " + paymentSelection.getProcessMsg());
        }
        //	
        paymentSelection.saveEx();
        //	Notify
        return paymentSelection.getDescription();
    }
    //	Default Ok
    return "@OK@";
}
Also used : MPaySelectionLine(org.compiere.model.MPaySelectionLine) MPaySelection(org.compiere.model.MPaySelection) AdempiereException(org.adempiere.exceptions.AdempiereException) MUser(org.compiere.model.MUser) BigDecimal(java.math.BigDecimal)

Aggregations

MPaySelection (org.compiere.model.MPaySelection)8 MPaySelectionLine (org.compiere.model.MPaySelectionLine)8 BigDecimal (java.math.BigDecimal)6 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 IDColumn (org.compiere.minigrid.IDColumn)1 MUser (org.compiere.model.MUser)1