Search in sources :

Example 6 with MPayment

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

the class DunningRunCreate method createPaymentLine.

//	addPayments
/**
	 * 	Create Payment Line
	 *	@param C_Payment_ID
	 *	@param C_Currency_ID
	 *	@param PayAmt
	 *	@param OpenAmt
	 *	@param C_BPartner_ID
	 *  @param c_DunningLevel_ID 
	 */
private boolean createPaymentLine(int C_Payment_ID, int C_Currency_ID, BigDecimal PayAmt, BigDecimal OpenAmt, int C_BPartner_ID, int c_DunningLevel_ID) {
    MDunningRunEntry entry = null;
    try {
        entry = m_run.getEntry(C_BPartner_ID, p_C_Currency_ID, p_SalesRep_ID, c_DunningLevel_ID);
    } catch (BPartnerNoAddressException e) {
        MPayment payment = new MPayment(getCtx(), C_Payment_ID, null);
        String msg = "@Skip@ @C_Payment_ID@ " + payment.getDocumentInfo() + ", @C_BPartner_ID@ " + MBPartner.get(getCtx(), C_BPartner_ID).getName() + " @No@ @IsActive@ @C_BPartner_Location_ID@";
        getProcessInfo().addLog(getProcessInfo().getAD_PInstance_ID(), null, null, msg);
        return false;
    }
    if (entry.get_ID() == 0)
        if (!entry.save())
            throw new IllegalStateException("Cannot save MDunningRunEntry");
    //
    MDunningRunLine line = new MDunningRunLine(entry);
    line.setPayment(C_Payment_ID, C_Currency_ID, PayAmt, OpenAmt);
    if (!line.save())
        throw new IllegalStateException("Cannot save MDunningRunLine");
    return true;
}
Also used : MDunningRunEntry(org.compiere.model.MDunningRunEntry) MPayment(org.compiere.model.MPayment) MDunningRunLine(org.compiere.model.MDunningRunLine) BPartnerNoAddressException(org.adempiere.exceptions.BPartnerNoAddressException)

Example 7 with MPayment

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

the class AllocationAuto method allocateBPOldestFirst.

//	allocateBPartnerAll
/**
	 * 	Allocate Oldest First using Accounting currency
	 *	@return allocations
	 */
