diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index dbe2d7dd58ba..a8a9ecee41e9 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -6,6 +6,7 @@ use App\Http\Requests\CreateProjectRequest; use App\Http\Requests\ProjectRequest; use App\Http\Requests\UpdateProjectRequest; use App\Models\Client; +use App\Models\Project; use App\Ninja\Datatables\ProjectDatatable; use App\Ninja\Repositories\ProjectRepository; use App\Services\ProjectService; @@ -95,6 +96,11 @@ class ProjectController extends BaseController Session::flash('message', trans('texts.updated_project')); + $action = Input::get('action'); + if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) { + return self::bulk(); + } + return redirect()->to($project->getRoute()); } @@ -102,14 +108,51 @@ class ProjectController extends BaseController { $action = Input::get('action'); $ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids'); - $count = $this->projectService->bulk($ids, $action); - if ($count > 0) { - $field = $count == 1 ? "{$action}d_project" : "{$action}d_projects"; - $message = trans("texts.$field", ['count' => $count]); - Session::flash('message', $message); + if ($action == 'invoice') { + $data = []; + $clientPublicId = false; + $lastClientId = false; + $lastProjectId = false; + $projects = Project::scope($ids) + ->with(['client', 'tasks' => function ($query) { + $query->whereNull('invoice_id'); + }]) + ->get(); + foreach ($projects as $project) { + if (! $clientPublicId) { + $clientPublicId = $project->client->public_id; + } + if ($lastClientId && $lastClientId != $project->client_id) { + return redirect('projects')->withError(trans('texts.project_error_multiple_clients')); + } + $lastClientId = $project->client_id; + + foreach ($project->tasks as $task) { + if ($task->is_running) { + return redirect('projects')->withError(trans('texts.task_error_running')); + } + $showProject = $lastProjectId != $task->project_id; + $data[] = [ + 'publicId' => $task->public_id, + 'description' => $task->present()->invoiceDescription(auth()->user()->account, $showProject), + 'duration' => $task->getHours(), + 'cost' => $task->getRate(), + ]; + $lastProjectId = $task->project_id; + } + } + return redirect("invoices/create/{$clientPublicId}")->with('tasks', $data); + } else { + $count = $this->projectService->bulk($ids, $action); + + if ($count > 0) { + $field = $count == 1 ? "{$action}d_project" : "{$action}d_projects"; + $message = trans("texts.$field", ['count' => $count]); + Session::flash('message', $message); + } + + return redirect()->to('/projects'); } - - return redirect()->to('/projects'); } } diff --git a/app/Models/Project.php b/app/Models/Project.php index 72bd1d10c1b7..ed5692dd3cd2 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -55,6 +55,14 @@ class Project extends EntityModel { return $this->belongsTo('App\Models\Client')->withTrashed(); } + + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function tasks() + { + return $this->hasMany('App\Models\Task'); + } } Project::creating(function ($project) { diff --git a/app/Ninja/Datatables/ProjectDatatable.php b/app/Ninja/Datatables/ProjectDatatable.php index 6165028e45aa..20b52c98ccfd 100644 --- a/app/Ninja/Datatables/ProjectDatatable.php +++ b/app/Ninja/Datatables/ProjectDatatable.php @@ -59,6 +59,15 @@ class ProjectDatatable extends EntityDatatable return Auth::user()->can('editByOwner', [ENTITY_PROJECT, $model->user_id]); }, ], + [ + trans('texts.invoice_project'), + function ($model) { + return "javascript:submitForm_project('invoice', {$model->public_id})"; + }, + function ($model) { + return Auth::user()->can('create', ENTITY_INVOICE); + }, + ], ]; } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 4be32c7f719b..42b3b1f52282 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1994,14 +1994,14 @@ $LANG = array( 'created_project' => 'Successfully created project', 'archived_project' => 'Successfully archived project', 'archived_projects' => 'Successfully archived :count projects', - 'restore_project' => 'Restore project', + 'restore_project' => 'Restore Project', 'restored_project' => 'Successfully restored project', - 'delete_project' => 'Delete project', + 'delete_project' => 'Delete Project', 'deleted_project' => 'Successfully deleted project', 'deleted_projects' => 'Successfully deleted :count projects', 'delete_expense_category' => 'Delete category', 'deleted_expense_category' => 'Successfully deleted category', - 'delete_product' => 'Delete product', + 'delete_product' => 'Delete Product', 'deleted_product' => 'Successfully deleted product', 'deleted_products' => 'Successfully deleted :count products', 'restored_product' => 'Successfully restored product', @@ -2586,7 +2586,8 @@ $LANG = array( 'edit_subscription' => 'Edit Subscription', 'archive_subscription' => 'Archive Subscription', 'archived_subscription' => 'Successfully archived subscription', - + 'project_error_multiple_clients' => 'The projects can\'t belong to different clients', + 'invoice_project' => 'Invoice Project', ); diff --git a/resources/views/list.blade.php b/resources/views/list.blade.php index 4c334b811cab..8762039213fe 100644 --- a/resources/views/list.blade.php +++ b/resources/views/list.blade.php @@ -8,7 +8,7 @@