# Database Structure Summary

## Overview
This document provides a comprehensive summary of the database structure for the BE-San-Luong (Rubber Production Management) system.

## Database Tables Created

### 1. Core Tables

#### don_vi (Units/Organizations)
- **Purpose**: Manages organizational hierarchy
- **Enum Values**: `TONG_CONG_TY`, `CONG_TY`, `DOI`, `TO`
- **Structure**: Hierarchical with parent-child relationships
- **Seeded Data**:
  - Tổng Công Ty Cao Su Đồng Nai (ID: 1)
  - Công Ty Cao Su Bình Long (ID: 2)
  - Công Ty Cao Su Phước Hòa (ID: 3)
  - Đội An Lộc (ID: 4)
  - Đội Cẩm Mỹ (ID: 5)
  - Tổ 1 (ID: 6)
  - Tổ 2 (ID: 7)

#### nguoi_dung (Users)
- **Purpose**: User authentication and management
- **Features**: Laravel Passport integration for API authentication
- **Seeded Data**:
  - admin / password123
  - user01 / password123
  - user02 / password123

#### cong_nhan (Workers)
- **Purpose**: Worker information management
- **Fields**: ma_cong_nhan, ho_ten, ngay_sinh, don_vi_id
- **Relationships**: Belongs to don_vi

#### khu_vuc_trong (Planting Areas)
- **Purpose**: Rubber plantation area management
- **Fields**: nien_do, don_vi, ma_lo, ten_lo, giong, hang_dat, nam_trong, nam_mo_cao, dien_tich_kiem_ke, so_cay_cao, tinh_trang, nam_cao_up, nam_tan_thu, dia_hinh
- **Relationships**: Belongs to don_vi

#### xe (Vehicles)
- **Purpose**: Vehicle management for latex collection
- **Fields**: ten_xe, bien_kiem_soat, don_vi_id
- **Relationships**: Belongs to don_vi

#### phieu_mu (Collection Tickets)
- **Purpose**: Latex collection ticket management
- **Fields**: ma_phieu, ma_xe, drc
- **Relationships**: Belongs to xe

#### san_luong_mu (Latex Production)
- **Purpose**: Daily latex production records
- **Fields**: phieu_id, ngay_cao, phien_cao, boi_dau, cong_nhan_id, khu_vuc_trong_id, loai_mu, khoi_luong, drc_cong_nhan, drc_to, drc_nha_may_pb, khoi_luong_kho, loai_cao, ghi_chu
- **Enum Values**: 
  - loai_mu: `MU_NUOC`, `MU_CHEN`, `MU_DAY`, `MU_LY_TAM`
  - loai_cao: `CAO_THUONG`, `CAO_CHOANG`, `CAO_THEM`
- **Relationships**: Belongs to phieu_mu, cong_nhan, khu_vuc_trong

#### ke_hoach (Plans)
- **Purpose**: Monthly production planning
- **Fields**: don_vi_id, khu_vuc_trong_id, cong_nhan_id, khoi_luong, thang, nam
- **Relationships**: Belongs to don_vi, khu_vuc_trong, cong_nhan

#### phan_cong_lo (Area Assignments)
- **Purpose**: Worker area assignment management
- **Fields**: cong_nhan_id, khu_vuc_trong_id, so_cay_cao, so_thu_tu_phan_cay, tu_ngay
- **Relationships**: Belongs to cong_nhan, khu_vuc_trong

### 2. History Tables (Audit Trail)

#### lich_su_chuyen_to (Team Transfer History)
- **Purpose**: Track worker team transfers
- **Fields**: cong_nhan_id, don_vi_cu_id, don_vi_moi_id, tu_ngay, den_ngay
- **Relationships**: Belongs to cong_nhan, don_vi (old and new)

#### lich_su_ke_hoach (Plan History)
- **Purpose**: Track plan changes
- **Fields**: ke_hoach_id, don_vi_id, khu_vuc_trong_id, cong_nhan_id, khoi_luong
- **Relationships**: Belongs to ke_hoach, don_vi, khu_vuc_trong, cong_nhan

#### lich_su_lo (Area History)
- **Purpose**: Track planting area changes
- **Fields**: khu_vuc_trong_id, don_vi, nhip_do_cao, che_do_cao, mat_do_cao, hinh_thuc_thu, so_cay_cao, tu_ngay, den_ngay, ghi_chu
- **Relationships**: Belongs to khu_vuc_trong, don_vi

#### lich_su_phan_cong_lo (Assignment History)
- **Purpose**: Track area assignment changes
- **Fields**: phan_cong_lo_id, khu_vuc_trong_id, so_cay_cao, so_thu_tu_phan_cay, tu_ngay, den_ngay
- **Relationships**: Belongs to phan_cong_lo, khu_vuc_trong

## Models Created

All models include proper relationships and follow Laravel conventions:

1. **DonVi** - Organization unit model with hierarchical relationships
2. **User** - User authentication model with Passport integration
3. **CongNhan** - Worker model
4. **KhuVucTrong** - Planting area model
5. **Xe** - Vehicle model
6. **PhieuMu** - Collection ticket model
7. **SanLuongMu** - Production record model
8. **KeHoach** - Planning model
9. **PhanCongLo** - Area assignment model
10. **LichSuChuyenTo** - Team transfer history model
11. **LichSuKeHoach** - Plan history model
12. **LichSuLo** - Area history model
13. **LichSuPhanCongLo** - Assignment history model

## Migrations Created

All migrations follow proper naming conventions and include:
- Foreign key constraints
- Indexes for performance
- Proper data types
- Timestamp columns (ngay_tao, ngay_cap_nhat)

### Migration Order:
1. `2026_02_07_130000_create_don_vi_table`
2. `2026_02_07_131108_create_nguoi_dung_table`
3. `2026_02_09_100000_create_cong_nhan_table`
4. `2026_02_09_100100_create_khu_vuc_trong_table`
5. `2026_02_09_100200_create_xe_table`
6. `2026_02_09_100300_create_phieu_mu_table`
7. `2026_02_09_100400_create_san_luong_mu_table`
8. `2026_02_09_100500_create_ke_hoach_table`
9. `2026_02_09_100600_create_phan_cong_lo_table`
10. `2026_02_09_100700_create_lich_su_chuyen_to_table`
11. `2026_02_09_100800_create_lich_su_ke_hoach_table`
12. `2026_02_09_100900_create_lich_su_lo_table`
13. `2026_02_09_101000_create_lich_su_phan_cong_lo_table`

## Seeders

### DonViSeeder
Creates organizational hierarchy:
- 1 Tổng Công Ty (Head Office)
- 2 Công Ty (Companies)
- 2 Đội (Teams)
- 2 Tổ (Groups)

### NguoiDungSeeder
Creates test users with hashed passwords for authentication testing.

## Key Features

1. **Hierarchical Organization Structure**: Support for multi-level organizational units
2. **Complete Audit Trail**: History tables track all major changes
3. **Proper Relationships**: All models have defined Eloquent relationships
4. **API Authentication**: Laravel Passport integration for secure API access
5. **Data Integrity**: Foreign key constraints ensure referential integrity
6. **Performance Optimization**: Indexes on frequently queried columns
7. **Vietnamese Field Names**: All database fields use Vietnamese naming conventions

## Database Status

✅ All migrations successfully run
✅ All tables created with proper structure
✅ All models created with relationships
✅ Seeders executed successfully
✅ Test data available for development

## Next Steps

1. Create API controllers for each model
2. Implement API routes
3. Add validation rules
4. Create API documentation
5. Implement business logic
6. Add unit tests
