コード例 #1
0
        public PaymentMetohdDetails DynInit()
        {
            pDetails = new PaymentMetohdDetails();

            SetPayments(_C_Payment_ID, _C_Currency_ID, _PaymentRule);


            if (_C_CashLine_ID == 0)
            {
                _cashLine = null;
            }
            else
            {
                _cashLine          = new MCashLine(ctx, _C_CashLine_ID, null);
                pDetails._DateAcct = DateTime.SpecifyKind(Convert.ToDateTime(_cashLine.GetStatementDate()), DateTimeKind.Utc);
            }
            if (pDetails._Currencies == null)
            {
                LoadCurrencies();
            }



            //Load Payment Terms
            String SQL = MRole.GetDefault(ctx).AddAccessSQL(
                "SELECT C_PaymentTerm_ID, Name FROM C_PaymentTerm WHERE IsActive='Y' ORDER BY Name",
                "C_PaymentTerm", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);

            IDataReader idr = null;
            DataTable   dt  = null;

            try
            {
                idr = DB.ExecuteReader(SQL, null, null);
                dt  = new DataTable();
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    int         key  = Util.GetValueOfInt(dr[0]); //.getInt(1);
                    String      name = dr[1].ToString();          //.getString(2);
                    KeyNamePair pp   = new KeyNamePair(key, name);
                    //cmbPTerm.Items.Add(pp);
                    if (pDetails.loadPaymentTerms == null)
                    {
                        pDetails.loadPaymentTerms = new List <KeyNamePair>();
                    }
                    pDetails.loadPaymentTerms.Add(pp);

                    if (key == _C_PaymentTerm_ID)
                    {
                        kp = pp;
                    }
                }
            }
            catch (Exception)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                // log.Log(Level.SEVERE, SQL, ept);
            }
            finally
            {
                if (idr != null)
                {
                    idr.Close();
                }
                dt = null;
            }


            //Load Accounts
            SQL = "SELECT a.C_BP_BankAccount_ID, NVL(b.Name, ' ')||a.AccountNo AS Acct "
                  + "FROM C_BP_BankAccount a,C_Bank b "
                  + "WHERE C_BPartner_ID=" + _C_BPartner_ID + " AND a.IsActive='Y'";
            kp = null;
            IDataReader idr1 = null;

            try
            {
                idr1 = DB.ExecuteReader(SQL, null, null);
                dt   = new DataTable();
                dt.Load(idr1);
                idr1.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    int         key  = Util.GetValueOfInt(dr[0]); // dr.getInt(1);
                    String      name = dr[1].ToString();          //.getString(2);
                    KeyNamePair pp   = new KeyNamePair(key, name);
                    // cmbTAccount.Items.Add(pp);

                    if (pDetails.loadAccounts == null)
                    {
                        pDetails.loadAccounts = new List <KeyNamePair>();
                    }

                    pDetails.loadAccounts.Add(pp);
                    /*			kp = pp;*/
                }
            }
            catch (Exception)
            {
                if (idr1 != null)
                {
                    idr1.Close();
                }
                // log.Log(Level.SEVERE, SQL, eac);
            }
            finally
            {
                if (idr1 != null)
                {
                    idr1.Close();
                }
                dt = null;
            }



            //Load Credit Cards
            pDetails.ccs = _mPayment.GetCreditCards();

            //Load Bank Accounts
            SQL = MRole.GetDefault(ctx).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;
            IDataReader idr2 = null;

            try
            {
                idr2 = DB.ExecuteReader(SQL, null, null);
                dt   = new DataTable();
                dt.Load(idr2);
                idr2.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    int         key  = Util.GetValueOfInt(dr[0]); // dr.getInt(1);
                    String      name = dr[1].ToString();          //.getString(2);
                    KeyNamePair pp   = new KeyNamePair(key, name);
                    //cmbSBankAccount.Items.Add(pp);

                    if (pDetails.loadBankAccounts == null)
                    {
                        pDetails.loadBankAccounts = new List <KeyNamePair>();
                    }

                    pDetails.loadBankAccounts.Add(pp);
                    if (key == _C_BankAccount_ID)
                    {
                        pDetails.Checkbook_ID = key;
                    }
                }

                if (pDetails.Checkbook_ID == 0)     //  Default
                {
                    DataRow[] drs = dt.Select("IsDefault='Y'");
                    if (drs != null && drs.Count() > 0)
                    {
                        pDetails.Checkbook_ID = Convert.ToInt32(drs.ElementAt(0)["C_BankAccount_ID"]);
                    }
                }
            }
            catch (Exception)
            {
                if (idr2 != null)
                {
                    idr2.Close();
                }
                //    log.Log(Level.SEVERE, SQL, ept);
            }
            finally
            {
                if (idr2 != null)
                {
                    idr2.Close();
                }
                dt = null;
            }



            //Load Cash Books
            SQL = MRole.GetDefault(ctx).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;
            IDataReader idr3 = null;

            try
            {
                idr3 = DB.ExecuteReader(SQL, null, null);
                dt   = new DataTable();
                dt.Load(idr3);
                idr3.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    int         key  = Util.GetValueOfInt(dr[0]); // dr.getInt(1);
                    String      name = dr[1].ToString();          //.getString(2);
                    KeyNamePair pp   = new KeyNamePair(key, name);
                    //cmbBCashBook.Items.Add(pp);

                    if (pDetails.loadCashBooks == null)
                    {
                        pDetails.loadCashBooks = new List <KeyNamePair>();
                    }

                    pDetails.loadCashBooks.Add(pp);
                    if (key == _C_CashBook_ID)
                    {
                        kp = pp;
                    }
                    if (kp == null && key == _AD_Org_ID)       //  Default Org
                    {
                        kp = pp;
                    }
                }
            }
            catch (Exception)
            {
                if (idr3 != null)
                {
                    idr3.Close();
                }
                // log.Log(Level.SEVERE, SQL, epc);
            }
            finally
            {
                if (idr3 != null)
                {
                    idr3.Close();
                }
                dt = null;
            }

            return(pDetails);
        }
