1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Models/Expense.php

351 lines
7.6 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2016-01-06 20:52:09 +01:00
use App\Events\ExpenseWasCreated;
2016-01-08 19:01:00 +01:00
use App\Events\ExpenseWasUpdated;
2017-01-30 20:40:43 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Laracasts\Presenter\PresentableTrait;
use Utils;
2016-01-06 20:52:09 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class Expense.
*/
2016-01-06 20:52:09 +01:00
class Expense extends EntityModel
{
2016-01-07 16:14:11 +01:00
// Expenses
2016-01-06 20:52:09 +01:00
use SoftDeletes;
use PresentableTrait;
/**
* @var array
*/
2016-02-18 11:02:30 +01:00
protected $dates = ['deleted_at'];
/**
* @var string
*/
2016-01-06 20:52:09 +01:00
protected $presenter = 'App\Ninja\Presenters\ExpensePresenter';
/**
* @var array
*/
2016-01-08 19:01:00 +01:00
protected $fillable = [
2016-01-21 21:36:49 +01:00
'client_id',
'vendor_id',
2016-02-01 23:07:09 +01:00
'expense_currency_id',
2016-10-02 09:10:28 +02:00
'expense_date',
2016-02-01 23:07:09 +01:00
'invoice_currency_id',
2016-01-08 19:01:00 +01:00
'amount',
'foreign_amount',
2016-01-08 19:01:00 +01:00
'exchange_rate',
'private_notes',
'public_notes',
2016-01-26 21:22:33 +01:00
'bank_id',
'transaction_id',
2016-07-05 20:49:47 +02:00
'expense_category_id',
2016-07-07 18:54:06 +02:00
'tax_rate1',
'tax_name1',
'tax_rate2',
'tax_name2',
2017-04-19 16:18:24 +02:00
'payment_date',
'payment_type_id',
'transaction_reference',
'invoice_documents',
2017-06-26 10:10:51 +02:00
'should_be_invoiced',
];
2016-10-02 09:10:28 +02:00
public static function getImportColumns()
{
return [
'client',
'vendor',
'amount',
'public_notes',
2016-10-05 22:46:15 +02:00
'expense_category',
2016-10-02 09:10:28 +02:00
'expense_date',
];
}
public static function getImportMap()
{
return [
'amount|total' => 'amount',
2016-10-05 22:46:15 +02:00
'category' => 'expense_category',
2016-10-02 09:10:28 +02:00
'client' => 'client',
'vendor' => 'vendor',
'notes|details' => 'public_notes',
'date' => 'expense_date',
];
}
2017-01-30 20:40:43 +01:00
2016-07-05 20:49:47 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function expense_category()
{
2016-07-06 20:35:16 +02:00
return $this->belongsTo('App\Models\ExpenseCategory')->withTrashed();
2016-07-05 20:49:47 +02:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 20:52:09 +01:00
public function account()
{
return $this->belongsTo('App\Models\Account');
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
2016-01-06 20:52:09 +01:00
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function vendor()
{
return $this->belongsTo('App\Models\Vendor')->withTrashed();
}
/**
* @return mixed
*/
2016-01-21 20:15:30 +01:00
public function client()
{
return $this->belongsTo('App\Models\Client')->withTrashed();
}
/**
* @return mixed
*/
2016-01-21 23:29:10 +01:00
public function invoice()
{
return $this->belongsTo('App\Models\Invoice')->withTrashed();
}
/**
* @return mixed
*/
public function documents()
{
return $this->hasMany('App\Models\Document')->orderBy('id');
}
2017-04-19 16:18:24 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function payment_type()
{
return $this->belongsTo('App\Models\PaymentType');
}
2017-06-26 20:36:58 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function recurring_expense()
{
return $this->belongsTo('App\Models\RecurringExpense');
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function getName()
{
2016-09-22 09:24:55 +02:00
if ($this->transaction_id) {
return $this->transaction_id;
} elseif ($this->public_notes) {
2017-03-08 07:46:24 +01:00
return Utils::truncateString($this->public_notes, 16);
2016-09-22 09:24:55 +02:00
} else {
return '#' . $this->public_id;
}
2016-01-08 19:01:00 +01:00
}
/**
* @return mixed
*/
2016-01-08 19:01:00 +01:00
public function getDisplayName()
{
return $this->getName();
2016-01-06 20:52:09 +01:00
}
/**
* @return string
*/
2016-01-08 19:01:00 +01:00
public function getRoute()
{
return "/expenses/{$this->public_id}";
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function getEntityType()
{
return ENTITY_EXPENSE;
}
/**
* @return bool
*/
public function isExchanged()
2016-01-06 20:52:09 +01:00
{
2016-07-27 12:54:00 +02:00
return $this->invoice_currency_id != $this->expense_currency_id || $this->exchange_rate != 1;
2016-01-06 20:52:09 +01:00
}
2017-04-19 16:18:24 +02:00
/**
* @return bool
*/
public function isPaid()
{
return $this->payment_date || $this->payment_type_id;
}
/**
* @return float
*/
public function convertedAmount()
{
return round($this->amount * $this->exchange_rate, 2);
}
2016-05-15 12:58:11 +02:00
/**
* @return array
*/
public function toArray()
{
$array = parent::toArray();
2016-05-15 12:58:11 +02:00
2017-01-30 17:05:31 +01:00
if (empty($this->visible) || in_array('converted_amount', $this->visible)) {
$array['converted_amount'] = $this->convertedAmount();
}
2016-05-15 12:58:11 +02:00
return $array;
}
2016-05-15 12:58:11 +02:00
2017-09-13 15:34:16 +02:00
/**
* @param $query
*
* @return mixed
*/
public function scopeDateRange($query, $startDate, $endDate)
{
return $query->whereBetween('expense_date', [$startDate, $endDate]);
}
/**
* @param $query
* @param null $bankdId
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-05-15 12:58:11 +02:00
public function scopeBankId($query, $bankdId = null)
{
if ($bankdId) {
$query->whereBankId($bankId);
}
return $query;
}
2016-10-27 16:26:42 +02:00
public function amountWithTax()
2018-01-23 19:32:11 +01:00
{
return $this->amount + $this->taxAmount();
}
public function taxAmount()
2016-10-27 16:26:42 +02:00
{
return Utils::calculateTaxes($this->amount, $this->tax_rate1, $this->tax_rate2);
}
2016-11-18 14:31:43 +01:00
public static function getStatuses($entityType = false)
{
2016-11-20 15:08:36 +01:00
$statuses = [];
2016-11-18 14:31:43 +01:00
$statuses[EXPENSE_STATUS_LOGGED] = trans('texts.logged');
2017-04-19 16:18:24 +02:00
$statuses[EXPENSE_STATUS_PENDING] = trans('texts.pending');
2016-11-18 14:31:43 +01:00
$statuses[EXPENSE_STATUS_INVOICED] = trans('texts.invoiced');
2017-04-19 16:18:24 +02:00
$statuses[EXPENSE_STATUS_BILLED] = trans('texts.billed');
2016-11-18 14:31:43 +01:00
$statuses[EXPENSE_STATUS_PAID] = trans('texts.paid');
2017-04-19 16:18:24 +02:00
$statuses[EXPENSE_STATUS_UNPAID] = trans('texts.unpaid');
2016-11-18 14:31:43 +01:00
return $statuses;
}
2016-12-26 19:09:49 +01:00
2017-04-19 16:18:24 +02:00
public static function calcStatusLabel($shouldBeInvoiced, $invoiceId, $balance, $paymentDate)
2016-12-26 19:09:49 +01:00
{
if ($invoiceId) {
if (floatval($balance) > 0) {
$label = 'invoiced';
} else {
2017-04-19 16:18:24 +02:00
$label = 'billed';
2016-12-26 19:09:49 +01:00
}
} elseif ($shouldBeInvoiced) {
$label = 'pending';
} else {
$label = 'logged';
}
2017-04-19 16:18:24 +02:00
$label = trans("texts.{$label}");
if ($paymentDate) {
$label = trans('texts.paid') . ' | ' . $label;
}
return $label;
2016-12-26 19:09:49 +01:00
}
public static function calcStatusClass($shouldBeInvoiced, $invoiceId, $balance)
{
if ($invoiceId) {
if (floatval($balance) > 0) {
return 'default';
} else {
return 'success';
}
} elseif ($shouldBeInvoiced) {
return 'warning';
} else {
return 'primary';
}
}
public function statusClass()
{
$balance = $this->invoice ? $this->invoice->balance : 0;
return static::calcStatusClass($this->should_be_invoiced, $this->invoice_id, $balance);
}
public function statusLabel()
{
$balance = $this->invoice ? $this->invoice->balance : 0;
2017-04-19 16:18:24 +02:00
return static::calcStatusLabel($this->should_be_invoiced, $this->invoice_id, $balance, $this->payment_date);
2016-12-26 19:09:49 +01:00
}
2016-01-06 20:52:09 +01:00
}
Expense::creating(function ($expense) {
$expense->setNullValues();
});
Expense::created(function ($expense) {
event(new ExpenseWasCreated($expense));
});
Expense::updating(function ($expense) {
$expense->setNullValues();
});
Expense::updated(function ($expense) {
event(new ExpenseWasUpdated($expense));
});
Expense::deleting(function ($expense) {
$expense->setNullValues();
});