private int allocateBPOldestFirst() throws Exception {
    int C_Currency_ID = MClient.get(getCtx()).getC_Currency_ID();
    Timestamp dateAcct = null;
    //	Payments
    BigDecimal totalPayments = Env.ZERO;
    for (int p = 0; p < m_payments.length; p++) {
        MPayment payment = m_payments[p];
        if (payment.isAllocated())
            continue;
        if (payment.getC_Currency_ID() != C_Currency_ID)
            continue;
        BigDecimal allocatedAmt = payment.getAllocatedAmt();
        log.info(payment + ", Allocated=" + allocatedAmt);
        BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
        availableAmt = availableAmt.subtract(allocatedAmt);
        if (!payment.isReceipt())
            availableAmt = availableAmt.negate();
        log.fine("Available=" + availableAmt);
        if (dateAcct == null || payment.getDateAcct().after(dateAcct))
            dateAcct = payment.getDateAcct();
        totalPayments = totalPayments.add(availableAmt);
    }
    //	Invoices
    BigDecimal totalInvoices = Env.ZERO;
    for (int i = 0; i < m_invoices.length; i++) {
        MInvoice invoice = m_invoices[i];
        if (invoice.isPaid())
            continue;
        if (invoice.getC_Currency_ID() != C_Currency_ID)
            continue;
        BigDecimal openAmt = invoice.getOpenAmt(true, null);
        log.fine("" + invoice);
        if (!invoice.isSOTrx())
            openAmt = openAmt.negate();
        //	Foreign currency
        log.fine("Open=" + openAmt);
        if (dateAcct == null || invoice.getDateAcct().after(dateAcct))
            dateAcct = invoice.getDateAcct();
        totalInvoices = totalInvoices.add(openAmt);
    }
    //	must be either AP or AR balance
    if (totalInvoices.signum() != totalPayments.signum()) {
        log.fine("Signum - Invoices=" + totalInvoices.signum() + " <> Payments=" + totalPayments.signum());
        return 0;
    }
    BigDecimal difference = totalInvoices.subtract(totalPayments);
    BigDecimal maxAmt = totalInvoices.abs().min(totalPayments.abs());
    if (totalInvoices.signum() < 0)
        maxAmt = maxAmt.negate();
    log.info("= Invoices=" + totalInvoices + " - Payments=" + totalPayments + " = Difference=" + difference + " - Max=" + maxAmt);
    //	Allocate Payments up to max
    BigDecimal allocatedPayments = Env.ZERO;
    for (int p = 0; p < m_payments.length; p++) {
        MPayment payment = m_payments[p];
        if (payment.isAllocated())
            continue;
        if (payment.getC_Currency_ID() != C_Currency_ID)
            continue;
        BigDecimal allocatedAmt = payment.getAllocatedAmt();
        // comment following lines to allow partial allocation
        // if (allocatedAmt != null && allocatedAmt.signum() != 0)
        // 	continue;
        BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
        availableAmt = availableAmt.subtract(allocatedAmt);
        if (!payment.isReceipt())
            availableAmt = availableAmt.negate();
        allocatedPayments = allocatedPayments.add(availableAmt);
        if ((totalInvoices.signum() > 0 && allocatedPayments.compareTo(maxAmt) > 0) || (totalInvoices.signum() < 0 && allocatedPayments.compareTo(maxAmt) < 0)) {
            BigDecimal diff = allocatedPayments.subtract(maxAmt);
            availableAmt = availableAmt.subtract(diff);
            allocatedPayments = allocatedPayments.subtract(diff);
        }
        log.fine("Payment Allocated=" + availableAmt);
        if (!createAllocation(C_Currency_ID, "BP Oldest (" + difference.abs() + ")", dateAcct, availableAmt, null, null, null, payment.getC_BPartner_ID(), payment.getC_Payment_ID(), 0, payment.getAD_Org_ID())) {
            throw new AdempiereSystemError("Cannot create Allocation");
        }
        if (allocatedPayments.compareTo(maxAmt) == 0)
            break;
    }
    //	for all payments
    //	Allocated Invoices up to max
    BigDecimal allocatedInvoices = Env.ZERO;
    for (int i = 0; i < m_invoices.length; i++) {
        MInvoice invoice = m_invoices[i];
        if (invoice.isPaid())
            continue;
        if (invoice.getC_Currency_ID() != C_Currency_ID)
            continue;
        BigDecimal openAmt = invoice.getOpenAmt(true, null);
        if (!invoice.isSOTrx())
            openAmt = openAmt.negate();
        allocatedInvoices = allocatedInvoices.add(openAmt);
        if ((totalInvoices.signum() > 0 && allocatedInvoices.compareTo(maxAmt) > 0) || (totalInvoices.signum() < 0 && allocatedInvoices.compareTo(maxAmt) < 0)) {
            BigDecimal diff = allocatedInvoices.subtract(maxAmt);
            openAmt = openAmt.subtract(diff);
            allocatedInvoices = allocatedInvoices.subtract(diff);
        }
        if (openAmt.signum() == 0)
            break;
        log.fine("Invoice Allocated=" + openAmt);
        if (!createAllocation(C_Currency_ID, "BP Oldest (" + difference.abs() + ")", dateAcct, openAmt, null, null, null, invoice.getC_BPartner_ID(), 0, invoice.getC_Invoice_ID(), invoice.getAD_Org_ID())) {
            throw new AdempiereSystemError("Cannot create Allocation");
        }
        if (allocatedInvoices.compareTo(maxAmt) == 0)
            break;
    }
    if (allocatedPayments.compareTo(allocatedInvoices) != 0) {
        throw new AdempiereSystemError("Allocated Payments=" + allocatedPayments + " <> Invoices=" + allocatedInvoices);
    }
    processAllocation();
    return 1;
}
Also used : AdempiereSystemError(org.compiere.util.AdempiereSystemError) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 8 with MPayment

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

