mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-02 05:10:59 +00:00
* refactor: stabilize model identities for domain migration * refactor: extract module platform context * refactor: assign models to domain contexts * refactor: extract ai platform context * refactor: extract storage platform context * refactor: extract mail platform context * refactor: extract pdf platform context * refactor: extract operations platform context * refactor: move installation into operations platform * refactor: extract money domain context * refactor: extract taxation domain context * refactor: extract catalog domain context * refactor: extract metadata domain context * refactor: extract reporting domain context * refactor: extract purchases domain context * refactor: extract receivables domain context * refactor: extract accounts domain context * refactor: complete reporting statement boundary * refactor: extract contacts domain context * refactor: extract sales domain context * refactor: remove legacy application layers * fix: migrate legacy bouncer role identities
258 lines
6.9 KiB
PHP
258 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Contacts\Models;
|
|
|
|
use App\Domains\Accounts\Models\Company;
|
|
use App\Domains\Accounts\Models\CompanySetting;
|
|
use App\Domains\Contacts\Notifications\CustomerMailResetPasswordNotification;
|
|
use App\Domains\Metadata\Concerns\HasCustomFields;
|
|
use App\Domains\Money\Models\Currency;
|
|
use App\Domains\Purchases\Models\Expense;
|
|
use App\Domains\Receivables\Models\Payment;
|
|
use App\Domains\Sales\Models\Estimate;
|
|
use App\Domains\Sales\Models\Invoice;
|
|
use App\Domains\Sales\Models\RecurringInvoice;
|
|
use App\Platform\Mail\Models\EmailLog;
|
|
use App\Support\SafeOrderBy;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Silber\Bouncer\Database\HasRolesAndAbilities;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
|
|
class Customer extends Authenticatable implements HasMedia
|
|
{
|
|
protected $table = 'customers';
|
|
|
|
use HasApiTokens;
|
|
use HasCustomFields;
|
|
use HasFactory;
|
|
use HasRolesAndAbilities;
|
|
use InteractsWithMedia;
|
|
use Notifiable;
|
|
|
|
protected $guarded = [
|
|
'id',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
protected $with = [
|
|
'currency',
|
|
];
|
|
|
|
protected $appends = [
|
|
'formattedCreatedAt',
|
|
'avatar',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'enable_portal' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function getFormattedCreatedAtAttribute($value)
|
|
{
|
|
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
|
|
|
|
return Carbon::parse($this->created_at)->translatedFormat($dateFormat);
|
|
}
|
|
|
|
public function setPasswordAttribute($value)
|
|
{
|
|
if ($value != null) {
|
|
$this->attributes['password'] = bcrypt($value);
|
|
}
|
|
}
|
|
|
|
public function estimates(): HasMany
|
|
{
|
|
return $this->hasMany(Estimate::class);
|
|
}
|
|
|
|
public function expenses(): HasMany
|
|
{
|
|
return $this->hasMany(Expense::class);
|
|
}
|
|
|
|
public function invoices(): HasMany
|
|
{
|
|
return $this->hasMany(Invoice::class);
|
|
}
|
|
|
|
public function payments(): HasMany
|
|
{
|
|
return $this->hasMany(Payment::class);
|
|
}
|
|
|
|
public function emailLogs(): MorphMany
|
|
{
|
|
return $this->morphMany(EmailLog::class, 'mailable');
|
|
}
|
|
|
|
public function addresses(): HasMany
|
|
{
|
|
return $this->hasMany(Address::class);
|
|
}
|
|
|
|
public function recurringInvoices(): HasMany
|
|
{
|
|
return $this->hasMany(RecurringInvoice::class);
|
|
}
|
|
|
|
public function currency(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Currency::class);
|
|
}
|
|
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class, 'creator_id');
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function billingAddress(): HasOne
|
|
{
|
|
return $this->hasOne(Address::class)->where('type', Address::BILLING_TYPE);
|
|
}
|
|
|
|
public function shippingAddress(): HasOne
|
|
{
|
|
return $this->hasOne(Address::class)->where('type', Address::SHIPPING_TYPE);
|
|
}
|
|
|
|
public function sendPasswordResetNotification(mixed $token): void
|
|
{
|
|
$this->notify(new CustomerMailResetPasswordNotification($token));
|
|
}
|
|
|
|
public function getAvatarAttribute()
|
|
{
|
|
$avatar = $this->getMedia('customer_avatar')->first();
|
|
|
|
if ($avatar) {
|
|
return asset($avatar->getUrl());
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public function scopePaginateData($query, $limit)
|
|
{
|
|
if ($limit == 'all') {
|
|
return $query->get();
|
|
}
|
|
|
|
return $query->paginate($limit);
|
|
}
|
|
|
|
public function scopeWhereCompany($query)
|
|
{
|
|
return $query->where('customers.company_id', request()->header('company'));
|
|
}
|
|
|
|
public function scopeWhereContactName($query, $contactName)
|
|
{
|
|
return $query->where('contact_name', 'LIKE', '%'.$contactName.'%');
|
|
}
|
|
|
|
public function scopeWhereDisplayName($query, $displayName)
|
|
{
|
|
return $query->where('name', 'LIKE', '%'.$displayName.'%');
|
|
}
|
|
|
|
public function scopeWhereOrder($query, $orderByField, $orderBy)
|
|
{
|
|
SafeOrderBy::apply($query, $orderByField, $orderBy);
|
|
}
|
|
|
|
public function scopeWhereSearch($query, $search)
|
|
{
|
|
foreach (explode(' ', $search) as $term) {
|
|
$query->where(function ($query) use ($term) {
|
|
$query->where('name', 'LIKE', '%'.$term.'%')
|
|
->orWhere('email', 'LIKE', '%'.$term.'%')
|
|
->orWhere('phone', 'LIKE', '%'.$term.'%');
|
|
});
|
|
}
|
|
}
|
|
|
|
public function scopeWherePhone($query, $phone)
|
|
{
|
|
return $query->where('phone', 'LIKE', '%'.$phone.'%');
|
|
}
|
|
|
|
public function scopeWhereCustomer($query, $customer_id)
|
|
{
|
|
$query->orWhere('customers.id', $customer_id);
|
|
}
|
|
|
|
public function scopeApplyInvoiceFilters($query, array $filters)
|
|
{
|
|
$filters = collect($filters);
|
|
|
|
if ($filters->get('from_date') && $filters->get('to_date')) {
|
|
$start = Carbon::createFromFormat('Y-m-d', $filters->get('from_date'));
|
|
$end = Carbon::createFromFormat('Y-m-d', $filters->get('to_date'));
|
|
$query->invoicesBetween($start, $end);
|
|
}
|
|
}
|
|
|
|
public function scopeInvoicesBetween($query, $start, $end)
|
|
{
|
|
$query->whereHas('invoices', function ($query) use ($start, $end) {
|
|
$query->whereBetween(
|
|
'invoice_date',
|
|
[$start->format('Y-m-d'), $end->format('Y-m-d')]
|
|
);
|
|
});
|
|
}
|
|
|
|
public function scopeApplyFilters($query, array $filters)
|
|
{
|
|
$filters = collect($filters);
|
|
|
|
if ($filters->get('search')) {
|
|
$query->whereSearch($filters->get('search'));
|
|
}
|
|
|
|
if ($filters->get('contact_name')) {
|
|
$query->whereContactName($filters->get('contact_name'));
|
|
}
|
|
|
|
if ($filters->get('display_name')) {
|
|
$query->whereDisplayName($filters->get('display_name'));
|
|
}
|
|
|
|
if ($filters->get('customer_id')) {
|
|
$query->whereCustomer($filters->get('customer_id'));
|
|
}
|
|
|
|
if ($filters->get('phone')) {
|
|
$query->wherePhone($filters->get('phone'));
|
|
}
|
|
|
|
if ($filters->get('orderByField') || $filters->get('orderBy')) {
|
|
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'name';
|
|
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'asc';
|
|
$query->whereOrder($field, $orderBy);
|
|
}
|
|
}
|
|
}
|