Files
InvoiceShelf/app/Models/Estimate.php
Darko Gjorgjijoski 0ce88ab817 Remove app/Space folder and extract model business logic into services
Relocate all 14 files from the catch-all app/Space namespace into proper
locations: data providers to Support/Formatters, installation utilities to
Services/Installation, PDF utils to Services/Pdf, module/update classes to
Services/Module and Services/Update, SiteApi trait to Traits, and helpers
to Support.

Extract ~1,400 lines of business logic from 8 fat models (Invoice, Payment,
Estimate, RecurringInvoice, Company, Customer, Expense, User) into 9 new
service classes with constructor injection. Controllers now depend on
services instead of calling static model methods. Shared item/tax creation
logic consolidated into DocumentItemService.
2026-04-03 15:37:22 +02:00

325 lines
8.7 KiB
PHP

<?php
namespace App\Models;
use App\Services\EstimateService;
use App\Services\Pdf\PdfTemplateUtils;
use App\Support\PdfHtmlSanitizer;
use App\Traits\GeneratesPdfTrait;
use App\Traits\HasCustomFieldsTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Estimate extends Model implements HasMedia
{
use GeneratesPdfTrait;
use HasCustomFieldsTrait;
use HasFactory;
use InteractsWithMedia;
public const STATUS_DRAFT = 'DRAFT';
public const STATUS_SENT = 'SENT';
public const STATUS_VIEWED = 'VIEWED';
public const STATUS_EXPIRED = 'EXPIRED';
public const STATUS_ACCEPTED = 'ACCEPTED';
public const STATUS_REJECTED = 'REJECTED';
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'estimate_date',
'expiry_date',
];
protected $appends = [
'formattedExpiryDate',
'formattedEstimateDate',
'estimatePdfUrl',
];
protected $guarded = ['id'];
protected function casts(): array
{
return [
'total' => 'integer',
'tax' => 'integer',
'sub_total' => 'integer',
'discount' => 'float',
'discount_val' => 'integer',
'exchange_rate' => 'float',
];
}
public function getEstimatePdfUrlAttribute()
{
return url('/estimates/pdf/'.$this->unique_hash);
}
public function emailLogs(): MorphMany
{
return $this->morphMany('App\Models\EmailLog', 'mailable');
}
public function items(): HasMany
{
return $this->hasMany(EstimateItem::class);
}
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class, 'customer_id');
}
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'creator_id');
}
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
public function currency(): BelongsTo
{
return $this->belongsTo(Currency::class);
}
public function taxes(): HasMany
{
return $this->hasMany(Tax::class);
}
public function getFormattedExpiryDateAttribute($value)
{
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
return Carbon::parse($this->expiry_date)->translatedFormat($dateFormat);
}
public function getFormattedEstimateDateAttribute($value)
{
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
return Carbon::parse($this->estimate_date)->translatedFormat($dateFormat);
}
public function scopeEstimatesBetween($query, $start, $end)
{
return $query->whereBetween(
'estimates.estimate_date',
[$start->format('Y-m-d'), $end->format('Y-m-d')]
);
}
public function scopeWhereStatus($query, $status)
{
return $query->where('estimates.status', $status);
}
public function scopeWhereEstimateNumber($query, $estimateNumber)
{
return $query->where('estimates.estimate_number', 'LIKE', '%'.$estimateNumber.'%');
}
public function scopeWhereEstimate($query, $estimate_id)
{
$query->orWhere('id', $estimate_id);
}
public function scopeWhereSearch($query, $search)
{
foreach (explode(' ', $search) as $term) {
$query->whereHas('customer', function ($query) use ($term) {
$query->where('name', 'LIKE', '%'.$term.'%')
->orWhere('contact_name', 'LIKE', '%'.$term.'%')
->orWhere('company_name', 'LIKE', '%'.$term.'%');
});
}
}
public function scopeApplyFilters($query, array $filters)
{
$filters = collect($filters);
if ($filters->get('search')) {
$query->whereSearch($filters->get('search'));
}
if ($filters->get('estimate_number')) {
$query->whereEstimateNumber($filters->get('estimate_number'));
}
if ($filters->get('status')) {
$query->whereStatus($filters->get('status'));
}
if ($filters->get('estimate_id')) {
$query->whereEstimate($filters->get('estimate_id'));
}
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->estimatesBetween($start, $end);
}
if ($filters->get('customer_id')) {
$query->whereCustomer($filters->get('customer_id'));
}
if ($filters->get('orderByField') || $filters->get('orderBy')) {
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'sequence_number';
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'desc';
$query->whereOrder($field, $orderBy);
}
}
public function scopeWhereOrder($query, $orderByField, $orderBy)
{
$query->orderBy($orderByField, $orderBy);
}
public function scopeWhereCompany($query)
{
$query->where('estimates.company_id', request()->header('company'));
}
public function scopeWhereCustomer($query, $customer_id)
{
$query->where('estimates.customer_id', $customer_id);
}
public function scopePaginateData($query, $limit)
{
if ($limit == 'all') {
return $query->get();
}
return $query->paginate($limit);
}
public function getPDFData()
{
return app(EstimateService::class)->getPdfData($this);
}
public function getCompanyAddress()
{
if ($this->company && (! $this->company->address()->exists())) {
return false;
}
$format = CompanySetting::getSetting('estimate_company_address_format', $this->company_id);
return $this->getFormattedString($format);
}
public function getCustomerShippingAddress()
{
if ($this->customer && (! $this->customer->shippingAddress()->exists())) {
return false;
}
$format = CompanySetting::getSetting('estimate_shipping_address_format', $this->company_id);
return $this->getFormattedString($format);
}
public function getCustomerBillingAddress()
{
if ($this->customer && (! $this->customer->billingAddress()->exists())) {
return false;
}
$format = CompanySetting::getSetting('estimate_billing_address_format', $this->company_id);
return $this->getFormattedString($format);
}
public function getNotes()
{
return PdfHtmlSanitizer::sanitize($this->getFormattedString($this->notes));
}
public function getEmailAttachmentSetting()
{
$estimateAsAttachment = CompanySetting::getSetting('estimate_email_attachment', $this->company_id);
if ($estimateAsAttachment == 'NO') {
return false;
}
return true;
}
public function getEmailBody($body)
{
$values = array_merge($this->getFieldsArray(), $this->getExtraFields());
$body = strtr($body, $values);
return preg_replace('/{(.*?)}/', '', $body);
}
public function getExtraFields()
{
return [
'{ESTIMATE_DATE}' => $this->formattedEstimateDate,
'{ESTIMATE_EXPIRY_DATE}' => $this->formattedExpiryDate,
'{ESTIMATE_NUMBER}' => $this->estimate_number,
'{ESTIMATE_REF_NUMBER}' => $this->reference_number,
];
}
public function getInvoiceTemplateName()
{
$templateName = Str::replace('estimate', 'invoice', $this->template_name);
$name = [];
foreach (PdfTemplateUtils::getFormattedTemplates('invoice') as $template) {
$name[] = $template['name'];
}
if (in_array($templateName, $name) == false) {
$templateName = 'invoice1';
}
return $templateName;
}
public function checkForEstimateConvertAction()
{
$convertEstimateAction = CompanySetting::getSetting(
'estimate_convert_action',
$this->company_id
);
if ($convertEstimateAction === 'delete_estimate') {
$this->delete();
}
if ($convertEstimateAction === 'mark_estimate_as_accepted') {
$this->status = self::STATUS_ACCEPTED;
$this->save();
}
return true;
}
}