diff --git a/app/Models/Agent/Agent.php b/app/Models/Agent/Agent.php index 28d83cd..fd9d9f3 100644 --- a/app/Models/Agent/Agent.php +++ b/app/Models/Agent/Agent.php @@ -15,10 +15,13 @@ use App\Notifications\AgentCreated; +use Modules\Payment\Traits\Paymentable; + class Agent extends Model { use HasFactory; use SoftDeletes; + use Paymentable; public const STATUS_ACTIVE = "ACTIVE"; public const STATUS_DISMISSED = "DISMISSED"; protected $fillable = [ diff --git a/app/Models/Company/Company.php b/app/Models/Company/Company.php index f08409a..989417c 100644 --- a/app/Models/Company/Company.php +++ b/app/Models/Company/Company.php @@ -4,12 +4,12 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; - +use Modules\Payment\Traits\Paymentable; class Company extends Model - { +{ use HasFactory; - - const STATUS_NEW = 'new'; + use Paymentable; + const STATUS_NEW = 'new'; const STATUS_ACCEPTED = 'accepted'; const STATUS_DECLINED = 'declined'; protected $fillable = [ @@ -27,6 +27,6 @@ class Company extends Model protected $casts = [ 'details' => 'array', - 'type' => CompanyType::class + 'type' => CompanyType::class ]; - } +} diff --git a/app/Modules/Admin/Http/Controllers/AdminPaymentsController.php b/app/Modules/Admin/Http/Controllers/AdminPaymentsController.php index cff92d4..1aef011 100644 --- a/app/Modules/Admin/Http/Controllers/AdminPaymentsController.php +++ b/app/Modules/Admin/Http/Controllers/AdminPaymentsController.php @@ -8,19 +8,12 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; -use App\Models\Company\Company; -use App\Models\Complex; -use App\Models\Agent\Agent; class AdminPaymentsController extends Controller { public function index() { - return view('admin::payments.index', [ - 'companies' => Company::orderBy('name')->get(), - 'complexes' => Complex::orderBy('name')->get(), - 'agents' => Agent::all() - ]); + return view('admin::payments.index'); } } diff --git a/app/Modules/Admin/Http/Livewire/Payments.php b/app/Modules/Admin/Http/Livewire/Payments.php new file mode 100644 index 0000000..f015c87 --- /dev/null +++ b/app/Modules/Admin/Http/Livewire/Payments.php @@ -0,0 +1,80 @@ +companyId = $company->id; + } + + public function back() + { + $this->companyId = null; + } + + public function setPaymentValueForCompany(Company $company, Complex $complex) + { + $company->setPayment($complex, $this->value); + $this->value = null; + } + + public function unsetPaymentForCompany(Company $company, Complex $complex) + { + $company->unsetPayment($complex); + } + + public function setPaymentValueForAgent(Agent $agent, Complex $complex) + { + $agent->setPayment($complex, $this->value); + $this->value = null; + } + + public function setPaymentAsParentForAgent(Agent $agent, Complex $complex) + { + $agent->setAsParentPayment($complex); + $this->value = null; + } + + public function unsetPaymentForAgent(Agent $agent, Complex $complex) + { + $agent->unsetPayment($complex); + } + + public function render() + { + if ($this->companyId) + { + return view('admin::payments.agents', [ + 'company' => Company::find($this->companyId), + 'complexes' => Complex::orderBy('name')->get(), + 'agents' => Agent::where('company_id', $this->companyId)-> + with([ + 'user' => function ($query) + { + $query->orderBy('name', 'desc'); + } + ])->get() + ]); + } + else + { + return view('admin::payments.companies', [ + 'companies' => Company::orderBy('name')->get(), + 'complexes' => Complex::orderBy('name')->get(), + ]); + } + } +} \ No newline at end of file diff --git a/app/Modules/Admin/Providers/ModuleServiceProvider.php b/app/Modules/Admin/Providers/ModuleServiceProvider.php index 378e6e6..e803811 100644 --- a/app/Modules/Admin/Providers/ModuleServiceProvider.php +++ b/app/Modules/Admin/Providers/ModuleServiceProvider.php @@ -63,6 +63,8 @@ protected function registerLivewire() { Livewire::component('admin.menu', \Modules\Admin\Http\Livewire\AdminMenu::class); Livewire::component('admin.posts', \Modules\Admin\Http\Livewire\Posts::class); + Livewire::component('admin.payments', \Modules\Admin\Http\Livewire\Payments::class); + } protected function registerComponent() diff --git a/app/Modules/Admin/Views/payments/agents.blade.php b/app/Modules/Admin/Views/payments/agents.blade.php new file mode 100644 index 0000000..a5616a5 --- /dev/null +++ b/app/Modules/Admin/Views/payments/agents.blade.php @@ -0,0 +1,124 @@ +
+ +
+ + + + + @foreach ($complexes as $complex) + + @endforeach + + + + @foreach ($agents as $agent) + + + @foreach ($complexes as $complex) + @php($paymentable = $agent->getPaymentable($complex)) + + @endforeach + + @endforeach + +
Агент + {{ $complex->name }} +
+ @php($compPayment = $company->getPaymentable($complex)) + + @if ($compPayment->value === null) + Не установлено + @elseif($compPayment->value == -1) + Наследовать + @else + {{ $compPayment->value }} % + @endif + +
+
+ {{ $agent->user->name }} + + @if ($paymentable->value == null) + + @elseif($paymentable->value == -1) + + @else + + @endif +
+
+
diff --git a/app/Modules/Admin/Views/payments/companies.blade.php b/app/Modules/Admin/Views/payments/companies.blade.php new file mode 100644 index 0000000..d7f45f7 --- /dev/null +++ b/app/Modules/Admin/Views/payments/companies.blade.php @@ -0,0 +1,97 @@ +
+ + + + + @foreach ($complexes as $complex) + + @endforeach + + + + + @foreach ($companies as $company) + + + @foreach ($complexes as $complex) + @php($paymentable = $company->getPaymentable($complex)) + + @endforeach + + + @endforeach + +
Компания{{ $complex->name }} +
+ {{ $company->name }} + + @if ($paymentable->value == null) + + @elseif($paymentable->value == -1) + + @else + + @endif + + +
+
diff --git a/app/Modules/Admin/Views/payments/index.blade.php b/app/Modules/Admin/Views/payments/index.blade.php index 9b60683..2e540ea 100644 --- a/app/Modules/Admin/Views/payments/index.blade.php +++ b/app/Modules/Admin/Views/payments/index.blade.php @@ -1,31 +1,5 @@ @php($title = 'Вознаграждения') @extends('layouts.admin') @section('content') -
- - - - - @foreach ($complexes as $complex) - - @endforeach - - - - @foreach ($companies as $company) - - - @foreach ($complexes as $complex) - - @endforeach - - @endforeach - -
Компания{{ $complex->name }} -
0 % -
-
- {{ $company->name }} - Наследовать
-
+ @livewire('admin.payments') @endsection diff --git a/app/Modules/Payment/Config/config.php b/app/Modules/Payment/Config/config.php new file mode 100644 index 0000000..ce09543 --- /dev/null +++ b/app/Modules/Payment/Config/config.php @@ -0,0 +1,5 @@ +id(); + $table->string('paymentable_type'); + $table->integer('paymentable_id'); + $table->foreignId('complex_id')->references('id')->on('complexes')->onDelete('cascade'); + $table->integer('value')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('payments'); + } +}; diff --git a/app/Modules/Payment/Http/Controllers/PaymentController.php b/app/Modules/Payment/Http/Controllers/PaymentController.php new file mode 100644 index 0000000..6221d8d --- /dev/null +++ b/app/Modules/Payment/Http/Controllers/PaymentController.php @@ -0,0 +1,17 @@ +app->register(RouteServiceProvider::class); + } + + public function boot() + { + $this->registerViews(); + $this->registerLivewireViews(); + $this->registerMigrations(); + $this->registerConfig(); + $this->registerComponent(); + $this->registerLivewire(); + } + + protected function registerViews() + { + $moduleViewsPath = __DIR__.'/../Views'; + $this->loadViewsFrom( + $moduleViewsPath, strtolower($this->moduleName) + ); + } + + protected function registerLivewireViews() + { + $moduleViewsPath = __DIR__.'/../Views/livewire'; + $this->loadViewsFrom( + $moduleViewsPath, strtolower($this->moduleName) + ); + } + + protected function registerMigrations() + { + $this->loadMigrationsFrom( + app_path('Modules/'.$this->moduleName.'/Database/Migrations') + ); + } + + protected function registerConfig() + { + $path = app_path('Modules/'.$this->moduleName.'/Config/config.php'); + $this->mergeConfigFrom( + $path, strtolower($this->moduleName) + ); + } + + protected function registerLivewire() + { + //Livewire::component('', \Modules\\Http\Livewire\::class); + } + + protected function registerComponent() + { + //Blade::component('', \Modules\\Http\Components\::class); + } +} \ No newline at end of file diff --git a/app/Modules/Payment/Providers/RouteServiceProvider.php b/app/Modules/Payment/Providers/RouteServiceProvider.php new file mode 100644 index 0000000..951c041 --- /dev/null +++ b/app/Modules/Payment/Providers/RouteServiceProvider.php @@ -0,0 +1,24 @@ +registerWebRoutes(); + } + + protected function registerWebRoutes() + { + //Add Web Routes with web Guard + Route::middleware('web') + //Set Default Controllers Namespace + ->namespace('Modules\\Payment\\Http\\Controllers') + ->group(app_path('Modules/Payment/Routes/web.php')); + } +} \ No newline at end of file diff --git a/app/Modules/Payment/Routes/web.php b/app/Modules/Payment/Routes/web.php new file mode 100644 index 0000000..ba188c0 --- /dev/null +++ b/app/Modules/Payment/Routes/web.php @@ -0,0 +1,13 @@ +group(function() { + + Route::get('/payment', [PaymentController::class, 'index']); + + Route::middleware(['hasAccess'])->group(function() { + /** Routes that need to be protected - Маршруты которые нужно защитить */ + }); +}); \ No newline at end of file diff --git a/app/Modules/Payment/Traits/Paymentable.php b/app/Modules/Payment/Traits/Paymentable.php new file mode 100644 index 0000000..c0aee12 --- /dev/null +++ b/app/Modules/Payment/Traits/Paymentable.php @@ -0,0 +1,98 @@ +getShortName(); + } + + public function getPaymentable(Complex $complex) + { + $paymentable = Payment::where('paymentable_type', $this->getClassName()) + ->where('paymentable_id', $this->id) + ->where('complex_id', $complex->id); + switch ( $paymentable->count() ) + { + case 0: + return Payment::create([ + 'paymentable_type' => $this->getClassName(), + 'paymentable_id' => $this->id, + 'complex_id' => $complex->id, + 'value' => ($this->getClassName() == 'Company') ? null : ($this->defaultValue == -1 ? $this->setAsParentPayment($complex) : null) + ]); + break; + case 1: + return $paymentable->first(); + break; + default: + abort(404); + + } + return false; + } + + public function setPayment(Complex $complex, $value) + { + $paymentable = $this->getPaymentable($complex); + if ($value >= 0 && $value <= 100) + { + $paymentable->update([ + 'value' => $value //-1 значит, что значение наследуется от родительского + ]); + return true; + } + + return false; + } + + public function setAsParentPayment(Complex $complex) + { + $value = -1; + if ($this->getClassName() == 'Agent') + { + $company = $this->company; + $companyPayment = $company->getPaymentable($complex); + if ($companyPayment->value === null || $companyPayment->value == -1) + { + $value = null; + } + } + + $paymentable = $this->getPaymentable($complex); + $paymentable->update([ + 'value' => $value //-1 значит, что значение наследуется от родительского + ]); + return true; + } + + public function unsetPayment(Complex $complex) + { + $paymentable = $this->getPaymentable($complex); + $paymentable->update([ + 'value' => null + ]); + return true; + } +} diff --git a/app/Modules/Payment/Views/index.blade.php b/app/Modules/Payment/Views/index.blade.php new file mode 100644 index 0000000..f8198cf --- /dev/null +++ b/app/Modules/Payment/Views/index.blade.php @@ -0,0 +1,4 @@ +@extends('layouts.app') + @section('content') +

Example views

+ @endsection \ No newline at end of file diff --git a/resources/sass/_variables.scss b/resources/sass/_variables.scss index 172daaa..792d6a4 100644 --- a/resources/sass/_variables.scss +++ b/resources/sass/_variables.scss @@ -5,3 +5,6 @@ $body-bg: #f8fafc; $font-family-sans-serif: 'Nunito', sans-serif; $font-size-base: 0.9rem; $line-height-base: 1.6; + +//colors +$primary: #e6662a; \ No newline at end of file