diff --git a/app/Entities/Payments/Payment.php b/app/Entities/Payments/Payment.php index 843c5d2..0c4a0eb 100755 --- a/app/Entities/Payments/Payment.php +++ b/app/Entities/Payments/Payment.php @@ -5,6 +5,7 @@ namespace App\Entities\Payments; use App\Entities\Partners\Partner; use App\Entities\Payments\PaymentPresenter; use App\Entities\Projects\Project; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Laracasts\Presenter\PresentableTrait; @@ -16,6 +17,26 @@ class Payment extends Model protected $presenter = PaymentPresenter::class; protected $guarded = ['id', 'created_at', 'updated_at']; + /** + * The "booting" method of the model. + * + * @return void + */ + protected static function boot() + { + parent::boot(); + + if (auth()->user()) { + static::addGlobalScope('by_owner', function (Builder $builder) { + if ( ! is_null(auth()->user()->agency)) { + $builder->where('owner_id', auth()->user()->agency->id); + } else { + $builder->where('owner_id', 0); + } + }); + } + } + public function project() { return $this->belongsTo(Project::class); diff --git a/app/Entities/Payments/PaymentsRepository.php b/app/Entities/Payments/PaymentsRepository.php index 2057250..dabe2f9 100755 --- a/app/Entities/Payments/PaymentsRepository.php +++ b/app/Entities/Payments/PaymentsRepository.php @@ -30,7 +30,6 @@ class PaymentsRepository extends BaseRepository } }) ->with('partner', 'project') - ->whereOwnerId(auth()->id()) ->paginate($this->_paginate); } diff --git a/app/Entities/Projects/Project.php b/app/Entities/Projects/Project.php index 3ea0548..d4b9a0d 100755 --- a/app/Entities/Projects/Project.php +++ b/app/Entities/Projects/Project.php @@ -9,6 +9,7 @@ use App\Entities\Payments\Payment; use App\Entities\Projects\ProjectPresenter; use App\Entities\Projects\Task; use App\Entities\Subscriptions\Subscription; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Laracasts\Presenter\PresentableTrait; @@ -21,6 +22,26 @@ class Project extends Model protected $guarded = ['id', 'created_at', 'updated_at']; // protected $dates = ['start_date','end_date']; + /** + * The "booting" method of the model. + * + * @return void + */ + protected static function boot() + { + parent::boot(); + + if (auth()->user()) { + static::addGlobalScope('by_owner', function (Builder $builder) { + if ( ! is_null(auth()->user()->agency)) { + $builder->where('owner_id', auth()->user()->agency->id); + } else { + $builder->where('owner_id', 0); + } + }); + } + } + public function nameLink() { return link_to_route('projects.show', $this->name, [$this->id]); diff --git a/app/Entities/Projects/ProjectsRepository.php b/app/Entities/Projects/ProjectsRepository.php index e93ad4b..3150e60 100755 --- a/app/Entities/Projects/ProjectsRepository.php +++ b/app/Entities/Projects/ProjectsRepository.php @@ -32,7 +32,6 @@ class ProjectsRepository extends BaseRepository }) ->withCount('payments') ->with('customer') - ->whereOwnerId(auth()->id()) ->paginate($this->_paginate); } diff --git a/tests/Feature/InvoiceEntryTest.php b/tests/Feature/InvoiceEntryTest.php index e7f7d33..493e307 100644 --- a/tests/Feature/InvoiceEntryTest.php +++ b/tests/Feature/InvoiceEntryTest.php @@ -16,7 +16,7 @@ class InvoiceEntryTest extends TestCase $this->adminUserSigningIn(); // Add new draft to collection - $cart = new InvoiceDraftCollection(); + $cart = new InvoiceDraftCollection(); $draft = $cart->add(new InvoiceDraft()); $this->visit(route('invoices.create')); @@ -31,7 +31,7 @@ class InvoiceEntryTest extends TestCase $this->visit(route('invoices.create')); $this->press(trans('invoice.create')); - $cart = new InvoiceDraftCollection(); + $cart = new InvoiceDraftCollection(); $draft = $cart->content()->last(); $this->seePageIs(route('invoices.create', $draft->draftKey)); } @@ -41,7 +41,7 @@ class InvoiceEntryTest extends TestCase { $this->adminUserSigningIn(); - $cart = new InvoiceDraftCollection(); + $cart = new InvoiceDraftCollection(); $draft = new InvoiceDraft(); $cart->add($draft); @@ -99,8 +99,9 @@ class InvoiceEntryTest extends TestCase /** @test */ public function user_can_update_draft_invoice_detail_and_get_confirm_page() { - $project = factory(Project::class)->create(); - $cart = new InvoiceDraftCollection(); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); + $cart = new InvoiceDraftCollection(); $draft = $cart->add(new InvoiceDraft()); @@ -111,7 +112,6 @@ class InvoiceEntryTest extends TestCase $cart->addItemToDraft($draft->draftKey, $item1); $cart->addItemToDraft($draft->draftKey, $item2); - $this->adminUserSigningIn(); $this->visit(route('invoices.create', $draft->draftKey)); $this->type($project->id, 'project_id'); @@ -136,7 +136,9 @@ class InvoiceEntryTest extends TestCase $item1 = new Item(['description' => 'Deskripsi item invoice', 'amount' => 1000]); $item2 = new Item(['description' => 'Deskripsi item invoice', 'amount' => 2000]); - $project = factory(Project::class)->create(); + + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); // Add items to draft $cart->addItemToDraft($draft->draftKey, $item1); @@ -144,11 +146,10 @@ class InvoiceEntryTest extends TestCase $draftAttributes = [ 'project_id' => $project->id, - 'notes' => 'Catatan', + 'notes' => 'Catatan', ]; $cart->updateDraftAttributes($draft->draftKey, $draftAttributes); - $user = $this->adminUserSigningIn(); $this->visit(route('invoices.create', [$draft->draftKey, 'action' => 'confirm'])); $this->press(trans('invoice.save')); diff --git a/tests/Feature/ManageFeaturesTest.php b/tests/Feature/ManageFeaturesTest.php index 81864d4..fe9ed7e 100644 --- a/tests/Feature/ManageFeaturesTest.php +++ b/tests/Feature/ManageFeaturesTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\Entities\Agencies\Agency; use App\Entities\Projects\Feature; use App\Entities\Projects\Project; use App\Entities\Projects\Task; @@ -45,10 +46,11 @@ class ManageFeaturesTest extends TestCase /** @test */ public function admin_can_edit_feature_data() { - $user = factory(User::class, 3)->create(); + $user = factory(User::class, 3)->create(); + $agency = factory(Agency::class)->create(['owner_id' => $user[0]->id]); $this->actingAs($user[0]); - $project = factory(Project::class)->create(['owner_id' => $user[0]->id]); + $project = factory(Project::class)->create(['owner_id' => $agency->id]); $feature = factory(Feature::class)->create(['worker_id' => $user[1]->id, 'project_id' => $project->id]); diff --git a/tests/Feature/ManageProjectsTest.php b/tests/Feature/ManageProjectsTest.php index 499c0b4..b56910c 100644 --- a/tests/Feature/ManageProjectsTest.php +++ b/tests/Feature/ManageProjectsTest.php @@ -77,10 +77,10 @@ class ManageProjectsTest extends TestCase { $user = $this->adminUserSigningIn(); - $project = factory(Project::class)->create(['owner_id' => $user->id]); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $feature = factory(Feature::class)->create(['project_id' => $project->id]); $task = factory(Task::class)->create(['feature_id' => $feature->id]); - $payment = factory(Payment::class)->create(['project_id' => $project->id]); + $payment = factory(Payment::class)->create(['project_id' => $project->id, 'owner_id' => $user->agency->id]); $this->visit('projects/'.$project->id); $this->click(trans('app.edit')); diff --git a/tests/Feature/ManageSubscriptionsTest.php b/tests/Feature/ManageSubscriptionsTest.php index a57b0c2..88a288c 100644 --- a/tests/Feature/ManageSubscriptionsTest.php +++ b/tests/Feature/ManageSubscriptionsTest.php @@ -14,7 +14,7 @@ class ManageSubscriptionsTest extends TestCase { $user = $this->adminUserSigningIn(); $vendor = factory(Partner::class)->create(); - $project = factory(Project::class)->create(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $customer = factory(Partner::class)->create(); $this->visit(route('subscriptions.index')); @@ -55,7 +55,7 @@ class ManageSubscriptionsTest extends TestCase $user = $this->adminUserSigningIn(); $vendor = factory(Partner::class)->create(); $eppCode = str_random(10); - $project = factory(Project::class)->create(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $customer = factory(Partner::class)->create(); $subscription = factory(Subscription::class)->create(['customer_id' => $customer->id, 'project_id' => $project->id]); @@ -94,9 +94,9 @@ class ManageSubscriptionsTest extends TestCase /** @test */ public function admin_can_delete_a_subscription() { - $user = $this->adminUserSigningIn(); - - $subscription = factory(Subscription::class)->create(); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); + $subscription = factory(Subscription::class)->create(['project_id' => $project->id]); $this->visit(route('subscriptions.edit', $subscription->id)); $this->click(trans('subscription.delete')); @@ -110,9 +110,9 @@ class ManageSubscriptionsTest extends TestCase /** @test */ public function admin_can_see_a_subscription() { - $user = $this->adminUserSigningIn(); - - $subscription = factory(Subscription::class)->create(); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); + $subscription = factory(Subscription::class)->create(['project_id' => $project->id]); $this->visit(route('subscriptions.show', $subscription->id)); diff --git a/tests/Feature/ManageTasksTest.php b/tests/Feature/ManageTasksTest.php index b088283..34ca093 100644 --- a/tests/Feature/ManageTasksTest.php +++ b/tests/Feature/ManageTasksTest.php @@ -3,8 +3,8 @@ namespace Tests\Feature; use App\Entities\Projects\Feature; +use App\Entities\Projects\Project; use App\Entities\Projects\Task; -use App\Entities\Users\User; use Tests\TestCase; class ManageTasksTest extends TestCase @@ -12,10 +12,10 @@ class ManageTasksTest extends TestCase /** @test */ public function admin_can_entry_task() { - $user = factory(User::class)->create(); - $this->actingAs($user); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); - $feature = factory(Feature::class)->create(['worker_id' => $user->id]); + $feature = factory(Feature::class)->create(['worker_id' => $user->id, 'project_id' => $project->id]); $this->visit('features/'.$feature->id); $this->seePageIs('features/'.$feature->id); $this->see(trans('feature.tasks')); @@ -41,10 +41,10 @@ class ManageTasksTest extends TestCase /** @test */ public function admin_can_edit_task_data() { - $user = factory(User::class)->create(); - $this->actingAs($user); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); - $feature = factory(Feature::class)->create(['worker_id' => $user->id]); + $feature = factory(Feature::class)->create(['worker_id' => $user->id, 'project_id' => $project->id]); $task = factory(Task::class)->create(['feature_id' => $feature->id]); @@ -71,10 +71,10 @@ class ManageTasksTest extends TestCase /** @test */ public function admin_can_delete_a_task() { - $user = factory(User::class)->create(); - $this->actingAs($user); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); - $feature = factory(Feature::class)->create(['worker_id' => $user->id]); + $feature = factory(Feature::class)->create(['worker_id' => $user->id, 'project_id' => $project->id]); $task = factory(Task::class)->create(['feature_id' => $feature->id]); diff --git a/tests/Feature/Payments/ManagePaymentsTest.php b/tests/Feature/Payments/ManagePaymentsTest.php index f40c46d..ef63f28 100644 --- a/tests/Feature/Payments/ManagePaymentsTest.php +++ b/tests/Feature/Payments/ManagePaymentsTest.php @@ -13,8 +13,8 @@ class ManagePaymentsTest extends TestCase public function admin_can_entry_project_an_income_payment() { $user = $this->adminUserSigningIn(); - $customer = factory(Partner::class)->create(); - $project = factory(Project::class)->create(); + $customer = factory(Partner::class)->create(['owner_id' => $user->agency->id]); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $this->visit(route('payments.index')); $this->seePageIs(route('payments.index')); @@ -44,8 +44,8 @@ class ManagePaymentsTest extends TestCase public function admin_can_entry_project_an_expanse_payment() { $user = $this->adminUserSigningIn(); - $vendor = factory(Partner::class)->create(); - $project = factory(Project::class)->create(); + $vendor = factory(Partner::class)->create(['owner_id' => $user->agency->id]); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $this->visit(route('payments.index')); $this->seePageIs(route('payments.index')); @@ -76,13 +76,13 @@ class ManagePaymentsTest extends TestCase public function admin_can_edit_payment_data() { $user = $this->adminUserSigningIn(); - $customer = factory(Partner::class)->create(); - $project = factory(Project::class)->create(); + $customer = factory(Partner::class)->create(['owner_id' => $user->agency->id]); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); $payment = factory(Payment::class)->create([ 'partner_id' => $customer->id, 'project_id' => $project->id, - 'owner_id' => $user->id, + 'owner_id' => $user->agency->id, ]); $this->visit(route('payments.edit', $payment->id)); @@ -107,9 +107,13 @@ class ManagePaymentsTest extends TestCase /** @test */ public function admin_can_delete_a_payment() { - $user = $this->adminUserSigningIn(); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class)->create(['owner_id' => $user->agency->id]); + $payment = factory(Payment::class)->create([ + 'project_id' => $project->id, + 'owner_id' => $user->agency->id, + ]); - $payment = factory(Payment::class)->create(['owner_id' => $user->id]); $this->visit(route('payments.index')); $this->click(trans('app.edit')); $this->click(trans('payment.delete')); diff --git a/tests/Feature/Payments/PaymentSearchTest.php b/tests/Feature/Payments/PaymentSearchTest.php index d68cd39..8f2c1f6 100644 --- a/tests/Feature/Payments/PaymentSearchTest.php +++ b/tests/Feature/Payments/PaymentSearchTest.php @@ -12,9 +12,10 @@ class PaymentSearchTest extends TestCase public function user_can_find_payment_by_project_name() { $admin = $this->adminUserSigningIn(); - $project = factory(Project::class)->create(['name' => 'Project']); - $payment = factory(Payment::class)->create(['owner_id' => $admin->id, 'project_id' => $project->id]); - $unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->id]); + $project = factory(Project::class)->create(['owner_id' => $admin->agency->id, 'name' => 'Project']); + $payment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project->id]); + $project2 = factory(Project::class)->create(['owner_id' => $admin->agency->id]); + $unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project2->id]); $this->visit(route('payments.index')); $this->submitForm(trans('app.search'), [ @@ -31,8 +32,10 @@ class PaymentSearchTest extends TestCase public function partner_find_payment_by_customer_id() { $admin = $this->adminUserSigningIn(); - $payment = factory(Payment::class)->create(['owner_id' => $admin->id]); - $unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->id]); + $project = factory(Project::class)->create(['owner_id' => $admin->agency->id, 'name' => 'Project']); + $payment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project->id]); + $project2 = factory(Project::class)->create(['owner_id' => $admin->agency->id]); + $unShownPayment = factory(Payment::class)->create(['owner_id' => $admin->agency->id, 'project_id' => $project2->id]); $this->visit(route('payments.index')); $this->submitForm(trans('app.search'), [ diff --git a/tests/Unit/Models/InvoiceTest.php b/tests/Unit/Models/InvoiceTest.php index c865376..fdaba7b 100644 --- a/tests/Unit/Models/InvoiceTest.php +++ b/tests/Unit/Models/InvoiceTest.php @@ -19,9 +19,9 @@ class InvoiceTest extends TestCase public function it_generates_its_own_number() { $invoice1 = factory(Invoice::class)->create(); - $this->assertEquals(date('ym') . '001', $invoice1->number); + $this->assertEquals(date('ym').'001', $invoice1->number); $invoice2 = factory(Invoice::class)->create(); - $this->assertEquals(date('ym') . '002', $invoice2->number); + $this->assertEquals(date('ym').'002', $invoice2->number); } }