Implemented basic expense report

This commit is contained in:
Hillel Coren 2016-02-24 23:54:43 +02:00
parent a1d13cc84c
commit 88fcc05902
4 changed files with 77 additions and 17 deletions

View File

@ -12,6 +12,7 @@ use View;
use App\Models\Account; use App\Models\Account;
use App\Models\Client; use App\Models\Client;
use App\Models\Payment; use App\Models\Payment;
use App\Models\Expense;
class ReportController extends BaseController class ReportController extends BaseController
{ {
@ -81,6 +82,7 @@ class ReportController extends BaseController
ENTITY_INVOICE => trans('texts.invoice'), ENTITY_INVOICE => trans('texts.invoice'),
ENTITY_PAYMENT => trans('texts.payment'), ENTITY_PAYMENT => trans('texts.payment'),
ENTITY_TAX_RATE => trans('texts.taxes'), ENTITY_TAX_RATE => trans('texts.taxes'),
ENTITY_EXPENSE => trans('texts.expenses'),
]; ];
$params = [ $params = [
@ -228,6 +230,8 @@ class ReportController extends BaseController
return $this->generatePaymentReport($startDate, $endDate, $isExport); return $this->generatePaymentReport($startDate, $endDate, $isExport);
} elseif ($reportType == ENTITY_TAX_RATE) { } elseif ($reportType == ENTITY_TAX_RATE) {
return $this->generateTaxRateReport($startDate, $endDate, $dateField, $isExport); return $this->generateTaxRateReport($startDate, $endDate, $dateField, $isExport);
} elseif ($reportType == ENTITY_EXPENSE) {
return $this->generateExpenseReport($startDate, $endDate, $isExport);
} }
} }
@ -294,8 +298,8 @@ class ReportController extends BaseController
]; ];
} }
$reportTotals = $this->addToTotals($reportTotals, $client, 'amount', $tax['amount']); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $tax['amount']);
$reportTotals = $this->addToTotals($reportTotals, $client, 'paid', $tax['paid']); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $tax['paid']);
} }
} }
@ -341,8 +345,8 @@ class ReportController extends BaseController
$payment->present()->method, $payment->present()->method,
]; ];
$reportTotals = $this->addToTotals($reportTotals, $client, 'amount', $invoice->amount); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount);
$reportTotals = $this->addToTotals($reportTotals, $client, 'paid', $payment->amount); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $payment->amount);
} }
return [ return [
@ -388,9 +392,9 @@ class ReportController extends BaseController
$account->formatMoney($invoice->getAmountPaid(), $client), $account->formatMoney($invoice->getAmountPaid(), $client),
$account->formatMoney($invoice->balance, $client), $account->formatMoney($invoice->balance, $client),
]; ];
$reportTotals = $this->addToTotals($reportTotals, $client, 'amount', $invoice->amount); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount);
$reportTotals = $this->addToTotals($reportTotals, $client, 'paid', $invoice->getAmountPaid()); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $invoice->getAmountPaid());
$reportTotals = $this->addToTotals($reportTotals, $client, 'balance', $invoice->balance); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'balance', $invoice->balance);
} }
} }
@ -410,14 +414,12 @@ class ReportController extends BaseController
$reportTotals = []; $reportTotals = [];
$clients = Client::scope() $clients = Client::scope()
->withTrashed() ->withArchived()
->with('contacts') ->with('contacts')
->where('is_deleted', '=', false)
->with(['invoices' => function($query) use ($startDate, $endDate) { ->with(['invoices' => function($query) use ($startDate, $endDate) {
$query->where('invoice_date', '>=', $startDate) $query->where('invoice_date', '>=', $startDate)
->where('invoice_date', '<=', $endDate) ->where('invoice_date', '<=', $endDate)
->where('is_deleted', '=', false) ->withArchived();
->withTrashed();
}]); }]);
foreach ($clients->get() as $client) { foreach ($clients->get() as $client) {
@ -436,9 +438,9 @@ class ReportController extends BaseController
$account->formatMoney($amount - $paid, $client) $account->formatMoney($amount - $paid, $client)
]; ];
$reportTotals = $this->addToTotals($reportTotals, $client, 'amount', $amount); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $amount);
$reportTotals = $this->addToTotals($reportTotals, $client, 'paid', $paid); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $paid);
$reportTotals = $this->addToTotals($reportTotals, $client, 'balance', $amount - $paid); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'balance', $amount - $paid);
} }
return [ return [
@ -448,8 +450,49 @@ class ReportController extends BaseController
]; ];
} }
private function addToTotals($data, $client, $field, $value) { private function generateExpenseReport($startDate, $endDate, $isExport)
$currencyId = $client->currency_id ?: Auth::user()->account->getCurrencyId(); {
$columns = ['vendor', 'client', 'date', 'expense_amount', 'invoiced_amount'];
$account = Auth::user()->account;
$displayData = [];
$reportTotals = [];
$expenses = Expense::scope()
->withTrashed()
->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;
$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 [
'columns' => $columns,
'displayData' => $displayData,
'reportTotals' => $reportTotals,
];
}
private function addToTotals($data, $currencyId, $field, $value) {
$currencyId = $currencyId ?: Auth::user()->account->getCurrencyId();
if (!isset($data[$currencyId][$field])) { if (!isset($data[$currencyId][$field])) {
$data[$currencyId][$field] = 0; $data[$currencyId][$field] = 0;

View File

@ -20,4 +20,14 @@ class ExpensePresenter extends Presenter {
{ {
return round($this->entity->amount * $this->entity->exchange_rate, 2); return round($this->entity->amount * $this->entity->exchange_rate, 2);
} }
public function invoiced_amount()
{
return $this->entity->invoice_id ? $this->converted_amount() : 0;
}
public function link()
{
return link_to('/expenses/' . $this->entity->public_id, $this->entity->name);
}
} }

View File

@ -9,4 +9,9 @@ class VendorPresenter extends Presenter {
{ {
return $this->entity->country ? $this->entity->country->name : ''; return $this->entity->country ? $this->entity->country->name : '';
} }
public function link()
{
return link_to('/vendors/' . $this->entity->public_id, $this->entity->name);
}
} }

View File

@ -1034,7 +1034,7 @@ $LANG = array(
'list_clients' => 'List Clients', 'list_clients' => 'List Clients',
'list_quotes' => 'List Quotes', 'list_quotes' => 'List Quotes',
'list_tasks' => 'List Tasks', 'list_tasks' => 'List Tasks',
'list_expensess' => 'List Expenses', 'list_expenses' => 'List Expenses',
'list_recurring_invoices' => 'List Recurring Invoices', 'list_recurring_invoices' => 'List Recurring Invoices',
'list_payments' => 'List Payments', 'list_payments' => 'List Payments',
'list_credits' => 'List Credits', 'list_credits' => 'List Credits',
@ -1045,6 +1045,8 @@ $LANG = array(
'new_user' => 'New User', 'new_user' => 'New User',
'new_product' => 'New Product', 'new_product' => 'New Product',
'new_tax_rate' => 'New Tax Rate', 'new_tax_rate' => 'New Tax Rate',
'invoiced_amount' => 'Invoiced Amount',
); );
return $LANG; return $LANG;