1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Working on expenses

This commit is contained in:
Hillel Coren 2016-01-22 00:29:10 +02:00
parent 7a26dd610c
commit 360b59baa0
10 changed files with 106 additions and 28 deletions

View File

@ -49,7 +49,7 @@ class ExpenseController extends BaseController
'checkbox',
'vendor',
'expense_date',
'expense_amount',
'amount',
'public_notes',
'status',
''
@ -96,17 +96,42 @@ class ExpenseController extends BaseController
$expense = Expense::scope($publicId)->firstOrFail();
$expense->expense_date = Utils::fromSqlDate($expense->expense_date);
$actions = [];
if ($expense->invoice) {
$actions[] = ['url' => URL::to("invoices/{$expense->invoice->public_id}/edit"), 'label' => trans("texts.view_invoice")];
} else {
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans("texts.invoice_expense")];
/*
// check for any open invoices
$invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id) : [];
foreach ($invoices as $invoice) {
$actions[] = ['url' => 'javascript:submitAction("add_to_invoice", '.$invoice->public_id.')', 'label' => trans("texts.add_to_invoice", ["invoice" => $invoice->invoice_number])];
}
*/
}
$actions[] = \DropdownButton::DIVIDER;
if (!$expense->trashed()) {
$actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_expense')];
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_expense')];
} else {
$actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_expense')];
}
$data = array(
'vendor' => null,
'expense' => $expense,
'method' => 'PUT',
'url' => 'expenses/'.$publicId,
'title' => 'Edit Expense',
'actions' => $actions,
'vendors' => Vendor::scope()->with('vendorcontacts')->orderBy('name')->get(),
'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null,
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
'clientPublicId' => $expense->client ? $expense->client->public_id : null,
);
);
$data = array_merge($data, self::getViewModel());
@ -131,6 +156,11 @@ class ExpenseController extends BaseController
Session::flash('message', trans('texts.updated_expense'));
$action = Input::get('action');
if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) {
return self::bulk();
}
return redirect()->to("expenses/{$expense->public_id}/edit");
}
@ -161,10 +191,10 @@ class ExpenseController extends BaseController
if ($expense->client_id) {
if (!$clientPublicId) {
$clientPublicId = $expense->client_id;
} else if ($clientPublicId != $expense->client_id) {
Session::flash('error', trans('texts.expense_error_multiple_clients'));
return Redirect::to('expenses');
}
} elseif ($clientPublicId != $expense->client_id) {
Session::flash('error', trans('texts.expense_error_multiple_clients'));
return Redirect::to('expenses');
}
}
if ($expense->invoice_id) {

View File

@ -258,7 +258,7 @@ class Utils
return $data->first();
}
public static function formatMoney($value, $currencyId = false, $countryId = false, $hideSymbol = false)
public static function formatMoney($value, $currencyId = false, $countryId = false, $showCode = false)
{
if (!$value) {
$value = 0;
@ -292,9 +292,7 @@ class Utils
$value = number_format($value, $currency->precision, $decimal, $thousand);
$symbol = $currency->symbol;
if ($hideSymbol) {
return $value;
} elseif (!$symbol) {
if ($showCode || !$symbol) {
return "{$value} {$code}";
} elseif ($swapSymbol) {
return "{$value} " . trim($symbol);

View File

@ -45,6 +45,11 @@ class Expense extends EntityModel
return $this->belongsTo('App\Models\Client')->withTrashed();
}
public function invoice()
{
return $this->belongsTo('App\Models\Invoice')->withTrashed();
}
public function getName()
{
if($this->expense_number)

View File

@ -31,12 +31,16 @@ class ExpenseRepository extends BaseRepository
->join('accounts', 'accounts.id', '=', 'expenses.account_id')
->where('expenses.account_id', '=', $accountid)
->where('expenses.vendor_id', '=', $vendorPublicId)
->select('expenses.id',
'expenses.expense_date',
'expenses.amount',
'expenses.public_notes',
'expenses.public_id',
'expenses.deleted_at', 'expenses.should_be_invoiced', 'expenses.created_at');
->select(
'expenses.id',
'expenses.expense_date',
'expenses.amount',
'expenses.public_notes',
'expenses.public_id',
'expenses.deleted_at',
'expenses.should_be_invoiced',
'expenses.created_at'
);
return $query;
}
@ -46,7 +50,9 @@ class ExpenseRepository extends BaseRepository
$accountid = \Auth::user()->account_id;
$query = DB::table('expenses')
->join('accounts', 'accounts.id', '=', 'expenses.account_id')
->leftjoin('vendors', 'vendors.public_id', '=', 'expenses.vendor_id')
->leftjoin('clients', 'clients.id', '=', 'expenses.client_id')
->leftjoin('vendors', 'vendors.id', '=', 'expenses.vendor_id')
->leftJoin('invoices', 'invoices.id', '=', 'expenses.invoice_id')
->where('expenses.account_id', '=', $accountid)
->select('expenses.account_id',
'expenses.amount',
@ -62,8 +68,13 @@ class ExpenseRepository extends BaseRepository
'expenses.public_notes',
'expenses.should_be_invoiced',
'expenses.vendor_id',
'invoices.public_id as invoice_public_id',
'vendors.name as vendor_name',
'vendors.public_id as vendor_public_id');
'vendors.public_id as vendor_public_id',
'accounts.country_id as account_country_id',
'accounts.currency_id as account_currency_id',
'clients.country_id as client_country_id'
);
$showTrashed = \Session::get('show_trash:expense');

View File

@ -69,7 +69,11 @@ class ExpenseService extends BaseService
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, false, false);
$str = Utils::formatMoney($model->amount, $model->account_currency_id, $model->account_country_id, true);
if ($model->exchange_rate != 1) {
$str .= ' | ' . Utils::formatMoney(round($model->amount * $model->exchange_rate,2), $model->currency_id, $model->client_country_id, true);
}
return $str;
}
],
[
@ -126,14 +130,24 @@ class ExpenseService extends BaseService
return URL::to("expenses/{$model->public_id}/edit") ;
}
],
/*
[
trans('texts.view_invoice'),
function ($model) {
return URL::to("/invoices/{$model->invoice_public_id}/edit");
},
function ($model) {
return $model->invoice_public_id;
}
],
[
trans('texts.invoice_expense'),
function ($model) {
return URL::to("expense/invoice/{$model->public_id}") . '?client=1';
return "javascript:invoiceEntity({$model->public_id})";
},
function ($model) {
return ! $model->invoice_id && (!$model->deleted_at || $model->deleted_at == '0000-00-00');
}
],
*/
];
}

