mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Include taxes in expense amount
This commit is contained in:
parent
c3c0ece14a
commit
7465eb1631
@ -464,36 +464,31 @@ class ReportController extends BaseController
|
||||
*/
|
||||
private function generateExpenseReport($startDate, $endDate, $isExport)
|
||||
{
|
||||
$columns = ['vendor', 'client', 'date', 'expense_amount', 'invoiced_amount'];
|
||||
$columns = ['vendor', 'client', 'date', 'expense_amount'];
|
||||
|
||||
$account = Auth::user()->account;
|
||||
$displayData = [];
|
||||
$reportTotals = [];
|
||||
|
||||
$expenses = Expense::scope()
|
||||
->withTrashed()
|
||||
->withArchived()
|
||||
->with('client.contacts', 'vendor')
|
||||
->where('expense_date', '>=', $startDate)
|
||||
->where('expense_date', '<=', $endDate);
|
||||
|
||||
|
||||
foreach ($expenses->get() as $expense) {
|
||||
$amount = $expense->amount;
|
||||
$invoiced = $expense->present()->invoiced_amount;
|
||||
$amount = $expense->amountWithTax();
|
||||
|
||||
$displayData[] = [
|
||||
$expense->vendor ? ($isExport ? $expense->vendor->name : $expense->vendor->present()->link) : '',
|
||||
$expense->client ? ($isExport ? $expense->client->getDisplayName() : $expense->client->present()->link) : '',
|
||||
$expense->present()->expense_date,
|
||||
Utils::formatMoney($amount, $expense->currency_id),
|
||||
Utils::formatMoney($invoiced, $expense->invoice_currency_id),
|
||||
];
|
||||
|
||||
$reportTotals = $this->addToTotals($reportTotals, $expense->expense_currency_id, 'amount', $amount);
|
||||
$reportTotals = $this->addToTotals($reportTotals, $expense->invoice_currency_id, 'amount', 0);
|
||||
|
||||
$reportTotals = $this->addToTotals($reportTotals, $expense->invoice_currency_id, 'invoiced', $invoiced);
|
||||
$reportTotals = $this->addToTotals($reportTotals, $expense->expense_currency_id, 'invoiced', 0);
|
||||
}
|
||||
|
||||
return [
|
||||
|
@ -1089,4 +1089,12 @@ class Utils
|
||||
|
||||
return url(NINJA_DOCS_URL . $page);
|
||||
}
|
||||
|
||||
public static function calculateTaxes($amount, $taxRate1, $taxRate2)
|
||||
{
|
||||
$tax1 = round($amount * $taxRate1 / 100, 2);
|
||||
$tax2 = round($amount * $taxRate2 / 100, 2);
|
||||
|
||||
return round($amount + $tax1 + $tax2, 2);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php namespace App\Models;
|
||||
|
||||
use Utils;
|
||||
use Laracasts\Presenter\PresentableTrait;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use App\Events\ExpenseWasCreated;
|
||||
@ -205,6 +206,11 @@ class Expense extends EntityModel
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function amountWithTax()
|
||||
{
|
||||
return Utils::calculateTaxes($this->amount, $this->tax_rate1, $this->tax_rate2);
|
||||
}
|
||||
}
|
||||
|
||||
Expense::creating(function ($expense) {
|
||||
|
@ -56,14 +56,16 @@ class ExpenseDatatable extends EntityDatatable
|
||||
[
|
||||
'amount',
|
||||
function ($model) {
|
||||
$amount = Utils::calculateTaxes($model->amount, $model->tax_rate1, $model->tax_rate2);
|
||||
$str = Utils::formatMoney($amount, $model->expense_currency_id);
|
||||
|
||||
// show both the amount and the converted amount
|
||||
if ($model->exchange_rate != 1) {
|
||||
$converted = round($model->amount * $model->exchange_rate, 2);
|
||||
return Utils::formatMoney($model->amount, $model->expense_currency_id) . ' | ' .
|
||||
Utils::formatMoney($converted, $model->invoice_currency_id);
|
||||
} else {
|
||||
return Utils::formatMoney($model->amount, $model->expense_currency_id);
|
||||
$converted = round($amount * $model->exchange_rate, 2);
|
||||
$str .= ' | ' . Utils::formatMoney($converted, $model->invoice_currency_id);
|
||||
}
|
||||
|
||||
return $str;
|
||||
}
|
||||
],
|
||||
[
|
||||
|
@ -24,12 +24,4 @@ class ExpensePresenter extends EntityPresenter
|
||||
return Utils::fromSqlDate($this->entity->expense_date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function invoiced_amount()
|
||||
{
|
||||
return $this->entity->invoice_id ? $this->entity->convertedAmount() : 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ class DashboardRepository
|
||||
->where('invoices.is_deleted', '=', false)
|
||||
->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]);
|
||||
} elseif ($entityType == ENTITY_EXPENSE) {
|
||||
$records->select(DB::raw('sum(expenses.amount) as total, count(expenses.id) as count, '.$timeframe.' as '.$groupBy));
|
||||
$records->select(DB::raw('sum(expenses.amount + (expenses.amount * expenses.tax_rate1 / 100) + (expenses.amount * expenses.tax_rate2 / 100)) as total, count(expenses.id) as count, '.$timeframe.' as '.$groupBy));
|
||||
}
|
||||
|
||||
return $records;
|
||||
@ -335,8 +335,12 @@ class DashboardRepository
|
||||
|
||||
public function expenses($accountId, $userId, $viewAll)
|
||||
{
|
||||
$amountField = DB::getQueryGrammar()->wrap('expenses.amount', true);
|
||||
$taxRate1Field = DB::getQueryGrammar()->wrap('expenses.tax_rate1', true);
|
||||
$taxRate2Field = DB::getQueryGrammar()->wrap('expenses.tax_rate2', true);
|
||||
|
||||
$select = DB::raw(
|
||||
'SUM('.DB::getQueryGrammar()->wrap('expenses.amount', true).') as value,'
|
||||
"SUM({$amountField} + ({$amountField} * {$taxRate1Field} / 100) + ({$amountField} * {$taxRate2Field} / 100)) as value,"
|
||||
.DB::getQueryGrammar()->wrap('expenses.expense_currency_id', true).' as currency_id'
|
||||
);
|
||||
$paidToDate = DB::table('accounts')
|
||||
|
@ -76,6 +76,8 @@ class ExpenseRepository extends BaseRepository
|
||||
'expenses.expense_currency_id',
|
||||
'expenses.invoice_currency_id',
|
||||
'expenses.user_id',
|
||||
'expenses.tax_rate1',
|
||||
'expenses.tax_rate2',
|
||||
'expense_categories.name as category',
|
||||
'invoices.public_id as invoice_public_id',
|
||||
'invoices.user_id as invoice_user_id',
|
||||
|
Loading…
x
Reference in New Issue
Block a user