the class AllocationAuto method getPayments.

//	alloc
/**
	 * 	Get Payments of BP
	 *	@param C_BPartner_ID id
	 *	@return unallocated payments
	 */
private MPayment[] getPayments(int C_BPartner_ID) {
    ArrayList<MPayment> list = new ArrayList<MPayment>();
    String sql = "SELECT * FROM C_Payment " + "WHERE IsAllocated='N' AND Processed='Y' AND C_BPartner_ID=?" + " AND IsPrepayment='N' AND C_Charge_ID IS NULL ";
    if (ONLY_AP.equals(p_APAR))
        sql += "AND IsReceipt='N' ";
    else if (ONLY_AR.equals(p_APAR))
        sql += "AND IsReceipt='Y' ";
    sql += "ORDER BY DateTrx";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, get_TrxName());
        pstmt.setInt(1, C_BPartner_ID);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            MPayment payment = new MPayment(getCtx(), rs, get_TrxName());
            BigDecimal allocated = payment.getAllocatedAmt();
            if (allocated != null && allocated.compareTo(payment.getPayAmt()) == 0) {
                payment.setIsAllocated(true);
                payment.saveEx();
            } else
                list.add(payment);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    m_payments = new MPayment[list.size()];
    list.toArray(m_payments);
    return m_payments;
}
Also used : MPayment(org.compiere.model.MPayment) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 9 with MPayment

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

the class AllocationAuto method allocateBPOneToOne.

//	allocateIndividualPayments
/**
	 * 	Allocate Payment:Invoice 1:1
	 *	@return allocations
	 */
private int allocateBPOneToOne() throws Exception {
    int count = 0;
    for (int p = 0; p < m_payments.length; p++) {
        MPayment payment = m_payments[p];
        if (payment.isAllocated())
            continue;
        BigDecimal allocatedAmt = payment.getAllocatedAmt();
        log.info(payment + ", Allocated=" + allocatedAmt);
        if (allocatedAmt != null && allocatedAmt.signum() != 0)
            continue;
        BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
        if (!payment.isReceipt())
            availableAmt = availableAmt.negate();
        log.fine("Available=" + availableAmt);
        for (int i = 0; i < m_invoices.length; i++) {
            MInvoice invoice = m_invoices[i];
            if (invoice == null || invoice.isPaid())
                continue;
            if (payment.getC_Currency_ID() == invoice.getC_Currency_ID()) {
                //	log.fine("allocateBPartnerAll - " + invoice);
                BigDecimal openAmt = invoice.getOpenAmt(true, null);
                if (!invoice.isSOTrx())
                    openAmt = openAmt.negate();
                BigDecimal difference = availableAmt.subtract(openAmt).abs();
                log.fine(invoice + ", Open=" + openAmt + " - Difference=" + difference);
                if (difference.signum() == 0) {
                    Timestamp dateAcct = payment.getDateAcct();
                    if (invoice.getDateAcct().after(dateAcct))
                        dateAcct = invoice.getDateAcct();
                    if (!createAllocation(payment.getC_Currency_ID(), "1:1 (" + availableAmt + ")", dateAcct, availableAmt, null, null, null, invoice.getC_BPartner_ID(), payment.getC_Payment_ID(), invoice.getC_Invoice_ID(), invoice.getAD_Org_ID())) {
                        throw new AdempiereSystemError("Cannot create Allocation");
                    }
                    processAllocation();
                    count++;
                    //	remove invoice
                    m_invoices[i] = null;
                    m_payments[p] = null;
                    payment = null;
                    break;
                }
            } else //	Multi-Currency
            {
            }
        }
    //	for all invoices
    }
    //	for all payments
    return count;
}
Also used : AdempiereSystemError(org.compiere.util.AdempiereSystemError) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 10 with MPayment

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

the class AllocationAuto method allocateBPartnerAll.

//	allocateOneToOne
/**
	 * 	Allocate all Payments/Invoices using Accounting currency
	 *	@return allocations
	 */
