From a52364a0396d6740cd52a659afdbbbd171fc3846 Mon Sep 17 00:00:00 2001 From: steenrabol Date: Thu, 7 Jan 2016 12:04:01 +0100 Subject: [PATCH] expense --- app/Http/Controllers/ExpenseController.php | 3 +- app/Http/routes.php | 14 +--- app/Models/Expense.php | 6 -- app/Models/ExpenseActivity.php | 64 +++++++++++++++ app/Ninja/Presenters/VendorPresenter.php | 12 +++ .../ExpenseActivityRepository.php | 80 +++++++++++++++++++ app/Ninja/Repositories/ExpenseRepository.php | 42 ++++++++-- app/Services/ExpenseService.php | 22 ++--- ...016_01_06_155001_create_expenses_table.php | 1 + ...191912_create_expense_activities_table.php | 51 ++++++++++++ resources/lang/en/texts.php | 4 +- 11 files changed, 262 insertions(+), 37 deletions(-) create mode 100644 app/Models/ExpenseActivity.php create mode 100644 app/Ninja/Presenters/VendorPresenter.php create mode 100644 app/Ninja/Repositories/ExpenseActivityRepository.php create mode 100644 database/migrations/2016_01_06_191912_create_expense_activities_table.php diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index c441a62dd0f6..8ecb5184ccd4 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -46,6 +46,7 @@ class ExpenseController extends BaseController 'expense_balance', 'expense_date', 'private_notes', + 'public_notes', '' ]), )); @@ -92,7 +93,7 @@ class ExpenseController extends BaseController public function store(CreateExpenseRequest $request) { $expense = $this->expenseRepo->save($request->input()); - + Session::flash('message', trans('texts.created_expense')); return redirect()->to('expenses'); diff --git a/app/Http/routes.php b/app/Http/routes.php index f4bdc8865d9c..c94f16641abe 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -190,20 +190,12 @@ Route::group(['middleware' => 'auth'], function() { Route::post('vendors/bulk', 'VendorController@bulk'); // Expense - Route::get('expenses/{id}/edit', function() { - return View::make('header'); - }); - Route::resource('expenses', 'ExpenseController'); Route::get('expenses/create/{vendor_id?}', 'ExpenseController@create'); - Route::get('api/expenses/{vendor_id?}', array('as'=>'api.expenses', 'uses'=>'ExpenseController@getDatatable')); - - //Route::get('api/expenseactivities/{vendor_id?}', array('as'=>'api.expenseactivities', 'uses'=>'ExpenseActivityController@getDatatable')); - //Route::post('vendors/bulk', 'VendorController@bulk'); - - Route::post('expenses/bulk', 'ExpenseController@bulk'); - + Route::get('api/expense/', array('as'=>'api.expenses', 'uses'=>'ExpenseController@getDatatable')); + Route::get('api/expenseactivities/{vendor_id?}', array('as'=>'api.expenseactivities', 'uses'=>'ExpenseActivityController@getDatatable')); + }); // Route groups for API diff --git a/app/Models/Expense.php b/app/Models/Expense.php index c1522fa1bda4..6376ac016f99 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -1,8 +1,5 @@ belongsTo('App\Models\Account'); diff --git a/app/Models/ExpenseActivity.php b/app/Models/ExpenseActivity.php new file mode 100644 index 000000000000..14f93e06c2f8 --- /dev/null +++ b/app/Models/ExpenseActivity.php @@ -0,0 +1,64 @@ +whereAccountId(Auth::user()->account_id); + } + + public function account() + { + return $this->belongsTo('App\Models\Account'); + } + + public function user() + { + return $this->belongsTo('App\Models\User')->withTrashed(); + } + + public function vendor() + { + return $this->belongsTo('App\Models\Vendor')->withTrashed(); + } + + public function expense() + { + return $this->belongsTo('App\Models\Expense')->withTrashed(); + } + + public function getMessage() + { + $activityTypeId = $this->activity_type_id; + $account = $this->account; + $vendor = $this->vendor; + $user = $this->user; + $contactId = $this->contact_id; + $isSystem = $this->is_system; + $expense = $this->expense; + + if($expense) { + $route = link_to($expense->getRoute(), $vendor->getDisplayName()); + } else { + $route ='no expense id'; + } + + + + $data = [ + 'expense' => $route, + 'user' => $isSystem ? '' . trans('texts.system') . '' : $user->getDisplayName(), + ]; + + return trans("texts.activity_{$activityTypeId}", $data); + } +} diff --git a/app/Ninja/Presenters/VendorPresenter.php b/app/Ninja/Presenters/VendorPresenter.php new file mode 100644 index 000000000000..ab2c9f56349e --- /dev/null +++ b/app/Ninja/Presenters/VendorPresenter.php @@ -0,0 +1,12 @@ +entity->country ? $this->entity->country->name : ''; + } +} \ No newline at end of file diff --git a/app/Ninja/Repositories/ExpenseActivityRepository.php b/app/Ninja/Repositories/ExpenseActivityRepository.php new file mode 100644 index 000000000000..aa9dc7e9823e --- /dev/null +++ b/app/Ninja/Repositories/ExpenseActivityRepository.php @@ -0,0 +1,80 @@ +vendor_id = $entity->vendor_id; + $activity->contact_id = $entity->contact_id; + $activity->activity_type_id = $activityTypeId; + $activity->message = $activity->getMessage(); + $activity->expense_id = $entity->id; + $activity->save(); + + return $activity; + } + + private function getBlank($entity) + { + $activity = new ExpenseActivity(); + + if (Auth::check() && Auth::user()->account_id == $entity->account_id) { + $activity->user_id = Auth::user()->id; + $activity->account_id = Auth::user()->account_id; + } else { + $activity->user_id = $entity->user_id; + $activity->account_id = $entity->account_id; + } + + $activity->token_id = session('token_id'); + $activity->ip = Request::getClientIp(); + + return $activity; + } + + + public function findByVendorId($vendorId) + { + return DB::table('expense_activities') + ->join('accounts', 'accounts.id', '=', 'vendor_activities.account_id') + ->join('users', 'users.id', '=', 'vendor_activities.user_id') + ->join('vendors', 'vendors.id', '=', 'vendor_activities.vendor_id') + ->leftJoin('vendor_contacts', 'vendor_contacts.vendor_id', '=', 'vendors.id') + ->where('vendors.id', '=', $vendorId) + ->where('vendor_contacts.is_primary', '=', 1) + ->whereNull('vendor_contacts.deleted_at') + ->select( + DB::raw('COALESCE(vendors.currency_id, accounts.currency_id) currency_id'), + DB::raw('COALESCE(vendors.country_id, accounts.country_id) country_id'), + 'vendor_activities.id', + 'vendor_activities.created_at', + 'vendor_activities.contact_id', + 'vendor_activities.activity_type_id', + 'vendor_activities.is_system', + 'vendor_activities.balance', + 'vendor_activities.adjustment', + 'users.first_name as user_first_name', + 'users.last_name as user_last_name', + 'users.email as user_email', + 'vendors.name as vendor_name', + 'vendors.public_id as vendor_public_id', + 'vendor_contacts.id as contact', + 'vendor_contacts.first_name as first_name', + 'vendor_contacts.last_name as last_name', + 'vendor_contacts.email as email' + + ); + } +} \ No newline at end of file diff --git a/app/Ninja/Repositories/ExpenseRepository.php b/app/Ninja/Repositories/ExpenseRepository.php index f585b740436b..2cfaa600c062 100644 --- a/app/Ninja/Repositories/ExpenseRepository.php +++ b/app/Ninja/Repositories/ExpenseRepository.php @@ -13,8 +13,18 @@ class ExpenseRepository extends BaseRepository return 'App\Models\Expense'; } - public function find($vendorPublicId = null, $filter = null) + public function all() { + return Expense::scope() + ->with('user') + ->withTrashed() + ->where('is_deleted', '=', false) + ->get(); + } + + public function find($filter = null) + { + /* $query = DB::table('expenses') ->join('accounts', 'accounts.id', '=', 'expenses.account_id') ->join('vendors', 'vendors.id', '=', 'expenses.vendor_id') @@ -39,21 +49,37 @@ class ExpenseRepository extends BaseRepository 'expenses.deleted_at', 'expenses.is_deleted' ); - - if ($vendorPublicId) { - $query->where('vendors.public_id', '=', $vendorPublicId); - } - + */ + $accountid = \Auth::user()->account_id; + $query = DB::table('expenses') + ->join('accounts', 'accounts.id', '=', 'expenses.account_id') + //->join('vendors', 'vendors.id', '=', 'expenses.vendor_id') + ->where('expenses.account_id', '=', $accountid) + ->where('expenses.deleted_at', '=', null) + ->select( + //DB::raw('COALESCE(vendors.currency_id, accounts.currency_id) currency_id'), + //DB::raw('COALESCE(vendors.country_id, accounts.country_id) country_id'), + 'expenses.public_id', + //'vendors.name as vendor_name', + //'vendors.public_id as vendor_public_id', + 'expenses.amount', + 'expenses.balance', + 'expenses.expense_date', + 'expenses.public_notes', + 'expenses.deleted_at', + 'expenses.is_deleted' + ); + if (!\Session::get('show_trash:expense')) { $query->where('expenses.deleted_at', '=', null); } - +/* if ($filter) { $query->where(function ($query) use ($filter) { $query->where('vendors.name', 'like', '%'.$filter.'%'); }); } - +*/ return $query; } diff --git a/app/Services/ExpenseService.php b/app/Services/ExpenseService.php index fb6333622315..92ffc85306ce 100644 --- a/app/Services/ExpenseService.php +++ b/app/Services/ExpenseService.php @@ -5,6 +5,7 @@ use URL; use App\Services\BaseService; use App\Ninja\Repositories\ExpenseRepository; + class ExpenseService extends BaseService { protected $expenseRepo; @@ -26,33 +27,33 @@ class ExpenseService extends BaseService return $this->expenseRepo->save($data); } - public function getDatatable($vendorPublicId, $search) + public function getDatatable($search) { - $query = $this->expenseRepo->find($vendorPublicId, $search); + $query = $this->expenseRepo->find($search); - return $this->createDatatable(ENTITY_CREDIT, $query, !$vendorPublicId); + return $this->createDatatable(ENTITY_EXPENSE, $query); } protected function getDatatableColumns($entityType, $hideClient) { return [ - [ + /*[ 'vendor_name', function ($model) { return $model->vendor_public_id ? link_to("vendors/{$model->vendor_public_id}", Utils::getVendorDisplayName($model)) : ''; }, ! $hideClient - ], + ],*/ [ 'amount', function ($model) { - return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id) . ''; + return Utils::formatMoney($model->amount, false, false) . ''; } ], [ 'balance', function ($model) { - return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id); + return Utils::formatMoney($model->balance, false, false); } ], [ @@ -62,14 +63,14 @@ class ExpenseService extends BaseService } ], [ - 'private_notes', + 'private_public', function ($model) { - return $model->private_notes; + return $model->public_notes; } ] ]; } - +/* protected function getDatatableActions($entityType) { return [ @@ -81,4 +82,5 @@ class ExpenseService extends BaseService ] ]; } + */ } \ No newline at end of file diff --git a/database/migrations/2016_01_06_155001_create_expenses_table.php b/database/migrations/2016_01_06_155001_create_expenses_table.php index 12dc669203f4..56e74fbc7963 100644 --- a/database/migrations/2016_01_06_155001_create_expenses_table.php +++ b/database/migrations/2016_01_06_155001_create_expenses_table.php @@ -32,6 +32,7 @@ class CreateExpensesTable extends Migration $table->date('expense_date')->nullable(); $table->string('expense_number')->nullable(); $table->text('private_notes'); + $table->text('public_notes'); $table->integer('currency_id',false, true)->nullable(); $table->boolean('is_invoiced')->default(false); $table->boolean('should_be_invoiced')->default(true); diff --git a/database/migrations/2016_01_06_191912_create_expense_activities_table.php b/database/migrations/2016_01_06_191912_create_expense_activities_table.php new file mode 100644 index 000000000000..a7106568cebe --- /dev/null +++ b/database/migrations/2016_01_06_191912_create_expense_activities_table.php @@ -0,0 +1,51 @@ +increments('id'); + $table->timestamps(); + + $table->unsignedInteger('account_id'); + $table->unsignedInteger('vendor_id'); + $table->unsignedInteger('user_id'); + $table->unsignedInteger('contact_id')->nullable(); + $table->unsignedInteger('expense_id'); + $table->unsignedInteger('invitation_id')->nullable(); + $table->text('message')->nullable(); + $table->text('json_backup')->nullable(); + $table->integer('activity_type_id'); + $table->decimal('adjustment', 13, 2)->nullable(); + $table->decimal('balance', 13, 2)->nullable(); + $table->unsignedInteger('token_id')->nullable(); + $table->string('ip')->nullable(); + $table->boolean('is_system')->default(0); + $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); + $table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade'); + $table->foreign('expense_id')->references('id')->on('expenses')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('expense_activities'); + } + +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 9687bf330869..e7993b59ac58 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -896,7 +896,7 @@ return array( 'activity_31' => ':user created :vendor', 'activity_32' => ':user created :vendor', 'activity_33' => ':user created :vendor', - 'activity_34' => ':user created :vendor', + 'activity_34' => ':user created :expense', 'activity_35' => ':user created :vendor', 'activity_36' => ':user created :vendor', 'activity_37' => ':user created :vendor', @@ -1030,4 +1030,6 @@ return array( 'expense_amount' => 'Expense amount', 'expense_balance' => 'Expense balance', 'expense_date' => 'Expense date', + 'expense_exchange_rate_100' => 'The amount for 100 in company currency', + 'expense_should_be_invoiced' => 'Should this expense be invoiced?', );