2020-12-24 01:28:38 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Document;
|
|
|
|
|
|
|
|
use App\Abstracts\Model;
|
|
|
|
use App\Traits\Currencies;
|
2022-07-28 23:06:30 +03:00
|
|
|
use Bkwld\Cloner\Cloneable;
|
2020-12-24 01:28:38 +03:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Znck\Eloquent\Traits\BelongsToThrough;
|
|
|
|
|
|
|
|
class DocumentItemTax extends Model
|
|
|
|
{
|
2022-07-28 23:06:30 +03:00
|
|
|
use Cloneable, Currencies, BelongsToThrough;
|
2020-12-24 01:28:38 +03:00
|
|
|
|
|
|
|
protected $table = 'document_item_taxes';
|
|
|
|
|
2021-09-07 10:33:34 +03:00
|
|
|
protected $fillable = ['company_id', 'type', 'document_id', 'document_item_id', 'tax_id', 'name', 'amount', 'created_from', 'created_by'];
|
2020-12-24 01:28:38 +03:00
|
|
|
|
|
|
|
public function document()
|
|
|
|
{
|
2022-06-10 19:26:32 +03:00
|
|
|
return $this->belongsTo('App\Models\Document\Document')->withoutGlobalScope('App\Scopes\Document');
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function item()
|
|
|
|
{
|
|
|
|
return $this->belongsToThrough('App\Models\Common\Item', 'App\Models\Document\DocumentItem', 'document_item_id')->withDefault(['name' => trans('general.na')]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tax()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na'), 'rate' => 0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeType(Builder $query, string $type)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', $type);
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeInvoice(Builder $query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_TYPE);
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 15:44:03 +03:00
|
|
|
public function scopeInvoiceRecurring(Builder $query): Builder
|
|
|
|
{
|
|
|
|
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_RECURRING_TYPE)
|
|
|
|
->whereHas('document.recurring', function (Builder $query) {
|
|
|
|
$query->whereNull('deleted_at');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
public function scopeBill(Builder $query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_TYPE);
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|
2022-07-28 23:06:30 +03:00
|
|
|
|
2023-07-20 15:44:03 +03:00
|
|
|
public function scopeBillRecurring(Builder $query): Builder
|
|
|
|
{
|
|
|
|
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_RECURRING_TYPE)
|
|
|
|
->whereHas('document.recurring', function (Builder $query) {
|
|
|
|
$query->whereNull('deleted_at');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-28 23:06:30 +03:00
|
|
|
public function onCloned($src)
|
|
|
|
{
|
|
|
|
$document_item = DocumentItem::find($this->document_item_id);
|
|
|
|
|
|
|
|
$this->update(['document_id' => $document_item->document_id]);
|
|
|
|
}
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|