-- Tabel Template Sampling
CREATE TABLE sampling_templates (
    SamplingTemplateID INT PRIMARY KEY AUTO_INCREMENT,
    SamplingTemplateName VARCHAR(100),
    SamplingTemplateDescription TEXT,
    SamplingTemplateIsActive BOOLEAN DEFAULT TRUE,
    SamplingTemplateCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    SamplingTemplateCreatedUserID INT,
    SamplingTemplateUpdatedAt DATETIME,
    SamplingTemplateUpdatedUserID INT,
    SamplingTemplateDeletedAt DATETIME,
    SamplingTemplateDeletedUserID INT
);

-- Tabel Template Analisis
CREATE TABLE analysis_templates (
    AnalysisTemplateID INT PRIMARY KEY AUTO_INCREMENT,
    AnalysisTemplateName VARCHAR(100),
    AnalysisTemplateDescription TEXT,
    AnalysisTemplateIsLabAnalysis BOOLEAN DEFAULT TRUE,
    AnalysisTemplateIsActive BOOLEAN DEFAULT TRUE,
    AnalysisTemplateCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    AnalysisTemplateCreatedUserID INT,
    AnalysisTemplateUpdatedAt DATETIME,
    AnalysisTemplateUpdatedUserID INT,
    AnalysisTemplateDeletedAt DATETIME,
    AnalysisTemplateDeletedUserID INT
);

-- Tabel Parameter Analisis
CREATE TABLE analysis_parameters (
    AnalysisParameterID INT PRIMARY KEY AUTO_INCREMENT,
    AnalysisParameterCode VARCHAR(20),
    AnalysisParameterName VARCHAR(100),
    AnalysisParameterTestingMethod VARCHAR(100),
    AnalysisParameterUnit VARCHAR(20),
    AnalysisParameterStandardMethodNumber VARCHAR(50),
    AnalysisParameterPrice DECIMAL(10,2),
    AnalysisParameterIsActive BOOLEAN DEFAULT TRUE,
    AnalysisParameterCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    AnalysisParameterCreatedUserID INT,
    AnalysisParameterUpdatedAt DATETIME,
    AnalysisParameterUpdatedUserID INT,
    AnalysisParameterDeletedAt DATETIME,
    AnalysisParameterDeletedUserID INT
);

-- Tabel Template Parameter
CREATE TABLE template_parameters (
    TemplateParameterID INT PRIMARY KEY AUTO_INCREMENT,
    AnalysisTemplateID INT,
    AnalysisParameterID INT,
    TemplateParameterIsRequired BOOLEAN DEFAULT FALSE,
    TemplateParameterCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    TemplateParameterCreatedUserID INT,
    TemplateParameterUpdatedAt DATETIME,
    TemplateParameterUpdatedUserID INT,
    TemplateParameterDeletedAt DATETIME,
    TemplateParameterDeletedUserID INT
);

-- Tabel Permintaan Layanan
CREATE TABLE service_requests (
    ServiceRequestID INT PRIMARY KEY AUTO_INCREMENT,
    ServiceRequestCode VARCHAR(20) UNIQUE,
    CustomerID INT,
    ServiceRequestType ENUM('sampling_analysis', 'analysis_only'),
    AnalysisTemplateID INT,
    ServiceRequestSamplingDate DATE,
    ServiceRequestSamplingLocation TEXT,
    ServiceRequestGPSCoordinates VARCHAR(50),
    ServiceRequestStatus ENUM('draft', 'submitted', 'approved', 'rejected', 'in_progress', 'completed'),
    ServiceRequestSamplingOfficerID INT,
    ServiceRequestTestingOfficerID INT,
    ServiceRequestTotalAmount DECIMAL(10,2),
    ServiceRequestDPAmount DECIMAL(10,2),
    ServiceRequestDPStatus ENUM('unpaid', 'paid') DEFAULT 'unpaid',
    ServiceRequestNotes TEXT,
    ServiceRequestCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    ServiceRequestCreatedUserID INT,
    ServiceRequestUpdatedAt DATETIME,
    ServiceRequestUpdatedUserID INT,
    ServiceRequestDeletedAt DATETIME,
    ServiceRequestDeletedUserID INT
);

