-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inventory.xaml.cs
261 lines (216 loc) · 9.25 KB
/
Inventory.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
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 MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
using System.ComponentModel;
namespace Invoice365
{
/// <summary>
/// Interaction logic for Inventory.xaml
/// </summary>
public partial class Inventory : Inv365Sortable
{
DataTable t = new DataTable();
string[] cols = new string[] { "Item Code", "Item", "Vendor", "Qty", "Price Buy", "Price Sell"};
public Inventory()
{
InitializeComponent();
initTable();
InvUtil.loadInv(cols, t);
}
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 initTable()
{
for (int i = 0; i < cols.Length; i++)
{
DataColumn column = new DataColumn(cols[i]);
column.DataType = typeof(string);
t.Columns.Add(column);
if (i == 0) t.PrimaryKey = new DataColumn[]{column};
}
GridView gridview = new GridView();
for (int i = 0; i < cols.Length; i++)
{
GridViewColumn gvcolumn = new GridViewColumn();
gvcolumn.Width = 150;
gvcolumn.Header = cols[i];
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);
}
private void Window_Initialized(object sender, EventArgs e)
{
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery("select distinct item_code from items order by item_code");
while (rdr.Read())
{
itemCodeCB.Items.Add(rdr.GetString(0));
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
rdr = DB.getInstance(DB.MSSQL).ExecuteQuery("select distinct item from items order by item");
while (rdr.Read())
{
itemCB.Items.Add(rdr.GetString(0));
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
rdr = DB.getInstance(DB.MSSQL).ExecuteQuery("select distinct vendor from items order by vendor");
while (rdr.Read())
{
object _str = rdr.GetValue(0);
string str = _str != null ? (string)_str : "";
if(!String.IsNullOrEmpty(str))
vendor.Items.Add(str);
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
GridView v = (GridView)invView.View;
}
private void itemCB_SelectedValueChanged(object sender, EventArgs e)
{
string item = (string)itemCodeCB.SelectedItem;
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(
"select item_code, item, vendor, qty, buy_price, sell_price, bar_code from items where item_code='" + item + "'");
if (rdr.Read())
{
buyTB.Text = rdr.GetDouble(4).ToString("N2");
sellTB.Text = rdr.GetDouble(5).ToString("N2");
itemCB.Text = rdr.GetString(1);
vendor.Text = rdr.GetString(2);
//unit.Text = rdr.GetString(7);
//unitValue.Text = rdr.GetDouble(8).ToString();
barCodeCombo.Text = rdr.GetString(6);
DataRow r = t.Rows.Find(itemCodeCB.Text);
if (r != null)
{
invView.SelectedIndex = t.Rows.IndexOf(r);
invView.ScrollIntoView(invView.SelectedItem);
//unit.IsEnabled = false;
//unitValue.IsEnabled = false;
}
}
else
{
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
}
private void save_Click(object sender, RoutedEventArgs e)
{
Item txn = new Item();
txn.item = itemCB.Text;
txn.item_code = itemCodeCB.Text;
if (txn.item_code == null || txn.item_code.Trim().Length == 0)
{
MessageBox.Show("Please Enter a Valid Item Code");
return;
}
txn.qty = Double.Parse(qtyTB.Text != null && qtyTB.Text.Trim().Length > 0 ? qtyTB.Text : "0.0");
txn.pricesell = Double.Parse(sellTB.Text != null && sellTB.Text.Trim().Length > 0 ? sellTB.Text : "0");
txn.barcode = barCodeCombo.Text;
txn.pricebuy = Double.Parse(buyTB.Text != null && buyTB.Text.Trim().Length > 0 ? buyTB.Text : "0");
txn.vendor = vendor.Text;
string str = "update items set qty=qty+" + txn.qty;
str = str + ",sell_price=" + txn.pricesell + ",buy_price=" + txn.pricebuy;
str = str + ",item='" + txn.item + "'";
str = str + ",vendor='" + txn.vendor + "'";
//str = str + ",bar_code='" + txn.barcode + "'";
str = str + ",bar_code='" + txn.barcode + "' where item_code='" + txn.item_code + "'";
int ret = DB.getInstance(DB.MSSQL).ExecuteNonQuery(str);
if (ret == 0)
{
str = "insert into items(item_code,item,vendor,bar_code,qty,buy_price,sell_price)";
str = str + " values('" + txn.item_code + "','" + txn.item + "','" + txn.vendor+"','"+txn.barcode + "',";
str = str + txn.qty + "," +txn.pricebuy + "," + txn.pricesell + ")";
DB.getInstance(DB.MSSQL).ExecuteNonQuery(str);
}
this.NavigationService.RemoveBackEntry();
this.NavigationService.Navigate(new Inventory());
}
private void barCodeCombo_TextChanged(object sender, TextChangedEventArgs e)
{
string barcode = (string)barCodeCombo.Text;
DbDataReader rdr = DB.getInstance(DB.MSSQL).ExecuteQuery(
"select item_code, item, vendor,qty, buy_price, sell_price, bar_code from items where bar_code='" + barcode + "'");
if (rdr.Read())
{
buyTB.Text = rdr.GetDouble(4).ToString("N2");
sellTB.Text = rdr.GetDouble(5).ToString("N2");
itemCB.Text = rdr.GetString(1);
itemCodeCB.Text = rdr.GetString(0);
vendor.Text = rdr.GetString(2);
//unit.Text = rdr.GetString(7);
//unitValue.Text = rdr.GetString(8);
barCodeCombo.Text = rdr.GetString(6);
DataRow r = t.Rows.Find(itemCodeCB.Text);
if (r != null)
{
invView.SelectedIndex = t.Rows.IndexOf(r);
invView.ScrollIntoView(invView.SelectedItem);
unit.IsEnabled = false;
unitValue.IsEnabled = false;
}
}
else
{
}
rdr.Close();
DB.getInstance(DB.MSSQL).close();
}
private void remove_Click(object sender, RoutedEventArgs e)
{
if (invView.SelectedIndex == -1) return;
if (System.Windows.Forms.MessageBox.Show("Really delete?", "Confirm delete", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
DB.getInstance(DB.MSSQL).ExecuteNonQuery(
"delete from items where item_code='" + itemCodeCB.Text + "'");
this.NavigationService.RemoveBackEntry();
this.NavigationService.Navigate(new Inventory());
}
}
private void invView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//MessageBox.Show("" + invView.SelectedIndex);
if (invView.SelectedIndex == -1)
{
return;
}
DataRow r = t.Rows[invView.SelectedIndex];
itemCodeCB.Text = (string)r[0];
itemCB.Text = (string)r[1];
//barCodeCombo.Text = (string)r[2];
//unit.IsEnabled = false;
//unitValue.IsEnabled = false;
}
private void vendor_SelectedValueChanged(object sender, EventArgs e)
{
}
}
}