Beispiel #1
0
        /**
         *  Confirm Print.
         *  Create Payments the first time
         *  @param checks checks
         *  @param batch batch
         *  @return last Document number or 0 if nothing printed
         */
        public static int ConfirmPrint(MPaySelectionCheck[] checks, MPaymentBatch batch)
        {
            int lastDocumentNo = 0;

            for (int i = 0; i < checks.Length; i++)
            {
                MPaySelectionCheck check   = checks[i];
                MPayment           payment = new MPayment(check.GetCtx(), check.GetC_Payment_ID(), null);
                //	Existing Payment
                if (check.GetC_Payment_ID() != 0)
                {
                    //	Update check number
                    if (check.GetPaymentRule().Equals(PAYMENTRULE_Check))
                    {
                        payment.SetCheckNo(check.GetDocumentNo());
                        if (!payment.Save())
                        {
                            _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                        }
                    }
                }
                else    //	New Payment
                {
                    int _CountVA009     = Util.GetValueOfInt(DB.ExecuteScalar("SELECT COUNT(AD_MODULEINFO_ID) FROM AD_MODULEINFO WHERE PREFIX='VA009_'  AND IsActive = 'Y'"));
                    int _CountColumnIPS = Util.GetValueOfInt(DB.ExecuteScalar("SELECT COUNT(AD_Column_ID) FROM AD_Column WHERE Columnname='C_InvoicePaySchedule_ID' AND AD_Table_ID=(SELECT AD_Table_ID FROM AD_Table WHERE tablename = 'C_PaySelectionLine' and export_id='VIS_427')"));
                    if (_CountVA009 > 0)
                    {
                        #region IF VA009 (VA Payment management is installed)

                        payment = new MPayment(check.GetCtx(), 0, null);
                        payment.SetAD_Org_ID(check.GetAD_Org_ID());
                        //
                        if (check.GetPaymentRule().Equals(PAYMENTRULE_Check))
                        {
                            payment.SetBankCheck(check.GetParent().GetC_BankAccount_ID(), false, check.GetDocumentNo());
                        }
                        else if (check.GetPaymentRule().Equals(PAYMENTRULE_CreditCard))
                        {
                            payment.SetTenderType(X_C_Payment.TENDERTYPE_CreditCard);
                        }
                        else if (check.GetPaymentRule().Equals(PAYMENTRULE_DirectDeposit) ||
                                 check.GetPaymentRule().Equals(PAYMENTRULE_DirectDebit))
                        {
                            payment.SetBankACH(check);
                        }
                        else
                        {
                            _log.Log(Level.SEVERE, "Unsupported Payment Rule=" + check.GetPaymentRule());
                            continue;
                        }
                        payment.SetTrxType(X_C_Payment.TRXTYPE_CreditPayment);
                        payment.SetAmount(check.GetParent().GetC_Currency_ID(), check.GetPayAmt());
                        payment.SetDiscountAmt(check.GetDiscountAmt());
                        payment.SetDateTrx(check.GetParent().GetPayDate());
                        payment.SetC_BPartner_ID(check.GetC_BPartner_ID());
                        //	Link to Batch
                        if (batch != null)
                        {
                            if (batch.GetC_PaymentBatch_ID() == 0)
                            {
                                batch.Save();   //	new
                            }
                            payment.SetC_PaymentBatch_ID(batch.GetC_PaymentBatch_ID());
                        }
                        //	Link to Invoice
                        MPaySelectionLine[] psls = check.GetPaySelectionLines(false);
                        _log.Fine("confirmPrint - " + check + " (#SelectionLines=" + psls.Length + ")");
                        if (check.GetQty() == 1 && psls != null && psls.Length == 1)
                        {
                            MPaySelectionLine psl = psls[0];
                            _log.Fine("Map to Invoice " + psl);
                            //
                            payment.SetC_Invoice_ID(psl.GetC_Invoice_ID());
                            if (_CountColumnIPS > 0)
                            {
                                payment.SetC_InvoicePaySchedule_ID(psl.GetC_InvoicePaySchedule_ID());
                            }
                            payment.SetDiscountAmt(psl.GetDiscountAmt());
                            if (psl.GetDifferenceAmt() > 0)
                            {
                                payment.SetWriteOffAmt(psl.GetDifferenceAmt());
                            }
                            else
                            {
                                payment.SetOverUnderAmt(psl.GetDifferenceAmt());
                            }
                        }
                        else
                        {
                            payment.SetDiscountAmt(Env.ZERO);
                        }
                        payment.SetWriteOffAmt(Env.ZERO);

                        if (psls.Length == 1)
                        {
                            MPaySelectionLine psl = psls[0];
                            if (_CountColumnIPS > 0)
                            {
                                payment.SetC_InvoicePaySchedule_ID(psl.GetC_InvoicePaySchedule_ID());
                            }
                            payment.SetC_Invoice_ID(psl.GetC_Invoice_ID());
                            payment.SetDiscountAmt(psl.GetDiscountAmt());
                            if (psl.GetDifferenceAmt() > 0)
                            {
                                payment.SetWriteOffAmt(psl.GetDifferenceAmt());
                            }
                            else
                            {
                                payment.SetOverUnderAmt(psl.GetDifferenceAmt());
                            }
                        }

                        if (!payment.Save())
                        {
                            _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                        }
                        //
                        int C_Payment_ID = payment.Get_ID();
                        if (C_Payment_ID < 1)
                        {
                            _log.Log(Level.SEVERE, "Payment not created=" + check);
                        }
                        else
                        {
                            check.SetC_Payment_ID(C_Payment_ID);
                            check.Save();       //	Payment process needs it
                            //	Should start WF
                            payment.ProcessIt(DocActionVariables.ACTION_COMPLETE);
                            if (!payment.Save())
                            {
                                _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                            }
                        }

                        #endregion
                    }
                    else
                    {
                        payment = new MPayment(check.GetCtx(), 0, null);
                        payment.SetAD_Org_ID(check.GetAD_Org_ID());
                        //
                        if (check.GetPaymentRule().Equals(PAYMENTRULE_Check))
                        {
                            payment.SetBankCheck(check.GetParent().GetC_BankAccount_ID(), false, check.GetDocumentNo());
                        }
                        else if (check.GetPaymentRule().Equals(PAYMENTRULE_CreditCard))
                        {
                            payment.SetTenderType(X_C_Payment.TENDERTYPE_CreditCard);
                        }
                        else if (check.GetPaymentRule().Equals(PAYMENTRULE_DirectDeposit) ||
                                 check.GetPaymentRule().Equals(PAYMENTRULE_DirectDebit))
                        {
                            payment.SetBankACH(check);
                        }
                        else
                        {
                            _log.Log(Level.SEVERE, "Unsupported Payment Rule=" + check.GetPaymentRule());
                            continue;
                        }
                        payment.SetTrxType(X_C_Payment.TRXTYPE_CreditPayment);
                        payment.SetAmount(check.GetParent().GetC_Currency_ID(), check.GetPayAmt());
                        payment.SetDiscountAmt(check.GetDiscountAmt());
                        payment.SetDateTrx(check.GetParent().GetPayDate());
                        payment.SetC_BPartner_ID(check.GetC_BPartner_ID());
                        //	Link to Batch
                        if (batch != null)
                        {
                            if (batch.GetC_PaymentBatch_ID() == 0)
                            {
                                batch.Save();   //	new
                            }
                            payment.SetC_PaymentBatch_ID(batch.GetC_PaymentBatch_ID());
                        }
                        //	Link to Invoice
                        MPaySelectionLine[] psls = check.GetPaySelectionLines(false);
                        _log.Fine("confirmPrint - " + check + " (#SelectionLines=" + psls.Length + ")");
                        if (check.GetQty() == 1 && psls != null && psls.Length == 1)
                        {
                            MPaySelectionLine psl = psls[0];
                            _log.Fine("Map to Invoice " + psl);
                            //
                            payment.SetC_Invoice_ID(psl.GetC_Invoice_ID());
                            payment.SetDiscountAmt(psl.GetDiscountAmt());
                            payment.SetWriteOffAmt(psl.GetDifferenceAmt());
                            Decimal overUnder = Decimal.Subtract(Decimal.Subtract(Decimal.Subtract(psl.GetOpenAmt(), psl.GetPayAmt()), psl.GetDiscountAmt()), psl.GetDifferenceAmt());
                            //Decimal overUnder = psl.GetOpenAmt().subtract(psl.GetPayAmt())
                            //	.subtract(psl.GetDiscountAmt()).subtract(psl.GetDifferenceAmt());
                            payment.SetOverUnderAmt(overUnder);
                        }
                        else
                        {
                            payment.SetDiscountAmt(Env.ZERO);
                        }
                        payment.SetWriteOffAmt(Env.ZERO);
                        if (!payment.Save())
                        {
                            _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                        }
                        //
                        int C_Payment_ID = payment.Get_ID();
                        if (C_Payment_ID < 1)
                        {
                            _log.Log(Level.SEVERE, "Payment not created=" + check);
                        }
                        else
                        {
                            check.SetC_Payment_ID(C_Payment_ID);
                            check.Save();       //	Payment process needs it
                            //	Should start WF
                            payment.ProcessIt(DocActionVariables.ACTION_COMPLETE);
                            if (!payment.Save())
                            {
                                _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                            }
                        }
                    }
                }//	new Payment

                //	Get Check Document No
                try
                {
                    int no = int.Parse(check.GetDocumentNo());
                    if (lastDocumentNo < no)
                    {
                        lastDocumentNo = no;
                    }
                }
                catch (FormatException ex)
                {
                    _log.Log(Level.SEVERE, "DocumentNo=" + check.GetDocumentNo(), ex);
                }
                check.SetIsPrinted(true);
                check.SetProcessed(true);
                if (!check.Save())
                {
                    _log.Log(Level.SEVERE, "Check not saved: " + check);
                }
            }   //	all checks

            _log.Fine("Last Document No = " + lastDocumentNo);
            return(lastDocumentNo);
        }
        /**
         *  Confirm Print.
         *  Create Payments the first time
         *  @param checks checks
         *  @param batch batch
         *  @return last Document number or 0 if nothing printed
         */
        public static int ConfirmPrint(MPaySelectionCheck[] checks, MPaymentBatch batch)
        {
            int lastDocumentNo = 0;

            for (int i = 0; i < checks.Length; i++)
            {
                MPaySelectionCheck check   = checks[i];
                MPayment           payment = new MPayment(check.GetCtx(), check.GetC_Payment_ID(), null);
                //	Existing Payment
                if (check.GetC_Payment_ID() != 0)
                {
                    //	Update check number
                    if (check.GetPaymentRule().Equals(PAYMENTRULE_Check))
                    {
                        payment.SetCheckNo(check.GetDocumentNo());
                        if (!payment.Save())
                        {
                            _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                        }
                    }
                }
                else    //	New Payment
                {
                    payment = new MPayment(check.GetCtx(), 0, null);
                    payment.SetAD_Org_ID(check.GetAD_Org_ID());
                    //
                    if (check.GetPaymentRule().Equals(PAYMENTRULE_Check))
                    {
                        payment.SetBankCheck(check.GetParent().GetC_BankAccount_ID(), false, check.GetDocumentNo());
                    }
                    else if (check.GetPaymentRule().Equals(PAYMENTRULE_CreditCard))
                    {
                        payment.SetTenderType(X_C_Payment.TENDERTYPE_CreditCard);
                    }
                    else if (check.GetPaymentRule().Equals(PAYMENTRULE_DirectDeposit) ||
                             check.GetPaymentRule().Equals(PAYMENTRULE_DirectDebit))
                    {
                        payment.SetBankACH(check);
                    }
                    else
                    {
                        _log.Log(Level.SEVERE, "Unsupported Payment Rule=" + check.GetPaymentRule());
                        continue;
                    }
                    payment.SetTrxType(X_C_Payment.TRXTYPE_CreditPayment);
                    payment.SetAmount(check.GetParent().GetC_Currency_ID(), check.GetPayAmt());
                    payment.SetDiscountAmt(check.GetDiscountAmt());
                    payment.SetDateTrx(check.GetParent().GetPayDate());
                    payment.SetC_BPartner_ID(check.GetC_BPartner_ID());
                    //	Link to Batch
                    if (batch != null)
                    {
                        if (batch.GetC_PaymentBatch_ID() == 0)
                        {
                            batch.Save();       //	new
                        }
                        payment.SetC_PaymentBatch_ID(batch.GetC_PaymentBatch_ID());
                    }
                    //	Link to Invoice
                    MPaySelectionLine[] psls = check.GetPaySelectionLines(false);
                    _log.Fine("confirmPrint - " + check + " (#SelectionLines=" + psls.Length + ")");
                    if (check.GetQty() == 1 && psls != null && psls.Length == 1)
                    {
                        MPaySelectionLine psl = psls[0];
                        _log.Fine("Map to Invoice " + psl);
                        //
                        payment.SetC_Invoice_ID(psl.GetC_Invoice_ID());
                        payment.SetDiscountAmt(psl.GetDiscountAmt());
                        payment.SetWriteOffAmt(psl.GetDifferenceAmt());
                        Decimal overUnder = Decimal.Subtract(Decimal.Subtract(Decimal.Subtract(psl.GetOpenAmt(), psl.GetPayAmt()), psl.GetDiscountAmt()), psl.GetDifferenceAmt());
                        //Decimal overUnder = psl.GetOpenAmt().subtract(psl.GetPayAmt())
                        //	.subtract(psl.GetDiscountAmt()).subtract(psl.GetDifferenceAmt());
                        payment.SetOverUnderAmt(overUnder);
                    }
                    else
                    {
                        payment.SetDiscountAmt(Env.ZERO);
                    }
                    payment.SetWriteOffAmt(Env.ZERO);
                    if (!payment.Save())
                    {
                        _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                    }
                    //
                    int C_Payment_ID = payment.Get_ID();
                    if (C_Payment_ID < 1)
                    {
                        _log.Log(Level.SEVERE, "Payment not created=" + check);
                    }
                    else
                    {
                        check.SetC_Payment_ID(C_Payment_ID);
                        check.Save();   //	Payment process needs it
                        //	Should start WF
                        payment.ProcessIt(DocActionVariables.ACTION_COMPLETE);
                        if (!payment.Save())
                        {
                            _log.Log(Level.SEVERE, "Payment not saved: " + payment);
                        }
                    }
                }       //	new Payment

                //	Get Check Document No
                try
                {
                    int no = int.Parse(check.GetDocumentNo());
                    if (lastDocumentNo < no)
                    {
                        lastDocumentNo = no;
                    }
                }
                catch (FormatException ex)
                {
                    _log.Log(Level.SEVERE, "DocumentNo=" + check.GetDocumentNo(), ex);
                }
                check.SetIsPrinted(true);
                check.SetProcessed(true);
                if (!check.Save())
                {
                    _log.Log(Level.SEVERE, "Check not saved: " + check);
                }
            }   //	all checks

            _log.Fine("Last Document No = " + lastDocumentNo);
            return(lastDocumentNo);
        }