This repository has been archived on 2025-12-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Caramel-CRM-Backup/app/Models/Customer.php
T

107 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
use HasFactory;
protected $casts = [
'billing_address' => 'array'
];
protected $fillable = [
'type',
'company_name',
'customer_nr',
'vat_id',
'tax_id', // Tax identification number
'global_id', // Global Location Number (GLN) or other identification
'legal_registration_id', // Legal registration ID
'url',
'email',
'phone',
'billing_address',
'payment_terms_id',
'status',
'logo',
];
/**
* Set the billing address attribute.
*
* @param array $value
* @return void
*/
public function setBillingAddressAttribute($value)
{
if (is_string($value)) {
$value = json_decode($value, true);
}
$this->attributes['billing_address'] = json_encode([
'line_one' => $value['line_one'] ?? '',
'line_two' => $value['line_two'] ?? '',
'city' => $value['city'] ?? '',
'postal_code' => $value['postal_code'] ?? '',
'country_code' => $value['country_code'] ?? 'DE', // Default to Germany
]);
}
/**
* Get the billing address attribute.
*
* @return array
*/
public function getBillingAddressAttribute()
{
return json_decode($this->attributes['billing_address'], true) ?? [
'line_one' => '',
'line_two' => '',
'city' => '',
'postal_code' => '',
'country_code' => 'DE',
];
}
/**
* Get the URL to the customer's logo.
*/
public function getLogoUrlAttribute()
{
if ($this->logo) {
return asset('storage/' . $this->logo);
}
// Return null if no logo is set
return null;
}
/**
* Get the contacts for the customer.
*/
public function contacts()
{
return $this->hasMany(Contact::class);
}
/**
* Get the payment term associated with the customer.
*/
public function paymentTerms()
{
return $this->belongsTo(PaymentTerms::class);
}
/**
* Get the notes for the customer
*/
public function notes()
{
return $this->morphMany(Note::class, 'notable');
}
}