public void  LoadControls()
        {
            dtInstallations             = (oCommonUtilities.CreateInstance()).GetInstallationList();
            lstInstallation.DataContext = dtInstallations.DefaultView;
            IShortPay objCashDesk = ShortPayBusinessObject.CreateInstance();

            dtInstallations      = objCashDesk.GetFailureReasons();
            lvReason.DataContext = dtInstallations.DefaultView;
            ClearControls();
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int iReasonID = 0;
                btnSave.IsEnabled = false;
                if (!CheckData())
                {
                    return;
                }

                int    iSelectedInstallation = 0;
                int    iSelectedReasoncode   = 0;
                string strExceptionDetails   = string.Empty;
                int    iExceptionCode        = 0;
                string strExpectedStatus     = string.Empty;
                string strReason             = "";
                string strComment            = "";

                IHandpay IUser_No         = HandpayBusinessObject.CreateInstance();
                int      AuthorizedUserNo = Security.SecurityHelper.CurrentUser.User_No;

                DataRowView drv = (DataRowView)lstInstallation.SelectedItem;
                iSelectedInstallation = Convert.ToInt32(drv["Installation_No"]);

                if (lvReason.SelectedIndex >= 0)
                {
                    iSelectedReasoncode = 0;
                    drv = (DataRowView)lvReason.SelectedItem;
                    iSelectedReasoncode = Convert.ToInt32(drv[0]);
                }
                else
                {
                    if (lvReason.Items.SourceCollection is System.Data.DataView)
                    {
                        DataView  dvReason = (DataView)lvReason.Items.SourceCollection;
                        DataTable dtReason = new DataTable();
                        dtReason = dvReason.ToTable().Copy();
                        DataView dvReasonClone = new DataView(dtReason);
                        dvReasonClone.RowFilter = "ReasonDescription='" + txtComments.Text + "'";
                        if (dvReasonClone.ToTable().Rows.Count > 0)
                        {
                            iSelectedReasoncode = Convert.ToInt32(dvReasonClone.ToTable().Rows[0]["ReasonCode"]);
                        }
                        else
                        {
                            iSelectedReasoncode = lvReason.Items.Count + 1;
                        }
                    }
                }

                double dValue = Convert.ToDouble(objValueCalc.txtDisplay.Text);
                bool   blnTicketFoundinException = false;

                IShortPay objCashDesk = ShortPayBusinessObject.CreateInstance();

                BMC.Transport.CashDeskOperatorEntity.Treasury objTreasuries = new BMC.Transport.CashDeskOperatorEntity.Treasury();
                objTreasuries.InstallationNumber = iSelectedInstallation;
                objTreasuries.UserID             = Security.SecurityHelper.CurrentUser.User_No;
                objTreasuries.TreasuryType       = "Shortpay";
                objTreasuries.TreasuryReasonCode = iSelectedReasoncode;

                if (lvReason.SelectedIndex >= 0)
                {
                    DataRowView drvReason = (DataRowView)lvReason.SelectedItem;
                    strReason  = drvReason[1].ToString();
                    strComment = strReason + ". Voucher Number is" + txtTicketNumber.Text;
                }
                else
                {
                    objReasonCode.Reason_Code       = iSelectedReasoncode;
                    objReasonCode.ReasonDescription = txtComments.Text;
                    if (!String.IsNullOrEmpty(txtComments.Text))
                    {
                        if (Settings.AddShortpayCommentstoDefault)
                        {
                            iReasonID = objCashDesk.SaveReasonDetails(objReasonCode);
                        }
                        strComment = txtComments.Text + " . Voucher Number is " + txtTicketNumber.Text;
                        strReason  = txtComments.Text;
                        objTreasuries.TreasuryReason = strComment;
                    }
                }

                if (strComment.Length > 199)
                {
                    objTreasuries.TreasuryReason = strComment.Substring(0, 199);
                }
                else
                {
                    objTreasuries.TreasuryReason = strComment;
                }

                objTreasuries.TreasuryAmount       = dValue;
                objTreasuries.TreasuryIssuerUserNo = AuthorizedUserNo;

                int iShortPayID    = 0;
                int UserSecurityId = 0;
                if (BMC.Transport.Settings.ShortPayAuthorizationRequired &&
                    objTreasuries.TreasuryAmount >= BMC.Transport.Settings.ShortPayAuthorizationLimit)
                {
                    if (objCashDesk.CreateShortPayForApproval(objTreasuries, ref iShortPayID))
                    {
                        LogManager.WriteLog("Shortpay saved for authorization. Ticketnumber: " + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
                    }
                    else
                    {
                        LogManager.WriteLog("Unable to save Shortpay authorization. Ticketnumber: " + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
                        MessageBox.ShowBox("MessageID198");
                        return;
                    }

                    CAuthorize oCAuthorize = new CAuthorize("BMC.Presentation.CShortPay.ShortPayApprover");
                    oCAuthorize.ShowDialog();
                    if (!oCAuthorize.IsAuthorized)
                    {
                        objCashDesk.CancelShortPayForApproval(iShortPayID);
                        return;
                    }
                    bIsAuthorised    = true;
                    AuthorisedUsr    = oCAuthorize.User;
                    UserSecurityId   = oCAuthorize.User.SecurityUserID;
                    AuthorizedUserNo = IUser_No.GetUserID(UserSecurityId);
                }

                //Update user no with authorized user if shortpay is authorised by different user
                objTreasuries.TreasuryIssuerUserNo = AuthorizedUserNo;
                objTreasuries.AuthorizedUser_No    = AuthorizedUserNo;
                objTreasuries.Authorized_Date      = DateTime.Now;

                int iTreasuryID = objCashDesk.SaveShortpayDetails(objTreasuries);

                if (iShortPayID > 0)
                {
                    objCashDesk.ApproveShortPay(iShortPayID.ToString(), UserSecurityId, iTreasuryID);
                }

                if (iTreasuryID > 0)
                {
                    LogManager.WriteLog("Shortpay saved in treasury. Ticketnumber: " + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
                    if (iReasonID > 0)
                    {
                        LogManager.WriteLog("ReasonCode saved in Reason_Code. ReasonCode: " + objReasonCode.Reason_Code, LogManager.enumLogLevel.Info);
                    }
                }
                else
                {
                    LogManager.WriteLog("Unable to save Shortpay in treasury. Ticketnumber: " + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
                    return;
                }

                strExceptionDetails = "Normal Treasury Entry";
                iExceptionCode      = (int)ShortpayExceptionCodes.NormalTreasuryEntry;
                IHandpay  handpay     = HandpayBusinessObject.CreateInstance();
                DataTable dtException = handpay.GetTicketingExceptionTable(txtTicketNumber.Text);

                if (dtException.Rows.Count > 0)
                {
                    LogManager.WriteLog("Voucher Number :" + txtTicketNumber.Text + " found in Ticket_Exception table.", LogManager.enumLogLevel.Info);
                    strExpectedStatus         = dtException.Rows[0]["TE_Status_Create_Expected"].ToString();
                    blnTicketFoundinException = true;
                    if (objCashDesk.UpdateTicketException(0, txtTicketNumber.Text, "V") == 0)
                    {
                        LogManager.WriteLog("Ticket Exception table updated. Voucher Number:" + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
                    }

                    strExceptionDetails = "House Keeping Void";
                    iExceptionCode      = (int)ShortpayExceptionCodes.HouseKeepingVoid;
                }

                if (blnTicketFoundinException && !string.IsNullOrEmpty(strExpectedStatus))
                {
                    if (strExpectedStatus == "VOID_SP" || strExpectedStatus == "ACTIVE")
                    {
                        BMC.Transport.CashDeskOperatorEntity.VoidOrExpiredTreasury objVoidTreasury = new BMC.Transport.CashDeskOperatorEntity.VoidOrExpiredTreasury();
                        objVoidTreasury.TicketNumber    = txtTicketNumber.Text;
                        objVoidTreasury.TransactionType = "Shortpay";
                        objVoidTreasury.TreasuryReason  = "Voiding Voucher for ShortPay";
                        objCashDesk.UpdateVoidorExpiredTreasury(objVoidTreasury);
                        LogManager.WriteLog("Updated void or expired treasury. Ticketnumber: " + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
                        strExceptionDetails = "Voiding Vocuher for ShortPay";
                        iExceptionCode      = (int)ShortpayExceptionCodes.VoidTicketForShortpay;
                    }
                }
                if (!blnTicketFoundinException)
                {
                    strExceptionDetails = "No entry found in ticket_exception table for the Voucher entered";
                    iExceptionCode      = (int)ShortpayExceptionCodes.NoEntryInTicket_Exception;
                }

                Transport.CashDeskOperatorEntity.TicketException objException = new BMC.Transport.CashDeskOperatorEntity.TicketException();
                objException.InstallationNumber = iSelectedInstallation;
                objException.ExceptionDetails   = strExceptionDetails;
                objException.ExceptionType      = iExceptionCode;
                objException.Reference          = txtTicketNumber.Text;
                objException.User = AuthorizedUserNo;
                objCashDesk.InsertException(objException);
                LogManager.WriteLog("Voucher inserted into Exception. Voucher Number :" + txtTicketNumber.Text, LogManager.enumLogLevel.Info);

                //MachineDetails.Value = dValue.ToString();
                //MachineDetails.TreasuryNo = iTreasuryID.ToString();

                string sDesc = "Position: " + ((System.Data.DataRowView)(lstInstallation.SelectedValue)).Row.ItemArray[0].ToString() + " Details: " + strReason + " TicketNo.: " + txtTicketNumber.Text + " Amount: " + dValue;
                sDesc += bIsAuthorised ? " Approved By : " + AuthorisedUsr.UserName : "";

                AuditViewerBusiness.InsertAuditData(new Audit.Transport.Audit_History
                {
                    AuditModuleName    = ModuleName.Shortpay,
                    Audit_Screen_Name  = "Shortpay",
                    Audit_Desc         = sDesc,
                    AuditOperationType = OperationType.ADD,
                    Audit_Field        = "Shortpay ",
                    Audit_New_Vl       = "NULL"
                });

                MessageBox.ShowBox("MessageID197");

                LoadControls();
                lstInstallation.Focus();
                if (dtInstallations.Rows.Count > 0)
                {
                    lstInstallation.SelectedIndex = 0;
                }

                //To Print Receipt.
                if (!bIsAuthorised)
                {
                    (oCommonUtilities.CreateInstance()).PrintCommonReceipt(false, "Shortpay", iTreasuryID.ToString());
                }
                else
                {
                    (oCommonUtilities.CreateInstance()).PrintCommonReceipt(false, "Shortpay", iTreasuryID.ToString(), AuthorisedUsr);
                }

                AuthorisedUsr = null;
                bIsAuthorised = false;
                LogManager.WriteLog("Recipt printed for shortpay. Voucher Number :" + txtTicketNumber.Text, LogManager.enumLogLevel.Info);
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
                MessageBox.ShowBox("MessageID198");
                AuditViewerBusiness.InsertAuditData(new Audit.Transport.Audit_History
                {
                    AuditModuleName    = ModuleName.Shortpay,
                    Audit_Screen_Name  = "Shortpay",
                    Audit_Desc         = "Exception Occured while saving the Data",
                    AuditOperationType = OperationType.ADD,
                    Audit_Field        = "Shortpay ",
                    Audit_New_Vl       = "NULL"
                });
            }
            finally
            {
                btnSave.IsEnabled = true;
            }
        }