-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bill.cpp
166 lines (130 loc) · 6.48 KB
/
Bill.cpp
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
//
// PROIekt - symulacja supermarketu
//
// Created by Ksawery Chodyniecki and Paweł Müller.
//
#include "Bill.hpp"
#include "Customer.hpp"
#include "AddOns/StringOperations.hpp"
#include <utility>
#include <fstream>
#include <exception>
using namespace std;
Bill::Bill(unsigned short argID, Customer argBuyer, Customer argSeller) {
ID = argID;
buyer = argBuyer;
seller = argSeller;
}
// Getters:
unsigned short Bill::getID() const {
return ID;
}
Customer Bill::getBuyer() const {
return buyer;
}
Customer Bill::getSeller() const {
return seller;
}
// Setters:
void Bill::setBuyer(Customer newBuyer) {
buyer = newBuyer;
}
void Bill::setSeller(Customer newSeller) {
seller = newSeller;
}
void Bill::setID(unsigned short newID) {
ID = newID;
}
string Bill::convertPricePLN(unsigned short price) const {
string prefix = to_string(price / 100), suffix = to_string(price % 100);
return prefix + "." + (suffix.length() == 1 ? ("0" + suffix) : suffix);
}
string Bill::convertToKg(unsigned short quantity) const {
string prefix = to_string(quantity / 1000), suffix = to_string(quantity % 1000);
return prefix + "." + (suffix.length() == 1 ? ("00" + suffix) : (suffix.length() == 2 ? ("0" + suffix) : suffix));
}
void Bill::save(string filename) {
fstream file;
file.open(filename, ios::out);
file << generate();
file.close();
}
/* ======================= INVOICE ======================= */
string Invoice::generate() const {
char invoiceWidth = 90;
unsigned short priceBrutto = 0, totalBrutto = 0;
string output = stringAlign("_", 2, invoiceWidth, "_");
output += "\n";
output += "|" + stringAlign("INVOICE", 2, invoiceWidth - 2) + "|\n";
output += "|" + stringAlign("#" + to_string(ID), 2, invoiceWidth - 2) + "|\n";
output += "|" + stringAlign("BILL FROM:", 2, (invoiceWidth - 4) / 2) + " " + stringAlign("BILL TO:", 2, (invoiceWidth - 4) / 2) + "|\n";
output += "|" + stringAlign(seller.getName(), 2, (invoiceWidth - 4) / 2) + " " + stringAlign(buyer.getName(), 2, (invoiceWidth - 4) / 2) + "|\n";
output += "|" + stringAlign(seller.getStreet() + " " + seller.getBuildingNumber(), 2, (invoiceWidth - 4) / 2) + " " + stringAlign(buyer.getStreet() + " " + buyer.getBuildingNumber(), 2, (invoiceWidth - 4) / 2) + "|\n";
output += "|" + stringAlign(seller.getCity() + " " + seller.getPostcode(), 2, (invoiceWidth - 4) / 2) + " " + stringAlign(buyer.getCity() + " " + buyer.getPostcode(), 2, (invoiceWidth - 4) / 2) + "|\n";
output += "|" + stringAlign(seller.getTaxNumber(), 2, (invoiceWidth - 4) / 2) + " " + stringAlign(buyer.getTaxNumber()!="0"?buyer.getTaxNumber():" ", 2, (invoiceWidth - 4) / 2) + "|\n";
output += "|" + stringAlign("_", 2, invoiceWidth - 2, "_") + "|\n";
output += "| NAME | QTY | U | NETTO | BRUTTO | VAT | VAT AMT | BRUTTO AMT |\n";
for (auto basketPair: buyer.getBasket()) {
Product *product = basketPair.first;
unsigned short quantity = basketPair.second;
output += "|";
output += stringAlign(product -> getName(), 0, 24);
output += "|";
output += stringAlign(product -> getMeasureUnits() == g ? convertToKg(quantity) : to_string(quantity), 1, 7);
output += "|";
output += stringAlign(product -> getMeasureUnits() == g ? "kg" : "pcs", 2, 3);
output += "|";
output += stringAlign(convertPricePLN(product -> getPrice()), 1, 9);
output += "|";
output += stringAlign(convertPricePLN(product -> calculatePriceBrutto()), 1, 10);
output += "|";
output += stringAlign(to_string(product -> getVAT()) + " %", 1, 5);
output += "|";
output += stringAlign(convertPricePLN(product -> getVAT() * quantity * product -> getPrice() / 100 / (product -> getMeasureUnits() == pcs ? 1 : 1000)), 1, 9);
output += "|";
priceBrutto = ceil(product -> calculatePriceBrutto() * quantity / (product -> getMeasureUnits() == pcs ? 1 : 1000));
output += stringAlign(convertPricePLN(priceBrutto), 1, 14);
output += "|\n";
totalBrutto += priceBrutto;
}
output += "|" + stringAlign(".", 2, invoiceWidth - 2, ".") + "|\n";
output += "|" + stringAlign("SUMMARY: ", 1, invoiceWidth - 14) + stringAlign(convertPricePLN(totalBrutto), 2, 12) + "|\n";
output += "|" + stringAlign("ID: " + to_string(ID), 0, invoiceWidth - 2) + "|\n";
output += stringAlign("¯", 0, invoiceWidth, "¯") += "\n";
return output;
}
/* ======================= RECEIPT ======================= */
string Receipt::generate() const {
char receiptWidth = 40;
unsigned short priceBrutto = 0, totalBrutto = 0;
string output = stringAlign("_", 0, 40, "_");
output += "\n";
output += "|" + stringAlign(seller.getName(), 2, receiptWidth - 2) + "|\n";
output += "|" + stringAlign(seller.getStreet() + " " + seller.getBuildingNumber(), 2, receiptWidth - 2) + "|\n";
output += "|" + stringAlign(seller.getCity() + " " + seller.getPostcode(), 2, receiptWidth - 2) + "|\n";
output += "|" + stringAlign(seller.getTaxNumber(), 2, receiptWidth - 2) + "|\n";
output += "|" + stringAlign(".", 2, receiptWidth - 2, ".") + "|\n";
output += "|" + stringAlign("RECEIPT", 2, receiptWidth - 2) + "|\n";
output += "|" + stringAlign(".", 2, receiptWidth - 2, ".") + "|\n";
output += "| NAME QTY x PRICE VALUE |\n";
for (auto basketPair: buyer.getBasket()) {
Product *product = basketPair.first;
unsigned short quantity = basketPair.second;
output += "|";
output += stringAlign(product -> getName(), 0, 14);
output += " ";
output += stringAlign(product -> getMeasureUnits() == g ? convertToKg(quantity) : to_string(quantity), 1, 5);
output += "x ";
output += stringAlign(convertPricePLN(product -> calculatePriceBrutto()), 1, 7);
output += " ";
priceBrutto = ceil(product -> calculatePriceBrutto() * quantity / (product -> getMeasureUnits() == pcs ? 1 : 1000));
output += stringAlign(convertPricePLN(priceBrutto), 1, 8);
output += "|\n";
totalBrutto += priceBrutto;
}
output += "|" + stringAlign(".", 2, receiptWidth - 2, ".") + "|\n";
output += "|" + stringAlign("SUMMARY: ", 1, receiptWidth - 10) + stringAlign(convertPricePLN(totalBrutto), 2, 8) + "|\n";
output += "|" + stringAlign("ID: ", 0, receiptWidth - 2) + "|\n";
output += stringAlign("¯", 0, receiptWidth, "¯") += "\n";
return output;
}