-
Notifications
You must be signed in to change notification settings - Fork 1
/
SalesReport.xaml.cs
314 lines (272 loc) · 12.1 KB
/
SalesReport.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Common;
using System.IO;
using Invoice365.util;
namespace Invoice365
{
/// <summary>
/// Interaction logic for Invoices.xaml
/// </summary>
public partial class SalesReport : Inv365Sortable
{
DataTable t = new DataTable();
public const int MONTHLY = 2;
public const int WEEKLY = 1;
public const int DAILY = 0;
public const int SALES = 0;
public const int CUSTOMER = 1;
public const int BILLER = 2;
public const int CUSTOMER_DUE = 3;
public int type = 0;
public int rtype = 0;
GridView gridview = null;
public static string[] filenames = { "sales_report.csv", "customer_report.csv", "biller_report.csv", "customer_due.csv" };
string[] salescols = new string[] { "Item Code", "Item", "Qty Sold", "Amount" };
string[] custcols = new string[] { "Customer", "Qty Sold", "Amount" };
string[] billercols = new string[] { "Biller", "Qty Sold", "Amount" };
string[] custduecols = new string[] { "Customer", "Amount Due"};
string[] cols = null;
public SalesReport(int type) : this(DAILY,type)
{
}
public DateTime getDate(bool start)
{
DateTime dt = DateTime.Today;
if (start)
{
if(rtype == DAILY)
dt = DateTime.Today;
else if(rtype == WEEKLY)
dt = dt.AddDays(-7);
return dt;
}
dt = dt.AddDays(1);
return dt;
}
public SalesReport(int rtype, int type)
{
this.type = type;
this.rtype = rtype;
InitializeComponent();
DateTime st = getDate(true);
DateTime end = getDate(false);
switch (type)
{
case SALES:
cols = salescols;
initTable();
loadSalesReport(st,end);
break;
case CUSTOMER:
reportTitle.Content = "Sales By Customer";
cols = custcols;
initTable();
loadCustomerReport(st,end);
break;
case BILLER:
reportTitle.Content = "Sales By Biller";
cols = billercols;
initTable();
loadBillerReport(st, end);
break;
case CUSTOMER_DUE:
reportTitle.Content = "Amount Due By Customer";
cols = custduecols;
initTable();
loadCustomerDueReport(st, end);
break;
}
}
public override DataTemplate getUpTemplate()
{
return Resources["HeaderTemplateArrowUp"] as DataTemplate;
}
public override DataTemplate getDownTemplate()
{
return Resources["HeaderTemplateArrowDown"] as DataTemplate;
}
public override ListView getView()
{
return invView;
}
private void Window_Initialized(object sender, EventArgs e)
{
string str = "select distinct company from customers order by company";
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(str);
while (rdr.Read())
{
customer.Items.Add(rdr.GetString(0));
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
str = "select distinct phone from customers order by phone";
rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(str);
while (rdr.Read())
{
custphone.Items.Add(rdr.GetString(0));
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
}
private void initTable()
{
for (int i = 0; i < cols.Length; i++)
{
DataColumn column = new DataColumn(cols[i]);
column.DataType = typeof(string);
t.Columns.Add(column);
}
gridview = new GridView();
for (int i = 0; i < cols.Length; i++)
{
GridViewColumn gvcolumn = new GridViewColumn();
gvcolumn.Header = cols[i];
if (i == 0) gvcolumn.Width = 200;
else
gvcolumn.Width = 150;
gvcolumn.DisplayMemberBinding = new Binding(cols[i]);
gridview.Columns.Add(gvcolumn);
}
invView.View = gridview;
Binding bind = new Binding();
invView.DataContext = t;
invView.SetBinding(ListView.ItemsSourceProperty, bind);
}
public void loadReport(DateTime st, DateTime end)
{
switch (type)
{
case SALES:
loadSalesReport(st, end);
break;
case CUSTOMER:
loadCustomerReport(st, end);
break;
case BILLER:
loadBillerReport(st, end);
break;
case CUSTOMER_DUE:
loadCustomerDueReport(st, end);
break;
}
}
public void loadSalesReport(DateTime st, DateTime end)
{
string qry = "select item_code,item,sum(qty), sum(invoice_detail.amount) from invoice_detail join invoice on invoice.invoice_id = invoice_detail.invoice_id ";
if(!string.IsNullOrEmpty(customer.Text))
qry = qry + "where invoice.customer='"+customer.Text+
"' and invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by item_code,item order by item_code";
else if(!string.IsNullOrEmpty(custphone.Text))
qry = "select item_code,item,sum(qty), sum(invoice_detail.amount) from invoice_detail join invoice on invoice.invoice_id = "+
"invoice_detail.invoice_id join customers on customers.company=invoice.customer where customers.phone='" + custphone.Text +
"' and invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by item_code,item order by item_code";
else
qry = qry + "where invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by item_code,item order by item_code";
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(qry);
loadReport(rdr, 3);
}
/*public void loadSalesReport(DateTime st, DateTime end)
{
string qry = "select item_code,item,sum(qty), sum(invoice_detail.amount) from invoice_detail join invoice on invoice.invoice_id = invoice_detail.invoice_id ";
if (!string.IsNullOrEmpty(customer.Text))
qry = qry + "where invoice.customer='" + customer.Text + " and";
else if (!string.IsNullOrEmpty(custphone.Text))
qry = "select item_code,item,sum(qty), sum(invoice_detail.amount) from invoice_detail join invoice on invoice.invoice_id = " +
"invoice_detail.invoice_id join customers on customers.company=invoice.customer where customers.phone='" + custphone.Text + " and";
else
qry = qry + " where";
qry = qry + " invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by item_code,item order by item_code";
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(qry);
loadReport(rdr, 3);
}*/
public void loadCustomerReport(DateTime st, DateTime end)
{
string qry = "select customer,sum(qty), sum(invoice_detail.amount) from invoice_detail join invoice on invoice.invoice_id = invoice_detail.invoice_id ";
qry = qry + "where invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by customer order by customer";
//MessageBox.Show(qry);
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(qry);
loadReport(rdr, 2);
}
public void loadCustomerDueReport(DateTime st, DateTime end)
{
string qry = "select customer,sum(amount-amount_paid) as due from invoice ";
//string qry = "select customer,sum(amount-amount_paid) from invoice ";
qry = qry + "where invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by customer order by due desc";
//MessageBox.Show(qry);
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(qry);
loadReport(rdr, 1);
}
public void loadBillerReport(DateTime st, DateTime end)
{
string qry = "select biller,sum(qty), sum(invoice_detail.amount) from invoice_detail join invoice on invoice.invoice_id = invoice_detail.invoice_id ";
qry = qry + "where invoice.last_updated <='" + end + "' and invoice.last_updated >='" + st + "' group by biller order by biller";
//MessageBox.Show(qry);
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(qry);
loadReport(rdr, 2);
}
public void loadReport(DbDataReader rdr, int idx)
{
t.Rows.Clear();
while (rdr.Read())
{
DataRow r = t.NewRow();
for (int i = 0; i < cols.Length; i++)
{
if(i == idx)
r[cols[i]] = rdr.GetDouble(i).ToString("N2");
else
r[cols[i]] = rdr.GetValue(i).ToString();
}
t.Rows.Add(r);
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
}
private void customer_SelectedValueChanged(object sender, EventArgs e)
{
/*string item = (string)customer.SelectedItem;
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(
"select item_code,item,sum(qty), sum(amount) from invoice_detail join invoice on invoice.invoice_id = invoice_detail.invoice_id where invoice.customer='" + item + "'" + " group by item_code,item order by item_code");
//"select item_code,item,sum(amount), qty, unit_qty from invoice_detail group by item_code order by item_code where customer='" + item + "'");
loadReport(rdr);*/
}
private void custphone_SelectedValueChanged(object sender, EventArgs e)
{
}
private void itemCode_SelectedValueChanged(object sender, EventArgs e) {
//{
//string item = (string)itemCode.Text;
//DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(
// "select item_code,item,sum(qty), sum(amount) from invoice_detail where item_code='" + item + "' group by item_code,item order by item_code ");
//loadReport(rdr);
}
private void generate_Click(object sender, RoutedEventArgs e)
{
if(startDate.SelectedDate == null || endDate.SelectedDate == null) {
MessageBox.Show("Please enter a valid start and end date");
return;
}
DateTime st = startDate.SelectedDate.Value;
DateTime end = endDate.SelectedDate.Value;
end = end.AddDays(1);
loadReport(st,end);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ExportUtil.exportReports(filenames[type], t);
}
}
}