Пример #1
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);
        }