Search in sources :

Example 11 with MAllocationLine

use of org.compiere.model.MAllocationLine in project lar_361 by comitsrl.

the class MLARPaymentHeader method completeIt.

@Override
public String completeIt() {
    log.info(toString());
    // Re-Check
    if (!m_justPrepared) {
        String status = prepareIt();
        if (!DocAction.STATUS_InProgress.equals(status))
            return status;
    }
    // Dispara la validación del documento
    m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_COMPLETE);
    if (m_processMsg != null)
        return DocAction.STATUS_Invalid;
    MPayment[] pays = getPayments(get_TrxName());
    int p = 0;
    for (; p < pays.length; p++) {
        pays[p].setDateAcct(getDateTrx());
        pays[p].setDateTrx(getCreated());
        pays[p].setTrxType(MPayment.TRXTYPE_CreditPayment);
        pays[p].processIt(ACTION_Complete);
        pays[p].save(get_TrxName());
        if (!DOCSTATUS_Completed.equals(pays[p].getDocStatus())) {
            m_processMsg = "@C_Payment_ID@: " + pays[p].getProcessMsg();
            return DocAction.STATUS_Invalid;
        }
    }
    // Asigna los cobros/pagos a las facturas
    MPaymentAllocate[] invoices = getInvoices(get_TrxName());
    if (invoices.length != 0) {
        p = 0;
        // Asignaciones
        for (int i = 0; (i < invoices.length && p < pays.length); ) {
            MAllocationHdr alloc = new MAllocationHdr(getCtx(), false, getDateTrx(), getC_Currency_ID(), "Asignación Pagos a Facturas - Cabecera: " + getDocumentNo(), get_TrxName());
            alloc.setAD_Org_ID(getAD_Org_ID());
            if (!alloc.save()) {
                log.severe("La Cabecera de Asignacion no pudo crearse");
                return DocAction.STATUS_Invalid;
            }
            MPaymentAllocate pa = invoices[i];
            MInvoice invoice = new MInvoice(Env.getCtx(), pa.getC_Invoice_ID(), get_TrxName());
            final BigDecimal impPago = pays[p].getPayAmt().add(pays[p].getWriteOffAmt()).subtract(pays[p].getAllocatedAmt().abs());
            final BigDecimal importeFactura = invoice.getOpenAmt().subtract(pa.getDiscountAmt());
            int comp = impPago.compareTo(importeFactura);
            MAllocationLine aLine = null;
            BigDecimal alineOUAmt = Env.ZERO;
            BigDecimal alineAmt;
            // Evita Sobrepagos
            if (comp <= 0)
                alineAmt = impPago;
            else
                alineAmt = importeFactura;
            alineOUAmt = importeFactura.subtract(impPago);
            if (isReceipt())
                aLine = new MAllocationLine(alloc, alineAmt, Env.ZERO, pa.getWriteOffAmt(), alineOUAmt);
            else
                aLine = new MAllocationLine(alloc, alineAmt.negate(), Env.ZERO, pa.getWriteOffAmt().negate(), alineOUAmt);
            aLine.setDocInfo(pa.getC_BPartner_ID(), 0, pa.getC_Invoice_ID());
            aLine.setPaymentInfo(pays[p].getC_Payment_ID(), 0);
            if (!aLine.save(get_TrxName()))
                log.warning("Asignación: No se pudo guradar la línea");
            else {
                pa.setC_AllocationLine_ID(aLine.getC_AllocationLine_ID());
                pa.saveEx();
            }
            if (comp >= 0) {
                i = i + 1;
                if (comp == 0)
                    p = p + 1;
            } else
                p = p + 1;
            // Cabecera de Asignación: Comienzo de WF
            alloc.processIt(DocAction.ACTION_Complete);
            alloc.save(get_TrxName());
            m_processMsg = "@C_AllocationHdr_ID@: " + alloc.getDocumentNo();
            log.fine(m_processMsg);
        }
    }
    // setC_BankAccount_ID(C_BankAccount_ID);
    // Dispara la validación del documento
    m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_AFTER_COMPLETE);
    if (m_processMsg != null)
        return DocAction.STATUS_Invalid;
    // @mzuniga - Marca los cobros/pagos como asignados si corresponde
    for (p = 0; p < pays.length; p++) {
        pays[p].testAllocation();
        pays[p].saveEx();
    }
    setDocStatus(ACTION_Complete);
    setDocAction(DOCACTION_Close);
    setProcessed(true);
    // Marca los Certificados de Retención como Procesados
    if (!isReceipt()) {
        final MLARPaymentWithholding[] certificados = MLARPaymentWithholding.get(this);
        if (certificados.length > 0)
            for (final MLARPaymentWithholding c : certificados) c.setProcessed(true);
    }
    return DocAction.STATUS_Completed;
}
Also used : MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) MAllocationLine(org.compiere.model.MAllocationLine) MPaymentAllocate(org.compiere.model.MPaymentAllocate) MAllocationHdr(org.compiere.model.MAllocationHdr) BigDecimal(java.math.BigDecimal)

Example 12 with MAllocationLine

use of org.compiere.model.MAllocationLine in project lar_361 by comitsrl.

the class LCO_Validator method reversePaymentWithholdings.

private String reversePaymentWithholdings(MAllocationHdr ah) {
    MAllocationLine[] als = ah.getLines(true);
    for (int i = 0; i < als.length; i++) {
        MAllocationLine al = als[i];
        if (al.getC_Invoice_ID() > 0) {
            String sql = "SELECT LCO_InvoiceWithholding_ID " + "FROM LCO_InvoiceWithholding " + "WHERE C_Invoice_ID = ? AND " + "IsActive = 'Y' AND " + "IsCalcOnPayment = 'Y' AND " + "Processed = 'Y' AND " + "C_AllocationLine_ID = ?";
            PreparedStatement pstmt = DB.prepareStatement(sql, ah.get_TrxName());
            ResultSet rs = null;
            try {
                pstmt.setInt(1, al.getC_Invoice_ID());
                pstmt.setInt(2, al.getC_AllocationLine_ID());
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    int iwhid = rs.getInt(1);
                    MLCOInvoiceWithholding iwh = new MLCOInvoiceWithholding(ah.getCtx(), iwhid, ah.get_TrxName());
                    iwh.setC_AllocationLine_ID(0);
                    iwh.setProcessed(false);
                    if (!iwh.save())
                        return "Error saving LCO_InvoiceWithholding reversePaymentWithholdings";
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return e.getLocalizedMessage();
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
        }
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) MAllocationLine(org.compiere.model.MAllocationLine) PreparedStatement(java.sql.PreparedStatement)

Aggregations

MAllocationLine (org.compiere.model.MAllocationLine)12 MPayment (org.compiere.model.MPayment)8 BigDecimal (java.math.BigDecimal)6 MAllocationHdr (org.compiere.model.MAllocationHdr)6 MInvoice (org.compiere.model.MInvoice)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Timestamp (java.sql.Timestamp)3 ArrayList (java.util.ArrayList)3 AdempiereException (org.adempiere.exceptions.AdempiereException)2 Doc (org.compiere.acct.Doc)2 DocTax (org.compiere.acct.DocTax)2 Fact (org.compiere.acct.Fact)2 FactLine (org.compiere.acct.FactLine)2 MAcctSchema (org.compiere.model.MAcctSchema)2 DocLine (org.compiere.acct.DocLine)1 IDColumn (org.compiere.minigrid.IDColumn)1 MPaymentAllocate (org.compiere.model.MPaymentAllocate)1 MTax (org.compiere.model.MTax)1