Search in sources :

Example 1 with MCashLine

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

the class ImmediateBankTransfer method createCashLines.

//  createCash
private MCashLine[] createCashLines(MCash cash) {
    ArrayList<MCashLine> cashLineList = new ArrayList<MCashLine>();
    // From Bank (From) to CashLine
    MCashLine cashLine = new MCashLine(cash);
    cashLine.setAmount(p_Amount);
    cashLine.setC_BankAccount_ID(p_From_C_BankAccount_ID);
    cashLine.setC_Currency_ID(m_C_Currency_ID);
    if (p_Description != null)
        cashLine.setDescription(p_Description);
    else
        cashLine.setDescription(p_Name);
    // Transfer
    cashLine.setCashType("T");
    if (!cashLine.save()) {
        throw new IllegalStateException("Could not create Cash line (From Bank)");
    }
    cashLineList.add(cashLine);
    // From CashLine to Bank (To)
    cashLine = new MCashLine(cash);
    cashLine.setAmount(p_Amount.negate());
    cashLine.setC_BankAccount_ID(p_To_C_BankAccount_ID);
    cashLine.setC_Currency_ID(m_C_Currency_ID);
    if (p_Description != null)
        cashLine.setDescription(p_Description);
    else
        cashLine.setDescription(p_Name);
    // Transfer
    cashLine.setCashType("T");
    if (!cashLine.save()) {
        throw new IllegalStateException("Could not create Cash line (To Bank)");
    }
    cashLineList.add(cashLine);
    MCashLine[] cashLines = new MCashLine[cashLineList.size()];
    cashLineList.toArray(cashLines);
    return cashLines;
}
Also used : ArrayList(java.util.ArrayList) MCashLine(org.compiere.model.MCashLine)

Example 2 with MCashLine

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

the class ImmediateBankTransfer method generateBankTransfer.

//  createCashLines
/**
	 * Generate CashJournal
	 *
	 */
private void generateBankTransfer() {
    //	Create Cash & CashLines
    MCash cash = createCash();
    MCashLine[] cashLines = createCashLines(cash);
    StringBuffer processMsg = new StringBuffer(cash.getDocumentNo());
    cash.setDocAction(p_docAction);
    if (!cash.processIt(p_docAction)) {
        processMsg.append(" (NOT Processed)");
        log.warning("Cash Processing failed: " + cash + " - " + cash.getProcessMsg());
        addLog(cash.getC_Cash_ID(), cash.getStatementDate(), null, "Cash Processing failed: " + cash + " - " + cash.getProcessMsg() + " / please complete it manually");
    }
    if (!cash.save()) {
        throw new IllegalStateException("Could not create Cash");
    }
    // Add processing information to process log
    addLog(cash.getC_Cash_ID(), cash.getStatementDate(), null, processMsg.toString());
    m_created++;
}
Also used : MCash(org.compiere.model.MCash) MCashLine(org.compiere.model.MCashLine)

Example 3 with MCashLine

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

the class WPayment method dynInit.

//	jbInit
/**************************************************************************
	 *	Dynamic Init.
	 *		B (Cash)		(Currency)
	 *		K (CreditCard)  Type, Number, Exp, Approval
	 *		L (DirectDebit)	BPartner_Bank
	 *		P (PaymentTerm)	PaymentTerm
	 *		S (Check)		(Currency) CheckNo, Routing
	 *
	 *	Currencies are shown, if member of EMU
	 *  @param button payment type button
	 *  @return true if init OK
	 *  @throws Exception
	 */