-- Tabel Detail Parameter Permintaan
CREATE TABLE request_parameters (
    RequestParameterID INT PRIMARY KEY AUTO_INCREMENT,
    ServiceRequestID INT,
    AnalysisParameterID INT,
    RequestParameterPrice DECIMAL(10,2),
    RequestParameterStatus ENUM('pending', 'in_progress', 'completed', 'rejected'),
    RequestParameterResultValue VARCHAR(50),
    RequestParameterResultUnit VARCHAR(20),
    RequestParameterIsCompliant BOOLEAN,
    RequestParameterNotes TEXT,
    RequestParameterCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    RequestParameterCreatedUserID INT,
    RequestParameterUpdatedAt DATETIME,
    RequestParameterUpdatedUserID INT,
    RequestParameterDeletedAt DATETIME,
    RequestParameterDeletedUserID INT
);

-- Tabel Quotation
CREATE TABLE quotations (
    QuotationID INT PRIMARY KEY AUTO_INCREMENT,
    QuotationCode VARCHAR(20) UNIQUE,
    ServiceRequestID INT,
    QuotationIssueDate DATE,
    QuotationValidUntil DATE,
    QuotationStatus ENUM('draft', 'sent', 'approved', 'rejected'),
    QuotationSubtotal DECIMAL(10,2),
    QuotationTaxPercentage DECIMAL(5,2),
    QuotationTaxAmount DECIMAL(10,2),
    QuotationTotalAmount DECIMAL(10,2),
    QuotationTermsAndConditions TEXT,
    QuotationNotes TEXT,
    QuotationCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    QuotationCreatedUserID INT,
    QuotationUpdatedAt DATETIME,
    QuotationUpdatedUserID INT,
    QuotationDeletedAt DATETIME,
    QuotationDeletedUserID INT
);

-- Tabel Peralatan Sampling
CREATE TABLE sampling_equipment (
    SamplingEquipmentID INT PRIMARY KEY AUTO_INCREMENT,
    SamplingEquipmentName VARCHAR(100),
    SamplingEquipmentCode VARCHAR(50),
    SamplingEquipmentIsAvailable BOOLEAN DEFAULT TRUE,
    SamplingEquipmentLastCalibrationDate DATE,
    SamplingEquipmentNextCalibrationDate DATE,
    SamplingEquipmentNotes TEXT,
    SamplingEquipmentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    SamplingEquipmentCreatedUserID INT,
    SamplingEquipmentUpdatedAt DATETIME,
    SamplingEquipmentUpdatedUserID INT,
    SamplingEquipmentDeletedAt DATETIME,
    SamplingEquipmentDeletedUserID INT
);

-- Tabel Peralatan yang Digunakan dalam Sampling
CREATE TABLE request_equipment (
    RequestEquipmentID INT PRIMARY KEY AUTO_INCREMENT,
    ServiceRequestID INT,
    SamplingEquipmentID INT,
    RequestEquipmentUsageDate DATE,
    RequestEquipmentReturnDate DATE,
    RequestEquipmentNotes TEXT,
    RequestEquipmentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    RequestEquipmentCreatedUserID INT,
    RequestEquipmentUpdatedAt DATETIME,
    RequestEquipmentUpdatedUserID INT,
    RequestEquipmentDeletedAt DATETIME,
    RequestEquipmentDeletedUserID INT
);

-- Tabel Master Komponen Harga
CREATE TABLE price_components (
    PriceComponentID INT PRIMARY KEY AUTO_INCREMENT,
    PriceComponentCode VARCHAR(20),
    PriceComponentName VARCHAR(100),
    PriceComponentDescription TEXT,
    PriceComponentIsActive BOOLEAN DEFAULT TRUE,
    PriceComponentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    PriceComponentCreatedUserID INT,
    PriceComponentUpdatedAt DATETIME,
    PriceComponentUpdatedUserID INT,
    PriceComponentDeletedAt DATETIME,
    PriceComponentDeletedUserID INT
);

