-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpos.js
187 lines (157 loc) · 6.18 KB
/
pos.js
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
import { InventoryManagement } from './InventoryManagement.js';
import { InvoiceGenerator } from './InvoiceGenerator.js';
export class POS {
constructor() {
this.inventory = new InventoryManagement();
this.cart = [];
this.logbook = []; // Logbuch für Ein- und Ausgänge
this.prices = new Map(); // Map zur Speicherung der Einkaufspreise und Verkaufspreise
}
// Produkte hinzufügen
addProductToInventory(product, quantity, purchasePrice) {
this.inventory.addProduct(product, quantity);
this.prices.set(product.id, { purchasePrice, sellPrice: purchasePrice }); // Einkaufspreis und initialer Verkaufspreis speichern
this.logbook.push({
action: 'added',
product: product.name,
quantity,
purchasePrice,
date: new Date().toISOString()
});
}
// Dynamische Preisberechnung
calculatePrice(product, context) {
const priceData = this.prices.get(product.id) || { purchasePrice: 0, sellPrice: 0 };
const basePrice = context.basePrice || priceData.sellPrice; // Verwende Basispreis oder Verkaufspreis
const discount = context.discount || 0;
const taxRate = context.taxRate || 0;
// Berechne Rabatt
const discountedPrice = basePrice - (basePrice * discount);
// Berechne Steuer
const finalPrice = discountedPrice + (discountedPrice * taxRate);
return parseFloat(finalPrice.toFixed(2));
}
// Produkt in den Warenkorb legen
addToCart(productId, quantity, context = {}) {
const product = this.inventory.products.find(p => p.id === productId);
if (!product) {
throw new Error('Produkt nicht gefunden.');
}
if (quantity > this.inventory.getStock(productId)) {
throw new Error('Nicht genügend Lagerbestand.');
}
const price = this.calculatePrice(product, context);
const cartItem = this.cart.find(item => item.product.id === productId);
if (cartItem) {
cartItem.quantity += quantity;
cartItem.price = price; // Update den Preis, falls er sich geändert hat
} else {
// Produkt unter dem Key "product" speichern
this.cart.push({ product, quantity, price });
}
this.inventory.updateStock(productId, quantity);
}
// Produkt aus dem Warenkorb entfernen
removeFromCart(productId, quantity) {
const cartItemIndex = this.cart.findIndex(item => item.product.id === productId);
if (cartItemIndex === -1) {
throw new Error('Produkt nicht im Warenkorb.');
}
const cartItem = this.cart[cartItemIndex];
if (quantity >= cartItem.quantity) {
// Wenn die Menge gleich oder größer ist, das Produkt komplett aus dem Warenkorb entfernen
this.cart.splice(cartItemIndex, 1);
} else {
// Ansonsten nur die angegebene Menge reduzieren
cartItem.quantity -= quantity;
}
// Bestand im Lager aktualisieren
this.inventory.updateStock(productId, quantity);
}
// Warenkorb anzeigen
listCart() {
return this.cart.map(item => ({
id: item.product.id,
name: item.product.name,
quantity: item.quantity,
unitPrice: item.price,
totalPrice: item.price * item.quantity
}));
}
// Gesamtsumme berechnen
calculateTotal() {
return this.cart.reduce((total, item) => {
return total + item.price * item.quantity;
}, 0).toFixed(2);
}
// Marge berechnen
calculateMargin() {
const margins = this.cart.map(item => {
const priceData = this.prices.get(item.product.id) || { purchasePrice: 0 };
const totalPurchasePrice = priceData.purchasePrice * item.quantity;
const totalSalePrice = item.price * item.quantity;
return {
name: item.product.name,
totalPurchasePrice: totalPurchasePrice.toFixed(2),
totalSalePrice: totalSalePrice.toFixed(2),
margin: (totalSalePrice - totalPurchasePrice).toFixed(2)
};
});
return margins;
}
// Verkaufspreis aktualisieren
updateSellPrice(productId, newPrice) {
const product = this.inventory.products.find(p => p.id === productId);
if (!product) {
throw new Error('Produkt nicht gefunden.');
}
const priceData = this.prices.get(productId);
if (priceData) {
priceData.sellPrice = newPrice;
this.prices.set(productId, priceData);
console.log(`Verkaufspreis für Produkt ${productId} auf ${newPrice.toFixed(2)} € aktualisiert.`);
}
}
// Rechnung erstellen
checkout(supplier, customer, category) {
if (this.cart.length === 0) {
throw new Error('Warenkorb ist leer.');
}
const invoice = InvoiceGenerator.generateInvoice(
this.cart.map(item => ({ item: item.product, quantity: item.quantity })),
supplier,
customer,
category
);
this.cart.forEach(item => {
const priceData = this.prices.get(item.product.id) || { purchasePrice: 0 };
this.logbook.push({
action: 'sold',
product: item.product.name,
quantity: item.quantity,
purchasePrice: priceData.purchasePrice,
salePrice: item.price,
date: new Date().toISOString()
});
});
const items = this.cart;
// Warenkorb leeren
this.cart = [];
return { invoice, items };
}
// Lagerbestand anzeigen
listInventory() {
return this.inventory.listAllProducts().map(product => {
const priceData = this.prices.get(product.id) || { purchasePrice: 0, sellPrice: 0 };
return {
...product,
purchasePrice: priceData.purchasePrice.toFixed(2),
sellPrice: priceData.sellPrice.toFixed(2)
};
});
}
// Logbuch anzeigen
getLogbook() {
return this.logbook;
}
}