Include taxes in expense amount

This commit is contained in:
Hillel Coren 2016-10-27 17:26:42 +03:00
parent c3c0ece14a
commit 7465eb1631
7 changed files with 32 additions and 23 deletions

View File

@ -464,36 +464,31 @@ class ReportController extends BaseController
*/ */
private function generateExpenseReport($startDate, $endDate, $isExport) private function generateExpenseReport($startDate, $endDate, $isExport)
{ {
$columns = ['vendor', 'client', 'date', 'expense_amount', 'invoiced_amount']; $columns = ['vendor', 'client', 'date', 'expense_amount'];
$account = Auth::user()->account; $account = Auth::user()->account;
$displayData = []; $displayData = [];
$reportTotals = []; $reportTotals = [];
$expenses = Expense::scope() $expenses = Expense::scope()
->withTrashed() ->withArchived()
->with('client.contacts', 'vendor') ->with('client.contacts', 'vendor')
->where('expense_date', '>=', $startDate) ->where('expense_date', '>=', $startDate)
->where('expense_date', '<=', $endDate); ->where('expense_date', '<=', $endDate);
foreach ($expenses->get() as $expense) { foreach ($expenses->get() as $expense) {
$amount = $expense->amount; $amount = $expense->amountWithTax();
$invoiced = $expense->present()->invoiced_amount;
$displayData[] = [ $displayData[] = [
$expense->vendor ? ($isExport ? $expense->vendor->name : $expense->vendor->present()->link) : '', $expense->vendor ? ($isExport ? $expense->vendor->name : $expense->vendor->present()->link) : '',
$expense->client ? ($isExport ? $expense->client->getDisplayName() : $expense->client->present()->link) : '', $expense->client ? ($isExport ? $expense->client->getDisplayName() : $expense->client->present()->link) : '',
$expense->present()->expense_date, $expense->present()->expense_date,
Utils::formatMoney($amount, $expense->currency_id), 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->expense_currency_id, 'amount', $amount);
$reportTotals = $this->addToTotals($reportTotals, $expense->invoice_currency_id, 'amount', 0); $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 [ return [

View File

@ -1089,4 +1089,12 @@ class Utils
return url(NINJA_DOCS_URL . $page); 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);
}
} }

View File

@ -1,5 +1,6 @@
<?php namespace App\Models; <?php namespace App\Models;
use Utils;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use App\Events\ExpenseWasCreated; use App\Events\ExpenseWasCreated;
@ -205,6 +206,11 @@ class Expense extends EntityModel
return $query; return $query;
} }
public function amountWithTax()
{
return Utils::calculateTaxes($this->amount, $this->tax_rate1, $this->tax_rate2);
}
} }
Expense::creating(function ($expense) { Expense::creating(function ($expense) {

View File

@ -56,14 +56,16 @@ class ExpenseDatatable extends EntityDatatable
[ [
'amount', 'amount',
function ($model) { 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 // show both the amount and the converted amount
if ($model->exchange_rate != 1) { if ($model->exchange_rate != 1) {
$converted = round($model->amount * $model->exchange_rate, 2); $converted = round($amount * $model->exchange_rate, 2);
return Utils::formatMoney($model->amount, $model->expense_currency_id) . ' | ' . $str .= ' | ' . Utils::formatMoney($converted, $model->invoice_currency_id);
Utils::formatMoney($converted, $model->invoice_currency_id);
} else {
return Utils::formatMoney($model->amount, $model->expense_currency_id);
} }
return $str;
} }
], ],
[ [

View File

@ -24,12 +24,4 @@ class ExpensePresenter extends EntityPresenter
return Utils::fromSqlDate($this->entity->expense_date); return Utils::fromSqlDate($this->entity->expense_date);
} }
/**
* @return int
*/
public function invoiced_amount()
{
return $this->entity->invoice_id ? $this->entity->convertedAmount() : 0;
}
} }

View File

@ -137,7 +137,7 @@ class DashboardRepository
->where('invoices.is_deleted', '=', false) ->where('invoices.is_deleted', '=', false)
->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]); ->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]);
} elseif ($entityType == ENTITY_EXPENSE) { } 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; return $records;
@ -335,8 +335,12 @@ class DashboardRepository
public function expenses($accountId, $userId, $viewAll) 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( $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' .DB::getQueryGrammar()->wrap('expenses.expense_currency_id', true).' as currency_id'
); );
$paidToDate = DB::table('accounts') $paidToDate = DB::table('accounts')

View File

@ -76,6 +76,8 @@ class ExpenseRepository extends BaseRepository
'expenses.expense_currency_id', 'expenses.expense_currency_id',
'expenses.invoice_currency_id', 'expenses.invoice_currency_id',
'expenses.user_id', 'expenses.user_id',
'expenses.tax_rate1',
'expenses.tax_rate2',
'expense_categories.name as category', 'expense_categories.name as category',
'invoices.public_id as invoice_public_id', 'invoices.public_id as invoice_public_id',
'invoices.user_id as invoice_user_id', 'invoices.user_id as invoice_user_id',