'int', 'shopping_order_id' => 'int', 'shipping' => 'float', 'shipping_net' => 'float', 'shipping_tax' => 'float', 'price_total_net' => 'float', 'price_total' => 'float', 'tax_total' => 'float', 'qty_total' => 'int', 'points' => 'float', 'status' => 'int', 'tax_split' => 'array', 'net_split' => 'array', 'orders' => AsArrayObject::class, 'shop_items' => AsArrayObject::class, ]; protected $fillable = [ 'user_id', 'shopping_order_id', // 'identifier', 'shipping', 'shipping_net', 'shipping_tax', 'price_total_net', 'price_total', 'tax_total', 'qty_total', 'points', 'tax_split', 'net_split', 'orders', 'shop_items', 'status', ]; public static $statusTypes = [ 0 => '', 1 => 'store / pre', 2 => 'order', ]; public function shopping_order() { return $this->belongsTo(ShoppingOrder::class); } public function user() { return $this->belongsTo(User::class); } public function setPointsAttribute($value) { $this->attributes['points'] = $value !== null ? \Util::reFormatNumber($value) : null; } public function getFormattedPoints() { return isset($this->attributes['points']) ? formatNumber($this->attributes['points']) : ''; } public function addTaxToSplit($tax_rate, $add_tax) { $tax_split = $this->tax_split; $add_tax = round($add_tax, 2); $existing_value = isset($tax_split[$tax_rate]) ? $this->parseNumericValue($tax_split[$tax_rate]) : 0; $tax_split[$tax_rate] = round($existing_value + $add_tax, 2); $this->tax_split = $tax_split; } public function addNetToSplit($tax_rate, $add_net) { $net_split = $this->net_split; $add_net = round($add_net, 2); $existing_value = isset($net_split[$tax_rate]) ? $this->parseNumericValue($net_split[$tax_rate]) : 0; $net_split[$tax_rate] = round($existing_value + $add_net, 2); $this->net_split = $net_split; } /** * Parst verschiedene Zahlenformate zu einem float-Wert * Unterstützt: 19.50, 1234.56, 1.234,56, 1,234.56 */ private function parseNumericValue($value) { // Bereits eine Zahl? Direkt zurückgeben if (is_numeric($value)) { return (float) $value; } // String zu String konvertieren für weitere Verarbeitung $value = (string) $value; // Entferne Leerzeichen $value = trim($value); // Prüfe verschiedene Formate if (preg_match('/^-?\d{1,3}(\.\d{3})*,\d{2}$/', $value)) { // Deutsches Format: 1.234,56 oder 1.234.567,89 return (float) str_replace(',', '.', str_replace('.', '', $value)); } elseif (preg_match('/^-?\d{1,3}(,\d{3})*\.\d{2}$/', $value)) { // Amerikanisches Format: 1,234.56 oder 1,234,567.89 return (float) str_replace(',', '', $value); } else { // Einfaches Format: 19.50, 123.45, etc. // Nur Kommas durch Punkte ersetzen return (float) str_replace(',', '.', $value); } } public function addShopItem($shop_item_id, $shop_item) { $numberFields = [ 'user_price_net', 'user_price_total_net', 'user_tax', 'user_tax_total', ]; // Points werden jetzt auch als Dezimalzahlen behandelt $decimalFields = [ 'points_total', 'points', ]; foreach ($numberFields as $field) { if (isset($shop_item->$field)) { $shop_item->$field = number_format($shop_item->$field, 2, '.', ''); } } foreach ($decimalFields as $field) { if (isset($shop_item->$field)) { $shop_item->$field = round((float) $shop_item->$field, 2); } } $this->shop_items[$shop_item_id] = $shop_item; } public function addOrder($order) { $this->orders[] = $order; } public function initShoppingOrder($order) { $order['shopping_order'] = ShoppingOrder::findOrFail($order['order_id']); return $order; } }