コード例 #2
0
        private TabDetails Save(PaymentInputValues inputs)
        {
            try
            {
                StringBuilder errorMsg       = new StringBuilder();
                string        SucessMsg      = "";
                String        newPaymentRule = inputs.cmbPayment;
                inputs._DateAcct = Convert.ToDateTime(inputs._DateAcct);
                DateTime?newDateAcct         = Convert.ToDateTime(inputs._DateAcct);
                int      newC_PaymentTerm_ID = inputs._C_PaymentTerm_ID;
                int      newC_CashLine_ID    = inputs._C_CashLine_ID;
                int      newC_CashBook_ID    = inputs._C_CashBook_ID;
                String   newCCType           = inputs.CCType;
                int      newC_BankAccount_ID = 0;

                //	B (Cash)		(Currency)
                if (newPaymentRule.Equals(X_C_Order.PAYMENTRULE_Cash))
                {
                    newC_CashBook_ID = inputs.cmbBCashBook;
                    //newDateAcct = (DateTime)bDateField.GetValue();
                    newDateAcct = Convert.ToDateTime(inputs.bDateField);
                }

                //	K (CreditCard)  Type, Number, Exp, Approval
                else if (newPaymentRule.Equals(X_C_Order.PAYMENTRULE_CreditCard))
                {
                    newCCType = inputs.cmbKType;
                }

                //	T (Transfer)	BPartner_Bank
                else if (newPaymentRule.Equals(MOrder.PAYMENTRULE_DirectDeposit) ||
                         newPaymentRule.Equals(MOrder.PAYMENTRULE_DirectDebit))
                {
                    // KeyNamePair myObj = (KeyNamePair)cmbTAccount.SelectedItem;
                }

                //	P (PaymentTerm)	PaymentTerm
                else if (newPaymentRule.Equals(X_C_Order.PAYMENTRULE_OnCredit))
                {
                    newC_PaymentTerm_ID = inputs.cmbPTerm;
                }

                //	S (Check)		(Currency) CheckNo, Routing
                else if (newPaymentRule.Equals(X_C_Order.PAYMENTRULE_Check))
                {
                    //	cmbSCurrency.SelectedItem;
                    newC_BankAccount_ID = inputs.cmbSBankAccount;
                }
                else
                {
                    return(details);
                }


                SetPayments(inputs._C_Payment_ID, inputs._C_Currency_ID, inputs._PaymentRule);


                //  find Bank Account if not qualified yet
                if ("KTSD".IndexOf(newPaymentRule) != -1 && newC_BankAccount_ID == 0)
                {
                    String tender = MPayment.TENDERTYPE_CreditCard;
                    if (newPaymentRule.Equals(MOrder.PAYMENTRULE_DirectDeposit))
                    {
                        tender = MPayment.TENDERTYPE_DirectDeposit;
                    }
                    else if (newPaymentRule.Equals(MOrder.PAYMENTRULE_DirectDebit))
                    {
                        tender = MPayment.TENDERTYPE_DirectDebit;
                    }
                    else if (newPaymentRule.Equals(MOrder.PAYMENTRULE_Check))
                    {
                        tender = MPayment.TENDERTYPE_Check;
                    }
                }

                /***********************
                 *  Changed PaymentRule
                 */
                if (!newPaymentRule.Equals(inputs._PaymentRule))
                {
                    //log.fine("Changed PaymentRule: " + _PaymentRule + " -> " + newPaymentRule);
                    //  We had a CashBook Entry
                    if (inputs._PaymentRule.Equals(X_C_Order.PAYMENTRULE_Cash))
                    {
                        //log.fine("Old Cash - " + _cashLine);
                        if (_cashLine != null)
                        {
                            MCashLine cl = _cashLine.CreateReversal();
                            if (cl.Save())
                            {
                                // log.Config("CashCancelled");
                            }
                            else
                            {
                                errorMsg.Append(Msg.GetMsg(ctx, "CashNotCancelled", true) + Environment.NewLine);
                                //   ShowMessage.Error("", true, Msg.GetMsg(Envs.GetContext(), "CashNotCancelled", true).ToString());
                            }
                        }
                        newC_CashLine_ID = 0;      //  reset
                    }
                    //  We had a change in Payment type (e.g. Check to CC)
                    else if ("KTSD".IndexOf(_PaymentRule) != -1 && "KTSD".IndexOf(newPaymentRule) != -1 && _mPaymentOriginal != null)
                    {
                        //log.fine("Old Payment(1) - " + _mPaymentOriginal);
                        _mPaymentOriginal.SetDocAction(DocActionVariables.ACTION_REVERSE_CORRECT);
                        bool ok = _mPaymentOriginal.ProcessIt(DocActionVariables.ACTION_REVERSE_CORRECT);
                        _mPaymentOriginal.Save();
                        if (ok)
                        {
                            // log.Info("Payment Canecelled - " + _mPaymentOriginal);
                        }
                        else
                        {
                            errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "PaymentNotCancelled", true) + Environment.NewLine);
                            // ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "PaymentNotCancelled", true).ToString() + _mPaymentOriginal.GetDocumentNo());
                        }
                        _mPayment.ReSetNew();
                    }
                    //	We had a Payment and something else (e.g. Check to Cash)
                    else if ("KTSD".IndexOf(_PaymentRule) != -1 && "KTSD".IndexOf(newPaymentRule) == -1)
                    {
                        //log.fine("Old Payment(2) - " + _mPaymentOriginal);
                        if (_mPaymentOriginal != null)
                        {
                            _mPaymentOriginal.SetDocAction(DocActionVariables.ACTION_REVERSE_CORRECT);
                            bool ok = _mPaymentOriginal.ProcessIt(DocActionVariables.ACTION_REVERSE_CORRECT);
                            _mPaymentOriginal.Save();
                            if (ok)        //  Cancel Payment
                            {
                                ///log.Fine("PaymentCancelled " + _mPayment.GetDocumentNo());
                                //_mTab.getTableModel().dataSave(true);
                                // DataRow datarow = null;
                                // _mTab.GetTableObj().DataSave(true, datarow);//**********************************2nd parameter
                                _mPayment.ReSetNew();
                                _mPayment.SetAmount(inputs._C_Currency_ID, inputs._Amount);
                            }
                            else
                            {
                                errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "PaymentNotCancelled", true) + _mPayment.GetDocumentNo() + Environment.NewLine);
                                //ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "PaymentNotCancelled", true).ToString() + _mPayment.GetDocumentNo());
                            }
                        }
                    }
                }


                //  Get Order and optionally Invoice
                //int C_Order_ID = ctx.GetContextAsInt(inputs.WindowNo, "C_Order_ID");
                //int C_Invoice_ID = ctx.GetContextAsInt(inputs.WindowNo, "C_Invoice_ID");
                if (inputs.C_Invoice_ID == 0 && inputs._DocStatus.Equals("CO"))
                {
                    inputs.C_Invoice_ID = GetInvoiceID(inputs.C_Order_ID);
                }
                //  Amount sign negative, if ARC (Credit Memo) or API (AP Invoice)
                bool     negateAmt = false;
                MInvoice invoice   = null;
                if (inputs.C_Invoice_ID != 0)
                {
                    invoice   = new MInvoice(ctx, inputs.C_Invoice_ID, null);
                    negateAmt = invoice.IsCreditMemo();
                }
                MOrder order = null;
                if (invoice == null && inputs.C_Order_ID != 0)
                {
                    order = new MOrder(ctx, inputs.C_Order_ID, null);
                }
                Decimal payAmount = inputs._Amount;
                if (negateAmt)
                {
                    payAmount = Decimal.Negate(inputs._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))
                {
                    //log.fine("Cash");
                    String description = inputs.Description;

                    if (inputs.C_Invoice_ID == 0 && order == null)
                    {
                        //log.Config("No Invoice!");
                        //     ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "CashNotCreated", true).ToString());
                        errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "CashNotCreated", true) + Environment.NewLine);
                    }
                    else
                    {
                        //  Changed Amount
                        if (inputs._C_CashLine_ID > 0)
                        {
                            _cashLine = new MCashLine(ctx, inputs._C_CashLine_ID, null);
                        }


                        if (_cashLine != null &&
                            payAmount.CompareTo(_cashLine.GetAmount()) != 0)
                        {
                            // log.Config("Changed CashBook Amount");
                            _cashLine.SetAmount(payAmount);
                            if (_cashLine.Save())
                            {
                                //     log.Config("CashAmt Changed");
                            }
                        }
                        //	Different Date/CashBook
                        if (_cashLine != null &&
                            (newC_CashBook_ID != inputs._C_CashBook_ID ||
                             !TimeUtil.IsSameDay(_cashLine.GetStatementDate(), newDateAcct)))
                        {
                            //log.Config("Changed CashBook/Date: " + _C_CashBook_ID + "->" + newC_CashBook_ID);
                            MCashLine reverse = _cashLine.CreateReversal();
                            if (!reverse.Save())
                            {
                                errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "CashNotCancelled", true) + Environment.NewLine);
                                //ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "CashNotCancelled", true).ToString());
                            }
                            _cashLine = null;
                        }

                        //	Create new
                        if (_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(ctx, newC_CashBook_ID, newDateAcct, null);
                            }
                            else        //	Default
                            {
                                cash = MCash.Get(ctx, inputs._AD_Org_ID, newDateAcct, C_Currency_ID, null);
                            }
                            if (cash == null || cash.Get_ID() == 0)
                            {
                                errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "CashNotCreated", true) + Environment.NewLine);
                                //ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "CashNotCreated", true).ToString());
                            }
                            else
                            {
                                MCashLine cl = new MCashLine(cash);
                                if (invoice != null)
                                {
                                    cl.SetInvoice(invoice);
                                }
                                if (order != null)
                                {
                                    cl.SetOrder(order, null);
                                    //_needSave = true;
                                }
                                if (cl.Save())
                                {
                                    //log.Config("CashCreated");
                                }
                                else
                                {
                                    errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "CashNotCreated", true) + Environment.NewLine);
                                    //ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "CashNotCreated", true).ToString());
                                }
                            }
                        }
                    }   //	have invoice
                }

                /***********************
                 *  Payments
                 */
                if ("KS".IndexOf(newPaymentRule) != -1)
                {
                    //log.fine("Payment - " + newPaymentRule);
                    //  Set Amount
                    _mPayment.SetAmount(inputs._C_Currency_ID, payAmount);
                    if (newPaymentRule.Equals(MOrder.PAYMENTRULE_CreditCard))
                    {
                        _mPayment.SetCreditCard(MPayment.TRXTYPE_Sales, newCCType,
                                                inputs.txtKNumber, "", inputs.txtKExp);
                        _mPayment.SetPaymentProcessor();
                    }
                    else if (newPaymentRule.Equals(MOrder.PAYMENTRULE_Check))
                    {
                        _mPayment.SetBankCheck(newC_BankAccount_ID, inputs._isSOTrx, inputs.txtSRouting,
                                               inputs.txtSNumber, inputs.txtSCheck);
                    }
                    _mPayment.SetC_BPartner_ID(inputs._C_BPartner_ID);
                    _mPayment.SetC_Invoice_ID(inputs.C_Invoice_ID);
                    if (order != null)
                    {
                        _mPayment.SetC_Order_ID(inputs.C_Order_ID);
                        //     _needSave = true;
                    }
                    _mPayment.SetDateTrx(DateTime.SpecifyKind(Convert.ToDateTime(inputs._DateAcct), DateTimeKind.Utc));
                    _mPayment.SetDateAcct(DateTime.SpecifyKind(Convert.ToDateTime(inputs._DateAcct), DateTimeKind.Utc));
                    _mPayment.Save();

                    //  Save/Post
                    if (MPayment.DOCSTATUS_Drafted.Equals(_mPayment.GetDocStatus()))
                    {
                        bool ok = _mPayment.ProcessIt(DocActionVariables.ACTION_COMPLETE);
                        _mPayment.Save();
                        if (ok)
                        {
                            SucessMsg = _mPayment.GetDocumentNo().ToString() + Msg.GetMsg(ctx, "PaymentCreated");
                            //ShowMessage.Info(_mPayment.GetDocumentNo().ToString(), true, "PaymentCreated", "");
                        }
                        else
                        {
                            errorMsg.Append("PaymentError---" + Msg.GetMsg(ctx, "PaymentNotCreated", true) + Environment.NewLine);
                            //ShowMessage.Error("", true, "PaymentError" + Msg.GetMsg(Envs.GetContext(), "PaymentNotCreated", true).ToString());
                        }
                    }
                    else
                    {
                        // log.Fine("NotDraft " + _mPayment);
                    }
                }
                if (details == null)
                {
                    details = new TabDetails();
                }
                details.newPaymentRule      = newPaymentRule;
                details.newDateAcct         = newDateAcct;
                details._C_CashLine_ID      = inputs._C_CashLine_ID;
                details._C_Payment_ID       = inputs._C_Payment_ID;
                details.newC_PaymentTerm_ID = inputs.cmbPTerm;
                details.ErrorMsg            = errorMsg.ToString();
                details.SucessMsg           = SucessMsg;
            }
            catch
            {
            }
            return(details);
        }