# Manual Local Loading Input - Documentation

## Overview
Fitur ini dibuat untuk mengatasi kekurangan sistem Terminal Manager yang tidak mencatat transaksi **Local Loading (BCU Metering)** ke database. 

### Masalah
- **Remote Loading**: Data otomatis tersimpan ke Terminal Manager ✅
- **Local Loading**: Data TIDAK tersimpan, menyebabkan realization di dashboard < aktual lapangan ❌

### Solusi
Halaman input manual untuk entry data transaksi Local Loading agar total realization dashboard = aktual lapangan.

---

## Installation Steps

### 1. Create Database Table
Jalankan script SQL ini di **KEDUA server** (PGN: 103.133.223.213 dan RWL: 103.133.223.212):

```sql
-- File: sql/create_table_local_loading_manual.sql
-- Jalankan di SQL Server Management Studio atau Azure Data Studio
```

Script akan membuat tabel `t_local_loading_manual` dengan 24 fields + audit fields.

### 2. Create Combined View
Jalankan script SQL ini di **KEDUA server** untuk menggabungkan data Remote + Local:

```sql
-- File: sql/create_view_combined_loading.sql
-- Membuat view v_dash_actual_lo_combined
```

View ini akan meng-UNION data dari:
- `ctbRewuluLoadingDetailsSynchronizationItems` (Remote Loading dari Terminal Manager)
- `t_local_loading_manual` (Local Loading input manual)

### 3. Update Dashboard Query
Edit file berikut untuk menggunakan view baru:

**PGN:**
- `pgn/media.php` - Ubah query dari `v_dash_actual_lo` menjadi `v_dash_actual_lo_combined`
- `pgn/module/dashboard.php` - Update query realization

**RWL:**
- `rwl/media.php` - Ubah query dari `v_dash_actual_lo` menjadi `v_dash_actual_lo_combined`
- `rwl/module/dashboard.php` - Update query realization

Contoh perubahan:
```php
// Sebelum
$sqlsrv = sqlsrv_query($conn, "SELECT SUM(realization) AS total_realization 
                                FROM ExperionTASBSI.dbo.v_dash_actual_lo 
                                WHERE ...");

// Sesudah
$sqlsrv = sqlsrv_query($conn, "SELECT SUM(realization) AS total_realization 
                                FROM ExperionTASBSI.dbo.v_dash_actual_lo_combined 
                                WHERE ...");
```

---

## Usage

### Akses Halaman Input

**PGN:**
```
https://dsp-report.web.id/pgn/media.php?module=report&act=input_local_loading
```

**RWL:**
```
https://dsp-report.web.id/rwl/media.php?module=report&act=input_local_loading
```

### Field Wajib Diisi (Required)
1. **Transaction Date** - Tanggal transaksi
2. **DO Number** - Nomor Delivery Order (harus unik)
3. **Filling Start Time** - Waktu mulai pengisian
4. **Filling Finish Time** - Waktu selesai pengisian (harus > start time)
5. **Loading Bay Code** - Kode bay (e.g., B1, B2)
6. **Loading Arm Code** - Kode arm (e.g., L1, L2)
7. **Product Name** - Nama produk BBM (dropdown)
8. **Actual Quantity** - Quantity aktual dalam Liter (harus > 0)
9. **Vehicle Number** - Nomor polisi kendaraan (auto-uppercase)

### Field Opsional
- GateInTime, GateOutTime
- Loading Bay/Arm Name
- DO Type, SIOD_ID
- DO Quantity
- Product Code (auto-fill dari Product Name)
- Product Density, Temperature, Pressure, UOM
- Truck Compartment Number
- Driver Name, Driver Code
- Seal Number
- Remarks

### Validasi
- Filling Finish Time harus lebih besar dari Start Time
- Actual Quantity harus > 0
- DO Number harus unik (tidak boleh duplikat)
- Confirmation dialog sebelum save

---

## Files Structure

```
dsp-report/
├── sql/
│   ├── create_table_local_loading_manual.sql      # Create table script
│   └── create_view_combined_loading.sql            # Create combined view
│
├── pgn/
│   └── module/
│       └── report/
│           ├── input_local_loading.php             # Form input PGN
│           ├── add_local_loading.php               # Backend processor PGN
│           └── view_local_loading.php              # List data PGN (TODO)
│
└── rwl/
    └── module/
        └── report/
            ├── input_local_loading.php             # Form input RWL
            ├── add_local_loading.php               # Backend processor RWL
            └── view_local_loading.php              # List data RWL (TODO)
```

---

## Features

