Search in sources :

Example 11 with MCashLine

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

the class WPayment method saveChangesInTrx.

// saveChanges
/**************************************************************************
	 *	Save Changes
	 *	@return true, if eindow can exit
	 */
private boolean saveChangesInTrx(final String trxName) {
    // set trxname for class objects
    if (m_cashLine != null)
        m_cashLine.set_TrxName(trxName);
    if (m_mPayment != null)
        m_mPayment.set_TrxName(trxName);
    if (m_mPaymentOriginal != null)
        m_mPaymentOriginal.set_TrxName(trxName);
    ValueNamePair vp = paymentCombo.getSelectedItem().toValueNamePair();
    String newPaymentRule = vp.getValue();
    log.info("New Rule: " + newPaymentRule);
    //  only Payment Rule
    if (m_onlyRule) {
        if (!newPaymentRule.equals(m_PaymentRule))
            m_mTab.setValue("PaymentRule", newPaymentRule);
        return true;
    }
    //	New Values
    Timestamp newDateAcct = m_DateAcct;
    int newC_PaymentTerm_ID = m_C_PaymentTerm_ID;
    int newC_CashLine_ID = m_C_CashLine_ID;
    int newC_CashBook_ID = m_C_CashBook_ID;
    String newCCType = m_CCType;
    int newC_BankAccount_ID = 0;
    String payTypes = m_Cash_As_Payment ? "KTSDB" : "KTSD";
    //	B (Cash)		(Currency)
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_Cash)) {
        if (m_Cash_As_Payment) {
            // get bank account
            ListItem selected = bBankAccountCombo.getSelectedItem();
            KeyNamePair kp = selected != null ? selected.toKeyNamePair() : null;
            if (kp != null)
                newC_BankAccount_ID = kp.getKey();
        } else {
            // get cash book
            ListItem selected = bCashBookCombo.getSelectedItem();
            KeyNamePair kp = selected != null ? selected.toKeyNamePair() : null;
            if (kp != null)
                newC_CashBook_ID = kp.getKey();
        }
        newDateAcct = (Timestamp) bDateField.getValue();
        // Get changes to cash amount
        m_mPayment.setAmount(m_C_Currency_ID, (BigDecimal) bAmountField.getValue());
        m_Amount = (BigDecimal) bAmountField.getValue();
    } else //	K (CreditCard)  Type, Number, Exp, Approval
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_CreditCard)) {
        ListItem selected = kTypeCombo.getSelectedItem();
        vp = selected != null ? selected.toValueNamePair() : null;
        if (vp != null)
            newCCType = vp.getValue();
    } else //	T (Transfer)	BPartner_Bank
    if (newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDeposit) || newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDebit)) {
        tAccountCombo.getSelectedItem();
    } else //	P (PaymentTerm)	PaymentTerm
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_OnCredit)) {
        ListItem selected = pTermCombo.getSelectedItem();
        KeyNamePair kp = selected != null ? selected.toKeyNamePair() : null;
        if (kp != null)
            newC_PaymentTerm_ID = kp.getKey();
    } else //	S (Check)		(Currency) CheckNo, Routing
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_Check)) {
        ListItem selected = sBankAccountCombo.getSelectedItem();
        KeyNamePair kp = selected != null ? selected.toKeyNamePair() : null;
        if (kp != null)
            newC_BankAccount_ID = kp.getKey();
    } else
        return false;
    /***********************
		 *  Changed PaymentRule
		 */
    if (!newPaymentRule.equals(m_PaymentRule)) {
        log.fine("Changed PaymentRule: " + m_PaymentRule + " -> " + newPaymentRule);
        //  We had a CashBook Entry
        if (m_PaymentRule.equals(X_C_Order.PAYMENTRULE_Cash)) {
            log.fine("Old Cash - " + m_cashLine);
            if (m_cashLine != null) {
                MCashLine cl = m_cashLine.createReversal();
                if (cl.save())
                    log.config("CashCancelled");
                else
                    FDialog.error(m_WindowNo, this, "PaymentError", "CashNotCancelled");
            }
            //  reset
            newC_CashLine_ID = 0;
        } else //  We had a change in Payment type (e.g. Check to CC)
        if (payTypes.indexOf(m_PaymentRule) != -1 && payTypes.indexOf(newPaymentRule) != -1 && m_mPaymentOriginal != null) {
            log.fine("Old Payment(1) - " + m_mPaymentOriginal);
            m_mPaymentOriginal.setDocAction(DocAction.ACTION_Reverse_Correct);
            boolean ok = m_mPaymentOriginal.processIt(DocAction.ACTION_Reverse_Correct);
            m_mPaymentOriginal.saveEx();
            if (ok)
                log.info("Payment Canecelled - " + m_mPaymentOriginal);
            else
                FDialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCancelled " + m_mPaymentOriginal.getDocumentNo());
            m_mPayment.resetNew();
        } else //	We had a Payment and something else (e.g. Check to Cash)
        if (payTypes.indexOf(m_PaymentRule) != -1 && payTypes.indexOf(newPaymentRule) == -1) {
            log.fine("Old Payment(2) - " + m_mPaymentOriginal);
            if (m_mPaymentOriginal != null) {
                m_mPaymentOriginal.setDocAction(DocAction.ACTION_Reverse_Correct);
                boolean ok = m_mPaymentOriginal.processIt(DocAction.ACTION_Reverse_Correct);
                m_mPaymentOriginal.saveEx();
                if (//  Cancel Payment
                ok) {
                    log.fine("PaymentCancelled " + m_mPayment.getDocumentNo());
                    m_mTab.getTableModel().dataSave(true);
                    m_mPayment.resetNew();
                    m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
                } else
                    FDialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCancelled " + m_mPayment.getDocumentNo());
            }
        }
    }
    //  Get Order and optionally Invoice
    int C_Order_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Order_ID");
    int C_Invoice_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Invoice_ID");
    if (C_Invoice_ID == 0 && m_DocStatus.equals("CO"))
        C_Invoice_ID = getInvoiceID(C_Order_ID);
    //  Amount sign negative, if ARC (Credit Memo) or API (AP Invoice)
    boolean negateAmt = false;
    MInvoice invoice = null;
    if (C_Invoice_ID != 0) {
        invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, null);
        negateAmt = invoice.isCreditMemo();
    }
    MOrder order = null;
    if (invoice == null && C_Order_ID != 0)
        order = new MOrder(Env.getCtx(), C_Order_ID, null);
    BigDecimal payAmount = m_Amount;
    if (negateAmt)
        payAmount = m_Amount.negate();
    // Info
    log.config("C_Order_ID=" + C_Order_ID + ", C_Invoice_ID=" + C_Invoice_ID + ", NegateAmt=" + negateAmt);
    /***********************
		 *  CashBook
		 */
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_Cash) && !m_Cash_As_Payment) {
        log.fine("Cash");
        if (C_Invoice_ID == 0 && order == null) {
            log.config("No Invoice!");
            FDialog.error(m_WindowNo, this, "PaymentError", "CashNotCreated");
        } else {
            payAmount = (BigDecimal) bAmountField.getValue();
            //  Changed Amount
            if (m_cashLine != null && payAmount.compareTo(m_cashLine.getAmount()) != 0) {
                log.config("Changed CashBook Amount");
                m_cashLine.setAmount((BigDecimal) bAmountField.getValue());
                m_cashLine.saveEx();
            }
            //	Different Date/CashBook
            if (m_cashLine != null && (newC_CashBook_ID != m_C_CashBook_ID || !TimeUtil.isSameDay(m_cashLine.getStatementDate(), newDateAcct))) {
                log.config("Changed CashBook/Date: " + m_C_CashBook_ID + "->" + newC_CashBook_ID);
                MCashLine reverse = m_cashLine.createReversal();
                reverse.saveEx();
                m_cashLine = null;
            }
            //	Create new
            if (m_cashLine == null) {
                log.config("New CashBook");
                int C_Currency_ID = 0;
                if (invoice != null)
                    C_Currency_ID = invoice.getC_Currency_ID();
                if (C_Currency_ID == 0 && order != null)
                    C_Currency_ID = order.getC_Currency_ID();
                MCash cash = null;
                if (newC_CashBook_ID != 0)
                    cash = MCash.get(Env.getCtx(), newC_CashBook_ID, newDateAcct, null);
                else
                    //	Default
                    cash = MCash.get(Env.getCtx(), m_AD_Org_ID, newDateAcct, C_Currency_ID, null);
                if (cash == null || cash.get_ID() == 0)
                    FDialog.error(m_WindowNo, this, "PaymentError", CLogger.retrieveErrorString("CashNotCreated"));
                else {
                    MCashLine cl = new MCashLine(cash);
                    //ADialog.info(m_WindowNo, this, "m_cashLine - New Cashbook", "Amount: "+cl.getAmount());
                    if (invoice != null)
                        // overrides amount
                        cl.setInvoice(invoice);
                    if (order != null) {
                        // overrides amount
                        cl.setOrder(order, null);
                        m_needSave = true;
                    }
                    cl.setAmount((BigDecimal) bAmountField.getValue());
                    cl.saveEx();
                    log.config("CashCreated");
                    if (invoice == null && C_Invoice_ID != 0) {
                        invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, null);
                    }
                    if (invoice != null) {
                        invoice.setC_CashLine_ID(cl.getC_CashLine_ID());
                        invoice.saveEx(trxName);
                    }
                    if (order == null && C_Order_ID != 0) {
                        order = new MOrder(Env.getCtx(), C_Order_ID, null);
                    }
                    if (order != null) {
                        order.setC_CashLine_ID(cl.getC_CashLine_ID());
                        order.saveEx(trxName);
                    }
                    log.config("Update Order & Invoice with CashLine");
                }
            }
        }
    //	have invoice
    }
    /***********************
		 *  Payments
		 */
    if (("KS".indexOf(newPaymentRule) != -1) || (newPaymentRule.equals(MOrder.PAYMENTRULE_Cash) && m_Cash_As_Payment)) {
        log.fine("Payment - " + newPaymentRule);
        //  Set Amount
        m_mPayment.setAmount(m_C_Currency_ID, payAmount);
        if (newPaymentRule.equals(MOrder.PAYMENTRULE_CreditCard)) {
            m_mPayment.setCreditCard(MPayment.TRXTYPE_Sales, newCCType, kNumberField.getText(), "", kExpField.getText());
            // Get changes to credit card amount
            m_mPayment.setAmount(m_C_Currency_ID, (BigDecimal) kAmountField.getValue());
            m_mPayment.setPaymentProcessor();
        } else if (newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDeposit) || newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDebit)) {
            m_mPayment.setBankACH(newC_BankAccount_ID, m_isSOTrx, newPaymentRule, tRoutingField.getText(), tNumberField.getText());
            m_mPayment.setAmount(m_C_Currency_ID, payAmount);
        } else if (newPaymentRule.equals(MOrder.PAYMENTRULE_Check)) {
            m_mPayment.setBankCheck(newC_BankAccount_ID, m_isSOTrx, sRoutingField.getText(), sNumberField.getText(), sCheckField.getText());
            // Get changes to check amount
            m_mPayment.setAmount(m_C_Currency_ID, (BigDecimal) sAmountField.getValue());
        } else if (newPaymentRule.equals(MOrder.PAYMENTRULE_Cash)) {
            // Get changes to cash amount
            m_mPayment.setTenderType(MPayment.TENDERTYPE_Cash);
            m_mPayment.setBankCash(newC_BankAccount_ID, m_isSOTrx, MPayment.TENDERTYPE_Cash);
            m_mPayment.setAmount(m_C_Currency_ID, payAmount);
        }
        m_mPayment.setC_BPartner_ID(m_C_BPartner_ID);
        m_mPayment.setC_Invoice_ID(C_Invoice_ID);
        if (order != null) {
            m_mPayment.setC_Order_ID(C_Order_ID);
            m_needSave = true;
        }
        m_mPayment.setDateTrx(m_DateAcct);
        m_mPayment.setDateAcct(m_DateAcct);
        m_mPayment.saveEx();
        //  Save/Post
        if (m_mPayment.get_ID() > 0 && MPayment.DOCSTATUS_Drafted.equals(m_mPayment.getDocStatus())) {
            boolean ok = m_mPayment.processIt(DocAction.ACTION_Complete);
            m_mPayment.saveEx();
            if (ok)
                FDialog.info(m_WindowNo, this, "PaymentCreated", m_mPayment.getDocumentNo());
            else
                FDialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCreated");
        } else
            log.fine("NotDraft " + m_mPayment);
    }
    /**********************
		 *	Save Values to mTab
		 */
    log.config("Saving changes");
    //
    if (!newPaymentRule.equals(m_PaymentRule))
        m_mTab.setValue("PaymentRule", newPaymentRule);
    //
    if (!newDateAcct.equals(m_DateAcct))
        m_mTab.setValue("DateAcct", newDateAcct);
    //
    if (newC_PaymentTerm_ID != m_C_PaymentTerm_ID)
        m_mTab.setValue("C_PaymentTerm_ID", new Integer(newC_PaymentTerm_ID));
    //	Set Payment
    if (m_mPayment.getC_Payment_ID() != m_C_Payment_ID) {
        if (m_mPayment.getC_Payment_ID() == 0)
            m_mTab.setValue("C_Payment_ID", null);
        else
            m_mTab.setValue("C_Payment_ID", new Integer(m_mPayment.getC_Payment_ID()));
    }
    //	Set Cash
    if (newC_CashLine_ID != m_C_CashLine_ID) {
        if (newC_CashLine_ID == 0)
            m_mTab.setValue("C_CashLine_ID", null);
        else
            m_mTab.setValue("C_CashLine_ID", new Integer(newC_CashLine_ID));
    }
    return true;
}
Also used : MInvoice(org.compiere.model.MInvoice) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MCashLine(org.compiere.model.MCashLine) MOrder(org.compiere.model.MOrder) ValueNamePair(org.compiere.util.ValueNamePair) ListItem(org.adempiere.webui.component.ListItem) KeyNamePair(org.compiere.util.KeyNamePair) MCash(org.compiere.model.MCash)