-- Tabel Master Template Quotation
CREATE TABLE quotation_templates (
    QuotationTemplateID INT PRIMARY KEY AUTO_INCREMENT,
    QuotationTemplateName VARCHAR(100),
    QuotationTemplateDescription TEXT,
    QuotationTemplateHeader TEXT,
    QuotationTemplateFooter TEXT,
    QuotationTemplateTerms TEXT,
    QuotationTemplateIsActive BOOLEAN DEFAULT TRUE,
    QuotationTemplateCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    QuotationTemplateCreatedUserID INT,
    QuotationTemplateUpdatedAt DATETIME,
    QuotationTemplateUpdatedUserID INT,
    QuotationTemplateDeletedAt DATETIME,
    QuotationTemplateDeletedUserID INT
);

-- Tabel Master Harga Default
CREATE TABLE default_prices (
    DefaultPriceID INT PRIMARY KEY AUTO_INCREMENT,
    PriceComponentID INT,
    DefaultPriceValue DECIMAL(10,2),
    DefaultPriceStartDate DATE,
    DefaultPriceEndDate DATE,
    DefaultPriceIsActive BOOLEAN DEFAULT TRUE,
    DefaultPriceCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    DefaultPriceCreatedUserID INT,
    DefaultPriceUpdatedAt DATETIME,
    DefaultPriceUpdatedUserID INT,
    DefaultPriceDeletedAt DATETIME,
    DefaultPriceDeletedUserID INT
);

-- Tabel Master Diskon
CREATE TABLE discount_rules (
    DiscountRuleID INT PRIMARY KEY AUTO_INCREMENT,
    DiscountRuleName VARCHAR(100),
    DiscountRuleType ENUM('percentage', 'fixed_amount'),
    DiscountRuleValue DECIMAL(10,2),
    DiscountRuleMinAmount DECIMAL(10,2),
    DiscountRuleMaxAmount DECIMAL(10,2),
    DiscountRuleStartDate DATE,
    DiscountRuleEndDate DATE,
    DiscountRuleIsActive BOOLEAN DEFAULT TRUE,
    DiscountRuleCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    DiscountRuleCreatedUserID INT,
    DiscountRuleUpdatedAt DATETIME,
    DiscountRuleUpdatedUserID INT,
    DiscountRuleDeletedAt DATETIME,
    DiscountRuleDeletedUserID INT
);

-- Contoh data template sampling
INSERT INTO sampling_templates (SamplingTemplateName, SamplingTemplateDescription) VALUES
('Air Limbah Industri', 'Template untuk sampling air limbah industri'),
('Kualitas Udara Ambien', 'Template untuk sampling kualitas udara'),
('Air Permukaan', 'Template untuk sampling air sungai/danau');

-- Contoh data parameter analisis
INSERT INTO analysis_parameters (AnalysisParameterCode, AnalysisParameterName, AnalysisParameterUnit, AnalysisParameterPrice, AnalysisParameterTestingMethod) VALUES
('BOD5', 'Biochemical Oxygen Demand', 'mg/L', 250000, 'SNI 6989.72:2009'),
('COD', 'Chemical Oxygen Demand', 'mg/L', 200000, 'SNI 6989.2:2009'),
('TSS', 'Total Suspended Solid', 'mg/L', 150000, 'SNI 06-6989.3-2004'),
('PH', 'Derajat Keasaman', '-', 100000, 'SNI 06-6989.11-2004');

-- Contoh data permintaan layanan
INSERT INTO service_requests (ServiceRequestCode, CustomerID, ServiceRequestType, ServiceRequestSamplingDate, ServiceRequestSamplingLocation, ServiceRequestStatus, ServiceRequestTotalAmount) VALUES
('REQ001', 1, 'sampling_analysis', '2024-03-20', 'Plant A - Outlet IPAL', 'approved', 700000),
('REQ002', 2, 'analysis_only', '2024-03-21', 'Lab Internal', 'submitted', 450000),
('REQ003', 3, 'sampling_analysis', '2024-03-22', 'Intake Water Treatment', 'draft', 550000);

-- Contoh data parameter yang diminta
INSERT INTO request_parameters (ServiceRequestID, AnalysisParameterID, RequestParameterPrice) VALUES
(1, 1, 250000),
(1, 2, 200000),
(1, 3, 150000),
(1, 4, 100000),
(2, 1, 250000),
(2, 2, 200000),
(3, 3, 150000),
(3, 4, 100000);

