Add all task to invoice by project #1650

This commit is contained in:
Hillel Coren 2017-11-30 13:46:30 +02:00
parent c31ba08a4c
commit c4e74f35d1
6 changed files with 107 additions and 16 deletions

View File

@ -6,6 +6,7 @@ use App\Http\Requests\CreateProjectRequest;
use App\Http\Requests\ProjectRequest; use App\Http\Requests\ProjectRequest;
use App\Http\Requests\UpdateProjectRequest; use App\Http\Requests\UpdateProjectRequest;
use App\Models\Client; use App\Models\Client;
use App\Models\Project;
use App\Ninja\Datatables\ProjectDatatable; use App\Ninja\Datatables\ProjectDatatable;
use App\Ninja\Repositories\ProjectRepository; use App\Ninja\Repositories\ProjectRepository;
use App\Services\ProjectService; use App\Services\ProjectService;
@ -95,6 +96,11 @@ class ProjectController extends BaseController
Session::flash('message', trans('texts.updated_project')); 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()); return redirect()->to($project->getRoute());
} }
@ -102,14 +108,51 @@ class ProjectController extends BaseController
{ {
$action = Input::get('action'); $action = Input::get('action');
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids'); $ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
$count = $this->projectService->bulk($ids, $action);
if ($count > 0) { if ($action == 'invoice') {
$field = $count == 1 ? "{$action}d_project" : "{$action}d_projects"; $data = [];
$message = trans("texts.$field", ['count' => $count]); $clientPublicId = false;
Session::flash('message', $message); $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');
} }
} }

View File

@ -55,6 +55,14 @@ class Project extends EntityModel
{ {
return $this->belongsTo('App\Models\Client')->withTrashed(); 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) { Project::creating(function ($project) {

View File

@ -59,6 +59,15 @@ class ProjectDatatable extends EntityDatatable
return Auth::user()->can('editByOwner', [ENTITY_PROJECT, $model->user_id]); 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);
},
],
]; ];
} }
} }

View File

@ -1994,14 +1994,14 @@ $LANG = array(
'created_project' => 'Successfully created project', 'created_project' => 'Successfully created project',
'archived_project' => 'Successfully archived project', 'archived_project' => 'Successfully archived project',
'archived_projects' => 'Successfully archived :count projects', 'archived_projects' => 'Successfully archived :count projects',
'restore_project' => 'Restore project', 'restore_project' => 'Restore Project',
'restored_project' => 'Successfully restored project', 'restored_project' => 'Successfully restored project',
'delete_project' => 'Delete project', 'delete_project' => 'Delete Project',
'deleted_project' => 'Successfully deleted project', 'deleted_project' => 'Successfully deleted project',
'deleted_projects' => 'Successfully deleted :count projects', 'deleted_projects' => 'Successfully deleted :count projects',
'delete_expense_category' => 'Delete category', 'delete_expense_category' => 'Delete category',
'deleted_expense_category' => 'Successfully deleted category', 'deleted_expense_category' => 'Successfully deleted category',
'delete_product' => 'Delete product', 'delete_product' => 'Delete Product',
'deleted_product' => 'Successfully deleted product', 'deleted_product' => 'Successfully deleted product',
'deleted_products' => 'Successfully deleted :count products', 'deleted_products' => 'Successfully deleted :count products',
'restored_product' => 'Successfully restored product', 'restored_product' => 'Successfully restored product',
@ -2586,7 +2586,8 @@ $LANG = array(
'edit_subscription' => 'Edit Subscription', 'edit_subscription' => 'Edit Subscription',
'archive_subscription' => 'Archive Subscription', 'archive_subscription' => 'Archive Subscription',
'archived_subscription' => 'Successfully archived subscription', 'archived_subscription' => 'Successfully archived subscription',
'project_error_multiple_clients' => 'The projects can\'t belong to different clients',
'invoice_project' => 'Invoice Project',
); );

View File

@ -8,7 +8,7 @@
</div> </div>
<div class="pull-left"> <div class="pull-left">
@if (in_array($entityType, [ENTITY_TASK, ENTITY_INVOICE, ENTITY_PRODUCT])) @if (in_array($entityType, [ENTITY_TASK, ENTITY_INVOICE, ENTITY_PRODUCT, ENTITY_PROJECT]))
@can('create', 'invoice') @can('create', 'invoice')
{!! Button::primary(trans('texts.invoice'))->withAttributes(['class'=>'invoice', 'onclick' =>'submitForm_'.$entityType.'("invoice")'])->appendIcon(Icon::create('check')) !!} {!! Button::primary(trans('texts.invoice'))->withAttributes(['class'=>'invoice', 'onclick' =>'submitForm_'.$entityType.'("invoice")'])->appendIcon(Icon::create('check')) !!}
@endcan @endcan

View File

@ -3,7 +3,7 @@
@section('content') @section('content')
{!! Former::open($url) {!! Former::open($url)
->addClass('col-md-10 col-md-offset-1 warn-on-exit') ->addClass('col-md-10 col-md-offset-1 warn-on-exit main-form')
->method($method) ->method($method)
->rules([ ->rules([
'name' => 'required', 'name' => 'required',
@ -17,6 +17,7 @@
<span style="display:none"> <span style="display:none">
{!! Former::text('public_id') !!} {!! Former::text('public_id') !!}
{!! Former::text('action') !!}
</span> </span>
<div class="row"> <div class="row">
@ -55,9 +56,27 @@
{!! Button::normal(trans('texts.cancel'))->large()->asLinkTo(HTMLUtils::previousUrl('/projects'))->appendIcon(Icon::create('remove-circle')) !!} {!! Button::normal(trans('texts.cancel'))->large()->asLinkTo(HTMLUtils::previousUrl('/projects'))->appendIcon(Icon::create('remove-circle')) !!}
{!! Button::success(trans('texts.save'))->submit()->large()->appendIcon(Icon::create('floppy-disk')) !!} {!! Button::success(trans('texts.save'))->submit()->large()->appendIcon(Icon::create('floppy-disk')) !!}
@if ($project && Auth::user()->can('create', ENTITY_TASK)) @if ($project && Auth::user()->can('create', ENTITY_TASK))
{!! Button::primary(trans('texts.new_task'))->large() {!! DropdownButton::normal(trans('texts.more_actions'))
->asLinkTo(url('/tasks/create/' . ($project->client ? $project->client->public_id : '0'). '/' . $project->public_id)) ->withContents([
->appendIcon(Icon::create('plus-sign')) !!} [
'url' => url('/tasks/create/' . ($project->client ? $project->client->public_id : '0'). '/' . $project->public_id),
'label' => trans('texts.new_task'),
],
[
'url' => 'javascript:submitAction("invoice")',
'label' => trans('texts.invoice_project'),
],
\DropdownButton::DIVIDER,
[
'url' => 'javascript:submitAction("archive")',
'label' => trans('texts.archive_project')
],
[
'url' => 'javascript:onDeleteClick()',
'label' => trans('texts.delete_project')
],
])
->large() !!}
@endif @endif
</center> </center>
@ -68,6 +87,17 @@
var clients = {!! $clients !!}; var clients = {!! $clients !!};
var clientMap = {}; var clientMap = {};
function submitAction(action) {
$('#action').val(action);
$('.main-form').submit();
}
function onDeleteClick() {
sweetConfirm(function() {
submitAction('delete');
});
}
$(function() { $(function() {
var $clientSelect = $('select#client_id'); var $clientSelect = $('select#client_id');
for (var i=0; i<clients.length; i++) { for (var i=0; i<clients.length; i++) {