root/Baza Reklam 2 - Faktury/Classes/Utils.cs @ 30

Wersja 30, 9.4 KB (wprowadzona przez dorota, 17 years temu)

poprawione usuwanie korekt

Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Data;
5using System.Data.SqlClient;
6
7namespace Baza_Reklam
8{
9    class Utils
10    {
11        public static object ExecuteQuery(string query)
12        {
13            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
14
15            SqlCommand command = new SqlCommand(query);
16            command.Connection = conn;
17
18            object result = null;
19
20            conn.Open();
21            result = command.ExecuteScalar();
22            conn.Close();
23
24            return result;           
25        }
26       
27        public static float pobierzSzerModulu(string mod_typ)
28        {
29            string query = "select MOD_SZER from dbo.[NAZWY MODU£ÓW] where MOD_TYP='" + mod_typ + "'";
30
31            return (float) (ExecuteQuery(query) ?? 0);
32        }
33
34        public static float pobierzWysModulu(string mod_typ)
35        {
36            string query = "select MOD_WYS from dbo.[NAZWY MODU£ÓW] where MOD_TYP='" + mod_typ + "'";
37
38            return (float)(ExecuteQuery(query) ?? 0);
39        }
40
41        /// <summary>
42        /// Zwraca customerId danej reklamy
43        /// </summary>
44        public static int customerId(int reklamaId)
45        {
46
47            string query = "select customerId from Reklama where reklamaId=" + reklamaId;
48
49            return (int)(ExecuteQuery(query) ?? 0);
50        }
51
52
53        /// <summary>
54        /// Zwraca Id loga przypisanego do danej reklamy
55        /// </summary>
56        public static int logoId(int reklamaId)
57        {
58            string query = "select logoId from Reklama where reklamaId=" + reklamaId;
59            return (int)(ExecuteQuery(query) ?? 0);
60        }
61
62
63        /// <summary>
64        /// Funkcja sprawdza czy logo jest u¿yte w ktorejs z reklam.
65        /// </summary>
66        public static bool logoByloWykorzystane(int logoID)
67        {
68            string query = "select count(*) from reklama where logoID=" + logoID;
69
70            int result = 0;
71            result = (int)(ExecuteQuery(query) ?? result);
72
73            return (result != 0 ? false : true);
74        }
75
76        /// <summary>
77        /// Funkcja sprawdza czy handlowiec wystawi³ ju¿w danym roku fakturê o danym numerze.
78        /// </summary>
79        public static bool istniejeFaktura(int nr, string kod, int rok)
80        {
81            string query = "select count(*) from faktury where NUMER=" + nr + " aND NUMER_ROZ='" + kod + "' AND NUMER_ROK=" + rok;
82         
83            int result = 0;
84            result = (int)(ExecuteQuery(query) ?? result);
85
86            return (result == 0 ? false : true);
87        }
88
89        /// <summary>
90        /// Zwraca nazwê agencji dla danego agenta.
91        /// </summary>
92        public static string nazwaAgencji(string symbolAgenta)
93        {
94            string query = "select A2.Symbol from agenci A left outer join Agencje A2 on A.id_agencji=A2.id_agencji where A.Symbol ='" + symbolAgenta +"'";
95
96            return (string)(ExecuteQuery(query) ?? "?");
97        }
98
99        /// <summary>
100        /// Zwraca id agencji dla danego agenta.
101        /// </summary>
102        public static int idAgencji(string symbolAgenta)
103        {
104            string query = "select id_Agencji from agenci where Symbol ='" + symbolAgenta +"'";
105
106            return (int)(ExecuteQuery(query) ?? 2);
107        }
108
109        /// <summary>
110        /// Zwraca id agencji dla danego agenta.
111        /// </summary>
112        public static int idAgencjiWgKoduAgenta(string kodAgenta)
113        {
114            string query = "select id_Agencji from agenci where F_ROZ ='" + kodAgenta + "'";
115
116            return (int)(ExecuteQuery(query) ?? 2);
117        }
118
119
120        /// <summary>
121        /// Sprawdza czy istnieje ju¿ w bazie dany kod rozliczeniowy.
122        /// </summary>
123        public static bool tagAgentaUnikalny(string tagAgenta, string symbolAgenta)
124        {
125            string query = "select count(*) from agenci where F_ROZ = '" +  tagAgenta +"' and symbol <> '" + symbolAgenta + "'";
126
127            int result = (int)ExecuteQuery(query);
128
129            return (result == 0 ? true : false);
130        }
131
132        /// <summary>
133        /// VIES ma wartoœci w bazie: -1,0,1,NULL...
134        /// </summary>
135        public static bool czyKlientMaNrVIES(int customerID)
136        {
137            string query = "select VIES from klienci where customerId=" + customerID + " and VIES is not null ";
138
139            short result = (short)(ExecuteQuery(query) ?? Convert.ToInt16(0));
140
141            result = Math.Abs(result);
142
143            if (result > 0) return true;
144           
145            return false;
146        }
147
148     
149        public static string mailKlienta(int customerID)
150        {
151            string query = "select Email from klienci where customerId=" + customerID + " and email is not null ";
152
153            string result = (string)(ExecuteQuery(query) ?? "");
154
155            return result;
156        }
157
158
159        /// <summary>
160        /// Zwraca kod rozliczeniowy agenta (tag Agenta).
161        /// </summary>
162        public static string tagAgenta(string symbolAgenta)
163        {
164           string query = "select F_ROZ from agenci where Symbol ='" + symbolAgenta + "'";
165           
166           return (string)(ExecuteQuery(query) ?? "?");
167        }
168
169        /// <summary>
170        /// Zwraca numer nastêpnej faktury danego agenta.
171        /// </summary>
172        public static int numerNowejFaktury(string tagAgenta, int year)
173        {
174            string query = "select count(*) from faktury where [numer_roz]='" + tagAgenta + "' AND [numer_rok]=" + year;
175
176            int nr = (int)ExecuteQuery(query);
177            return (nr + 1);
178        }
179
180
181        /// <summary>
182        /// Zwraca numer nowego reklamy. W try - catch z powoodu bzdurnych danych w bazie.
183        /// </summary>
184        public static int numerNowejReklamy(string symbolAgenta, string tagAgenta)
185        {
186
187            SqlConnection conn = new SqlConnection(
188                ConnString.getConnString().Value);
189
190            SqlCommand command = new SqlCommand();
191            command.CommandType = CommandType.Text;
192            //  command.CommandText = "select distinct [id reklamy] from reklama where [symbol akwizytora]=@symbolAgenta";
193            //  command.Parameters.AddWithValue("@symbolAgenta", symbolAgenta);
194            command.CommandText = "select distinct [id reklamy] from reklama where [id reklamy] like '%' + @tagAgenta + '%'";
195            command.Parameters.AddWithValue("@tagAgenta", tagAgenta);
196            command.Connection = conn;
197
198            conn.Open();
199
200            SqlDataReader reader = command.ExecuteReader();
201
202            int max = 0;
203            int nr = 0;
204
205            while (reader.Read())
206            {
207                if ((reader.GetValue(0).ToString()).Substring(0, tagAgenta.Length) == tagAgenta)
208                {
209
210                    // Int32.TryParse((reader.GetValue(0).ToString()).Substring(tagAgenta.Length, 3), out nr);
211                    //W try - catch z powoodu bzdurnych danych w bazie.
212                    try
213                    {
214                        nr = Int32.Parse((reader.GetValue(0).ToString()).Substring(tagAgenta.Length, 3));
215                    }
216                    catch (Exception e1)
217                    {
218
219                    }
220
221                    if (nr > max)
222                    {
223                        max = nr;
224                    }
225                }
226            }
227            conn.Close();
228
229            return max + 1;
230        }
231
232
233        /// <summary>
234        /// Zwraca iloœæ kontaktów z poprzedniego miesi¹ca
235        /// </summary>
236        public static int iloscKontaktowAgenta(string login)
237        {
238            string query = "select count(*) from dbo.KONTAKTY where symbol_agenta ='" +
239                login + "' and year(data)=" + DateTime.Today.AddMonths(-1).Year +" and month(data)=" + DateTime.Today.AddMonths(-1).Month;
240
241            int ilosc = (int)ExecuteQuery(query);
242            return ilosc;
243        }
244
245        /// <summary>
246        /// Zwraca numer nowego zamówienia
247        /// </summary>
248        public static int numerNowegoZamowienia(int idKlienta, int year)
249        {
250            string query = "select count(*) from zamowienia where idKlienta=" + idKlienta + " AND rokZamowienia=" + year;
251
252            int nr = (int)ExecuteQuery(query);
253            return (nr + 1);
254        }
255
256        /// <summary>
257        /// Zwraca numer nastêpnej faktury korekty dla danego agenta.
258        /// </summary>
259        public static int numerNowejFakturyKorekty(string tagAgenta, int year)
260        {
261            string query = "select count(*) from faktury where idFakturyKorekta is not null AND [numer_rok]=" + year;
262
263            int nr = (int)ExecuteQuery(query);
264            return (nr + 1);
265        }
266
267        /// <summary>
268        /// Sprawdza czy istnieje ju¿ w bazie dany kod rozliczeniowy.
269        /// </summary>
270        public static bool kodKlientaUnikalny(string kodKlienta, string symbolAgenta, int customerId)
271        {
272            string query = "select count(*) from klienci where kodKlienta = '" + kodKlienta + "' and username = '" + symbolAgenta + "'" +
273                " and customerId <> " + customerId;
274
275            int result = (int)ExecuteQuery(query);
276
277            return (result == 0 ? true : false);
278        }
279
280    }
281}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.