private boolean dynInit(WButtonEditor button) throws Exception {
    m_DocStatus = (String) m_mTab.getValue("DocStatus");
    log.config(m_DocStatus);
    if (m_mTab.getValue("C_BPartner_ID") == null) {
        FDialog.error(0, this, "SaveErrorRowNotFound");
        return false;
    }
    //	Is the Trx posted?
    //	String Posted = (String)m_mTab.getValue("Posted");
    //	if (Posted != null && Posted.equals("Y"))
    //		return false;
    //  DocStatus
    m_DocStatus = (String) m_mTab.getValue("DocStatus");
    if (m_DocStatus == null)
        m_DocStatus = "";
    //	Is the Trx closed?		Reversed / Voided / Cloased
    if (m_DocStatus.equals("RE") || m_DocStatus.equals("VO") || m_DocStatus.equals("CL"))
        return false;
    //  Document is not complete - allow to change the Payment Rule only
    if (m_DocStatus.equals("CO") || m_DocStatus.equals("WP"))
        m_onlyRule = false;
    else
        m_onlyRule = true;
    //	PO only  Rule
    if (//	Only order has Warehouse
    !m_onlyRule && !m_isSOTrx && m_mTab.getValue("M_Warehouse_ID") != null)
        m_onlyRule = true;
    centerPanel.setVisible(!m_onlyRule);
    //  Amount
    m_Amount = (BigDecimal) m_mTab.getValue("GrandTotal");
    if (!m_onlyRule && m_Amount.compareTo(Env.ZERO) == 0) {
        FDialog.error(m_WindowNo, this, "PaymentZero");
        return false;
    }
    bAmountField.setValue(m_Amount);
    sAmountField.setValue(m_Amount);
    kAmountField.setValue(m_Amount);
    /**
		 *	Get Data from Grid
		 */
    m_AD_Client_ID = ((Integer) m_mTab.getValue("AD_Client_ID")).intValue();
    m_Cash_As_Payment = MSysConfig.getBooleanValue("CASH_AS_PAYMENT", true, m_AD_Client_ID);
    m_AD_Org_ID = ((Integer) m_mTab.getValue("AD_Org_ID")).intValue();
    m_C_BPartner_ID = ((Integer) m_mTab.getValue("C_BPartner_ID")).intValue();
    m_PaymentRule = (String) m_mTab.getValue("PaymentRule");
    m_C_Currency_ID = ((Integer) m_mTab.getValue("C_Currency_ID")).intValue();
    m_DateAcct = (Timestamp) m_mTab.getValue("DateAcct");
    if (m_mTab.getValue("C_PaymentTerm_ID") != null)
        m_C_PaymentTerm_ID = ((Integer) m_mTab.getValue("C_PaymentTerm_ID")).intValue();
    //  Existing Payment
    if (m_mTab.getValue("C_Payment_ID") != null) {
        m_C_Payment_ID = ((Integer) m_mTab.getValue("C_Payment_ID")).intValue();
        if (m_C_Payment_ID != 0) {
            m_mPayment = new MPayment(Env.getCtx(), m_C_Payment_ID, null);
            //	full copy
            m_mPaymentOriginal = new MPayment(Env.getCtx(), m_C_Payment_ID, null);
            //  CreditCard
            m_CCType = m_mPayment.getCreditCardType();
            kNumberField.setText(m_mPayment.getCreditCardNumber());
            kExpField.setText(m_mPayment.getCreditCardExp(null));
            kApprovalField.setText(m_mPayment.getVoiceAuthCode());
            kStatus.setText(m_mPayment.getR_PnRef());
            kAmountField.setValue(m_mPayment.getPayAmt());
            //	if approved/paid, don't let it change
            kTypeCombo.setEnabled(!m_mPayment.isApproved());
            kNumberField.setReadonly(m_mPayment.isApproved());
            kExpField.setReadonly(m_mPayment.isApproved());
            kApprovalField.setReadonly(m_mPayment.isApproved());
            kOnline.setEnabled(!m_mPayment.isApproved());
            kAmountField.setReadWrite(!m_mPayment.isApproved());
            //  Check
            m_C_BankAccount_ID = m_mPayment.getC_BankAccount_ID();
            sRoutingField.setText(m_mPayment.getRoutingNo());
            sNumberField.setText(m_mPayment.getAccountNo());
            sCheckField.setText(m_mPayment.getCheckNo());
            sStatus.setText(m_mPayment.getR_PnRef());
            sAmountField.setValue(m_mPayment.getPayAmt());
            //  Transfer
            tRoutingField.setText(m_mPayment.getRoutingNo());
            tNumberField.setText(m_mPayment.getAccountNo());
            tStatus.setText(m_mPayment.getR_PnRef());
            // Cash
            bAmountField.setValue(m_mPayment.getPayAmt());
        }
    }
    if (m_mPayment == null) {
        m_mPayment = new MPayment(Env.getCtx(), 0, null);
        m_mPayment.setAD_Org_ID(m_AD_Org_ID);
        m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
    }
    //  Existing Cashbook entry
    m_cashLine = null;
    m_C_CashLine_ID = 0;
    if (m_mTab.getValue("C_CashLine_ID") != null) {
        m_C_CashLine_ID = ((Integer) m_mTab.getValue("C_CashLine_ID")).intValue();
        if (m_C_CashLine_ID == 0)
            m_cashLine = null;
        else {
            m_cashLine = new MCashLine(Env.getCtx(), m_C_CashLine_ID, null);
            m_DateAcct = m_cashLine.getStatementDate();
            m_C_CashBook_ID = m_cashLine.getCashBook().getC_CashBook_ID();
            bAmountField.setValue(m_cashLine.getAmount());
        }
    }
    //	Accounting Date
    bDateField.setValue(m_DateAcct);
    if (s_Currencies == null)
        loadCurrencies();
    //	Is the currency an EMU currency?
    Integer C_Currency_ID = new Integer(m_C_Currency_ID);
    if (s_Currencies.containsKey(C_Currency_ID)) {
        Enumeration<Integer> en = s_Currencies.keys();
        while (en.hasMoreElements()) {
            Object key = en.nextElement();
            bCurrencyCombo.addItem(s_Currencies.get(key));
            sCurrencyCombo.addItem(s_Currencies.get(key));
        }
        sCurrencyCombo.addActionListener(this);
        sCurrencyCombo.setSelectedKeyNamePair(s_Currencies.get(C_Currency_ID));
        bCurrencyCombo.addActionListener(this);
        bCurrencyCombo.setSelectedKeyNamePair(s_Currencies.get(C_Currency_ID));
    } else //	No EMU Currency
    {
        //	Cash
        bCurrencyLabel.setVisible(false);
        bCurrencyCombo.setVisible(false);
        //	Check
        sCurrencyLabel.setVisible(false);
        sCurrencyCombo.setVisible(false);
    }
    /**
		 *	Payment Combo
		 */
    if (m_PaymentRule == null)
        m_PaymentRule = "";
    ValueNamePair vp = null;
    HashMap<String, String> values = button.getValues();
    Object[] a = values.keySet().toArray();
    for (int i = 0; i < a.length; i++) {
        //	used for Panel selection
        String PaymentRule = (String) a[i];
        if (//	SO
        X_C_Order.PAYMENTRULE_DirectDebit.equals(PaymentRule) && !m_isSOTrx)
            continue;
        else if (//	PO 
        X_C_Order.PAYMENTRULE_DirectDeposit.equals(PaymentRule) && m_isSOTrx)
            continue;
        ValueNamePair pp = new ValueNamePair(PaymentRule, (String) values.get(a[i]));
        paymentCombo.addItem(pp);
        if (//	to select
        PaymentRule.toString().equals(m_PaymentRule))
            vp = pp;
    }
    //	Set PaymentRule
    paymentCombo.addActionListener(this);
    if (vp != null) {
        paymentCombo.setSelectedValueNamePair(vp);
        onPaymentComboSelection();
    }
    /**
		 * 	Load Payment Terms
		 */
    String SQL = MRole.getDefault().addAccessSQL("SELECT C_PaymentTerm_ID, Name FROM C_PaymentTerm WHERE IsActive='Y' ORDER BY Name", "C_PaymentTerm", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
    KeyNamePair kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            pTermCombo.addItem(pp);
            if (key == m_C_PaymentTerm_ID)
                kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException ept) {
        log.log(Level.SEVERE, SQL, ept);
    }
    //	Set Selection
    if (kp != null)
        pTermCombo.setSelectedKeyNamePair(kp);
    /**
		 * 	Load Accounts
		 */
    SQL = "SELECT a.C_BP_BankAccount_ID, NVL(b.Name, ' ')||'_'||NVL(a.AccountNo, ' ') AS Acct " + "FROM C_BP_BankAccount a" + " LEFT OUTER JOIN C_Bank b ON (a.C_Bank_ID=b.C_Bank_ID) " + "WHERE C_BPartner_ID=?" + "AND a.IsActive='Y' AND a.IsACH='Y'";
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        pstmt.setInt(1, m_C_BPartner_ID);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            tAccountCombo.addItem(pp);
        }
        rs.close();
        pstmt.close();
    } catch (SQLException eac) {
        log.log(Level.SEVERE, SQL, eac);
    }
    /**
		 *	Load Credit Cards
		 */
    ValueNamePair[] ccs = m_mPayment.getCreditCards();
    vp = null;
    for (int i = 0; i < ccs.length; i++) {
        kTypeCombo.addItem(ccs[i]);
        if (ccs[i].getValue().equals(m_CCType))
            vp = ccs[i];
    }
    //	Set Selection
    if (vp != null)
        kTypeCombo.setSelectedValueNamePair(vp);
    /**
		 *  Load Bank Accounts
		 */
    SQL = MRole.getDefault().addAccessSQL("SELECT C_BankAccount_ID, Name || ' ' || AccountNo, IsDefault " + "FROM C_BankAccount ba" + " INNER JOIN C_Bank b ON (ba.C_Bank_ID=b.C_Bank_ID) " + "WHERE b.IsActive='Y'", "ba", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO);
    kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            sBankAccountCombo.addItem(pp);
            bBankAccountCombo.addItem(pp);
            if (key == m_C_BankAccount_ID)
                kp = pp;
            if (//  Default
            kp == null && rs.getString(3).equals("Y"))
                kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException ept) {
        log.log(Level.SEVERE, SQL, ept);
    }
    //	Set Selection
    if (kp != null) {
        sBankAccountCombo.setSelectedKeyNamePair(kp);
        bBankAccountCombo.setSelectedKeyNamePair(kp);
    }
    /**
		 *  Load Cash Books
		 */
    SQL = MRole.getDefault().addAccessSQL("SELECT C_CashBook_ID, Name, AD_Org_ID FROM C_CashBook WHERE IsActive='Y'", "C_CashBook", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
    kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            bCashBookCombo.addItem(pp);
            if (key == m_C_CashBook_ID)
                kp = pp;
            if (//  Default Org
            kp == null && key == m_AD_Org_ID)
                kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException epc) {
        log.log(Level.SEVERE, SQL, epc);
    }
    //	Set Selection
    if (kp != null) {
        bCashBookCombo.setSelectedKeyNamePair(kp);
        if (m_C_CashBook_ID == 0)
            //  set to default to avoid 'cashbook changed' message
            m_C_CashBook_ID = kp.getKey();
    }
    //
    return true;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) MCashLine(org.compiere.model.MCashLine) MPayment(org.compiere.model.MPayment) ResultSet(java.sql.ResultSet) ValueNamePair(org.compiere.util.ValueNamePair) KeyNamePair(org.compiere.util.KeyNamePair)

