feat: implement barangay system phases 2-14
Complete adaptation from BukidBountyApp to Philippine barangay governance: - Barangay models: Resident, Household, HouseholdMember, Blotter, BlotterHearing, DocumentRequest, RequestPayment, RequestType, BarangayProject, BarangayBudget - Controllers: ResidentController, HouseholdController, BlotterController, BlotterHearingController, DocumentRequestController, RequestTypeController, ProjectController, BudgetController, QRPHController, AdminConsoleController, UserController, FileController, ChapterController, LoginController - Vue pages: Home, ManageResidents, ResidentProfile, ManageHouseholds, ManageBlotters, BlotterDetail, RequestDocument, ManageDocumentRequests, DocumentRequestDetail, ManageRequestTypes, ManageProjects, BudgetLedger, AdminConsole - Barangay roles: PunongBarangay, Kagawad, Secretary, Treasurer, SK, Tanod, BHW, Staff, Resident - UserPermissions matrix rewritten with barangay-specific permission mappings - VueRouteMap replaced with barangay SPA routes - UserActions enum references corrected across all controllers - Removed all market/cooperative/POS/subscription code and models
This commit is contained in:
@@ -4,14 +4,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Market\UserInfo;
|
||||
|
||||
class Chapter extends Model
|
||||
{
|
||||
protected ?string $table = 'chapters';
|
||||
|
||||
protected array $fillable = [
|
||||
'hashkey', 'name', 'cooperative_id', 'level', 'parent_id', 'location_key',
|
||||
'hashkey', 'name', 'level', 'parent_id', 'location_key',
|
||||
'lat', 'lng', 'is_active', 'created_by', 'updated_by',
|
||||
];
|
||||
|
||||
@@ -26,11 +24,6 @@ class Chapter extends Model
|
||||
return $this->belongsTo(Chapter::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function cooperative()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Market\Organization::class, 'cooperative_id');
|
||||
}
|
||||
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(Chapter::class, 'parent_id');
|
||||
@@ -51,61 +44,19 @@ class Chapter extends Model
|
||||
return $this->activeMembers()->whereNotNull('position');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find or create a chapter by level + location_key (normalized address field).
|
||||
*/
|
||||
public static function findOrCreateByLocation(string $level, string $locationKey, ?int $parentId = null): self
|
||||
{
|
||||
$key = strtolower(trim($locationKey));
|
||||
return static::firstOrCreate(
|
||||
['level' => $level, 'location_key' => $key],
|
||||
[
|
||||
'hashkey' => \Ramsey\Uuid\Uuid::uuid4()->toString(),
|
||||
'name' => ucwords(strtolower($locationKey)),
|
||||
'level' => $level,
|
||||
'location_key'=> $key,
|
||||
'parent_id' => $parentId,
|
||||
'is_active' => true,
|
||||
'hashkey' => hash('sha256', uniqid((string) now(), true)),
|
||||
'name' => ucwords(strtolower($locationKey)),
|
||||
'level' => $level,
|
||||
'location_key' => $key,
|
||||
'parent_id' => $parentId,
|
||||
'is_active' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-assign a user to the appropriate chapters based on their UserInfo address.
|
||||
* Creates chapter records on the fly if they don't exist.
|
||||
*/
|
||||
public static function autoAssignUser(int $userId): void
|
||||
{
|
||||
$info = UserInfo::where('user_id', $userId)->first();
|
||||
if (!$info) {
|
||||
return;
|
||||
}
|
||||
|
||||
$national = static::firstOrCreate(
|
||||
['level' => 'national', 'location_key' => 'philippines'],
|
||||
['hashkey' => \Ramsey\Uuid\Uuid::uuid4()->toString(), 'name' => 'Philippines', 'level' => 'national', 'location_key' => 'philippines', 'is_active' => true]
|
||||
);
|
||||
|
||||
ChapterMember::syncAutoAssignment($userId, $national->id);
|
||||
|
||||
if ($info->region) {
|
||||
$region = static::findOrCreateByLocation('region', $info->region, $national->id);
|
||||
ChapterMember::syncAutoAssignment($userId, $region->id);
|
||||
|
||||
if ($info->province) {
|
||||
$province = static::findOrCreateByLocation('province', $info->province, $region->id);
|
||||
ChapterMember::syncAutoAssignment($userId, $province->id);
|
||||
|
||||
if ($info->city) {
|
||||
$city = static::findOrCreateByLocation('city', $info->city, $province->id);
|
||||
ChapterMember::syncAutoAssignment($userId, $city->id);
|
||||
|
||||
if ($info->barangay) {
|
||||
$barangay = static::findOrCreateByLocation('barangay', $info->barangay, $city->id);
|
||||
ChapterMember::syncAutoAssignment($userId, $barangay->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user