-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommande.cpp
More file actions
392 lines (320 loc) · 13.8 KB
/
Commande.cpp
File metadata and controls
392 lines (320 loc) · 13.8 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "Commande.hpp"
extern connection C;
namespace pqxx {
template<> struct string_traits<wxString> : public string_traits<char const*>
{
static const bool is_null(const wxString& str) noexcept
{
return str.IsNull();
}
static const char* c_str(const wxString& str) noexcept
{
return str.mb_str();
}
};
}
Commande::Commande(wxPanel* panel_parent, Membre* membre) : wxBoxSizer(wxHORIZONTAL), membre(membre)
{
static_sizer_commandes = new wxStaticBoxSizer(wxHORIZONTAL, panel_parent, "Commandes");
static_sizer_commandes->SetMinSize(wxSize(300, 200));
this->panel_parent = panel_parent;
scrole_commandes = new wxScrolledWindow(panel_parent);
sizer_commandes = new wxBoxSizer(wxVERTICAL);
sizer_valid_info = new wxBoxSizer(wxVERTICAL);
sizer_prix = new wxBoxSizer(wxHORIZONTAL);
sizer_monnaie = new wxBoxSizer(wxHORIZONTAL);
sizer_rendre = new wxBoxSizer(wxHORIZONTAL);
sizer_anuler_valider = new wxBoxSizer(wxHORIZONTAL);
affiche_prix = new wxStaticText(panel_parent, -1, "Prix de la commande : 0.00");
sizer_prix->Add(affiche_prix, 0, wxALL | wxEXPAND, 0);
monnaie = new wxSpinCtrlDouble(panel_parent, -1, wxEmptyString, wxDefaultPosition, wxSize(130,30), 16384L, 0.0, 100.0, 0.0, 0.01);
wxStaticText* text_monnaie = new wxStaticText(panel_parent, -1, "Monnaie : ");
sizer_monnaie->Add(text_monnaie, 0, wxALL | wxEXPAND, 0);
sizer_monnaie->Add(monnaie, 0, wxALL | wxEXPAND, 0);
affiche_monnaie_rendu = new wxStaticText(panel_parent, -1, "Monnaie a rendre : 0.00");
sizer_rendre->Add(affiche_monnaie_rendu, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(sizer_prix, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(sizer_monnaie, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(sizer_rendre, 0, wxALL | wxEXPAND, 0);
btn_anulation_commande = new wxButton(panel_parent, -1, "Annuler");
btn_validation_commande = new wxButton(panel_parent, -1, "Valider");
btn_sup_presedante_commande = new wxButton(panel_parent, -1, "Supprimer la commande precedante");
vendre_prix_coutent = new wxButton(panel_parent, -1, "Vendre a prix coutant");
vendre_a_perte = new wxButton(panel_parent, -1, "Vendre a perte");
sizer_anuler_valider->Add(btn_anulation_commande, 0, wxALL | wxEXPAND, 0);
sizer_anuler_valider->Add(btn_validation_commande, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(sizer_anuler_valider, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(btn_sup_presedante_commande, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(vendre_prix_coutent, 0, wxALL | wxEXPAND, 0);
sizer_valid_info->Add(vendre_a_perte, 0, wxALL | wxEXPAND, 0);
scrole_commandes->SetSizer(sizer_commandes);
static_sizer_commandes->Add(scrole_commandes, 1, wxALL | wxEXPAND, 0);
this->Add(static_sizer_commandes, 1, wxALL | wxEXPAND, 0);
this->Add(sizer_valid_info, 1, wxALL | wxEXPAND, 15);
MoodAdmin();
monnaie->Bind(wxEVT_SPINCTRLDOUBLE, &Commande::EventMonnaie, this);
btn_anulation_commande->Bind(wxEVT_BUTTON, &Commande::EventAnulationCommande, this);
btn_validation_commande->Bind(wxEVT_BUTTON, &Commande::EventValidationCommande, this);
btn_sup_presedante_commande->Bind(wxEVT_BUTTON, &Commande::EventSuprimeCommande, this);
vendre_prix_coutent->Bind(wxEVT_BUTTON, &Commande::CommandeCoutent, this);
vendre_a_perte->Bind(wxEVT_BUTTON, &Commande::CommandePerte, this);
}
void Commande::NewCommande(Article* article)
{
wxString nom = article->GetNom();
if (commandes.find(article) == commandes.end())
{
Produit* new_produit;
if ((new_produit = new Produit(scrole_commandes, this, nom)))
{
commandes[article] = new_produit;
sizer_commandes->Add(new_produit, 0, wxALL | wxEXPAND, 0);
sizer_commandes->Layout();
scrole_commandes->FitInside();
scrole_commandes->SetVirtualSize(sizer_commandes->GetSize());
scrole_commandes->SetScrollRate(5, 5);
}
else
{
wxLogError("Une erreur est survenue au moment de l'ajout du produit");
}
}
else{
commandes[article]->AddProduit();
}
ReffrechMonnaieARendre();
ReffrechTotal();
}
double Commande::CalculMonnaie(){
double totale = CalculTotal();
double monnaie_a_rendre = 0.0;
double argent_doner = monnaie->GetValue();
if(totale < argent_doner){
monnaie_a_rendre = argent_doner-totale;
}
return monnaie_a_rendre;
}
double Commande::CalculTotal(){
double totale = 0.0;
for (auto it = commandes.begin(); it != commandes.end(); ++it)
{
totale += it->first->GetPrix()*it->second->GetNbProduit();
}
return totale;
}
void Commande::ReffrechTotal(){
wxString afichage = wxString::Format("Prix de la commande : %5.2lf", CalculTotal());
affiche_prix->SetLabel(afichage);
}
void Commande::ReffrechMonnaieARendre(){
wxString afichage = wxString::Format("Monnaie a rendre : %5.2lf", CalculMonnaie());
affiche_monnaie_rendu->SetLabel(afichage);
}
void Commande::EventMonnaie(wxCommandEvent& event){
ReffrechMonnaieARendre();
}
void Commande::ReffrechCommande(){
ClearCommande();
cout << "la" << endl;
ReffrechMonnaieARendre();
ReffrechTotal();
monnaie->SetValue(0.0);
}
void Commande::CommandeCoutent(wxCommandEvent& event){
AjouterVenteCoutent();
Validation();
ReffrechCommande();
}
void Commande::CommandePerte(wxCommandEvent& event){
AjouterVentePerte();
Validation();
ReffrechCommande();
}
void Commande::EventValidationCommande(wxCommandEvent& event){
AjouterVente();
Validation();
ReffrechCommande();
}
void Commande::AnulationCommande(){
Anulation();
ReffrechCommande();
}
void Commande::EventAnulationCommande(wxCommandEvent& event){
AnulationCommande();
}
void Commande::Validation(){
for (auto it = commandes.begin(); it != commandes.end(); ++it)
it->first->ConfirmeVente();
}
void Commande::Anulation(){
for (auto it = commandes.begin(); it != commandes.end(); ++it)
it->first->AnulationsVente();
}
void Commande::ClearCommande(){
for (auto it = commandes.begin(); it != commandes.end(); it++)
{
// Suppression de l'élément du sizer
sizer_commandes->Detach(it->second);
it->second->Destroy();
}
// Suppression de l'élément de la map
commandes.clear();
sizer_commandes->Layout();
}
void Commande::SupprimerProduit(Produit* produit)
{
AugmentArticle(produit);
if (produit->GetNbProduit() > 1){
produit->SupProduit();
}else{
DestroyProduit(produit);
}
ReffrechMonnaieARendre();
ReffrechTotal();
}
void Commande::AugmentArticle(Produit* produit){
for (auto it = commandes.begin(); it != commandes.end(); ++it)
{
if (it->second == produit)
{
it->first->AnuleProduit();
break;
}
}
}
void Commande::DestroyProduit(Produit* produit)
{
for (auto it = commandes.begin(); it != commandes.end(); ++it)
{
if (it->second == produit)
{
// Suppression de l'élément du sizer
sizer_commandes->Detach(produit);
// Suppression de l'élément de la map
commandes.erase(it);
// Destruction de l'objet produit
produit->Destroy();
break;
}
}
sizer_commandes->Layout();
}
void Commande::MoodUtilisateur(){
btn_anulation_commande->Enable(true);
btn_validation_commande->Enable(true);
btn_sup_presedante_commande->Enable(true);
vendre_prix_coutent->Enable(true);
vendre_a_perte->Enable(true);
}
void Commande::MoodAdmin(){
AnulationCommande();
cout << "peuter la" << endl;
btn_anulation_commande->Enable(false);
btn_validation_commande->Enable(false);
btn_sup_presedante_commande->Enable(false);
vendre_prix_coutent->Enable(false);
vendre_a_perte->Enable(false);
}
void Commande::AjouterVente() {
try {
vector<pair<string, string>> vendeurs = membre->GetListPersonne();
work txn(C);
// Insertion de la vente dans la table HISTORIQUE_VENTE avec la date actuelle
string insertVente = "INSERT INTO HISTORIQUE_VENTE (DATE_ET_HEURE) VALUES (CURRENT_TIMESTAMP)";
txn.exec(insertVente);
// Récupération de la date et heure de la vente insérée
result res = txn.exec("SELECT DATE_ET_HEURE FROM HISTORIQUE_VENTE ORDER BY DATE_ET_HEURE DESC LIMIT 1");
string dateVente = res[0][0].as<string>();
// Insertion des articles vendus dans la table CONTENU_VENTE
string insertContenuVente = "INSERT INTO CONTENU_VENTE (NOM_SNACK, DATE_ET_HEURE, OCCURRENCE, PRIX, PRIX_ACHAT) VALUES ($1, $2, $3, $4, $5)";
for (auto article = commandes.begin(); article != commandes.end(); ++article){
txn.exec_params(insertContenuVente, article->first->GetNom(), dateVente, article->second->GetNbProduit(), article->first->GetPrix(), article->first->GetPrixAchat());
}
// Insertion des vendeurs dans la table VENDU_PAR
string insertVenduPar = "INSERT INTO VENDU_PAR (DATE_ET_HEURE, NOM, PRENOM) VALUES ($1, $2, $3)";
for (const auto& vendeur : vendeurs) {
txn.exec_params(insertVenduPar, dateVente, vendeur.first, vendeur.second);
}
txn.commit();
} catch (const exception& e) {
cerr << "Une erreur s'est produite lors de l'ajout de la vente : " << e.what() << std::endl;
}
}
void Commande::AjouterVenteCoutent() {
try {
vector<pair<string, string>> vendeurs = membre->GetListPersonne();
work txn(C);
// Insertion de la vente dans la table HISTORIQUE_VENTE avec la date actuelle
string insertVente = "INSERT INTO HISTORIQUE_VENTE (DATE_ET_HEURE) VALUES (CURRENT_TIMESTAMP)";
txn.exec(insertVente);
// Récupération de la date et heure de la vente insérée
result res = txn.exec("SELECT DATE_ET_HEURE FROM HISTORIQUE_VENTE ORDER BY DATE_ET_HEURE DESC LIMIT 1");
string dateVente = res[0][0].as<string>();
// Insertion des articles vendus dans la table CONTENU_VENTE
string insertContenuVente = "INSERT INTO CONTENU_VENTE (NOM_SNACK, DATE_ET_HEURE, OCCURRENCE, PRIX, PRIX_ACHAT) VALUES ($1, $2, $3, $4, $5)";
for (auto article = commandes.begin(); article != commandes.end(); ++article){
txn.exec_params(insertContenuVente, article->first->GetNom(), dateVente, article->second->GetNbProduit(), article->first->GetPrixAchat(), article->first->GetPrixAchat());
}
// Insertion des vendeurs dans la table VENDU_PAR
string insertVenduPar = "INSERT INTO VENDU_PAR (DATE_ET_HEURE, NOM, PRENOM) VALUES ($1, $2, $3)";
for (const auto& vendeur : vendeurs) {
txn.exec_params(insertVenduPar, dateVente, vendeur.first, vendeur.second);
}
txn.commit();
} catch (const exception& e) {
cerr << "Une erreur s'est produite lors de l'ajout de la vente : " << e.what() << std::endl;
}
}
void Commande::AjouterVentePerte() {
try {
vector<pair<string, string>> vendeurs = membre->GetListPersonne();
work txn(C);
// Insertion de la vente dans la table HISTORIQUE_VENTE avec la date actuelle
string insertVente = "INSERT INTO HISTORIQUE_VENTE (DATE_ET_HEURE) VALUES (CURRENT_TIMESTAMP)";
txn.exec(insertVente);
// Récupération de la date et heure de la vente insérée
result res = txn.exec("SELECT DATE_ET_HEURE FROM HISTORIQUE_VENTE ORDER BY DATE_ET_HEURE DESC LIMIT 1");
string dateVente = res[0][0].as<string>();
// Insertion des articles vendus dans la table CONTENU_VENTE
string insertContenuVente = "INSERT INTO CONTENU_VENTE (NOM_SNACK, DATE_ET_HEURE, OCCURRENCE, PRIX, PRIX_ACHAT) VALUES ($1, $2, $3, $4, $5)";
for (auto article = commandes.begin(); article != commandes.end(); ++article){
txn.exec_params(insertContenuVente, article->first->GetNom(), dateVente, article->second->GetNbProduit(), 0.0f, article->first->GetPrixAchat());
}
// Insertion des vendeurs dans la table VENDU_PAR
string insertVenduPar = "INSERT INTO VENDU_PAR (DATE_ET_HEURE, NOM, PRENOM) VALUES ($1, $2, $3)";
for (const auto& vendeur : vendeurs) {
txn.exec_params(insertVenduPar, dateVente, vendeur.first, vendeur.second);
}
txn.commit();
} catch (const exception& e) {
cerr << "Une erreur s'est produite lors de l'ajout de la vente : " << e.what() << std::endl;
}
}
void Commande::EventSuprimeCommande(wxCommandEvent& event)
{
if(HistoriqueNotEmpty()){
string date_heur = RecentCommande();
CommandeSup* cs = new CommandeSup(panel_parent, date_heur);
if(cs->ShowModal() == wxID_OK){
deletCommande(date_heur);
}
}
}
CommandeSup::CommandeSup(wxPanel* panel_parent, string date_heur) : wxDialog(panel_parent, -1, wxString("La commande " + date_heur +" va etre suprimer"), wxDefaultPosition, wxSize(400,500))
{
sizer_commande = new wxBoxSizer(wxVERTICAL);
description = descriptionCommande(date_heur);
text_description = new wxStaticText(this, -1, description);
sizer_commande->Add(text_description, 1, wxALL | wxEXPAND, 0);
wxStdDialogButtonSizer* ButtonSizer = new wxStdDialogButtonSizer;
wxButton* BtnOk = new wxButton(this, wxID_OK, _("&OK"), wxDefaultPosition,
wxDefaultSize, 0);
BtnOk->SetDefault();
ButtonSizer->AddButton(BtnOk);
wxButton* BtnCancel = new wxButton(this, wxID_CANCEL, _("&Cancel"),
wxDefaultPosition, wxDefaultSize, 0);
ButtonSizer->AddButton(BtnCancel);
ButtonSizer->Realize();
sizer_commande->Add(ButtonSizer, 0, wxALIGN_RIGHT|wxALL, 5);
this->SetSizer(sizer_commande);
}