root/tags/BazaReklam_1.2.12/OrderViewer.cs

Wersja 981, 10.3 KB (wprowadzona przez sylwek, 16 years temu)

Re #235 Migracja źródeł do VS 2008

Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Data.SqlClient;
6using System.Text;
7using System.Windows.Forms;
8using Baza_Reklam.raportyTableAdapters;
9using Microsoft.Reporting.WinForms;
10using System.Drawing.Printing;
11using System.Drawing.Imaging;
12using System.IO;
13using System.Reflection;
14using Baza_Reklam.Classes.Helpers;
15using Baza_Reklam.Classes.Interfaces;
16using Baza_Reklam.Classes.Model;
17using Baza_Reklam.Classes.Repositories;
18
19namespace Baza_Reklam
20{
21    public partial class OrderViewer : Form
22    {
23        private int reklamaId;
24        DataSet ds;
25
26        public OrderViewer(int idReklamy)
27        {
28            InitializeComponent();
29            reklamaId = idReklamy;
30                     
31            ds = new DataSet();
32
33            zamowienieTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
34
35            Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Baza_Reklam.Raporty.zamowienie.xml");
36            ds.ReadXml(s);
37            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("tlumaczenia", ds.Tables[0]));
38            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("zamowienie", zamowienieTableAdapter.GetDataByReklamaId(reklamaId)));
39
40            SqlCommand cm = new SqlCommand();
41            cm.Connection = new SqlConnection();
42            cm.CommandText =
43                " declare @test varchar(800); " +
44                " set @test = ''; " +
45                " declare @nr_wyd int; " +
46                " DECLARE kur2 CURSOR FOR SELECT [Nr Wydania] FROM [UKA¯E SIÊ W NR] WHERE (ReklamaId = @param) order by [Nr Wydania];" +
47                " OPEN kur2; " +
48                " FETCH NEXT from kur2 into @nr_wyd; " +
49                " while @@fetch_status=0 " +
50                "  begin " +
51                "   set @test = @test + ', ' + CAST(@nr_wyd AS varchar); " +
52                "   fetch next from kur2 into @nr_wyd; " +
53                "  end " +
54                " CLOSE kur2; " +
55                " DEALLOCATE kur2; " +
56                " select @test; ";
57
58            cm.Parameters.AddWithValue("@param", reklamaId);
59            cm.Connection.ConnectionString = ConnString.getConnString().Value;
60
61            cm.Connection.Open();
62            string emisje = (string)cm.ExecuteScalar();
63            cm.Connection.Close();
64
65            raporty r = new raporty();
66
67            raporty.emisjeRow row = r.emisje.NewemisjeRow();
68            row.ID = emisje;
69            r.emisje.AddemisjeRow(row);
70
71            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("raporty_emisje", r.emisje));
72                       
73            jezykComboBox.SelectedIndex = 0;
74        }
75
76        public OrderViewer(int idZamowienia, int a)
77        {
78            InitializeComponent();
79            reklamaId = idZamowienia;
80
81            ds = new DataSet();
82
83            zamowienie2TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
84
85            reportViewer1.LocalReport.ReportEmbeddedResource = "Baza_Reklam.Raporty.zamowienie2.rdlc";
86
87            Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Baza_Reklam.Raporty.zamowienie.xml");
88            ds.ReadXml(s);
89            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("tlumaczenia", ds.Tables[0]));
90           
91            DataTable dt = GetOrder(idZamowienia);
92            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("zamowienie2", dt));
93
94            DataTable dtSubscription = GetOrderSubscription(idZamowienia);
95            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("zamowienieSubscription", dtSubscription));
96
97            TitleRepository repository = new TitleRepository(ConnString.getConnString().Value);
98            List<Title> _titles = repository.FindAllActive();
99
100            bool bIsReklama = dt.Rows.Count > 0 && !dt.Rows[0].IsNull("ID REKLAMY");
101            bool bIsSubscription = dtSubscription.Rows.Count > 0;
102
103            int invoiceProvider = 1;
104
105            if (dt.Rows.Count > 0)
106            {
107                Title title = _titles.Find(delegate(Title t) { return t.ShortName == dt.Rows[0]["TYTU£"].ToString(); });
108                if (title != null)
109                    invoiceProvider = title.InvoiceProvider;
110            }
111
112            List<ReportParameter> paramList = new List<ReportParameter>();
113            paramList.Add(new ReportParameter("jezyk", "PL", true));
114            paramList.Add(new ReportParameter("reklamaId", reklamaId.ToString(), true));
115
116            paramList.Add(new ReportParameter("isReklama", bIsReklama.ToString(), false));
117            paramList.Add(new ReportParameter("isSubscription", bIsSubscription.ToString(), false));
118
119            reportViewer1.LocalReport.SetParameters(paramList);
120
121            reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Baza_Reklam_InvoiceProvider",
122                                                                           InvoiceProviderFactory.GetInvoiceProvidersById(invoiceProvider)));
123
124            jezykComboBox.SelectedIndex = 0;
125        }
126
127        //TODO: Helper method to get the Order details...
128        private static DataTable GetOrder(int orderId)
129        {
130            SqlConnection conn = null;
131            SqlCommand cmd = null;
132            DataTable dt = new DataTable();
133            const string query = "sp_GetOrderDetails";
134            try
135            {
136                conn = new SqlConnection(ConnString.getConnString().Value);
137                conn.Open();
138                cmd = new SqlCommand(query, conn);
139                cmd.CommandType = CommandType.StoredProcedure;
140                cmd.Parameters.AddWithValue("@orderId", orderId);
141
142                SqlDataAdapter da = new SqlDataAdapter(cmd);
143                da.Fill(dt);
144            }
145            finally
146            {
147                if (cmd != null) cmd.Dispose();
148                if (conn != null) conn.Close();
149            }
150            return dt;
151        }
152
153        private DataTable GetOrderSubscription(int orderId)
154        {
155            SqlConnection conn = null;
156            SqlCommand cmd = null;
157            DataTable dt = new DataTable();
158            const string query = "sp_GetOrderSubscription";
159            try
160            {
161                conn = new SqlConnection(ConnString.getConnString().Value);
162                conn.Open();
163                cmd = new SqlCommand(query, conn);
164                cmd.CommandType = CommandType.StoredProcedure;
165                cmd.Parameters.AddWithValue("@orderId", orderId);
166
167                SqlDataAdapter da = new SqlDataAdapter(cmd);
168                da.Fill(dt);
169            }
170            finally
171            {
172                if (cmd != null) cmd.Dispose();
173                if (conn != null) conn.Close();
174            }
175            return dt;
176        }
177
178        private void OrderViewer_Load(object sender, EventArgs e)
179        {
180            reportViewer1.RefreshReport();
181        }
182
183        private void zapiszButton_Click(object sender, EventArgs e)
184        {
185            Cursor = Cursors.WaitCursor;
186
187            List<ReportParameter> paramList = new List<ReportParameter>();
188            paramList.Add(new ReportParameter("jezyk", jezykComboBox.SelectedItem.ToString(), true));
189            reportViewer1.LocalReport.SetParameters(paramList);
190            reportViewer1.RefreshReport();
191
192            Cursor = Cursors.Default;
193        }
194
195        // drukowanie
196        private int m_currentPageIndex;
197        private IList<Stream> m_streams;
198
199        private Stream CreateStream(string name,
200          string fileNameExtension, Encoding encoding,
201          string mimeType, bool willSeek)
202        {
203            Stream stream = new FileStream(name +
204               "." + fileNameExtension, FileMode.Create);
205            m_streams.Add(stream);
206            return stream;
207        }
208
209        private void Export(LocalReport report)
210        {
211            string deviceInfo =
212              "<DeviceInfo>" +
213              "  <OutputFormat>EMF</OutputFormat>" +
214              "  <PageWidth>21cm</PageWidth>" +
215              "  <PageHeight>29.7cm</PageHeight>" +
216              "  <MarginTop>1cm</MarginTop>" +
217              "  <MarginLeft>1.4cm</MarginLeft>" +
218              "  <MarginRight>1.4cm</MarginRight>" +
219              "  <MarginBottom>0cm</MarginBottom>" +
220              "</DeviceInfo>";
221            Warning[] warnings;
222            m_streams = new List<Stream>();
223            report.Render("Image", deviceInfo, CreateStream,
224               out warnings);
225            foreach (Stream stream in m_streams)
226                stream.Position = 0;
227        }
228
229        private void PrintPage(object sender, PrintPageEventArgs ev)
230        {
231            Metafile pageImage = new
232               Metafile(m_streams[m_currentPageIndex]);
233            ev.Graphics.DrawImage(pageImage, ev.PageBounds);           
234            m_currentPageIndex++;
235            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
236        }
237
238        private void Print(PrinterSettings printerSettings)
239        {
240            if (m_streams == null || m_streams.Count == 0)
241                return;
242
243            PrintDocument printDoc = new PrintDocument();
244            printDoc.PrinterSettings = printerSettings;
245            printDoc.PrinterSettings.Duplex = Duplex.Vertical;
246            printDoc.PrintPage += PrintPage;
247            printDoc.Print();
248        }
249
250        private void Run()
251        {
252             PrintDialog printDialog = new PrintDialog();
253             if (printDialog.ShowDialog() == DialogResult.OK)
254             {
255                 Export(reportViewer1.LocalReport);
256                 m_currentPageIndex = 0;
257                 Print(printDialog.PrinterSettings);
258             }
259        }
260
261        public void Dispose()
262        {
263            if (m_streams != null)
264            {
265                foreach (Stream stream in m_streams)
266                    stream.Close();
267                m_streams = null;
268            }
269            Dispose();
270        }
271
272        public void HandlePrint(object sender, CancelEventArgs e)
273        {
274            Run();
275            e.Cancel = true;
276        }
277    }
278}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.