import React, { Component } from 'react'; import Box from '@mui/material/Box'; import List from '@mui/material/List'; import Typography from '@mui/material/Typography'; import Button from '@mui/material/Button'; import Table from '@mui/material/Table'; import TableBody from '@mui/material/TableBody'; import TableCell from '@mui/material/TableCell'; import TableRow from '@mui/material/TableRow'; import CartItem from './CartItem.js'; import { withI18n } from '../i18n/withTranslation.js'; class CartDropdown extends Component { render() { const { cartItems = [], onClose, onCheckout, showDetailedSummary = false, deliveryMethod = '', deliveryCost = 0 } = this.props; // Calculate the total weight of all items in the cart const totalWeight = cartItems.reduce((sum, item) => { const weightPerItem = item.weight || 0; const quantity = item.quantity || 1; return sum + weightPerItem * quantity; }, 0); // Calculate price breakdowns const priceCalculations = cartItems.reduce((acc, item) => { const totalItemPrice = item.price * item.quantity; const netPrice = totalItemPrice / (1 + item.vat / 100); const vatAmount = totalItemPrice - netPrice; acc.totalGross += totalItemPrice; acc.totalNet += netPrice; if (item.vat === 7) { acc.vat7 += vatAmount; } else if (item.vat === 19) { acc.vat19 += vatAmount; } return acc; }, { totalGross: 0, totalNet: 0, vat7: 0, vat19: 0 }); // Calculate detailed summary with shipping (similar to OrderSummary) const currencyFormatter = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }); const shippingNetPrice = deliveryCost > 0 ? deliveryCost / (1 + 19 / 100) : 0; const shippingVat = deliveryCost > 0 ? deliveryCost - shippingNetPrice : 0; const totalVat7 = priceCalculations.vat7; const totalVat19 = priceCalculations.vat19 + shippingVat; const totalGross = priceCalculations.totalGross + deliveryCost; return ( <> {cartItems.length} {cartItems.length === 1 ? (this.props.t ? this.props.t('cart.itemCount.singular') : 'Produkt') : (this.props.t ? this.props.t('cart.itemCount.plural') : 'Produkte')} { cartItems && ( <> {cartItems.map((item) => ( ))} {/* Display total weight if greater than 0 */} {totalWeight > 0 && ( {this.props.t ? this.props.t('cart.summary.totalWeight', { weight: totalWeight.toFixed(2) }) : `Gesamtgewicht: ${totalWeight.toFixed(2)} kg`} )} {/* Price breakdown table */} {cartItems.length > 0 && ( {showDetailedSummary ? ( // Detailed summary with shipping costs <> {this.props.t ? this.props.t('cart.summary.title') : 'Bestellübersicht'} {deliveryMethod && ( Versandart: {deliveryMethod} )} {this.props.t ? this.props.t('cart.summary.goodsNet') : 'Waren (netto):'} {currencyFormatter.format(priceCalculations.totalNet)} {deliveryCost > 0 && ( {this.props.t ? this.props.t('cart.summary.shippingNet') : 'Versandkosten (netto):'} {currencyFormatter.format(shippingNetPrice)} )} {totalVat7 > 0 && ( {this.props.t ? this.props.t('tax.vat7') : '7% Mehrwertsteuer'}: {currencyFormatter.format(totalVat7)} )} {totalVat19 > 0 && ( {this.props.t ? this.props.t('tax.vat19WithShipping') : '19% Mehrwertsteuer (inkl. Versand)'}: {currencyFormatter.format(totalVat19)} )} {this.props.t ? this.props.t('cart.summary.totalGoods') : 'Gesamtsumme Waren:'} {currencyFormatter.format(priceCalculations.totalGross)} {this.props.t ? this.props.t('cart.summary.shippingCosts') : 'Versandkosten:'} {deliveryCost === 0 && priceCalculations.totalGross < 100 && ( {this.props.t ? this.props.t('cart.summary.freeFrom100') : '(kostenlos ab 100€)'} )} {deliveryCost === 0 ? ( {this.props.t ? this.props.t('cart.summary.free') : 'kostenlos'} ) : ( currencyFormatter.format(deliveryCost) )} {this.props.t ? this.props.t('cart.summary.total') : 'Gesamtsumme:'} {currencyFormatter.format(totalGross)}
) : ( // Simple summary without shipping costs {this.props.t ? this.props.t('tax.totalNet') : 'Gesamtnettopreis'}: {new Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(priceCalculations.totalNet)} {priceCalculations.vat7 > 0 && ( {this.props.t ? this.props.t('tax.vat7') : '7% Mehrwertsteuer'}: {new Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(priceCalculations.vat7)} )} {priceCalculations.vat19 > 0 && ( {this.props.t ? this.props.t('tax.vat19') : '19% Mehrwertsteuer'}: {new Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(priceCalculations.vat19)} )} {this.props.t ? this.props.t('tax.totalGross') : 'Gesamtbruttopreis ohne Versand'}: {new Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(priceCalculations.totalGross)}
)}
)} {onClose && ( )} {onCheckout && cartItems.length > 0 && ( )} )} ); } } export default withI18n()(CartDropdown);