
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using ContractNotesWrapper.GlobalFunctions;
using System.Data.SqlClient;
using System.Configuration;

namespace ContractNotesWrapper.Forms
{
    public partial class ConsolidatedContractNote : Form
    {
        SqlConnection conObj = new SqlConnection(ConfigurationSettings.AppSettings["ContractNoteConString"]);
        SqlCommand cmdObj = new SqlCommand();
        SqlDataAdapter daObj = new SqlDataAdapter();
        CommonFunctions objCommanFunc = new CommonFunctions();
        DateTime dtiBatchDateStCCN;

        public ConsolidatedContractNote()
        {
            InitializeComponent();
        }

        #region Method which will be called from GMO TrustOffice Application,later this has to be changed by passing arguments/parameters
        private void ConsolidatedContractNote_Load(object sender, EventArgs e)
        {

            try
            {
                conObj.Open();
                Warning[] warningsStCCN = null;
                byte[] bytesStCCN = null;

                //Path where main rdlc report  is present                
                CommonFunctions.strReportPath = Application.StartupPath + "\\Reports\\ConsolidatedContractNote.rdlc";

                //Path where Contract Note will be saved in pdf format
                CommonFunctions.strPdfPath = Application.StartupPath + "\\ContractNotes\\ConsolidatedContractNote.pdf";
                dtiBatchDateStCCN = Convert.ToDateTime("6-dec-2012");

                //Calling event to process sub-report at the time of loading main report
                reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(prcProcessSubReport);

                //Getting Data from Data Source and fetching in DataSet/Processing main report
                this.sto_getTRDContractNoteTableAdapter.Fill(this.sto_ea_devDataSet2.sto_getTRDContractNote, "610|BRAN0004W", dtiBatchDateStCCN, 14, 0);
                reportViewer1.LocalReport.ReportPath = CommonFunctions.strReportPath;

                //Refreshing Report(this time prcProcessSubReport for Subreports will also be executed)
                this.reportViewer1.RefreshReport();

                //The below line should be written after report is processed and rendered in report viewer,other wise it will take only rdlc which doesn't contain DB info. 
                bytesStCCN = reportViewer1.LocalReport.Render("PDF", CommonFunctions.deviceInfo, out CommonFunctions.mimeType, out CommonFunctions.encoding, out CommonFunctions.extension, out CommonFunctions.streamids, out warningsStCCN);

                //Calling the CreatePdf method which will generate file and save it on the local machine.
                objCommanFunc.CreatePdf(CommonFunctions.strPdfPath, bytesStCCN);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conObj.Close();
            
            }
        }
        #endregion


        #region In this method 2 sub reports will be processed under main report.
        private void prcProcessSubReport(object sender, SubreportProcessingEventArgs e)
        {
            try
            {
                
                switch (e.ReportPath)
                {

                    case "SubTrade"://Name of the Sub-report Trade Details
                        //Need to call Method which call SP to fill Data in DataTable
                        DataTable dtTradeDetails = new DataTable();
                        dtTradeDetails = GetTradeDetails();
                        ReportDataSource rdsTradeDetails = new ReportDataSource("DataSet1", dtTradeDetails);
                        e.DataSources.Add(rdsTradeDetails);
                        break;

                        
                    case "SubRecDisb"://Name of the Sub-Report Cash Details
                        //Need to call Method which call SP to fill Data in DataTable
                        DataTable dtCashDetails = new DataTable();
                        dtCashDetails = GetCashDetails();
                        ReportDataSource rdsCashDetails = new ReportDataSource("DataSet1", dtCashDetails);
                        e.DataSources.Add(rdsCashDetails);
                        break;

                    default:
                        break;

                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Getting Data for Trade Details
        private DataTable GetTradeDetails()
        {
            cmdObj = new SqlCommand("sto_getTRDContractNote", conObj);
            cmdObj.CommandType = CommandType.StoredProcedure;
            cmdObj.Parameters.Add("@RecipientID", SqlDbType.VarChar).Value = "610|BRAN0004W";
            cmdObj.Parameters.Add("@BatchDate", SqlDbType.DateTime).Value = dtiBatchDateStCCN;
            cmdObj.Parameters.Add("@TransmittalReportID", SqlDbType.Int).Value = 14;
            cmdObj.Parameters.Add("@DataType", SqlDbType.Int).Value = 1;
            cmdObj.ExecuteNonQuery();
            daObj.SelectCommand = cmdObj;
            DataSet dsTradeDetails = new DataSet();
            daObj.Fill(dsTradeDetails);
            return dsTradeDetails.Tables[0];
        }
        #endregion

        #region Getting Data for Cash Details
        private DataTable GetCashDetails()
        {
            cmdObj = new SqlCommand("sto_getTRDContractNote", conObj);
            cmdObj.CommandType = CommandType.StoredProcedure;
            cmdObj.Parameters.Add("@RecipientID", SqlDbType.VarChar).Value = "610|BRAN0004W";
            cmdObj.Parameters.Add("@BatchDate", SqlDbType.DateTime).Value = dtiBatchDateStCCN;
            cmdObj.Parameters.Add("@TransmittalReportID", SqlDbType.Int).Value = 14;
            cmdObj.Parameters.Add("@DataType", SqlDbType.Int).Value = 2;
            cmdObj.ExecuteNonQuery();
            daObj.SelectCommand = cmdObj;
            DataSet dsCashDetails = new DataSet();
            daObj.Fill(dsCashDetails);
            return dsCashDetails.Tables[0];

        }
        #endregion



    }
}
