diff --git a/app/Policies/Partners/CustomerPolicy.php b/app/Policies/Partners/CustomerPolicy.php index 0b907ce..9c6dfbe 100644 --- a/app/Policies/Partners/CustomerPolicy.php +++ b/app/Policies/Partners/CustomerPolicy.php @@ -59,11 +59,12 @@ class CustomerPolicy * * @param \App\Entities\Users\User $user * @param \App\Entities\Partners\Customer $customer + * @param int $dependentRecordsCount * * @return mixed */ - public function delete(User $user, Customer $customer) + public function delete(User $user, Customer $customer, int $dependentRecordsCount = 0) { - return $this->view($user, $customer); + return $user->hasRole('admin') && $dependentRecordsCount == 0; } } diff --git a/resources/lang/en/customer.php b/resources/lang/en/customer.php index 9043d54..8d4234f 100644 --- a/resources/lang/en/customer.php +++ b/resources/lang/en/customer.php @@ -32,8 +32,9 @@ return [ 'projects_count' => 'Projects count', // Relations - 'projects' => 'Project List', - 'payments' => 'Payment History', - 'subscriptions' => 'Subscription List', - 'invoices' => 'Invoice List', + 'projects' => 'Project List', + 'payments' => 'Payment History', + 'subscriptions' => 'Subscription List', + 'subscriptions_count' => 'Subscriptions Count', + 'invoices' => 'Invoice List', ]; diff --git a/resources/lang/id/customer.php b/resources/lang/id/customer.php index fc896a3..6a7b881 100644 --- a/resources/lang/id/customer.php +++ b/resources/lang/id/customer.php @@ -32,8 +32,9 @@ return [ 'projects_count' => 'Jml Project', // Relations - 'projects' => 'List Project', - 'payments' => 'History Pembayaran', - 'subscriptions' => 'List Langganan', - 'invoices' => 'List Invoice', + 'projects' => 'List Project', + 'payments' => 'History Pembayaran', + 'subscriptions' => 'List Langganan', + 'subscriptions_count' => 'Jumlah Langganan', + 'invoices' => 'List Invoice', ]; diff --git a/resources/views/customers/edit.blade.php b/resources/views/customers/edit.blade.php index 7006511..b9c7cf3 100644 --- a/resources/views/customers/edit.blade.php +++ b/resources/views/customers/edit.blade.php @@ -10,8 +10,9 @@ {{ $customer->name }} {{ trans('customer.edit') }} -@includeWhen(Request::has('action'), 'customers.forms') - +@if (Request::has('action')) + @include('customers.forms') +@else {!! Form::model($customer, ['route' => ['customers.update', $customer->id],'method' => 'patch']) !!}
@@ -50,4 +51,5 @@
{!! Form::close() !!} +@endif @endsection diff --git a/resources/views/customers/forms.blade.php b/resources/views/customers/forms.blade.php index 2cd2e5c..b17ce9d 100644 --- a/resources/views/customers/forms.blade.php +++ b/resources/views/customers/forms.blade.php @@ -1,4 +1,7 @@ @if (Request::get('action') == 'delete' && $customer) +@php + $dependentRecordsCount = 0; +@endphp
@@ -8,29 +11,37 @@

{{ $customer->name }}

{{ $customer->email }}

- -

{{ $customer->phone }}

- -

{{ $customer->address }}

{{ $customer->is_active }}

+ +

{{ $projectsCount = $customer->projects()->count() }}

+ @php $dependentRecordsCount += $projectsCount; @endphp + +

{{ $subscriptionsCount = $customer->subscriptions()->count() }}

+ @php $dependentRecordsCount += $subscriptionsCount; @endphp

{{ $customer->notes }}

{!! $errors->first('customer_id', ':message') !!}

-
{{ trans('app.delete_confirm') }}
+ @if ($dependentRecordsCount) +
{{ trans('customer.undeleteable') }}
+ @else +
{{ trans('app.delete_confirm') }}
+ @endif
diff --git a/tests/Unit/Policies/CustomerPolicyTest.php b/tests/Unit/Policies/CustomerPolicyTest.php index 1f55558..0116fe8 100644 --- a/tests/Unit/Policies/CustomerPolicyTest.php +++ b/tests/Unit/Policies/CustomerPolicyTest.php @@ -3,7 +3,7 @@ namespace Tests\Unit\Policies; use App\Entities\Partners\Customer; -use Tests\TestCase as TestCase; +use Tests\TestCase; /** * Customer Policy Test. @@ -54,4 +54,14 @@ class CustomerPolicyTest extends TestCase $this->assertTrue($admin->can('delete', $customer)); $this->assertFalse($worker->can('delete', $customer)); } + + /** @test */ + public function admin_cannot_delete_customer_if_it_has_dependent_records() + { + $admin = $this->createUser('admin'); + $customer = factory(Customer::class)->create(); + + $this->assertTrue($admin->can('delete', [$customer, 0])); + $this->assertFalse($admin->can('delete', [$customer, 1])); + } }