diff --git a/app/Entities/Partners/Vendor.php b/app/Entities/Partners/Vendor.php
index 8fcd169..e82cd19 100644
--- a/app/Entities/Partners/Vendor.php
+++ b/app/Entities/Partners/Vendor.php
@@ -7,4 +7,9 @@ use Illuminate\Database\Eloquent\Model;
class Vendor extends Model
{
protected $fillable = ['name', 'notes', 'website', 'is_active'];
+
+ public function payments()
+ {
+ return $this->hasMany('App\Entities\Payments\Payment', 'partner_id');
+ }
}
diff --git a/app/Policies/Partners/VendorPolicy.php b/app/Policies/Partners/VendorPolicy.php
index 0d69748..95c6404 100644
--- a/app/Policies/Partners/VendorPolicy.php
+++ b/app/Policies/Partners/VendorPolicy.php
@@ -64,6 +64,6 @@ class VendorPolicy
*/
public function delete(User $user, Vendor $vendor)
{
- return $this->view($user, $vendor);
+ return $user->hasRole('admin') && $vendor->payments->isEmpty();
}
}
diff --git a/resources/views/vendors/forms.blade.php b/resources/views/vendors/forms.blade.php
index 71c4505..d55bbb6 100644
--- a/resources/views/vendors/forms.blade.php
+++ b/resources/views/vendors/forms.blade.php
@@ -32,8 +32,13 @@
{!! $errors->first('vendor_id', ':message') !!}
- {{ trans('app.delete_confirm') }}
+ @can('delete', $editableVendor)
+ {{ trans('app.delete_confirm') }}
+ @else
+ {{ trans('vendor.undeleteable') }}
+ @endcan
diff --git a/tests/Unit/Models/VendorTest.php b/tests/Unit/Models/VendorTest.php
index 794f275..3af220b 100644
--- a/tests/Unit/Models/VendorTest.php
+++ b/tests/Unit/Models/VendorTest.php
@@ -3,7 +3,9 @@
namespace Tests\Unit\Models;
use App\Entities\Partners\Vendor;
+use App\Entities\Payments\Payment;
use Illuminate\Foundation\Testing\DatabaseMigrations;
+use Illuminate\Support\Collection;
use Tests\TestCase as TestCase;
class VendorTest extends TestCase
@@ -16,4 +18,14 @@ class VendorTest extends TestCase
$vendor = factory(Vendor::class)->make(['name' => 'Vendor 1 name']);
$this->assertEquals('Vendor 1 name', $vendor->name);
}
+
+ /** @test */
+ public function a_vendor_has_many_payments_relation()
+ {
+ $vendor = factory(Vendor::class)->create();
+ $payment = factory(Payment::class)->create(['partner_id' => $vendor->id]);
+
+ $this->assertInstanceOf(Collection::class, $vendor->payments);
+ $this->assertInstanceOf(Payment::class, $vendor->payments->first());
+ }
}
diff --git a/tests/Unit/Policies/VendorPolicyTest.php b/tests/Unit/Policies/VendorPolicyTest.php
index 92f6394..8b2d975 100644
--- a/tests/Unit/Policies/VendorPolicyTest.php
+++ b/tests/Unit/Policies/VendorPolicyTest.php
@@ -3,7 +3,8 @@
namespace Tests\Unit\Policies;
use App\Entities\Partners\Vendor;
-use Tests\TestCase as TestCase;
+use App\Entities\Payments\Payment;
+use Tests\TestCase;
/**
* Vendor Policy Test.
@@ -54,4 +55,19 @@ class VendorPolicyTest extends TestCase
$this->assertTrue($admin->can('delete', $vendor));
$this->assertFalse($worker->can('delete', $vendor));
}
+
+ /** @test */
+ public function admin_cannot_delete_vendor_if_it_has_dependent_records()
+ {
+ $admin = $this->createUser('admin');
+ $vendor = factory(Vendor::class)->create();
+ $this->assertTrue($admin->can('delete', $vendor));
+
+ $payment = factory(Payment::class)->create([
+ 'partner_type' => Vendor::class,
+ 'partner_id' => $vendor->id,
+ ]);
+
+ $this->assertFalse($admin->can('delete', $vendor->fresh()));
+ }
}