-- Contoh data quotation
INSERT INTO quotations (QuotationCode, ServiceRequestID, QuotationIssueDate, QuotationValidUntil, QuotationStatus, QuotationTotalAmount) VALUES
('QUO001', 1, '2024-03-15', '2024-04-15', 'approved', 700000),
('QUO002', 2, '2024-03-16', '2024-04-16', 'sent', 450000),
('QUO003', 3, '2024-03-17', '2024-04-17', 'draft', 550000);

-- Insert data untuk Price Components
INSERT INTO price_components (
    PriceComponentID, PriceComponentCode, PriceComponentName,
    PriceComponentDescription, PriceComponentIsActive,
    PriceComponentCreatedUserID
) VALUES 
(1, 'SAMP', 'Biaya Sampling',
    'Biaya pengambilan sampel termasuk transportasi', TRUE, 1),
(2, 'TRANS', 'Biaya Transportasi Tambahan',
    'Biaya transportasi untuk lokasi di luar kota', TRUE, 1),
(3, 'EQUIP', 'Biaya Sewa Peralatan',
    'Biaya penggunaan peralatan khusus', TRUE, 1),
(4, 'RUSH', 'Biaya Rush Analysis',
    'Biaya tambahan untuk analisis cepat', TRUE, 1),
(5, 'ADMIN', 'Biaya Administrasi',
    'Biaya pembuatan laporan dan dokumentasi', TRUE, 1);

-- Insert data untuk Quotation Templates
INSERT INTO quotation_templates (
    QuotationTemplateID, QuotationTemplateName,
    QuotationTemplateDescription, QuotationTemplateHeader,
    QuotationTemplateTerms, QuotationTemplateIsActive,
    QuotationTemplateCreatedUserID
) VALUES 
(1, 'Template Standar',
    'Template quotation untuk pelanggan umum',
    'Dengan hormat,\nBerdasarkan permintaan pengujian yang diajukan, berikut kami sampaikan penawaran harga:',
    '1. Harga belum termasuk PPN 11%\n2. Pembayaran 50% di muka\n3. Hasil pengujian akan diserahkan dalam 7 hari kerja\n4. Penawaran berlaku 30 hari',
    TRUE, 1),
(2, 'Template Pelanggan Tetap',
    'Template quotation untuk pelanggan kontrak',
    'Dengan hormat,\nSesuai dengan kontrak kerjasama, berikut penawaran harga pengujian:',
    '1. Harga sudah termasuk PPN 11%\n2. Pembayaran 30 hari setelah invoice\n3. Hasil pengujian akan diserahkan dalam 5 hari kerja',
    TRUE, 1);

-- Insert data untuk Default Prices
INSERT INTO default_prices (
    DefaultPriceID, PriceComponentID, DefaultPriceValue,
    DefaultPriceStartDate, DefaultPriceEndDate,
    DefaultPriceIsActive, DefaultPriceCreatedUserID
) VALUES 
(1, 1, 500000.00, '2024-01-01', '2024-12-31', TRUE, 1),  -- Biaya Sampling
(2, 2, 1000000.00, '2024-01-01', '2024-12-31', TRUE, 1), -- Transport Luar Kota
(3, 3, 250000.00, '2024-01-01', '2024-12-31', TRUE, 1),  -- Sewa Peralatan
(4, 4, 750000.00, '2024-01-01', '2024-12-31', TRUE, 1),  -- Rush Analysis
(5, 5, 100000.00, '2024-01-01', '2024-12-31', TRUE, 1);  -- Biaya Admin

-- Insert data untuk Discount Rules
INSERT INTO discount_rules (
    DiscountRuleID, DiscountRuleName, DiscountRuleType,
    DiscountRuleValue, DiscountRuleMinAmount,
    DiscountRuleMaxAmount, DiscountRuleStartDate,
    DiscountRuleEndDate, DiscountRuleIsActive,
    DiscountRuleCreatedUserID
) VALUES 
(1, 'Diskon Bulk Order', 'percentage',
    10.00, 5000000.00, NULL,
    '2024-01-01', '2024-12-31', TRUE, 1),
(2, 'Diskon Pelanggan Tetap', 'percentage',
    15.00, 0.00, 10000000.00,
    '2024-01-01', '2024-12-31', TRUE, 1),
(3, 'Diskon Akhir Tahun', 'fixed_amount',
    500000.00, 2000000.00, NULL,
    '2024-12-01', '2024-12-31', TRUE, 1);
