diff --git a/app/Entities/Agencies/Agency.php b/app/Entities/Agencies/Agency.php index c31ba27..ae3625f 100644 --- a/app/Entities/Agencies/Agency.php +++ b/app/Entities/Agencies/Agency.php @@ -12,4 +12,9 @@ class Agency extends Model { return $this->belongsTo('App\Entities\Users\User'); } + + public function projects() + { + return $this->hasMany('App\Entities\Projects\Project', 'owner_id'); + } } diff --git a/app/Entities/Payments/Payment.php b/app/Entities/Payments/Payment.php index e5ee315..b01d2bf 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; @@ -15,6 +16,18 @@ class Payment extends Model protected $presenter = PaymentPresenter::class; protected $guarded = ['id', 'created_at', 'updated_at']; + protected static function boot() + { + parent::boot(); + + static::addGlobalScope('by_owner_project', function (Builder $builder) { + if (auth()->user() && auth()->user()->agency) { + $projectIds = auth()->user()->agency->projects->pluck('id')->all(); + $builder->whereIn('project_id', $projectIds); + } + }); + } + public function project() { return $this->belongsTo(Project::class); diff --git a/app/Entities/Payments/PaymentsRepository.php b/app/Entities/Payments/PaymentsRepository.php index dabe2f9..a5b9f13 100755 --- a/app/Entities/Payments/PaymentsRepository.php +++ b/app/Entities/Payments/PaymentsRepository.php @@ -35,8 +35,7 @@ class PaymentsRepository extends BaseRepository public function create($paymentData) { - $paymentData['owner_id'] = auth()->id(); - $paymentData['amount'] = str_replace('.', '', $paymentData['amount']); + $paymentData['amount'] = str_replace('.', '', $paymentData['amount']); return $this->storeArray($paymentData); } diff --git a/app/Entities/Reports/ReportsRepository.php b/app/Entities/Reports/ReportsRepository.php index c0a6e31..6bdd2c5 100755 --- a/app/Entities/Reports/ReportsRepository.php +++ b/app/Entities/Reports/ReportsRepository.php @@ -8,8 +8,8 @@ use App\Entities\Projects\Project; use DB; /** -* Reports Repository Class -*/ + * Reports Repository Class + */ class ReportsRepository extends BaseRepository { protected $model; @@ -21,10 +21,9 @@ class ReportsRepository extends BaseRepository public function getDailyReports($date, $q) { - return Payment::orderBy('date','desc') + return Payment::orderBy('date', 'desc') ->where('date', $date) - ->with('customer','project') - ->where('owner_id',auth()->id()) + ->with('partner', 'project') ->get(); } @@ -34,8 +33,7 @@ class ReportsRepository extends BaseRepository ->where(DB::raw('YEAR(date)'), $year) ->where(DB::raw('MONTH(date)'), $month) ->groupBy('date') - ->orderBy('date','asc') - ->where('owner_id',auth()->id()) + ->orderBy('date', 'asc') ->get(); } @@ -45,18 +43,17 @@ class ReportsRepository extends BaseRepository ->where(DB::raw('YEAR(date)'), $year) ->groupBy(DB::raw('YEAR(date)')) ->groupBy(DB::raw('MONTH(date)')) - ->orderBy('date','asc') - ->where('owner_id',auth()->id()) + ->orderBy('date', 'asc') ->get(); } public function getCurrentCredits() { // On Progress, Done, On Hold - $projects = Project::whereIn('status_id',[2,3,6])->with('payments','customer')->get(); - return $projects->filter(function($project) { + $projects = Project::whereIn('status_id', [2, 3, 6])->with('payments', 'customer')->get(); + return $projects->filter(function ($project) { return $project->cashInTotal() < $project->project_value; })->values(); } -} \ No newline at end of file +} diff --git a/app/Traits/OwnedByAgency.php b/app/Traits/OwnedByAgency.php index 610c96c..9834022 100644 --- a/app/Traits/OwnedByAgency.php +++ b/app/Traits/OwnedByAgency.php @@ -9,10 +9,8 @@ trait OwnedByAgency public static function bootOwnedByAgency() { static::addGlobalScope('by_owner', function (Builder $builder) { - if ( ! is_null(auth()->user()->agency)) { + if (auth()->user() && auth()->user()->agency) { $builder->where('owner_id', auth()->user()->agency->id); - } else { - $builder->where('owner_id', 0); } }); } diff --git a/database/factories/PaymentFactory.php b/database/factories/PaymentFactory.php index bd0b5be..7398b4f 100644 --- a/database/factories/PaymentFactory.php +++ b/database/factories/PaymentFactory.php @@ -3,7 +3,6 @@ use App\Entities\Partners\Partner; use App\Entities\Payments\Payment; use App\Entities\Projects\Project; -use App\Entities\Users\User; use Faker\Generator as Faker; $factory->define(Payment::class, function (Faker $faker) { @@ -17,9 +16,6 @@ $factory->define(Payment::class, function (Faker $faker) { 'type_id' => rand(1, 3), 'date' => $faker->dateTimeBetween('-1 year', '-1 month')->format('Y-m-d'), 'description' => $faker->paragraph, - 'owner_id' => function () { - return factory(User::class)->create()->id; - }, 'partner_id' => function () { return factory(Partner::class)->create()->id; }, diff --git a/database/migrations/2016_11_15_151228_create_payments_table.php b/database/migrations/2016_11_15_151228_create_payments_table.php index 7404f68..3c468b7 100644 --- a/database/migrations/2016_11_15_151228_create_payments_table.php +++ b/database/migrations/2016_11_15_151228_create_payments_table.php @@ -22,7 +22,6 @@ class CreatePaymentsTable extends Migration $table->date('date'); $table->string('description'); $table->integer('partner_id')->unsigned(); - $table->integer('owner_id')->unsigned(); $table->timestamps(); }); } diff --git a/resources/views/reports/payments/daily.blade.php b/resources/views/reports/payments/daily.blade.php index 591f843..c7c0cff 100755 --- a/resources/views/reports/payments/daily.blade.php +++ b/resources/views/reports/payments/daily.blade.php @@ -3,7 +3,7 @@ @section('title', 'Laporan Harian : ' . dateId($date)) @section('content') - +