-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewTableForm.cs
346 lines (318 loc) · 13.7 KB
/
ViewTableForm.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using ayoti.ServiceReference1;//ayoti
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Services.Client;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace ayoti
{
public partial class ViewTableForm : Form
{
class SaleP
{
public string Customer { get; set; }
public string SalesPerson { get; set; }
}
class SP
{
public string Code { get; set; }
public string SalesPerson { get; set; }
}
private string routecode;
List<SP> sps = new List<SP>() {
new SP() { Code = "AY016", SalesPerson = "James Ogwalla" },
new SP() { Code = "AY018", SalesPerson = "Isaac Wandera" },
new SP() { Code = "AY019", SalesPerson = "Ezekiel Owino" },
new SP() { Code = "AY020", SalesPerson = "Harrison Wandera" },
new SP() { Code = "AY021", SalesPerson = "Wilson Mukuna" },
new SP() { Code = "AY022", SalesPerson = "Tobias Otieno" },
new SP() { Code = "AY024", SalesPerson = "Bob Ndong'a" },
new SP() { Code = "AY028", SalesPerson = "Michael Oduor" },
new SP() { Code = "AY029", SalesPerson = "Lameck Oketch" },
new SP() { Code = "AY035", SalesPerson = "Gordon Otieno" },
};
List<SP> routes = new List<SP>() {
new SP() { Code = "R001", SalesPerson = "Mashinani/Rural" },
new SP() { Code = "R002", SalesPerson = "Kwisero" },
new SP() { Code = "R737", SalesPerson = "Staff" },
new SP() { Code = "R782", SalesPerson = "Lake Key accounts" },
new SP() { Code = "R066", SalesPerson = "Kondele" },
new SP() { Code = "R067", SalesPerson = "Town" },
new SP() { Code = "R071", SalesPerson = "Khayega" },
new SP() { Code = "R072", SalesPerson = "Khemaio" },
new SP() { Code = "R073", SalesPerson = "Luanda" },
new SP() { Code = "R074", SalesPerson = "Mbale" },
new SP() { Code = "R075", SalesPerson = "Serem" },
new SP() { Code = "R075", SalesPerson = "Kapsabet" },
new SP() { Code = "R077", SalesPerson = "Muhoroni" },
new SP() { Code = "R078", SalesPerson = "Gem Alego" },
new SP() { Code = "R079", SalesPerson = "Uyoma" },
new SP() { Code = "R080", SalesPerson = "Yimbo" },
};
private SqlConnection con;
string DBCon = ConfigurationManager.ConnectionStrings["ayoti.Properties.Settings.AyotiConnectionString"].ConnectionString;
List<Sale> saleslst = new List<Sale>();
List<Sales_Invoice> salesInvoicelst = new List<Sales_Invoice>();
IEnumerable<SaleP> customerlst = null;
public ViewTableForm()
{
InitializeComponent();
}
private void salesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.salesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.ayotiDataSet);
}
private void ViewTableForm_Load(object sender, EventArgs e)
{
getLocations();
this.salesTableAdapter.Fill(this.ayotiDataSet.Sales);
saleslst = this.ayotiDataSet.Tables[0].AsEnumerable().Select(d => new Sale
{
Id = d.Field<int>("Id"),
Product = d.Field<string>("Product"),
Category = d.Field<string>("Category"),
Quantity = d.Field<double>("Quantity"),
Customer = d.Field<string>("Customer"),
SalesPerson = d.Field<string>("SalesPerson"),
Uploaded = d.Field<bool>("Uploaded")
}).Where(d => d.Uploaded == false).ToList();
customerlst = saleslst.GroupBy(x => new { x.Customer, x.SalesPerson })
.Select(grp => new SaleP()
{
Customer = grp.First().Customer,
SalesPerson = grp.First().SalesPerson
}).ToList();
loadRoutes();
cmbRoute.Text = "Mashinani/Rural";
}
void loadRoutes()
{
foreach (SP route in routes)
{
cmbRoute.Items.Add(route.SalesPerson);
}
}
private void btnUpload_Click(object sender, EventArgs e)
{
btnUpload.Enabled = false;
btnBBack.Enabled = false;
label1.Text = "Started Upload...";
NAV nav = new NAV(new Uri("https://test.financials.dynamics.com:7048/MS/OData/Company('AYOTI')"));//AYOTI LIVE
nav.Credentials = new NetworkCredential("ADMIN", "testpass");
routecode = routes.Where(r => r.SalesPerson.Equals(cmbRoute.Text)).FirstOrDefault().Code;
// PrintCustomersCalledCust(nav);
CreateInvoice(nav);
//MockCreateInvoice(nav);
}
public void getLocations()
{
DataTable dt = new DataTable();
using (var con = new SqlConnection(DBCon))
{
con.Open();
try
{
SqlCommand cmd = new SqlCommand("SELECT location FROM locations", con);
dt.Load(cmd.ExecuteReader());
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
return;
}
}
cmbLocation.DataSource = dt;
cmbLocation.ValueMember = dt.Columns[0].ColumnName; ;
cmbLocation.DisplayMember = dt.Columns[0].ColumnName;
}
void MockCreateInvoice(NAV nav)
{
Sales_Invoice invoice = null;
Sales_InvoiceSalesLines line = null;
invoice = new Sales_Invoice()
{
Document_Type = "Invoice",
Sell_to_Customer_No = "KE0001340",//"10000",
Salesperson_Code = "AY016",
Posting_Date = dateTimePicker.Value,
//Currency_Code = "KES",
//live
// Shortcut_Dimension_1_Code = "R737",
//other
Shortcut_Dimension_2_Code = "AY016",
Location_Code = "KAJ295 Y",
};
nav.AddObject("Sales_Invoice", invoice);
line = new Sales_InvoiceSalesLines()
{
Description = "IT00004",
//"1025",
Quantity = (Decimal)2
};
nav.AddRelatedObject(invoice, "Sales_InvoiceSalesLines", line);
try
{
DataServiceResponse response = nav.SaveChanges();
if (response != null)
{
int uploaded = setUploaded("KE0001340", "KE0001340");
if (uploaded < 0)
{
string ex = string.Format("Did not update any records with customer: {0} and salesperson: {1}", "KE0001340", "KE0001340");
throw new System.InvalidOperationException(ex);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
var confirmResult = MessageBox.Show("Do you want to exit the application ??",
"Confirm Exit",
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
// If 'Yes', do something here.
Application.Exit();
return;
}
else
{
// If 'No', do something here.
}
}
finally
{
}
btnUpload.Enabled = true;
btnBBack.Enabled = true;
label1.Text = "Completed Upload";
}
void CreateInvoice(NAV nav)
{
Sales_Invoice invoice = null;
Sales_InvoiceSalesLines line = null;
foreach (var c in customerlst)
{
Sale currentsale = null;
List<Sale> salesline = saleslst.Where(x => x.Customer == c.Customer).ToList();
invoice = new Sales_Invoice()
{
Document_Type = "Invoice",
Sell_to_Customer_No = c.Customer.Trim(),
Salesperson_Code = getSalesPerson(c.SalesPerson.Trim()),
Posting_Date = dateTimePicker.Value,
Shipment_Date = dateTimePicker.Value,
Due_Date = dateTimePicker.Value,
//Currency_Code = "KES",
//live
Shortcut_Dimension_1_Code = routecode,
//other
Shortcut_Dimension_2_Code = getSalesPerson(c.SalesPerson.Trim()),
Location_Code = cmbLocation.Text
};
nav.AddObject("Sales_Invoice", invoice);
foreach (var saleline in salesline)
{
currentsale = saleline;
line = new Sales_InvoiceSalesLines()
{
Description = saleline.Product,
Quantity = (Decimal)saleline.Quantity
};
nav.AddRelatedObject(invoice, "Sales_InvoiceSalesLines", line);
}
try
{
DataServiceResponse response = nav.SaveChanges();
if (response != null)
{
int uploaded = setUploaded(c.Customer, c.SalesPerson.Trim());
if(uploaded< 0)
{
string ex = string.Format("Did not update any records with customer: {0} and salesperson: {1}", c.Customer, c.SalesPerson);
throw new System.InvalidOperationException(ex);
}
}
}
catch ( Exception ex)
{
if(currentsale != null)
{
String errorstr = String.Format("Customer :{0} with Product {1} Quantity {2} .Error: ", c.Customer, currentsale.Product, currentsale.Quantity);
Logger.LogThisLine(errorstr + ex.Message);
}
MessageBox.Show(ex.ToString());
var confirmResult = MessageBox.Show("Do you want to exit the application ??",
"Confirm Exit",
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
// If 'Yes', do something here.
Application.Exit();
return;
}
else
{
// If 'No', do something here.
}
}
finally
{
}
}
btnUpload.Enabled = true;
btnBBack.Enabled = true;
label1.Text = "Completed Upload";
System.Media.SystemSounds.Exclamation.Play();
MessageBox.Show("Completed Upload");
}
int setUploaded(string customer,string salesperson)
{
int rowsUpdated = 0;
using (con = new SqlConnection(DBCon))
{
con.Open();
string sql = "UPDATE Sales SET Uploaded=1 WHERE Customer =@customer AND SalesPerson=@salesperson";
using (SqlCommand command = new SqlCommand(sql, con))
{
command.Parameters.AddWithValue("@customer", customer);
command.Parameters.AddWithValue("@salesperson", salesperson);
rowsUpdated = command.ExecuteNonQuery();
}
}
return rowsUpdated;
}
string getSalesPerson(string salesp)
{
return sps.Where(x => x.SalesPerson == salesp).Select(x => x.Code).FirstOrDefault();
}
private void btnFetch_Click(object sender, EventArgs e)
{
//// TODO: This line of code loads data into the 'ayotiDataSet.Sales' table. You can move, or remove it, as needed.
//this.salesTableAdapter.Fill(this.ayotiDataSet.Sales);
//saleslst = this.ayotiDataSet.Tables[0].AsEnumerable().Select(d => new Sale
//{
// Id = d.Field<int>("Id"),
// Product = d.Field<string>("Product"),
// Category = d.Field<string>("Category"),
// Quantity = d.Field<double>("Quantity"),
// Customer = d.Field<string>("Customer"),
// SalesPerson = d.Field<string>("SalesPerson")
//}).ToList();
//customerlst = saleslst.Select(x => new SaleP() { Customer = x.Customer, SalesPerson = x.SalesPerson }).ToList().Distinct();
}
private void btnBBack_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.Show();
this.Hide();
}
}
}