akaunting/app/Models/Banking/Transaction.php

420 lines
10 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Banking;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
use App\Models\Common\Media as MediaModel;
2019-11-16 10:21:14 +03:00
use App\Models\Setting\Category;
2020-12-26 16:13:34 +03:00
use App\Scopes\Transaction as Scope;
2019-11-16 10:21:14 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
2020-08-26 15:14:16 +03:00
use App\Traits\Transactions;
2019-11-16 10:21:14 +03:00
use Bkwld\Cloner\Cloneable;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020-08-26 15:14:16 +03:00
use Illuminate\Support\Str;
2017-09-14 22:21:00 +03:00
class Transaction extends Model
{
2020-10-14 17:07:59 +03:00
use Cloneable, Currencies, DateTime, HasFactory, Media, Recurring, Transactions;
2019-11-16 10:21:14 +03:00
protected $table = 'transactions';
protected $dates = ['deleted_at', 'paid_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2021-06-17 10:59:07 +03:00
protected $fillable = [
'company_id',
'type',
'account_id',
'paid_at',
'amount',
'currency_code',
'currency_rate',
'document_id',
'contact_id',
'description',
'category_id',
'payment_method',
'reference',
'parent_id',
'created_by',
];
2019-11-16 10:21:14 +03:00
2020-11-13 15:15:27 +03:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'amount' => 'double',
'currency_rate' => 'double',
];
2019-11-16 10:21:14 +03:00
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['paid_at', 'amount','category.name', 'account.name'];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['recurring'];
2020-12-26 16:13:34 +03:00
/**
2021-01-19 17:27:19 +03:00
* The "booted" method of the model.
2020-12-26 16:13:34 +03:00
*
* @return void
*/
2021-01-19 17:27:19 +03:00
protected static function booted()
2020-12-26 16:13:34 +03:00
{
static::addGlobalScope(new Scope);
}
2019-11-16 10:21:14 +03:00
public function account()
{
return $this->belongsTo('App\Models\Banking\Account')->withDefault(['name' => trans('general.na')]);
}
public function bill()
{
2020-12-24 01:28:38 +03:00
return $this->belongsTo('App\Models\Document\Document', 'document_id');
2019-11-16 10:21:14 +03:00
}
public function category()
{
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
}
public function contact()
{
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function invoice()
{
2020-12-24 01:28:38 +03:00
return $this->belongsTo('App\Models\Document\Document', 'document_id');
}
public function document()
{
return $this->belongsTo('App\Models\Document\Document', 'document_id');
2019-11-16 10:21:14 +03:00
}
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
public function user()
{
return $this->belongsTo('App\Models\Auth\User', 'contact_id', 'id');
}
/**
* Scope to only include contacts of a given type.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $types
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeType($query, $types)
{
if (empty($types)) {
return $query;
2017-09-14 22:21:00 +03:00
}
2020-02-22 00:30:45 +03:00
return $query->whereIn($this->table . '.type', (array) $types);
2019-11-16 10:21:14 +03:00
}
/**
* Scope to include only income.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIncome($query)
{
2020-08-26 15:14:16 +03:00
return $query->whereIn($this->table . '.type', (array) $this->getIncomeTypes());
}
/**
* Scope to include only expense.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeExpense($query)
{
2020-08-26 15:14:16 +03:00
return $query->whereIn($this->table . '.type', (array) $this->getExpenseTypes());
}
2019-11-16 10:21:14 +03:00
/**
* Get only transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsTransfer($query)
{
return $query->where('category_id', '=', Category::transfer());
}
/**
* Skip transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotTransfer($query)
{
return $query->where('category_id', '<>', Category::transfer());
}
/**
* Get only documents (invoice/bill).
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsDocument($query)
{
2020-01-02 17:39:06 +03:00
return $query->whereNotNull('document_id');
}
/**
* Get only transactions (revenue/payment).
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotDocument($query)
{
return $query->whereNull('document_id');
2019-11-16 10:21:14 +03:00
}
/**
2021-01-02 13:06:02 +03:00
* Get by document id.
2019-11-16 10:21:14 +03:00
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $document_id
* @return \Illuminate\Database\Eloquent\Builder
*/
2021-01-02 13:06:02 +03:00
public function scopeDocumentId($query, $document_id)
2019-11-16 10:21:14 +03:00
{
return $query->where('document_id', '=', $document_id);
}
2021-01-02 13:06:02 +03:00
/**
* Get by account id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $account_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAccountId($query, $account_id)
{
return $query->where('account_id', '=', $account_id);
}
/**
* Get by contact id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $contact_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeContactId($query, $contact_id)
{
return $query->where('contact_id', '=', $contact_id);
}
/**
* Get by category id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $category_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCategoryId($query, $category_id)
{
return $query->where('category_id', '=', $category_id);
}
2019-11-16 10:21:14 +03:00
/**
* Order by paid date.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Scope paid invoice.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePaid($query)
{
return $query->sum('amount');
}
/**
* Get only reconciled.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsReconciled($query)
{
return $query->where('reconciled', 1);
}
/**
* Get only not reconciled.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotReconciled($query)
{
return $query->where('reconciled', 0);
}
public function onCloning($src, $child = null)
{
$this->document_id = null;
}
/**
* Convert amount to double.
*
2020-06-08 23:40:09 +03:00
* @return float
*/
2020-07-06 09:54:18 +03:00
public function getAmountForAccountAttribute()
{
2020-06-08 23:40:09 +03:00
$amount = $this->amount;
// Convert amount if not same currency
if ($this->account->currency_code != $this->currency_code) {
2020-07-06 09:54:18 +03:00
$to_code = $this->account->currency_code;
$to_rate = config('money.' . $this->account->currency_code . '.rate');
2020-07-06 09:54:18 +03:00
$amount = $this->convertBetween($amount, $this->currency_code, $this->currency_rate, $to_code, $to_rate);
}
2020-06-08 23:40:09 +03:00
return $amount;
}
2019-11-16 10:21:14 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->all();
}
public function delete_attachment()
{
if ($attachments = $this->attachment) {
foreach ($attachments as $file) {
MediaModel::where('id', $file->id)->delete();
}
}
2019-11-16 10:21:14 +03:00
}
2020-08-26 15:14:16 +03:00
/**
* Check if the record is attached to a transfer.
*
* @return bool
*/
public function getHasTransferRelationAttribute()
{
return (bool) (optional($this->category)->id == optional($this->category)->transfer());
}
2020-08-26 15:14:16 +03:00
/**
* Get the title of type.
*
* @return string
*/
public function getTypeTitleAttribute($value)
{
return $value ?? trans_choice('general.' . Str::plural($this->type), 1);
}
2020-09-03 10:42:53 +03:00
/**
* Get the route name.
*
* @return string
*/
public function getRouteNameAttribute($value)
{
if ($value) {
return $value;
}
if ($this->isIncome()) {
2021-06-21 16:21:50 +03:00
return !empty($this->document_id) ? 'invoices.show' : 'revenues.show';
2020-09-03 10:42:53 +03:00
}
if ($this->isExpense()) {
2021-06-21 16:21:50 +03:00
return !empty($this->document_id) ? 'bills.show' : 'payments.show';
2020-09-03 10:42:53 +03:00
}
return 'transactions.index';
}
/**
* Get the route id.
*
* @return string
*/
public function getRouteIdAttribute($value)
{
2021-02-26 11:34:34 +03:00
return !empty($value) ? $value : (!empty($this->document_id) ? $this->document_id : $this->id);
2020-09-03 10:42:53 +03:00
}
2020-10-14 17:07:59 +03:00
2021-06-24 13:52:49 +03:00
public function getTemplatePathAttribute($value = null)
{
2021-06-28 18:38:56 +03:00
$type_for_theme = ($this->type == 'income') ? 'sales.revenues.print_default' : 'purchases.payments.print_default';
return $value ?: $type_for_theme;
2021-06-24 13:52:49 +03:00
}
2020-10-14 17:07:59 +03:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Transaction::new();
}
2017-09-14 22:21:00 +03:00
}