Example 4 with MCashLine

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

the class VPayment method dynInit.

//	jbInit
/**************************************************************************
	 *	Dynamic Init.
	 *		B (Cash)		(Currency)
	 *		K (CreditCard)  Type, Number, Exp, Approval
	 *		L (DirectDebit)	BPartner_Bank
	 *		P (PaymentTerm)	PaymentTerm
	 *		S (Check)		(Currency) CheckNo, Routing
	 *
	 *	Currencies are shown, if member of EMU
	 *  @param button payment type button
	 *  @return true if init OK
	 *  @throws Exception
	 */
private boolean dynInit(VButton button) throws Exception {
    m_DocStatus = (String) m_mTab.getValue("DocStatus");
    log.config(m_DocStatus);
    if (m_mTab.getValue("C_BPartner_ID") == null) {
        ADialog.error(0, this, "SaveErrorRowNotFound");
        return false;
    }
    //	Is the Trx posted?
    //	String Posted = (String)m_mTab.getValue("Posted");
    //	if (Posted != null && Posted.equals("Y"))
    //		return false;
    //  DocStatus
    m_DocStatus = (String) m_mTab.getValue("DocStatus");
    if (m_DocStatus == null)
        m_DocStatus = "";
    //	Is the Trx closed?		Reversed / Voided / Closed
    if (m_DocStatus.equals("RE") || m_DocStatus.equals("VO") || m_DocStatus.equals("CL"))
        return false;
    //  Document is not complete - allow to change the Payment Rule only
    if (m_DocStatus.equals("CO") || m_DocStatus.equals("WP"))
        m_onlyRule = false;
    else
        m_onlyRule = true;
    //	PO only  Rule
    if (//	Only order has Warehouse
    !m_onlyRule && !m_isSOTrx && m_mTab.getValue("M_Warehouse_ID") != null)
        m_onlyRule = true;
    centerPanel.setVisible(!m_onlyRule);
    //  Amount
    m_Amount = (BigDecimal) m_mTab.getValue("GrandTotal");
    if (!m_onlyRule && m_Amount.compareTo(Env.ZERO) == 0) {
        ADialog.error(m_WindowNo, this, "PaymentZero");
        return false;
    }
    bAmountField.setValue(m_Amount);
    sAmountField.setValue(m_Amount);
    kAmountField.setValue(m_Amount);
    /**
		 *	Get Data from Grid
		 */
    m_AD_Client_ID = ((Integer) m_mTab.getValue("AD_Client_ID")).intValue();
    m_Cash_As_Payment = MSysConfig.getBooleanValue("CASH_AS_PAYMENT", true, m_AD_Client_ID);
    m_AD_Org_ID = ((Integer) m_mTab.getValue("AD_Org_ID")).intValue();
    m_C_BPartner_ID = ((Integer) m_mTab.getValue("C_BPartner_ID")).intValue();
    m_PaymentRule = (String) m_mTab.getValue("PaymentRule");
    m_C_Currency_ID = ((Integer) m_mTab.getValue("C_Currency_ID")).intValue();
    m_DateAcct = (Timestamp) m_mTab.getValue("DateAcct");
    if (m_mTab.getValue("C_PaymentTerm_ID") != null)
        m_C_PaymentTerm_ID = ((Integer) m_mTab.getValue("C_PaymentTerm_ID")).intValue();
    //  Existing Payment
    if (m_mTab.getValue("C_Payment_ID") != null) {
        m_C_Payment_ID = ((Integer) m_mTab.getValue("C_Payment_ID")).intValue();
        if (m_C_Payment_ID != 0) {
            m_mPayment = new MPayment(Env.getCtx(), m_C_Payment_ID, null);
            //	full copy
            m_mPaymentOriginal = new MPayment(Env.getCtx(), m_C_Payment_ID, null);
            //  CreditCard
            m_CCType = m_mPayment.getCreditCardType();
            kNumberField.setText(m_mPayment.getCreditCardNumber());
            kNameField.setText(m_mPayment.getA_Name());
            kExpField.setText(m_mPayment.getCreditCardExp(null));
            kApprovalField.setText(m_mPayment.getVoiceAuthCode());
            kStatus.setText(m_mPayment.getR_PnRef());
            kAmountField.setValue(m_mPayment.getPayAmt());
            //	if approved/paid, don't let it change
            kTypeCombo.setReadWrite(!m_mPayment.isApproved());
            kNumberField.setReadWrite(!m_mPayment.isApproved());
            kNameField.setReadWrite(!m_mPayment.isApproved());
            kExpField.setReadWrite(!m_mPayment.isApproved());
            kApprovalField.setReadWrite(!m_mPayment.isApproved());
            kOnline.setReadWrite(!m_mPayment.isApproved());
            kAmountField.setReadWrite(!m_mPayment.isApproved());
            //  Check
            m_C_BankAccount_ID = m_mPayment.getC_BankAccount_ID();
            sRoutingField.setText(m_mPayment.getRoutingNo());
            sNumberField.setText(m_mPayment.getAccountNo());
            sCheckField.setText(m_mPayment.getCheckNo());
            sStatus.setText(m_mPayment.getR_PnRef());
            sAmountField.setValue(m_mPayment.getPayAmt());
            //  Transfer
            tRoutingField.setText(m_mPayment.getRoutingNo());
            tNumberField.setText(m_mPayment.getAccountNo());
            tStatus.setText(m_mPayment.getR_PnRef());
            // Cash
            bAmountField.setValue(m_mPayment.getPayAmt());
        }
    }
    if (m_mPayment == null) {
        m_mPayment = new MPayment(Env.getCtx(), 0, null);
        m_mPayment.setAD_Org_ID(m_AD_Org_ID);
        m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
    }
    //  Existing Cashbook entry
    m_cashLine = null;
    m_C_CashLine_ID = 0;
    if (m_mTab.getValue("C_CashLine_ID") != null) {
        m_C_CashLine_ID = ((Integer) m_mTab.getValue("C_CashLine_ID")).intValue();
        if (m_C_CashLine_ID == 0)
            m_cashLine = null;
        else {
            m_cashLine = new MCashLine(Env.getCtx(), m_C_CashLine_ID, null);
            m_DateAcct = m_cashLine.getStatementDate();
            m_C_CashBook_ID = m_cashLine.getCashBook().getC_CashBook_ID();
            bAmountField.setValue(m_cashLine.getAmount());
        }
    }
    //	Accounting Date
    bDateField.setValue(m_DateAcct);
    if (s_Currencies == null)
        loadCurrencies();
    //	Is the currency an EMU currency?
    Integer C_Currency_ID = new Integer(m_C_Currency_ID);
    if (s_Currencies.containsKey(C_Currency_ID)) {
        Enumeration<Integer> en = s_Currencies.keys();
        while (en.hasMoreElements()) {
            Object key = en.nextElement();
            bCurrencyCombo.addItem(s_Currencies.get(key));
            sCurrencyCombo.addItem(s_Currencies.get(key));
        }
        sCurrencyCombo.addActionListener(this);
        sCurrencyCombo.setSelectedItem(s_Currencies.get(C_Currency_ID));
        bCurrencyCombo.addActionListener(this);
        bCurrencyCombo.setSelectedItem(s_Currencies.get(C_Currency_ID));
    } else //	No EMU Currency
    {
        //	Cash
        bCurrencyLabel.setVisible(false);
        bCurrencyCombo.setVisible(false);
        //	Check
        sCurrencyLabel.setVisible(false);
        sCurrencyCombo.setVisible(false);
    }
    /**
		 *	Payment Combo
		 */
    if (m_PaymentRule == null)
        m_PaymentRule = "";
    ValueNamePair vp = null;
    HashMap<?, ?> values = button.getValues();
    Object[] a = values.keySet().toArray();
    for (int i = 0; i < a.length; i++) {
        //	used for Panel selection
        String PaymentRule = (String) a[i];
        if (//	SO
        X_C_Order.PAYMENTRULE_DirectDebit.equals(PaymentRule) && !m_isSOTrx)
            continue;
        else if (//	PO 
        X_C_Order.PAYMENTRULE_DirectDeposit.equals(PaymentRule) && m_isSOTrx)
            continue;
        ValueNamePair pp = new ValueNamePair(PaymentRule, (String) values.get(a[i]));
        paymentCombo.addItem(pp);
        if (//	to select
        PaymentRule.toString().equals(m_PaymentRule))
            vp = pp;
    }
    //	Set PaymentRule
    paymentCombo.addActionListener(this);
    if (vp != null)
        paymentCombo.setSelectedItem(vp);
    /**
		 * 	Load Payment Terms
		 */
    String SQL = MRole.getDefault().addAccessSQL("SELECT C_PaymentTerm_ID, Name FROM C_PaymentTerm WHERE IsActive='Y' ORDER BY Name", "C_PaymentTerm", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
    KeyNamePair kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            pTermCombo.addItem(pp);
            if (key == m_C_PaymentTerm_ID)
                kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException ept) {
        log.log(Level.SEVERE, SQL, ept);
    }
    //	Set Selection
    if (kp != null)
        pTermCombo.setSelectedItem(kp);
    /**
		 * 	Load Accounts
		 */
    SQL = "SELECT a.C_BP_BankAccount_ID, NVL(b.Name, ' ')||'_'||NVL(a.AccountNo, ' ') AS Acct " + "FROM C_BP_BankAccount a" + " LEFT OUTER JOIN C_Bank b ON (a.C_Bank_ID=b.C_Bank_ID) " + "WHERE C_BPartner_ID=?" + "AND a.IsActive='Y' AND a.IsACH='Y'";
    kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        pstmt.setInt(1, m_C_BPartner_ID);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            tAccountCombo.addItem(pp);
        //			kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException eac) {
        log.log(Level.SEVERE, SQL, eac);
    }
    //	Set Selection
    if (kp != null)
        tAccountCombo.setSelectedItem(kp);
    /**
		 *	Load Credit Cards
		 */
    ValueNamePair[] ccs = m_mPayment.getCreditCards();
    vp = null;
    for (int i = 0; i < ccs.length; i++) {
        kTypeCombo.addItem(ccs[i]);
        if (ccs[i].getValue().equals(m_CCType))
            vp = ccs[i];
    }
    //	Set Selection
    if (vp != null)
        kTypeCombo.setSelectedItem(vp);
    /**
		 *  Load Bank Accounts
		 */
    SQL = MRole.getDefault().addAccessSQL("SELECT C_BankAccount_ID, ba.accountno, IsDefault " + "FROM C_BankAccount ba" + " INNER JOIN C_Bank b ON (ba.C_Bank_ID=b.C_Bank_ID) " + "WHERE b.IsActive='Y'", "ba", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO);
    kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            sBankAccountCombo.addItem(pp);
            if (key == m_C_BankAccount_ID)
                kp = pp;
            if (//  Default
            kp == null && rs.getString(3).equals("Y"))
                kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException ept) {
        log.log(Level.SEVERE, SQL, ept);
    }
    //	Set Selection
    if (kp != null)
        sBankAccountCombo.setSelectedItem(kp);
    /**
		 *  Load Cash Books
		 */
    SQL = MRole.getDefault().addAccessSQL("SELECT C_CashBook_ID, Name, AD_Org_ID FROM C_CashBook WHERE IsActive='Y'", "C_CashBook", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
    kp = null;
    try {
        PreparedStatement pstmt = DB.prepareStatement(SQL, null);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int key = rs.getInt(1);
            String name = rs.getString(2);
            KeyNamePair pp = new KeyNamePair(key, name);
            bCashBookCombo.addItem(pp);
            if (key == m_C_CashBook_ID)
                kp = pp;
            if (//  Default Org
            kp == null && key == m_AD_Org_ID)
                kp = pp;
        }
        rs.close();
        pstmt.close();
    } catch (SQLException epc) {
        log.log(Level.SEVERE, SQL, epc);
    }
    //	Set Selection
    if (kp != null) {
        bCashBookCombo.setSelectedItem(kp);
        if (m_C_CashBook_ID == 0)
            //  set to default to avoid 'cashbook changed' message
            m_C_CashBook_ID = kp.getKey();
    }
    //
    return true;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) MCashLine(org.compiere.model.MCashLine) MPayment(org.compiere.model.MPayment) ResultSet(java.sql.ResultSet) ValueNamePair(org.compiere.util.ValueNamePair) KeyNamePair(org.compiere.util.KeyNamePair)