View File

@ -106,7 +106,7 @@ class TaskService extends BaseService
[
trans('texts.invoice_task'),
function ($model) {
return "javascript:invoiceTask({$model->public_id})";
return "javascript:invoiceEntity({$model->public_id})";
},
function ($model) {
return ! $model->invoice_number && (!$model->deleted_at || $model->deleted_at == '0000-00-00');

View File

@ -8,7 +8,10 @@
@section('content')
{!! Former::open($url)->addClass('warn-on-exit')->method($method) !!}
{!! Former::open($url)->addClass('warn-on-exit main-form')->method($method) !!}
<div style="display:none">
{!! Former::text('action') !!}
</div>
@if ($expense)
{!! Former::populate($expense) !!}
@ -83,6 +86,12 @@
<center class="buttons">
{!! Button::normal(trans('texts.cancel'))->large()->asLinkTo(URL::to('/expenses'))->appendIcon(Icon::create('remove-circle')) !!}
{!! Button::success(trans('texts.save'))->submit()->large()->appendIcon(Icon::create('floppy-disk')) !!}
@if ($expense)
{!! DropdownButton::normal(trans('texts.more_actions'))
->withContents($actions)
->large()
->dropup() !!}
@endif
</center>
{!! Former::close() !!}
@ -106,6 +115,17 @@
}
}
function submitAction(action) {
$('#action').val(action);
$('.main-form').submit();
}
function onDeleteClick() {
if (confirm('{!! trans("texts.are_you_sure") !!}')) {
submitAction('delete');
}
}
$(function() {
var $vendorSelect = $('select#vendor_id');

View File

@ -373,9 +373,9 @@
{!! HTML::nav_link('dashboard', 'dashboard') !!}
{!! HTML::menu_link('client') !!}
{!! HTML::menu_link('task') !!}
{!! HTML::menu_link('expense') !!}
{!! HTML::menu_link('invoice') !!}
{!! HTML::menu_link('payment') !!}
{!! HTML::menu_link('expense') !!}
</ul>
<div class="navbar-form navbar-right">

View File

@ -91,7 +91,7 @@
submitForm('stop');
}
function invoiceTask(id) {
function invoiceEntity(id) {
$('#public_id').val(id);
submitForm('invoice');
}

View File

@ -101,7 +101,7 @@
<header>
@if ($client)
<h2>{{ $client->getDisplayName() }}</h2>
<h3>{{ trans('texts.invoice') . ' ' . $invoiceNumber }}<span>|&nbsp; {{ trans('texts.amount_due') }}: <em>{{ $account->formatMoney($amount, $client, true) }} {{ $currencyCode }}</em></span></h3>
<h3>{{ trans('texts.invoice') . ' ' . $invoiceNumber }}<span>|&nbsp; {{ trans('texts.amount_due') }}: <em>{{ $account->formatMoney($amount, $client, true) }}</em></span></h3>
@elseif ($paymentTitle)
<h2>{{ $paymentTitle }}<br/><small>{{ $paymentSubtitle }}</small></h2>
@endif
@ -280,7 +280,7 @@
<div class="row">
<div class="col-md-4 col-md-offset-4">
{!! Button::success(strtoupper(trans('texts.pay_now') . ' - ' . $account->formatMoney($amount, $client, true) . ' ' . $currencyCode ))->submit()->block()->large() !!}
{!! Button::success(strtoupper(trans('texts.pay_now') . ' - ' . $account->formatMoney($amount, $client, true) ))->submit()->block()->large() !!}
</div>
</div>