### ✅ Implemented
1. **Form Input** - 24 fields dengan validasi lengkap
2. **Backend Processor** - Insert data ke database dengan error handling
3. **Auto-fill Product Code** - Otomatis terisi saat pilih Product Name
4. **Time Validation** - Finish Time harus > Start Time
5. **Duplicate Prevention** - DO Number harus unik
6. **Confirmation Dialog** - Konfirmasi sebelum save
7. **Dynamic Base URL** - Auto-detect localhost vs hosting
8. **Separate PGN & RWL** - Form terpisah untuk masing-masing lokasi

### 🔄 In Progress
1. **View List Data** - Halaman untuk melihat data yang sudah diinput
2. **Edit/Delete** - Fitur untuk edit atau hapus data manual
3. **Dashboard Integration** - Update query dashboard menggunakan combined view

### 📋 TODO
1. **Export to Excel** - Export data local loading
2. **Bulk Import** - Import dari Excel untuk entry massal
3. **Audit Log** - Track siapa yang input/edit/delete
4. **Approval Workflow** - (jika diperlukan di masa depan)

---

## Database Schema

### Table: t_local_loading_manual

| Column | Type | Nullable | Description |
|--------|------|----------|-------------|
| ID | int | NO | Primary Key (Identity) |
| TransactionDate | datetime | NO | Tanggal transaksi |
| GateInTime | datetime | YES | Waktu masuk gate |
| FillingStartTime | datetime | NO | Waktu mulai isi |
| FillingFinishTime | datetime | NO | Waktu selesai isi |
| GateOutTime | datetime | YES | Waktu keluar gate |
| LoadingBayCode | nvarchar(50) | YES | Kode bay |
| LoadingBayName | nvarchar(100) | YES | Nama bay |
| LoadingArmCode | nvarchar(50) | YES | Kode arm |
| LoadingArmName | nvarchar(100) | YES | Nama arm |
| do_number | nvarchar(max) | NO | Nomor DO (unik) |
| do_type | nvarchar(50) | YES | Tipe DO |
| SIOD_ID | nvarchar(50) | YES | ID SIOD |
| ActualQuantity | float | NO | Quantity aktual (Liter) |
| UOM | nvarchar(50) | YES | Unit (default: L) |
| SealNumber | nvarchar(max) | YES | Nomor segel |
| vehicle_number | nvarchar(58) | NO | Nomor kendaraan |
| TruckCompartmentNumber | nvarchar(10) | YES | Jumlah kompartemen |
| DriverName | nvarchar(100) | YES | Nama driver |
| DriverCode | nvarchar(58) | YES | Kode driver |
| DO_Quantity | float | YES | Quantity DO |
| Product_Code | nvarchar(58) | YES | Kode produk |
| Product_Name | nvarchar(100) | NO | Nama produk |
| Product_density | float | YES | Density produk |
| Product_temperature | float | YES | Temperature produk |
| Product_pressure | float | YES | Pressure produk |
| created_at | datetime | YES | Waktu input |
| created_by | nvarchar(100) | YES | User yang input |
| updated_at | datetime | YES | Waktu update |
| updated_by | nvarchar(100) | YES | User yang update |
| is_deleted | bit | YES | Soft delete flag |
| remarks | nvarchar(500) | YES | Catatan tambahan |

**Indexes:**
- `IX_local_loading_TransactionDate` on TransactionDate
- `IX_local_loading_do_number` on do_number

---

## Testing Checklist

### Pre-Deployment
- [ ] Run SQL scripts di server PGN (103.133.223.213)
- [ ] Run SQL scripts di server RWL (103.133.223.212)
- [ ] Verify tabel `t_local_loading_manual` created
- [ ] Verify view `v_dash_actual_lo_combined` created
- [ ] Update dashboard queries to use combined view

### Functional Testing
- [ ] Akses form input PGN
- [ ] Akses form input RWL
- [ ] Test validasi required fields
- [ ] Test validasi time sequence
- [ ] Test validasi duplicate DO number
- [ ] Test product code auto-fill
- [ ] Submit data dan verify success message
- [ ] Check data masuk ke database
- [ ] Verify dashboard menampilkan data Remote + Local

### Integration Testing
- [ ] Input 1 transaksi local loading di PGN
- [ ] Verify total realization di dashboard bertambah
- [ ] Input 1 transaksi local loading di RWL
- [ ] Verify total realization di dashboard bertambah
- [ ] Compare dashboard total dengan data lapangan

---

## Support

Jika ada pertanyaan atau issue:
1. Check console browser untuk JavaScript errors
2. Check PHP error log untuk backend errors
3. Verify database connection
4. Verify SQL Server permissions

## Version History

- **v1.0.0** (2025-11-05)
  - Initial release
  - Form input untuk PGN & RWL
  - Backend processor dengan validasi
  - SQL scripts untuk table & view
  - Documentation

---

**Created by:** GitHub Copilot  
**Date:** November 5, 2025  
**License:** Internal Use Only