Example 5 with MCashLine

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

the class Doc_AllocationTax method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  CMA.
	 *  <pre>
	 *  AR_Invoice_Payment
	 *      UnAllocatedCash DR
	 *      or C_Prepayment
	 *      DiscountExp     DR
	 *      WriteOff        DR
	 *      Receivables             CR
	 *  AR_Invoice_Cash
	 *      CashTransfer    DR
	 *      DiscountExp     DR
	 *      WriteOff        DR
	 *      Receivables             CR
	 * 
	 *  AP_Invoice_Payment
	 *      Liability       DR
	 *      DiscountRev             CR
	 *      WriteOff                CR
	 *      PaymentSelect           CR
	 *      or V_Prepayment
	 *  AP_Invoice_Cash
	 *      Liability       DR
	 *      DiscountRev             CR
	 *      WriteOff                CR
	 *      CashTransfer            CR
	 *  CashBankTransfer
	 *      -
	 *  ==============================
	 *  Realized Gain & Loss
	 * 		AR/AP			DR		CR
	 * 		Realized G/L	DR		CR
	 * 
	 *
	 *  </pre>
	 *  Tax needs to be corrected for discount & write-off;
	 *  Currency gain & loss is realized here.
	 *  @param as accounting schema
	 *  @return Fact
	 */