private int allocateBPartnerAll() throws Exception {
    int C_Currency_ID = MClient.get(getCtx()).getC_Currency_ID();
    Timestamp dateAcct = null;
    //	Payments
    BigDecimal totalPayments = Env.ZERO;
    for (int p = 0; p < m_payments.length; p++) {
        MPayment payment = m_payments[p];
        if (payment.isAllocated())
            continue;
        BigDecimal allocatedAmt = payment.getAllocatedAmt();
        //	log.info("allocateBPartnerAll - " + payment + ", Allocated=" + allocatedAmt);
        if (allocatedAmt != null && allocatedAmt.signum() != 0)
            continue;
        BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
        if (!payment.isReceipt())
            availableAmt = availableAmt.negate();
        //	Foreign currency
        if (payment.getC_Currency_ID() != C_Currency_ID)
            continue;
        //	log.fine("allocateBPartnerAll - Available=" + availableAmt);
        if (dateAcct == null || payment.getDateAcct().after(dateAcct))
            dateAcct = payment.getDateAcct();
        totalPayments = totalPayments.add(availableAmt);
    }
    //	Invoices
    BigDecimal totalInvoices = Env.ZERO;
    for (int i = 0; i < m_invoices.length; i++) {
        MInvoice invoice = m_invoices[i];
        if (invoice.isPaid())
            continue;
        //	log.info("allocateBPartnerAll - " + invoice);
        BigDecimal openAmt = invoice.getOpenAmt(true, null);
        if (!invoice.isSOTrx())
            openAmt = openAmt.negate();
        //	Foreign currency
        if (invoice.getC_Currency_ID() != C_Currency_ID)
            continue;
        //	log.fine("allocateBPartnerAll - Open=" + openAmt);
        if (dateAcct == null || invoice.getDateAcct().after(dateAcct))
            dateAcct = invoice.getDateAcct();
        totalInvoices = totalInvoices.add(openAmt);
    }
    BigDecimal difference = totalInvoices.subtract(totalPayments);
    log.info("= Invoices=" + totalInvoices + " - Payments=" + totalPayments + " = Difference=" + difference);
    if (difference.signum() == 0) {
        for (int p = 0; p < m_payments.length; p++) {
            MPayment payment = m_payments[p];
            if (payment.isAllocated())
                continue;
            BigDecimal allocatedAmt = payment.getAllocatedAmt();
            if (allocatedAmt != null && allocatedAmt.signum() != 0)
                continue;
            BigDecimal availableAmt = payment.getPayAmt().add(payment.getDiscountAmt()).add(payment.getWriteOffAmt()).add(payment.getOverUnderAmt());
            if (!payment.isReceipt())
                availableAmt = availableAmt.negate();
            //	Foreign currency
            if (payment.getC_Currency_ID() != C_Currency_ID)
                continue;
            if (!createAllocation(C_Currency_ID, "BP All", dateAcct, availableAmt, null, null, null, payment.getC_BPartner_ID(), payment.getC_Payment_ID(), 0, payment.getAD_Org_ID())) {
                throw new AdempiereSystemError("Cannot create Allocation");
            }
        }
        //
        for (int i = 0; i < m_invoices.length; i++) {
            MInvoice invoice = m_invoices[i];
            if (invoice.isPaid())
                continue;
            BigDecimal openAmt = invoice.getOpenAmt(true, null);
            if (!invoice.isSOTrx())
                openAmt = openAmt.negate();
            //	Foreign currency
            if (invoice.getC_Currency_ID() != C_Currency_ID)
                continue;
            if (!createAllocation(C_Currency_ID, "BP All", dateAcct, openAmt, null, null, null, invoice.getC_BPartner_ID(), 0, invoice.getC_Invoice_ID(), invoice.getAD_Org_ID())) {
                throw new AdempiereSystemError("Cannot create Allocation");
            }
        }
        //	for all invoices
        processAllocation();
        return 1;
    }
    return 0;
}
Also used : AdempiereSystemError(org.compiere.util.AdempiereSystemError) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

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