akaunting 3.0 (the last dance)

This commit is contained in:
Burak Civan
2022-06-01 10:15:55 +03:00
parent cead09f6d4
commit d9c0764572
3812 changed files with 126831 additions and 102949 deletions

View File

@ -12,6 +12,7 @@ use App\Traits\Media;
use App\Traits\Recurring;
use App\Traits\Transactions;
use Bkwld\Cloner\Cloneable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
@ -19,6 +20,13 @@ class Transaction extends Model
{
use Cloneable, Currencies, DateTime, HasFactory, Media, Recurring, Transactions;
public const INCOME_TYPE = 'income';
public const INCOME_SPLIT_TYPE = 'income-split';
public const INCOME_RECURRING_TYPE = 'income-recurring';
public const EXPENSE_TYPE = 'expense';
public const EXPENSE_SPLIT_TYPE = 'expense-split';
public const EXPENSE_RECURRING_TYPE = 'expense-recurring';
protected $table = 'transactions';
protected $dates = ['deleted_at', 'paid_at'];
@ -31,6 +39,7 @@ class Transaction extends Model
protected $fillable = [
'company_id',
'type',
'number',
'account_id',
'paid_at',
'amount',
@ -43,6 +52,7 @@ class Transaction extends Model
'payment_method',
'reference',
'parent_id',
'split_id',
'created_from',
'created_by',
];
@ -62,7 +72,7 @@ class Transaction extends Model
*
* @var array
*/
public $sortable = ['paid_at', 'amount','category.name', 'account.name'];
public $sortable = ['type', 'number', 'paid_at', 'amount','category.name', 'account.name', 'customer.name', 'invoice.document_number'];
/**
* Clonable relationships.
@ -96,6 +106,11 @@ class Transaction extends Model
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
}
public function children()
{
return $this->hasMany('App\Models\Banking\Transaction', 'parent_id');
}
public function contact()
{
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
@ -116,29 +131,27 @@ class Transaction extends Model
return $this->belongsTo('App\Models\Document\Document', 'document_id');
}
public function parent()
{
return $this->belongsTo('App\Models\Banking\Transaction', 'parent_id');
}
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
public function splits()
{
return $this->hasMany('App\Models\Banking\Transaction', 'split_id');
}
public function user()
{
return $this->belongsTo('App\Models\Auth\User', 'contact_id', 'id');
}
public function parent()
{
return $this->belongsTo('App\Models\Banking\Transaction', 'parent_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)
public function scopeType(Builder $query, $types): Builder
{
if (empty($types)) {
return $query;
@ -147,167 +160,110 @@ class Transaction extends Model
return $query->whereIn($this->qualifyColumn('type'), (array) $types);
}
/**
* Scope to include only income.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIncome($query)
public function scopeIncome(Builder $query): Builder
{
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getIncomeTypes());
}
/**
* Scope to include only expense.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeExpense($query)
public function scopeIncomeRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::INCOME_RECURRING_TYPE);
}
public function scopeExpense(Builder $query): Builder
{
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getExpenseTypes());
}
/**
* Get only transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsTransfer($query)
public function scopeExpenseRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::EXPENSE_RECURRING_TYPE);
}
public function scopeIsRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'like', '%-recurring');
}
public function scopeIsNotRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'not like', '%-recurring');
}
public function scopeIsTransfer(Builder $query): Builder
{
return $query->where('category_id', '=', Category::transfer());
}
/**
* Skip transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotTransfer($query)
public function scopeIsNotTransfer(Builder $query): Builder
{
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)
public function scopeIsDocument(Builder $query): Builder
{
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)
public function scopeIsNotDocument(Builder $query): Builder
{
return $query->whereNull('document_id');
}
/**
* Get by document id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $document_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDocumentId($query, $document_id)
public function scopeDocumentId(Builder $query, int $document_id): Builder
{
return $query->where('document_id', '=', $document_id);
}
/**
* 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)
public function scopeAccountId(Builder $query, int $account_id): Builder
{
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)
public function scopeContactId(Builder $query, int $contact_id): Builder
{
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)
public function scopeCategoryId(Builder $query, int $category_id): Builder
{
return $query->where('category_id', '=', $category_id);
}
/**
* Order by paid date.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeLatest($query)
public function scopeLatest(Builder $query): Builder
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Scope paid invoice.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePaid($query)
public function scopePaid(Builder $query): Builder
{
return $query->sum('amount');
}
/**
* Get only reconciled.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsReconciled($query)
public function scopeIsReconciled(Builder $query): Builder
{
return $query->where('reconciled', 1);
}
/**
* Get only not reconciled.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotReconciled($query)
public function scopeIsNotReconciled(Builder $query): Builder
{
return $query->where('reconciled', 0);
}
public function onCloning($src, $child = null)
{
$this->document_id = null;
if (app()->has(\App\Console\Commands\RecurringCheck::class)) {
$suffix = '';
} else {
$suffix = $src->isRecurringTransaction() ? '-recurring' : '';
}
$this->number = $this->getNextTransactionNumber($suffix);
$this->document_id = null;
$this->split_id = null;
}
/**
@ -346,6 +302,16 @@ class Transaction extends Model
return $this->getMedia('attachment')->all();
}
/**
* Get the splittable status.
*
* @return bool
*/
public function getIsSplittableAttribute()
{
return is_null($this->split_id);
}
public function delete_attachment()
{
if ($attachments = $this->attachment) {
@ -362,7 +328,7 @@ class Transaction extends Model
*/
public function getHasTransferRelationAttribute()
{
return (bool) (optional($this->category)->id == optional($this->category)->transfer());
return (bool) ($this->category?->id == $this->category?->transfer());
}
/**
@ -372,7 +338,9 @@ class Transaction extends Model
*/
public function getTypeTitleAttribute($value)
{
return $value ?? trans_choice('general.' . Str::plural($this->type), 1);
$type = $this->getRealTypeOfRecurringTransaction($this->type);
return $value ?? trans_choice('general.' . Str::plural($type), 1);
}
/**
@ -387,18 +355,18 @@ class Transaction extends Model
}
if ($this->isIncome()) {
if (! empty($this->document) && $this->document->type != 'invoice') {
if (! empty($this->document_id) && $this->document->type != 'invoice') {
return $this->getRouteFromConfig();
} else {
return ! empty($this->document_id) ? 'invoices.show' : 'revenues.show';
return !empty($this->document_id) ? 'invoices.show' : 'transactions.show';
}
}
if ($this->isExpense()) {
if (! empty($this->document) && $this->document->type != 'bill') {
if (! empty($this->document_id) && $this->document->type != 'bill') {
return $this->getRouteFromConfig();
} else {
return ! empty($this->document_id) ? 'bills.show' : 'payments.show';
return !empty($this->document_id) ? 'bills.show' : 'transactions.show';
}
}
@ -409,8 +377,8 @@ class Transaction extends Model
{
$route = '';
$alias = config('type.' . $this->document->type . '.alias');
$prefix = config('type.' . $this->document->type . '.route.prefix');
$alias = config('type.document.' . $this->document->type . '.alias');
$prefix = config('type.document.' . $this->document->type . '.route.prefix');
// if use module set module alias
if (!empty($alias)) {
@ -438,10 +406,195 @@ class Transaction extends Model
return !empty($value) ? $value : (!empty($this->document_id) ? $this->document_id : $this->id);
}
public function getTemplatePathAttribute($value = null)
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$type_for_theme = ($this->type == 'income') ? 'sales.revenues.print_default' : 'purchases.payments.print_default';
return $value ?: $type_for_theme;
$actions = [];
$prefix = 'transactions';
if (Str::contains($this->type, 'recurring')) {
$prefix = 'recurring-transactions';
}
try {
$actions[] = [
'title' => trans('general.show'),
'icon' => 'visibility',
'url' => route($prefix. '.show', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-show-' . $this->id,
],
];
} catch (\Exception $e) {}
try {
if (! $this->reconciled) {
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route($prefix. '.edit', $this->id),
'permission' => 'update-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-edit-' . $this->id,
],
];
}
} catch (\Exception $e) {}
try {
if (empty($this->document_id)) {
$actions[] = [
'title' => trans('general.duplicate'),
'icon' => 'file_copy',
'url' => route($prefix. '.duplicate', $this->id),
'permission' => 'create-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-duplicate-' . $this->id,
],
];
}
} catch (\Exception $e) {}
try {
if ($this->is_splittable && empty($this->document_id) && empty($this->recurring)) {
$conenct = [
'type' => 'button',
'title' => trans('general.connect'),
'icon' => 'sensors',
'permission' => 'create-banking-transactions',
'attributes' => [
'id' => 'index-transactions-more-actions-connect-' . $this->id,
],
];
$transaction = $this->load('account')->toJson();
$currency = $this->currency->toJson();
if ($this->contact->exists) {
$document = $this->contact->invoices()->notPaid()->where('currency_code', $this->currency_code)->with(['media', 'totals', 'transactions'])->get()->toJson();
$conenct['attributes']['@click'] = 'onConnect()';
} else {
$document = \App\Models\Document\Document::invoice()->notPaid()->where('currency_code', $this->currency_code)->with(['media', 'totals', 'transactions'])->get()->toJson();
$conenct['attributes']['@click'] = 'onConnect()';
}
$actions[] = $conenct;
$actions[] = [
'type' => 'divider',
];
}
} catch (\Exception $e) {}
try {
$actions[] = [
'title' => trans('general.print'),
'icon' => 'print',
'url' => route($prefix. '.print', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-print-' . $this->id,
'target' => '_blank',
],
];
} catch (\Exception $e) {}
try {
$actions[] = [
'title' => trans('general.download_pdf'),
'icon' => 'pdf',
'url' => route($prefix. '.pdf', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-pdf-' . $this->id,
'target' => '_blank',
],
];
} catch (\Exception $e) {}
if ($prefix != 'recurring-transactions') {
$actions[] = [
'type' => 'divider',
];
try {
$actions[] = [
'type' => 'button',
'title' => trans('general.share_link'),
'icon' => 'share',
'url' => route('modals.transactions.share.create', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-share-' . $this->id,
'@click' => 'onShareLink("' . route('modals.transactions.share.create', $this->id) . '")',
],
];
} catch (\Exception $e) {}
try {
$actions[] = [
'type' => 'button',
'title' => trans('invoices.send_mail'),
'icon' => 'email',
'url' => route('modals.transactions.emails.create', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
'id' => 'index-more-actions-send-email-' . $this->id,
'@click' => 'onEmail("' . route('modals.transactions.emails.create', $this->id) . '")',
],
];
} catch (\Exception $e) {}
$actions[] = [
'type' => 'divider',
];
try {
if (! $this->reconciled) {
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'text' => ! empty($this->recurring) ? 'transactions' : 'recurring_template',
'route' => $prefix. '.destroy',
'permission' => 'delete-banking-transactions',
'model' => $this,
];
}
} catch (\Exception $e) {}
} else {
try {
$actions[] = [
'title' => trans('general.end'),
'icon' => 'block',
'url' => route($prefix. '.end', $this->id),
'permission' => 'update-banking-transactions',
];
} catch (\Exception $e) {}
}
return $actions;
}
/**
* Get the recurring status label.
*
* @return string
*/
public function getRecurringStatusLabelAttribute()
{
return match($this->recurring->status) {
'active' => 'status-partial',
'ended' => 'status-success',
default => 'status-success',
};
}
/**