public ArrayList<Fact> createFacts(MAcctSchema as) {
    m_facts = new ArrayList<Fact>();
    //  create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    // dummy fact (not posted) to calculate Realized Gain & Loss
    Fact factForRGL = new Fact(this, as, Fact.POST_Actual);
    boolean isInterOrg = isInterOrg(as);
    for (int i = 0; i < p_lines.length; i++) {
        DocLine_Allocation line = (DocLine_Allocation) p_lines[i];
        setC_BPartner_ID(line.getC_BPartner_ID());
        //  CashBankTransfer - all references null and Discount/WriteOff = 0
        if (line.getC_Payment_ID() != 0 && line.getC_Invoice_ID() == 0 && line.getC_Order_ID() == 0 && line.getC_CashLine_ID() == 0 && line.getC_BPartner_ID() == 0 && Env.ZERO.compareTo(line.getDiscountAmt()) == 0 && Env.ZERO.compareTo(line.getWriteOffAmt()) == 0)
            continue;
        //	Receivables/Liability Amt
        BigDecimal allocationSource = line.getAmtSource().add(line.getDiscountAmt()).add(line.getWriteOffAmt());
        // for realized gain & loss purposes
        BigDecimal allocationSourceForRGL = allocationSource;
        // AR/AP balance corrected
        BigDecimal allocationAccounted = Env.ZERO;
        // for realized gain & loss purposes
        BigDecimal allocationAccountedForRGL = Env.ZERO;
        FactLine fl = null;
        FactLine flForRGL = null;
        //	Liability/Receivables
        MAccount bpAcct = null;
        //
        MPayment payment = null;
        if (line.getC_Payment_ID() != 0)
            payment = new MPayment(getCtx(), line.getC_Payment_ID(), getTrxName());
        MInvoice invoice = null;
        if (line.getC_Invoice_ID() != 0)
            invoice = new MInvoice(getCtx(), line.getC_Invoice_ID(), getTrxName());
        //	No Invoice
        if (invoice == null) {
            /**
				   * Adaxa: Paul
				   * Allocate to charges
				   */
            if (line.getC_Invoice_ID() == 0 && line.getC_Payment_ID() == 0 && line.getC_Charge_ID() != 0) {
                fl = fact.createLine(line, line.getChargeAccount(as, line.getAmtSource()), getC_Currency_ID(), line.getAmtSource());
            } else //	Payment Only
            if (line.getC_Invoice_ID() == 0 && line.getC_Payment_ID() != 0) {
                if (line.getAmtSource().signum() >= 0)
                    fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), line.getAmtSource(), null);
                else
                    fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), null, line.getAmtSource().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            } else {
                p_Error = "Cannot determine SO/PO";
                log.log(Level.SEVERE, p_Error);
                return null;
            }
        } else //	Sales Invoice	
        if (invoice.isSOTrx()) {
            // Avoid usage of clearing accounts
            // If both accounts Unallocated Cash and Receivable are equal
            // then don't post
            MAccount acct_unallocated_cash = null;
            if (line.getC_Payment_ID() != 0)
                acct_unallocated_cash = getPaymentAcct(as, line.getC_Payment_ID());
            else if (line.getC_CashLine_ID() != 0)
                acct_unallocated_cash = getCashAcct(as, line.getC_CashLine_ID());
            MAccount acct_receivable = getAccount(Doc.ACCTTYPE_C_Receivable, as);
            if ((!as.isPostIfClearingEqual()) && acct_unallocated_cash != null && acct_unallocated_cash.equals(acct_receivable) && (!isInterOrg)) {
                // if not using clearing accounts, then don't post amtsource
                // change the allocationsource to be writeoff + discount
                allocationSource = line.getDiscountAmt().add(line.getWriteOffAmt());
            } else {
                //	Payment/Cash	DR
                if (line.getC_Payment_ID() != 0) {
                    fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), line.getAmtSource(), null);
                    if (fl != null && payment != null)
                        fl.setAD_Org_ID(payment.getAD_Org_ID());
                } else if (line.getC_CashLine_ID() != 0) {
                    fl = fact.createLine(line, getCashAcct(as, line.getC_CashLine_ID()), getC_Currency_ID(), line.getAmtSource(), null);
                    MCashLine cashLine = new MCashLine(getCtx(), line.getC_CashLine_ID(), getTrxName());
                    if (fl != null && cashLine.get_ID() != 0)
                        fl.setAD_Org_ID(cashLine.getAD_Org_ID());
                }
            }
            //	Discount		DR
            if (Env.ZERO.compareTo(line.getDiscountAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_DiscountExp, as), getC_Currency_ID(), line.getDiscountAmt(), null);
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	Write off		DR
            if (Env.ZERO.compareTo(line.getWriteOffAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_WriteOff, as), getC_Currency_ID(), line.getWriteOffAmt(), null);
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	AR Invoice Amount	CR
            if (as.isAccrual()) {
                bpAcct = getAccount(Doc.ACCTTYPE_C_Receivable, as);
                if (allocationSource.signum() >= 0)
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), null, allocationSource);
                else
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), allocationSource.negate(), null);
                if (fl != null)
                    allocationAccounted = fl.getAcctBalance().negate();
                if (fl != null && invoice != null)
                    fl.setAD_Org_ID(invoice.getAD_Org_ID());
                // for Realized Gain & Loss
                flForRGL = factForRGL.createLine(line, bpAcct, getC_Currency_ID(), null, //	payment currency 
                allocationSourceForRGL);
                if (flForRGL != null)
                    allocationAccountedForRGL = flForRGL.getAcctBalance().negate();
            } else //	Cash Based
            {
                allocationAccounted = createCashBasedAcct(as, fact, invoice, allocationSource);
                allocationAccountedForRGL = allocationAccounted;
            }
        } else //	Purchase Invoice
        {
            // Avoid usage of clearing accounts
            // If both accounts Payment Select and Liability are equal
            // then don't post
            MAccount acct_payment_select = null;
            if (line.getC_Payment_ID() != 0)
                acct_payment_select = getPaymentAcct(as, line.getC_Payment_ID());
            else if (line.getC_CashLine_ID() != 0)
                acct_payment_select = getCashAcct(as, line.getC_CashLine_ID());
            MAccount acct_liability = getAccount(Doc.ACCTTYPE_V_Liability, as);
            boolean isUsingClearing = true;
            // Save original allocation source for realized gain & loss purposes
            allocationSourceForRGL = allocationSourceForRGL.negate();
            if ((!as.isPostIfClearingEqual()) && acct_payment_select != null && acct_payment_select.equals(acct_liability) && (!isInterOrg)) {
                // if not using clearing accounts, then don't post amtsource
                // change the allocationsource to be writeoff + discount
                allocationSource = line.getDiscountAmt().add(line.getWriteOffAmt());
                isUsingClearing = false;
            }
            // End Avoid usage of clearing accounts
            //	allocation is negative
            allocationSource = allocationSource.negate();
            //	AP Invoice Amount	DR
            if (as.isAccrual()) {
                bpAcct = getAccount(Doc.ACCTTYPE_V_Liability, as);
                if (allocationSource.signum() >= 0)
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), allocationSource, null);
                else
                    //	payment currency
                    fl = fact.createLine(line, bpAcct, getC_Currency_ID(), null, allocationSource.negate());
                if (fl != null)
                    allocationAccounted = fl.getAcctBalance();
                if (fl != null && invoice != null)
                    fl.setAD_Org_ID(invoice.getAD_Org_ID());
                // for Realized Gain & Loss
                flForRGL = factForRGL.createLine(line, bpAcct, getC_Currency_ID(), allocationSourceForRGL, //	payment currency
                null);
                if (flForRGL != null)
                    allocationAccountedForRGL = flForRGL.getAcctBalance();
            } else //	Cash Based
            {
                allocationAccounted = createCashBasedAcct(as, fact, invoice, allocationSource);
                allocationAccountedForRGL = allocationAccounted;
            }
            //	Discount		CR
            if (Env.ZERO.compareTo(line.getDiscountAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_DiscountRev, as), getC_Currency_ID(), null, line.getDiscountAmt().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	Write off		CR
            if (Env.ZERO.compareTo(line.getWriteOffAmt()) != 0) {
                fl = fact.createLine(line, getAccount(Doc.ACCTTYPE_WriteOff, as), getC_Currency_ID(), null, line.getWriteOffAmt().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            }
            //	Payment/Cash	CR
            if (// Avoid usage of clearing accounts
            isUsingClearing && line.getC_Payment_ID() != 0) {
                fl = fact.createLine(line, getPaymentAcct(as, line.getC_Payment_ID()), getC_Currency_ID(), null, line.getAmtSource().negate());
                if (fl != null && payment != null)
                    fl.setAD_Org_ID(payment.getAD_Org_ID());
            } else if (// Avoid usage of clearing accounts
            isUsingClearing && line.getC_CashLine_ID() != 0) {
                fl = fact.createLine(line, getCashAcct(as, line.getC_CashLine_ID()), getC_Currency_ID(), null, line.getAmtSource().negate());
                MCashLine cashLine = new MCashLine(getCtx(), line.getC_CashLine_ID(), getTrxName());
                if (fl != null && cashLine.get_ID() != 0)
                    fl.setAD_Org_ID(cashLine.getAD_Org_ID());
            }
        }
        //	VAT Tax Correction
        if (invoice != null && as.isTaxCorrection()) {
            BigDecimal taxCorrectionAmt = Env.ZERO;
            if (as.isTaxCorrectionDiscount())
                taxCorrectionAmt = line.getDiscountAmt();
            if (as.isTaxCorrectionWriteOff())
                taxCorrectionAmt = taxCorrectionAmt.add(line.getWriteOffAmt());
            //
            if (taxCorrectionAmt.signum() != 0) {
                if (!createTaxCorrection(as, fact, line, getAccount(invoice.isSOTrx() ? Doc.ACCTTYPE_DiscountExp : Doc.ACCTTYPE_DiscountRev, as), getAccount(Doc.ACCTTYPE_WriteOff, as), invoice.isSOTrx())) {
                    p_Error = "Cannot create Tax correction";
                    return null;
                }
            }
        }
        //	Realized Gain & Loss
        if (invoice != null && (//	payment allocation in foreign currency
        getC_Currency_ID() != as.getC_Currency_ID() || //	allocation <> invoice currency
        getC_Currency_ID() != line.getInvoiceC_Currency_ID())) {
            p_Error = createRealizedGainLoss(line, as, fact, bpAcct, invoice, allocationSource, allocationAccounted);
            if (p_Error != null)
                return null;
        }
    }
    // FR [ 1840016 ] Avoid usage of clearing accounts - subject to C_AcctSchema.IsPostIfClearingEqual
    if ((!as.isPostIfClearingEqual()) && p_lines.length > 0 && (!isInterOrg)) {
        boolean allEquals = true;
        // more than one line (i.e. crossing one payment+ with a payment-, or an invoice against a credit memo)
        // verify if the sum of all facts is zero net
        FactLine[] factlines = fact.getLines();
        BigDecimal netBalance = Env.ZERO;
        FactLine prevFactLine = null;
        for (FactLine factLine : factlines) {
            netBalance = netBalance.add(factLine.getAmtSourceDr()).subtract(factLine.getAmtSourceCr());
            if (prevFactLine != null) {
                if (!equalFactLineIDs(prevFactLine, factLine)) {
                    allEquals = false;
                    break;
                }
            }
            prevFactLine = factLine;
        }
        if (netBalance.compareTo(Env.ZERO) == 0 && allEquals) {
            // delete the postings
            for (FactLine factline : factlines) fact.remove(factline);
        }
    }
    //	reset line info
    setC_BPartner_ID(0);
    //
    m_facts.add(fact);
    return m_facts;
}
Also used : MAccount(org.compiere.model.MAccount) MPayment(org.compiere.model.MPayment) MInvoice(org.compiere.model.MInvoice) BigDecimal(java.math.BigDecimal) MCashLine(org.compiere.model.MCashLine)

Aggregations

MCashLine (org.compiere.model.MCashLine)12 BigDecimal (java.math.BigDecimal)5 MCash (org.compiere.model.MCash)5 MPayment (org.compiere.model.MPayment)5 Timestamp (java.sql.Timestamp)4 MInvoice (org.compiere.model.MInvoice)4 KeyNamePair (org.compiere.util.KeyNamePair)4 ValueNamePair (org.compiere.util.ValueNamePair)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 MOrder (org.compiere.model.MOrder)3 SQLException (java.sql.SQLException)2 MessageFormat (java.text.MessageFormat)2 ArrayList (java.util.ArrayList)2 ListItem (org.adempiere.webui.component.ListItem)1 MAccount (org.compiere.model.MAccount)1 MAcctSchemaElement (org.compiere.model.MAcctSchemaElement)1 MCashBook (org.compiere.model.MCashBook)1 Trx (org.compiere.util.Trx)1