Example 12 with MCashLine

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

the class VPayment method saveChangesInTrx.

// saveChanges
private boolean saveChangesInTrx(final String trxName) {
    // set trxname for class objects
    if (m_cashLine != null)
        m_cashLine.set_TrxName(trxName);
    if (m_mPayment != null)
        m_mPayment.set_TrxName(trxName);
    if (m_mPaymentOriginal != null)
        m_mPaymentOriginal.set_TrxName(trxName);
    ValueNamePair vp = (ValueNamePair) paymentCombo.getSelectedItem();
    String newPaymentRule = vp.getValue();
    log.info("New Rule: " + newPaymentRule);
    //  only Payment Rule
    if (m_onlyRule) {
        if (!newPaymentRule.equals(m_PaymentRule))
            m_mTab.setValue("PaymentRule", newPaymentRule);
        return true;
    }
    //	New Values
    Timestamp newDateAcct = m_DateAcct;
    int newC_PaymentTerm_ID = m_C_PaymentTerm_ID;
    int newC_CashLine_ID = m_C_CashLine_ID;
    int newC_CashBook_ID = m_C_CashBook_ID;
    String newCCType = m_CCType;
    int newC_BankAccount_ID = 0;
    String payTypes = m_Cash_As_Payment ? "KTSDB" : "KTSD";
    //	B (Cash)		(Currency)
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_Cash)) {
        if (m_Cash_As_Payment) {
            // get bank account
            KeyNamePair kp = (KeyNamePair) sBankAccountCombo.getSelectedItem();
            if (kp != null)
                newC_BankAccount_ID = kp.getKey();
        } else {
            // get cash book
            KeyNamePair kp = (KeyNamePair) bCashBookCombo.getSelectedItem();
            if (kp != null)
                newC_CashBook_ID = kp.getKey();
        }
        newDateAcct = (Timestamp) bDateField.getValue();
        // Get changes to cash amount
        m_mPayment.setAmount(m_C_Currency_ID, (BigDecimal) bAmountField.getValue());
        m_Amount = (BigDecimal) bAmountField.getValue();
    } else //	K (CreditCard)  Type, Number, Exp, Approval
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_CreditCard)) {
        vp = (ValueNamePair) kTypeCombo.getSelectedItem();
        if (vp != null)
            newCCType = vp.getValue();
    } else //	T (Transfer)	BPartner_Bank
    if (newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDeposit) || newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDebit)) {
        tAccountCombo.getSelectedItem();
    } else //	P (PaymentTerm)	PaymentTerm
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_OnCredit)) {
        KeyNamePair kp = (KeyNamePair) pTermCombo.getSelectedItem();
        if (kp != null)
            newC_PaymentTerm_ID = kp.getKey();
    } else //	S (Check)		(Currency) CheckNo, Routing
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_Check)) {
        //	sCurrencyCombo.getSelectedItem();
        KeyNamePair kp = (KeyNamePair) sBankAccountCombo.getSelectedItem();
        if (kp != null)
            newC_BankAccount_ID = kp.getKey();
    } else
        return false;
    /***********************
		 *  Changed PaymentRule
		 */
    if (!newPaymentRule.equals(m_PaymentRule)) {
        log.fine("Changed PaymentRule: " + m_PaymentRule + " -> " + newPaymentRule);
        //  We had a CashBook Entry
        if (m_PaymentRule.equals(X_C_Order.PAYMENTRULE_Cash) && !m_Cash_As_Payment) {
            log.fine("Old Cash - " + m_cashLine);
            if (m_cashLine != null) {
                MCashLine cl = m_cashLine.createReversal();
                cl.saveEx();
            }
            //  reset
            newC_CashLine_ID = 0;
        } else //  We had a change in Payment type (e.g. Check to CC)
        if (payTypes.indexOf(m_PaymentRule) != -1 && payTypes.indexOf(newPaymentRule) != -1 && m_mPaymentOriginal != null) {
            log.fine("Old Payment(1) - " + m_mPaymentOriginal);
            m_mPaymentOriginal.setDocAction(DocAction.ACTION_Reverse_Correct);
            boolean ok = m_mPaymentOriginal.processIt(DocAction.ACTION_Reverse_Correct);
            m_mPaymentOriginal.saveEx();
            if (ok)
                log.info("Payment Cancelled - " + m_mPaymentOriginal);
            else
                ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCancelled " + m_mPaymentOriginal.getDocumentNo());
            m_mPayment.resetNew();
        } else //	We had a Payment and something else (e.g. Check to Cash)
        if (payTypes.indexOf(m_PaymentRule) != -1 && payTypes.indexOf(newPaymentRule) == -1) {
            log.fine("Old Payment(2) - " + m_mPaymentOriginal);
            if (m_mPaymentOriginal != null) {
                m_mPaymentOriginal.setDocAction(DocAction.ACTION_Reverse_Correct);
                boolean ok = m_mPaymentOriginal.processIt(DocAction.ACTION_Reverse_Correct);
                m_mPaymentOriginal.saveEx();
                if (//  Cancel Payment
                ok) {
                    log.fine("PaymentCancelled " + m_mPayment.getDocumentNo());
                    m_mTab.getTableModel().dataSave(true);
                    m_mPayment.resetNew();
                    m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
                } else
                    ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCancelled " + m_mPayment.getDocumentNo());
            }
        }
    }
    //  Get Order and optionally Invoice
    int C_Order_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Order_ID");
    int C_Invoice_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Invoice_ID");
    if (C_Invoice_ID == 0 && m_DocStatus.equals("CO"))
        C_Invoice_ID = getInvoiceID(C_Order_ID, trxName);
    //  Amount sign negative, if ARC (Credit Memo) or API (AP Invoice)
    boolean negateAmt = false;
    MInvoice invoice = null;
    if (C_Invoice_ID != 0) {
        invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, trxName);
        negateAmt = invoice.isCreditMemo();
    }
    MOrder order = null;
    if (invoice == null && C_Order_ID != 0)
        order = new MOrder(Env.getCtx(), C_Order_ID, trxName);
    BigDecimal payAmount = m_Amount;
    if (negateAmt)
        payAmount = m_Amount.negate();
    // Info
    log.config("C_Order_ID=" + C_Order_ID + ", C_Invoice_ID=" + C_Invoice_ID + ", NegateAmt=" + negateAmt);
    /***********************
		 *  CashBook
		 */
    if (newPaymentRule.equals(X_C_Order.PAYMENTRULE_Cash) && !m_Cash_As_Payment) {
        log.fine("Cash");
        if (C_Invoice_ID == 0 && order == null) {
            log.config("No Invoice!");
            ADialog.error(m_WindowNo, this, "PaymentError", "CashNotCreated");
        } else {
            payAmount = (BigDecimal) bAmountField.getValue();
            //  Changed Amount
            if (m_cashLine != null && payAmount.compareTo(m_cashLine.getAmount()) != 0) {
                log.config("Changed CashBook Amount");
                //m_cashLine.setAmount(payAmount);
                m_cashLine.setAmount((BigDecimal) bAmountField.getValue());
                // ADialog.info(m_WindowNo, this, "m_cashLine - Changed Amount", "Amount: "+m_cashLine.getAmount());
                m_cashLine.saveEx();
                log.config("CashAmt Changed");
            }
            //	Different Date/CashBook
            if (m_cashLine != null && (newC_CashBook_ID != m_C_CashBook_ID || !TimeUtil.isSameDay(m_cashLine.getStatementDate(), newDateAcct))) {
                log.config("Changed CashBook/Date: " + m_C_CashBook_ID + "->" + newC_CashBook_ID);
                MCashLine reverse = m_cashLine.createReversal();
                reverse.saveEx();
                m_cashLine = null;
            }
            //	Create new
            if (m_cashLine == null) {
                log.config("New CashBook");
                int C_Currency_ID = 0;
                if (invoice != null)
                    C_Currency_ID = invoice.getC_Currency_ID();
                if (C_Currency_ID == 0 && order != null)
                    C_Currency_ID = order.getC_Currency_ID();
                MCash cash = null;
                if (newC_CashBook_ID != 0)
                    cash = MCash.get(Env.getCtx(), newC_CashBook_ID, newDateAcct, trxName);
                else
                    //	Default
                    cash = MCash.get(Env.getCtx(), m_AD_Org_ID, newDateAcct, C_Currency_ID, trxName);
                if (cash == null || cash.get_ID() == 0)
                    ADialog.error(m_WindowNo, this, "PaymentError", CLogger.retrieveErrorString("CashNotCreated"));
                else {
                    MCashLine cl = new MCashLine(cash);
                    //ADialog.info(m_WindowNo, this, "m_cashLine - New Cashbook", "Amount: "+cl.getAmount());
                    if (invoice != null)
                        // overrides amount
                        cl.setInvoice(invoice);
                    if (order != null) {
                        // overrides amount
                        cl.setOrder(order, trxName);
                        m_needSave = true;
                    }
                    cl.setAmount((BigDecimal) bAmountField.getValue());
                    cl.saveEx();
                    log.config("CashCreated");
                    if (invoice == null && C_Invoice_ID != 0) {
                        invoice = new MInvoice(Env.getCtx(), C_Invoice_ID, trxName);
                    }
                    if (invoice != null) {
                        invoice.setC_CashLine_ID(cl.getC_CashLine_ID());
                        invoice.saveEx(trxName);
                    }
                    if (order == null && C_Order_ID != 0) {
                        order = new MOrder(Env.getCtx(), C_Order_ID, trxName);
                    }
                    if (order != null) {
                        order.setC_CashLine_ID(cl.getC_CashLine_ID());
                        order.saveEx(trxName);
                    }
                    log.config("Update Order & Invoice with CashLine");
                }
            }
        }
    //	have invoice
    }
    /***********************
		 *  Payments
		 */
    if (("KS".indexOf(newPaymentRule) != -1) || (newPaymentRule.equals(MOrder.PAYMENTRULE_Cash) && m_Cash_As_Payment)) {
        log.fine("Payment - " + newPaymentRule);
        //  Set Amount
        m_mPayment.setAmount(m_C_Currency_ID, payAmount);
        if (newPaymentRule.equals(MOrder.PAYMENTRULE_CreditCard)) {
            m_mPayment.setCreditCard(MPayment.TRXTYPE_Sales, newCCType, kNumberField.getText(), "", kExpField.getText());
            m_mPayment.setA_Name(kNameField.getText());
            // Get changes to credit card amount
            m_mPayment.setAmount(m_C_Currency_ID, (BigDecimal) kAmountField.getValue());
            m_mPayment.setPaymentProcessor();
        } else if (newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDeposit) || newPaymentRule.equals(MOrder.PAYMENTRULE_DirectDebit)) {
            m_mPayment.setBankACH(newC_BankAccount_ID, m_isSOTrx, newPaymentRule, tRoutingField.getText(), tNumberField.getText());
            m_mPayment.setAmount(m_C_Currency_ID, payAmount);
        } else if (newPaymentRule.equals(MOrder.PAYMENTRULE_Check)) {
            m_mPayment.setBankCheck(newC_BankAccount_ID, m_isSOTrx, sRoutingField.getText(), sNumberField.getText(), sCheckField.getText());
            // Get changes to check amount
            m_mPayment.setAmount(m_C_Currency_ID, (BigDecimal) sAmountField.getValue());
        } else if (newPaymentRule.equals(MOrder.PAYMENTRULE_Cash)) {
            // Get changes to cash amount
            m_mPayment.setTenderType(MPayment.TENDERTYPE_Cash);
            m_mPayment.setBankCash(newC_BankAccount_ID, m_isSOTrx, MPayment.TENDERTYPE_Cash);
            m_mPayment.setAmount(m_C_Currency_ID, payAmount);
        }
        m_mPayment.setC_BPartner_ID(m_C_BPartner_ID);
        m_mPayment.setC_Invoice_ID(C_Invoice_ID);
        if (order != null) {
            m_mPayment.setC_Order_ID(C_Order_ID);
            m_needSave = true;
        }
        m_mPayment.setDateTrx(m_DateAcct);
        m_mPayment.setDateAcct(m_DateAcct);
        m_mPayment.saveEx();
        //  Save/Post
        if (m_mPayment.get_ID() > 0 && MPayment.DOCSTATUS_Drafted.equals(m_mPayment.getDocStatus())) {
            boolean ok = m_mPayment.processIt(DocAction.ACTION_Complete);
            m_mPayment.saveEx();
            if (ok)
                ADialog.info(m_WindowNo, this, "PaymentCreated", m_mPayment.getDocumentNo());
            else
                ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCreated");
        } else
            log.fine("NotDraft " + m_mPayment);
    }
    /**********************
		 *	Save Values to mTab
		 */
    log.config("Saving changes");
    //
    if (!newPaymentRule.equals(m_PaymentRule))
        m_mTab.setValue("PaymentRule", newPaymentRule);
    //
    if (!newDateAcct.equals(m_DateAcct))
        m_mTab.setValue("DateAcct", newDateAcct);
    //
    if (newC_PaymentTerm_ID != m_C_PaymentTerm_ID)
        m_mTab.setValue("C_PaymentTerm_ID", new Integer(newC_PaymentTerm_ID));
    //	Set Payment
    if (m_mPayment.getC_Payment_ID() != m_C_Payment_ID) {
        if (m_mPayment.getC_Payment_ID() == 0)
            m_mTab.setValue("C_Payment_ID", null);
        else
            m_mTab.setValue("C_Payment_ID", new Integer(m_mPayment.getC_Payment_ID()));
    }
    //	Set Cash
    if (newC_CashLine_ID != m_C_CashLine_ID) {
        if (newC_CashLine_ID == 0)
            m_mTab.setValue("C_CashLine_ID", null);
        else
            m_mTab.setValue("C_CashLine_ID", new Integer(newC_CashLine_ID));
    }
    return true;
}
Also used : MOrder(org.compiere.model.MOrder) MInvoice(org.compiere.model.MInvoice) ValueNamePair(org.compiere.util.ValueNamePair) KeyNamePair(org.compiere.util.KeyNamePair) Timestamp(java.sql.Timestamp) MCash(org.compiere.model.MCash) 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