From 360b59baa0ee49c8baad236644bb1410d7c65032 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 22 Jan 2016 00:29:10 +0200 Subject: [PATCH] Working on expenses --- app/Http/Controllers/ExpenseController.php | 42 +++++++++++++++++--- app/Libraries/Utils.php | 6 +-- app/Models/Expense.php | 5 +++ app/Ninja/Repositories/ExpenseRepository.php | 27 +++++++++---- app/Services/ExpenseService.php | 22 ++++++++-- app/Services/TaskService.php | 2 +- resources/views/expenses/edit.blade.php | 22 +++++++++- resources/views/header.blade.php | 2 +- resources/views/list.blade.php | 2 +- resources/views/payments/payment.blade.php | 4 +- 10 files changed, 106 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index 9630fb5ee18c..25f78be2d361 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -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) { diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index cfeb191bb8e8..113a78671071 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -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); diff --git a/app/Models/Expense.php b/app/Models/Expense.php index f1bea2625a98..ce1241b11b2a 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -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) diff --git a/app/Ninja/Repositories/ExpenseRepository.php b/app/Ninja/Repositories/ExpenseRepository.php index a4d971d56186..8b47a48e5aff 100644 --- a/app/Ninja/Repositories/ExpenseRepository.php +++ b/app/Ninja/Repositories/ExpenseRepository.php @@ -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'); diff --git a/app/Services/ExpenseService.php b/app/Services/ExpenseService.php index c6de78685273..92a2a048c14f 100644 --- a/app/Services/ExpenseService.php +++ b/app/Services/ExpenseService.php @@ -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'); } ], - */ ]; } diff --git a/app/Services/TaskService.php b/app/Services/TaskService.php index abf49d2d65ef..5eded5db6e26 100644 --- a/app/Services/TaskService.php +++ b/app/Services/TaskService.php @@ -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'); diff --git a/resources/views/expenses/edit.blade.php b/resources/views/expenses/edit.blade.php index 6eb092af3670..8071c0b44ef6 100644 --- a/resources/views/expenses/edit.blade.php +++ b/resources/views/expenses/edit.blade.php @@ -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) !!} +
+ {!! Former::text('action') !!} +
@if ($expense) {!! Former::populate($expense) !!} @@ -83,6 +86,12 @@
{!! 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
{!! 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'); diff --git a/resources/views/header.blade.php b/resources/views/header.blade.php index f20244b6416c..0236388c8786 100644 --- a/resources/views/header.blade.php +++ b/resources/views/header.blade.php @@ -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') !!}