/
frmVendaInfo34.cs
345 lines (303 loc) · 12.6 KB
/
frmVendaInfo34.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SistemaLojaGames
{
public partial class frmVendaInfo34 : Form
{
public frmVendaInfo34()
{
InitializeComponent();
}
private List<ClassCarrinho> lstCarrinho = new List<ClassCarrinho>();
private decimal VendaT = 0;
private int CodLastProd = 0;
private int QtdeN = 0;
private void frmVendaInfo34_Load(object sender, EventArgs e)
{
ClassFuncionario c = new ClassFuncionario();
cbFuncionario.DataSource = c.SearchFuncV();
cbFuncionario.DisplayMember = "Nome";
cbFuncionario.ValueMember = "CodFuncionario";
cbFuncionario.SelectedIndex = -1;
}
private void btBuscaCliente_Click(object sender, EventArgs e)
{
ClassCliente c = new ClassCliente();
string s = txtPesqCliente.Text;
dgvCliente.DataSource = c.SearchClieNomeV(s);
}
private void btBuscaProduto_Click(object sender, EventArgs e)
{
ClassProduto c = new ClassProduto();
string s = txtPesqProduto.Text;
dgvProduto.DataSource = c.SearchProdNomeV(s);
}
private void dgvProduto_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
ClassProduto cp = new ClassProduto();
bool a = cp.VProdFill((int)dgvProduto.SelectedRows[0].Cells[0].Value);
if (a)
{
int Estq, Qtde;
bool tem = lstCarrinho.Any(c => c.Produto == (int)dgvProduto.SelectedRows[0].Cells[0].Value);
if (tem)
{
int linha = -1;
foreach (DataGridViewRow row in dgvItens.Rows)
{
if (row.Cells[0].Value.ToString().Equals(dgvProduto.SelectedRows[0].Cells[0].Value.ToString()))
{
linha = row.Index;
break;
}
}
Estq = cp.QtdeProd - Convert.ToInt32(dgvItens.Rows[linha].Cells[2].Value);
if (Estq == 0) Qtde = 0;
else Qtde = 1;
}
else
{
Estq = cp.QtdeProd;
Qtde = 1;
}
txtProduto.Text = cp.NomeProd;
txtEstoque.Text = Estq.ToString();
txtValor.Text = cp.PrecoProd.ToString("n2");
txtQtde.Text = Qtde.ToString();
txtQtde.Select();
txtQtde_TextChanged(this, new EventArgs());
CodLastProd = cp.CodProd;
QtdeN = 0;
}
}
private void txtQtde_TextChanged(object sender, EventArgs e)
{
if (txtQtde.Text != "" && txtValor.Text != "")
{
double res = Convert.ToDouble(txtQtde.Text) * Convert.ToDouble(txtValor.Text);
if (Convert.ToDouble(txtQtde.Text) > Convert.ToDouble(txtEstoque.Text))
{
MessageBox.Show("A Quantidade excede o Estoque!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtQtde.Text = "0";
res = 0;
}
txtTotal.Text = res.ToString("n2");
}
}
private void txtValorDesconto_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtValorDesconto.Text))
{
double t = Convert.ToDouble(txtValorTotal.Text);
txtTotalVenda.Text = t.ToString("n2");
txtTotalDesconto.Text = "0,00";
}
else
{
double p, t, v;
p = 1 - (Convert.ToDouble(txtValorDesconto.Text) / 100);
v = Convert.ToDouble(txtValorTotal.Text);
t = v * p;
txtTotalVenda.Text = t.ToString("n2");
txtTotalDesconto.Text = (Convert.ToDouble(txtValorDesconto.Text) / 100 * v).ToString("n2");
}
}
private void AttGrid()
{
ClassProduto cp = new ClassProduto();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Código"));
dt.Columns.Add(new DataColumn("Produto"));
dt.Columns.Add(new DataColumn("Qtde."));
dt.Columns.Add(new DataColumn("Preço"));
foreach (ClassCarrinho item in lstCarrinho)
{
if (item.Produto==CodLastProd && QtdeN != 0) item.Qtde = QtdeN;
dt.Rows.Add(item.Produto, cp.VProdNome(item.Produto), item.Qtde, item.Valor);
dt.AcceptChanges();
}
dgvItens.DataSource = dt;
}
private void AttQtde(int qtde, int cod)
{
ClassProduto cp = new ClassProduto();
cp.VProdFill(cod);
int e = cp.QtdeProd;
cp.VProdUpdate(e-qtde, cod);
}
private void btFechaVenda_Click(object sender, EventArgs e)
{
if (cbFuncionario.SelectedIndex != -1 && dgvCliente.DataSource != null && dgvItens.DataSource != null && cbFormaPagamento.SelectedIndex != -1)
{
ClassVenda cv = new ClassVenda();
cv.Cliente = (int)dgvCliente.SelectedRows[0].Cells[0].Value;
cv.Vendedor = Convert.ToInt32(cbFuncionario.SelectedValue);
cv.Valor = Convert.ToDecimal(txtTotalVenda.Text);
cv.Obs = txtObservacao.Text;
cv.FormaPgto = cbFormaPagamento.SelectedItem.ToString();
cv.PDesconto = Convert.ToInt32(txtValorDesconto.Text);
cv.VDesconto = Convert.ToDecimal(txtTotalDesconto.Text);
bool aux = cv.FVenda();
if (aux)
{
aux = false;
foreach (ClassCarrinho i in lstCarrinho)
{
i.Venda = cv.Venda;
aux = i.CCarrinho(cv.CodVendaCr);
AttQtde(i.Qtde, i.Produto);
}
if (aux==false)
{
MessageBox.Show("Sucesso");
this.FrmClean();
}
}
else MessageBox.Show("Erro query");
}
else MessageBox.Show("erro campos");
}
private void FrmClean()
{
txtProduto.Clear();
txtQtde.Clear();
txtEstoque.Clear();
txtValor.Clear();
txtTotal.Clear();
txtTotalDesconto.Clear();
txtValorDesconto.Clear();
txtPesqCliente.Clear();
txtPesqProduto.Clear();
txtObservacao.Clear();
txtValorTotal.Clear();
txtTotalVenda.Text = "";
cbFormaPagamento.SelectedIndex = -1;
cbFuncionario.SelectedIndex = -1;
dgvCliente.DataSource = null;
dgvItens.DataSource = null;
dgvProduto.DataSource = null;
}
private void btAddProduto_Click(object sender, EventArgs e)
{
bool tem = lstCarrinho.Any(c => c.Produto == CodLastProd);
if (string.IsNullOrEmpty(txtTotal.Text) || string.IsNullOrEmpty(txtQtde.Text) || Convert.ToInt32(txtQtde.Text) == 0)
{
MessageBox.Show("Não há um Produto para ser Inserido!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (tem)
{
int linha = -1;
foreach (DataGridViewRow row in dgvItens.Rows)
{
if (row.Cells[0].Value.ToString().Equals(CodLastProd.ToString()))
{
linha = row.Index;
break;
}
}
QtdeN = Convert.ToInt32(dgvItens.Rows[linha].Cells[2].Value) + Convert.ToInt32(txtQtde.Text);
dgvItens.Rows[linha].Cells[2].Value = QtdeN;
AttGrid();
txtValorDesconto.Text = "0";
txtValorDesconto_TextChanged(this, new EventArgs());
txtProduto.Clear();
txtQtde.Text = "0";
txtEstoque.Clear();
txtValor.Clear();
txtTotal.Clear();
}
else
{
ClassCarrinho cr = new ClassCarrinho();
decimal vTotal = 0;
int qtdeE = Convert.ToInt32(txtEstoque.Text);
int qtdeV = Convert.ToInt32(txtQtde.Text);
if (!string.IsNullOrEmpty(txtTotal.Text))
{
vTotal = Convert.ToDecimal(txtTotal.Text);
VendaT = vTotal + VendaT;
txtValorTotal.Text = VendaT.ToString("n2");
txtEstoque.Text = (qtdeE - qtdeV).ToString();
cr.Valor = Convert.ToDecimal(txtTotal.Text);
cr.Produto = (int)(dgvProduto.SelectedRows[0].Cells[0].Value);
cr.Qtde = Convert.ToInt32(txtQtde.Text);
lstCarrinho.Add(cr);
AttGrid();
txtValorDesconto.Text = "0";
txtValorDesconto_TextChanged(this, new EventArgs());
txtProduto.Clear();
txtQtde.Text = "0";
txtEstoque.Clear();
txtValor.Clear();
txtTotal.Clear();
}
else
{
MessageBox.Show("Selecione um Produto!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
private void btRemoveProduto_Click(object sender, EventArgs e)
{
if (dgvItens.Rows.Count > 0)
{
decimal v = Convert.ToDecimal(dgvItens.SelectedRows[0].Cells[3].Value);
VendaT = VendaT - v;
txtTotalVenda.Text = VendaT.ToString("n2");
txtValorTotal.Text = VendaT.ToString("n2");
lstCarrinho.RemoveAt(dgvItens.CurrentRow.Index);
AttGrid();
txtValorDesconto_TextChanged(this, new EventArgs());
}
else MessageBox.Show("Não há Item para ser Removido!");
}
private void dgvProduto_CellContentClick(object sender, DataGridViewCellMouseEventArgs e)
{
}
private void dgvProduto_CellContentClick(object sender, KeyPressEventArgs e)
{
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
public const int WM_LBUTTONDOWN = 0x0201;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_LBUTTONDOWN)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void Arrastar(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
}