chore: update document approval workflow and bug fixes
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ContractorManagement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Inertia\Inertia;
|
||||
use Modules\ContractorManagement\Enums\InvoiceStatus;
|
||||
use Modules\ContractorManagement\Events\ContractorInvoicePaid;
|
||||
use Modules\ContractorManagement\Events\ContractorInvoiceSubmitted;
|
||||
use Modules\ContractorManagement\Models\Contractor;
|
||||
use Modules\ContractorManagement\Models\ContractorInvoice;
|
||||
use Modules\ProjectManagement\Models\Project;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class ContractorController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Contractor::query();
|
||||
|
||||
if ($search = $request->search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('company_name', 'like', "%{$search}%")
|
||||
->orWhere('contact_person', 'like', "%{$search}%")
|
||||
->orWhere('email', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($spec = $request->specialization) {
|
||||
$query->where('specialization', $spec);
|
||||
}
|
||||
|
||||
if ($status = $request->status) {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
|
||||
$contractors = $query->withCount(['equipment', 'certifications', 'invoices', 'projects'])
|
||||
->latest()
|
||||
->paginate(15)
|
||||
->withQueryString();
|
||||
|
||||
return Inertia::render('ContractorManagement::Contractors/Index', [
|
||||
'contractors' => $contractors,
|
||||
'filters' => $request->only(['search', 'specialization', 'status']),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return Inertia::render('ContractorManagement::Contractors/Create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'company_name' => 'required|string|max:255',
|
||||
'contact_person' => 'required|string|max:255',
|
||||
'email' => 'required|email|unique:contractors',
|
||||
'phone' => 'nullable|string|max:50',
|
||||
'specialization' => 'nullable|string|max:100',
|
||||
'address' => 'nullable|string',
|
||||
'tax_id' => 'nullable|string|max:50',
|
||||
'payment_terms' => 'required|in:net_15,net_30,net_60',
|
||||
'shares_materials_catalog' => 'nullable|boolean',
|
||||
'create_admin' => 'nullable|boolean',
|
||||
'admin_name' => 'required_if:create_admin,true|string|max:255',
|
||||
'admin_email' => 'required_if:create_admin,true|email|unique:users,email',
|
||||
'admin_password' => 'required_if:create_admin,true|string|min:8',
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($validated) {
|
||||
$contractorData = collect($validated)->except([
|
||||
'create_admin',
|
||||
'admin_name',
|
||||
'admin_email',
|
||||
'admin_password'
|
||||
])->toArray();
|
||||
|
||||
$contractor = Contractor::create($contractorData);
|
||||
|
||||
if (!empty($validated['create_admin']) && $validated['create_admin']) {
|
||||
$user = User::create([
|
||||
'name' => $validated['admin_name'],
|
||||
'email' => $validated['admin_email'],
|
||||
'password' => Hash::make($validated['admin_password']),
|
||||
'user_type' => 'contractor',
|
||||
'contractor_id' => $contractor->id,
|
||||
'status' => 'active',
|
||||
'must_change_password' => true,
|
||||
]);
|
||||
|
||||
$role = Role::firstOrCreate(['name' => 'Contractor']);
|
||||
$user->assignRole($role);
|
||||
}
|
||||
});
|
||||
|
||||
return redirect()->route('contractors.index')->with('success', 'Contractor created successfully.');
|
||||
}
|
||||
|
||||
public function show(Contractor $contractor)
|
||||
{
|
||||
$contractor->load([
|
||||
'equipment.deployments.project:id,name',
|
||||
'certifications',
|
||||
'invoices.project:id,name',
|
||||
'projects:id,name,code,status',
|
||||
]);
|
||||
|
||||
$projects = Project::select('id', 'ulid', 'name', 'code')->get();
|
||||
$employees = User::select('id', 'ulid', 'name')->get();
|
||||
|
||||
return Inertia::render('ContractorManagement::Contractors/Show', [
|
||||
'contractor' => $contractor,
|
||||
'projects' => $projects,
|
||||
'employees' => $employees,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(Contractor $contractor)
|
||||
{
|
||||
return Inertia::render('ContractorManagement::Contractors/Edit', [
|
||||
'contractor' => $contractor,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Contractor $contractor)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'company_name' => 'required|string|max:255',
|
||||
'contact_person' => 'required|string|max:255',
|
||||
'email' => "required|email|unique:contractors,email,{$contractor->id}",
|
||||
'phone' => 'nullable|string|max:50',
|
||||
'specialization' => 'nullable|string|max:100',
|
||||
'address' => 'nullable|string',
|
||||
'tax_id' => 'nullable|string|max:50',
|
||||
'payment_terms' => 'required|in:net_15,net_30,net_60',
|
||||
'rating' => 'nullable|numeric|min:0|max:5',
|
||||
'shares_materials_catalog' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$contractor->update($validated);
|
||||
|
||||
return redirect()->route('contractors.show', $contractor)->with('success', 'Contractor updated.');
|
||||
}
|
||||
|
||||
public function destroy(Contractor $contractor)
|
||||
{
|
||||
$contractor->delete();
|
||||
return redirect()->route('contractors.index')->with('success', 'Contractor deleted.');
|
||||
}
|
||||
|
||||
// --- Equipment ---
|
||||
public function storeEquipment(Request $request, Contractor $contractor)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'type' => 'nullable|string|max:50',
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
'daily_rate' => 'nullable|numeric|min:0',
|
||||
]);
|
||||
|
||||
$contractor->equipment()->create($validated);
|
||||
|
||||
return back()->with('success', 'Equipment added.');
|
||||
}
|
||||
|
||||
// --- Certifications ---
|
||||
public function storeCertification(Request $request, Contractor $contractor)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'certification_number' => 'nullable|string|max:100',
|
||||
'issued_date' => 'nullable|date',
|
||||
'expiry_date' => 'nullable|date|after:issued_date',
|
||||
]);
|
||||
|
||||
$contractor->certifications()->create($validated);
|
||||
|
||||
return back()->with('success', 'Certification added.');
|
||||
}
|
||||
|
||||
// --- Project Assignment ---
|
||||
public function assignProject(Request $request, Contractor $contractor)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'project_id' => 'required|string',
|
||||
'role' => 'required|in:subcontractor,supplier,consultant',
|
||||
'contract_amount' => 'nullable|numeric|min:0',
|
||||
]);
|
||||
|
||||
$projectId = Project::resolveUlidToId($validated['project_id']);
|
||||
|
||||
$contractor->projects()->syncWithoutDetaching([
|
||||
$projectId => [
|
||||
'role' => $validated['role'],
|
||||
'contract_amount' => $validated['contract_amount'] ?? 0,
|
||||
],
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Contractor assigned to project.');
|
||||
}
|
||||
|
||||
public function removeProject(Contractor $contractor, Project $project)
|
||||
{
|
||||
$contractor->projects()->detach($project->id);
|
||||
return back()->with('success', 'Contractor removed from project.');
|
||||
}
|
||||
|
||||
// --- Invoices ---
|
||||
public function storeInvoice(Request $request, Contractor $contractor)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'project_id' => 'nullable|string',
|
||||
'invoice_number' => 'required|string|unique:contractor_invoices',
|
||||
'amount' => 'required|numeric|min:0.01',
|
||||
'description' => 'nullable|string',
|
||||
'invoice_date' => 'required|date',
|
||||
'due_date' => 'nullable|date',
|
||||
]);
|
||||
|
||||
// Resolve ULID → ID for project
|
||||
if (!empty($validated['project_id'])) {
|
||||
$validated['project_id'] = Project::resolveUlidToId($validated['project_id']);
|
||||
}
|
||||
|
||||
$contractor->invoices()->create($validated);
|
||||
|
||||
return back()->with('success', 'Invoice created.');
|
||||
}
|
||||
|
||||
public function submitInvoice(ContractorInvoice $invoice)
|
||||
{
|
||||
try {
|
||||
$invoice->transitionTo(InvoiceStatus::Submitted);
|
||||
ContractorInvoiceSubmitted::dispatch($invoice);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return back()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Invoice submitted for approval.');
|
||||
}
|
||||
|
||||
public function approveInvoice(ContractorInvoice $invoice)
|
||||
{
|
||||
try {
|
||||
$invoice->transitionTo(InvoiceStatus::Approved);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return back()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Invoice approved.');
|
||||
}
|
||||
|
||||
public function payInvoice(Request $request, ContractorInvoice $invoice)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'amount' => 'required|numeric|min:0.01',
|
||||
'payment_method' => 'required|in:bank_transfer,check,cash',
|
||||
'reference_number' => 'nullable|string',
|
||||
'payment_date' => 'required|date',
|
||||
]);
|
||||
|
||||
$invoice->payments()->create($validated);
|
||||
|
||||
try {
|
||||
$invoice->transitionTo(InvoiceStatus::Paid);
|
||||
ContractorInvoicePaid::dispatch($invoice);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return back()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Payment recorded and invoice marked as paid.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ContractorManagement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ContractorManagementController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('contractormanagement::index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('contractormanagement::create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request) {}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('contractormanagement::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
return view('contractormanagement::edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id) {}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id) {}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ContractorManagement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Inertia\Inertia;
|
||||
use Modules\ContractorManagement\Events\ContractorApproved;
|
||||
use Modules\ContractorManagement\Events\ContractorOnboarded;
|
||||
use Modules\ContractorManagement\Models\Contractor;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class ContractorOnboardingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Show the contractor registration form.
|
||||
*/
|
||||
public function showRegistrationForm()
|
||||
{
|
||||
return Inertia::render('ContractorManagement::Contractors/Register');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle guest contractor onboarding registration.
|
||||
*/
|
||||
public function register(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
// Contractor info
|
||||
'company_name' => 'required|string|max:255',
|
||||
'contact_person' => 'required|string|max:255',
|
||||
'email' => 'required|email|unique:contractors,email',
|
||||
'phone' => 'nullable|string|max:50',
|
||||
'specialization' => 'nullable|string|max:100',
|
||||
'address' => 'nullable|string',
|
||||
'tax_id' => 'nullable|string|max:50',
|
||||
'payment_terms' => 'required|in:net_15,net_30,net_60',
|
||||
|
||||
// Admin User info
|
||||
'admin_name' => 'required|string|max:255',
|
||||
'admin_email' => 'required|email|unique:users,email',
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($validated) {
|
||||
// 1. Create the contractor in 'pending' status
|
||||
$contractor = Contractor::create([
|
||||
'company_name' => $validated['company_name'],
|
||||
'contact_person' => $validated['contact_person'],
|
||||
'email' => $validated['email'],
|
||||
'phone' => $validated['phone'] ?? null,
|
||||
'specialization' => $validated['specialization'] ?? null,
|
||||
'address' => $validated['address'] ?? null,
|
||||
'tax_id' => $validated['tax_id'] ?? null,
|
||||
'payment_terms' => $validated['payment_terms'],
|
||||
'status' => 'pending',
|
||||
'type' => 'main', // Default to main if self-registered
|
||||
]);
|
||||
|
||||
// Ensure the Contractor role exists
|
||||
$role = Role::firstOrCreate(['name' => 'Contractor']);
|
||||
|
||||
// 2. Create the inactive admin user for this contractor
|
||||
$user = User::create([
|
||||
'name' => $validated['admin_name'],
|
||||
'email' => $validated['admin_email'],
|
||||
'password' => Hash::make($validated['password']),
|
||||
'user_type' => 'admin',
|
||||
'status' => 'inactive', // Inactive until approved
|
||||
'contractor_id' => $contractor->id,
|
||||
]);
|
||||
|
||||
$user->assignRole($role);
|
||||
|
||||
event(new ContractorOnboarded($contractor));
|
||||
});
|
||||
|
||||
return redirect()->route('login')->with('success', 'Your contractor registration has been submitted and is pending system approval.');
|
||||
}
|
||||
|
||||
/**
|
||||
* List all pending contractor onboarding requests for Platform Owner review.
|
||||
*/
|
||||
public function pending()
|
||||
{
|
||||
// Only Platform Owners (contractor_id === null) should access this
|
||||
if (auth()->user()->contractor_id !== null) {
|
||||
abort(403, 'Unauthorized access.');
|
||||
}
|
||||
|
||||
// Fetch contractors in pending status
|
||||
$pendingContractors = Contractor::where('status', 'pending')
|
||||
->withCount('users')
|
||||
->latest()
|
||||
->get();
|
||||
|
||||
return Inertia::render('ContractorManagement::Contractors/Pending', [
|
||||
'contractors' => $pendingContractors,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve a pending contractor and activate their primary admin user.
|
||||
*/
|
||||
public function approve(Contractor $contractor)
|
||||
{
|
||||
if (auth()->user()->contractor_id !== null) {
|
||||
abort(403, 'Unauthorized access.');
|
||||
}
|
||||
|
||||
if ($contractor->status !== 'pending') {
|
||||
return back()->with('error', 'Only pending contractors can be approved.');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($contractor) {
|
||||
// Update contractor status to active
|
||||
$contractor->update(['status' => 'active']);
|
||||
|
||||
// Activate associated users
|
||||
User::where('contractor_id', $contractor->id)
|
||||
->where('status', 'inactive')
|
||||
->update(['status' => 'active']);
|
||||
|
||||
event(new ContractorApproved($contractor));
|
||||
});
|
||||
|
||||
return back()->with('success', "Contractor '{$contractor->company_name}' has been successfully approved.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject a pending contractor registration and delete the submission.
|
||||
*/
|
||||
public function reject(Contractor $contractor)
|
||||
{
|
||||
if (auth()->user()->contractor_id !== null) {
|
||||
abort(403, 'Unauthorized access.');
|
||||
}
|
||||
|
||||
if ($contractor->status !== 'pending') {
|
||||
return back()->with('error', 'Only pending contractors can be rejected.');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($contractor) {
|
||||
// Delete all users belonging to this contractor
|
||||
User::where('contractor_id', $contractor->id)->delete();
|
||||
|
||||
// Delete the contractor record
|
||||
$contractor->delete();
|
||||
});
|
||||
|
||||
return back()->with('success', 'Contractor registration request has been rejected and deleted.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user