223 lines
6.7 KiB
PHP
223 lines
6.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use App\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* Class ShoppingCollectOrder
|
|
*
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int|null $shopping_order_id
|
|
* @property float|null $shipping
|
|
* @property float|null $shipping_net
|
|
* @property float|null $shipping_tax
|
|
* @property float|null $price_total_net
|
|
* @property float|null $price_total
|
|
* @property float|null $tax_total
|
|
* @property int $qty_total
|
|
* @property int|null $points
|
|
* @property string|null $tax_split
|
|
* @property AsArrayObject|null $orders
|
|
* @property AsArrayObject|null $shop_items
|
|
* @property int $status
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property ShoppingOrder|null $shopping_order
|
|
* @property User $user
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereOrders($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder wherePoints($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder wherePriceTotal($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder wherePriceTotalNet($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereQtyTotal($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereShipping($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereShippingNet($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereShippingTax($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereShopItems($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereShoppingOrderId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereStatus($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereTaxSplit($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereTaxTotal($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereUserId($value)
|
|
*
|
|
* @property array|null $net_split
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingCollectOrder whereNetSplit($value)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class ShoppingCollectOrder extends Model
|
|
{
|
|
protected $table = 'shopping_collect_orders';
|
|
|
|
protected $casts = [
|
|
'user_id' => '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;
|
|
}
|
|
}
|