-- ============================================
-- 采购-付款-物流-退库一体化流程 - 数据库迁移脚本
-- 版本：v1.0
-- 日期：2026-04-07
-- 遵循设计方案：采购-付款-物流-退库一体化流程设计方案.md
-- ============================================

-- ============================================
-- 第一部分：创建新表
-- ============================================

-- 表1：跨境物流公司表 (logistics_companies)
-- 设计方案章节：9.9 跨境物流公司表
CREATE TABLE IF NOT EXISTS logistics_companies (
  id SERIAL PRIMARY KEY,
  code TEXT UNIQUE NOT NULL,
  name TEXT NOT NULL,
  address TEXT,
  phone TEXT,
  email TEXT,
  quotation_description TEXT,
  status TEXT DEFAULT 'active',
  remark TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_logistics_companies_code ON logistics_companies(code);
CREATE INDEX IF NOT EXISTS idx_logistics_companies_status ON logistics_companies(status);

-- 表2：物流公司收款信息表 (logistics_company_payment_infos)
-- 设计方案章节：9.10 物流公司收款信息表
CREATE TABLE IF NOT EXISTS logistics_company_payment_infos (
  id SERIAL PRIMARY KEY,
  logistics_company_id INTEGER NOT NULL,
  account_name TEXT,
  account_number TEXT,
  bank_name TEXT,
  qr_code TEXT,
  is_default INTEGER DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (logistics_company_id) REFERENCES logistics_companies(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_lc_payment_infos_company ON logistics_company_payment_infos(logistics_company_id);

-- 表3：物流单表 (logistics_records)
-- 设计方案章节：9.4 物流单表
CREATE TABLE IF NOT EXISTS logistics_records (
  id SERIAL PRIMARY KEY,
  code TEXT UNIQUE NOT NULL,
  purchase_order_id INTEGER NOT NULL,
  
  ship_from TEXT DEFAULT 'Laos',
  
  logistics_company_id INTEGER,
  logistics_company TEXT,
  tracking_number TEXT,
  ship_date DATE,
  ship_location TEXT,
  estimated_arrival_date DATE,
  
  customs_arrival_date DATE,
  customs_clearance_date DATE,
  
  use_hub INTEGER DEFAULT 0,
  hub_arrival_date DATE,
  hub_receiver TEXT,
  hub_verified_quantity REAL,
  second_ship_date DATE,
  
  primary_freight REAL DEFAULT 0,
  primary_freight_currency TEXT DEFAULT 'CNY',
  primary_freight_status TEXT DEFAULT 'pending',
  primary_freight_document TEXT,
  
  secondary_freight REAL DEFAULT 0,
  secondary_freight_currency TEXT DEFAULT 'LAK',
  secondary_freight_status TEXT DEFAULT 'pending',
  driver_phone TEXT,
  cargo_weight REAL,
  transport_distance REAL,
  
  final_arrival_date DATE,
  final_location TEXT,
  
  status TEXT DEFAULT 'pending',
  remark TEXT,
  created_by TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (purchase_order_id) REFERENCES purchase_orders(id),
  FOREIGN KEY (logistics_company_id) REFERENCES logistics_companies(id)
);

CREATE INDEX IF NOT EXISTS idx_logistics_records_code ON logistics_records(code);
CREATE INDEX IF NOT EXISTS idx_logistics_records_order ON logistics_records(purchase_order_id);
CREATE INDEX IF NOT EXISTS idx_logistics_records_status ON logistics_records(status);
CREATE INDEX IF NOT EXISTS idx_logistics_records_company ON logistics_records(logistics_company_id);

-- 表4：验收单表 (verification_records)
-- 设计方案章节：9.5 验收单表
CREATE TABLE IF NOT EXISTS verification_records (
  id SERIAL PRIMARY KEY,
  code TEXT UNIQUE NOT NULL,
  purchase_order_id INTEGER NOT NULL,
  logistics_record_id INTEGER,
  
  verification_type TEXT DEFAULT 'direct',
  verification_date DATE NOT NULL,
  verifier TEXT NOT NULL,
  
  items TEXT,
  
  total_ordered REAL,
  total_received REAL,
  total_verified REAL,
  total_rejected REAL DEFAULT 0,
  
  project_id INTEGER,
  storage_type TEXT,
  
  status TEXT DEFAULT 'pending',
  remark TEXT,
  attachments TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (purchase_order_id) REFERENCES purchase_orders(id),
  FOREIGN KEY (logistics_record_id) REFERENCES logistics_records(id),
  FOREIGN KEY (project_id) REFERENCES projects(id)
);

CREATE INDEX IF NOT EXISTS idx_verification_records_code ON verification_records(code);
CREATE INDEX IF NOT EXISTS idx_verification_records_order ON verification_records(purchase_order_id);
CREATE INDEX IF NOT EXISTS idx_verification_records_status ON verification_records(status);

-- 表5：退库单表 (return_records)
-- 设计方案章节：9.6 退库单表
CREATE TABLE IF NOT EXISTS return_records (
  id SERIAL PRIMARY KEY,
  code TEXT UNIQUE NOT NULL,
  project_id INTEGER NOT NULL,
  
  return_type TEXT DEFAULT 'warehouse',
  return_date DATE NOT NULL,
  applicant TEXT NOT NULL,
  
  items TEXT,
  
  total_quantity REAL,
  total_amount REAL,
  
  cost_adjustment REAL DEFAULT 0,
  refund_amount REAL DEFAULT 0,
  
  status TEXT DEFAULT 'pending',
  remark TEXT,
  attachments TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (project_id) REFERENCES projects(id)
);

CREATE INDEX IF NOT EXISTS idx_return_records_code ON return_records(code);
CREATE INDEX IF NOT EXISTS idx_return_records_project ON return_records(project_id);
CREATE INDEX IF NOT EXISTS idx_return_records_status ON return_records(status);

-- 表6：材料价格历史表 (material_price_history)
-- 设计方案章节：9.7 材料价格历史表
CREATE TABLE IF NOT EXISTS material_price_history (
  id SERIAL PRIMARY KEY,
  product_id INTEGER NOT NULL,
  purchase_order_id INTEGER,
  supplier_id INTEGER,
  supplier_country TEXT,
  
  unit_price REAL NOT NULL,
  currency TEXT DEFAULT 'CNY',
  quantity REAL,
  purchase_date DATE NOT NULL,
  
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (product_id) REFERENCES products(id),
  FOREIGN KEY (purchase_order_id) REFERENCES purchase_orders(id),
  FOREIGN KEY (supplier_id) REFERENCES suppliers(id)
);

CREATE INDEX IF NOT EXISTS idx_material_price_history_product ON material_price_history(product_id);
CREATE INDEX IF NOT EXISTS idx_material_price_history_supplier ON material_price_history(supplier_id);
CREATE INDEX IF NOT EXISTS idx_material_price_history_date ON material_price_history(purchase_date);

-- 表7：项目材料库存表 (project_material_inventory)
-- 设计方案章节：9.8 项目材料库存表
CREATE TABLE IF NOT EXISTS project_material_inventory (
  id SERIAL PRIMARY KEY,
  project_id INTEGER NOT NULL,
  product_id INTEGER NOT NULL,
  product_name TEXT,
  unit TEXT,
  
  purchased_quantity REAL DEFAULT 0,
  received_quantity REAL DEFAULT 0,
  used_quantity REAL DEFAULT 0,
  returned_quantity REAL DEFAULT 0,
  current_quantity REAL DEFAULT 0,
  
  total_amount REAL DEFAULT 0,
  average_price REAL DEFAULT 0,
  
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (project_id) REFERENCES projects(id),
  FOREIGN KEY (product_id) REFERENCES products(id),
  UNIQUE(project_id, product_id)
);

CREATE INDEX IF NOT EXISTS idx_project_material_inventory_project ON project_material_inventory(project_id);
CREATE INDEX IF NOT EXISTS idx_project_material_inventory_product ON project_material_inventory(product_id);

-- ============================================
-- 第二部分：扩展现有表字段（SQLite不支持IF NOT EXISTS，需要手动处理）
-- ============================================

-- 扩展 purchase_orders 表
-- 设计方案章节：9.1 采购订单表
ALTER TABLE purchase_orders ADD COLUMN project_id INTEGER;
ALTER TABLE purchase_orders ADD COLUMN supplier_country TEXT DEFAULT 'Laos';
ALTER TABLE purchase_orders ADD COLUMN estimated_amount REAL DEFAULT 0;
ALTER TABLE purchase_orders ADD COLUMN paid_amount REAL DEFAULT 0;
ALTER TABLE purchase_orders ADD COLUMN contract_url TEXT;
ALTER TABLE purchase_orders ADD COLUMN quotation_url TEXT;
ALTER TABLE purchase_orders ADD COLUMN actual_delivery_date DATE;
ALTER TABLE purchase_orders ADD COLUMN remark TEXT;

-- 扩展 purchase_order_items 表
-- 设计方案章节：9.2 采购订单明细表
ALTER TABLE purchase_order_items ADD COLUMN received_quantity REAL DEFAULT 0;
ALTER TABLE purchase_order_items ADD COLUMN verified_quantity REAL DEFAULT 0;

-- 扩展 payment_plans 表
-- 设计方案章节：9.3 付款计划表
ALTER TABLE payment_plans ADD COLUMN stage TEXT;
ALTER TABLE payment_plans ADD COLUMN planned_date DATE;
ALTER TABLE payment_plans ADD COLUMN planned_amount REAL;
ALTER TABLE payment_plans ADD COLUMN planned_percentage REAL;
ALTER TABLE payment_plans ADD COLUMN actual_amount REAL DEFAULT 0;
ALTER TABLE payment_plans ADD COLUMN actual_date DATE;
ALTER TABLE payment_plans ADD COLUMN payment_request_id INTEGER;
ALTER TABLE payment_plans ADD COLUMN reminder_days INTEGER DEFAULT 3;
ALTER TABLE payment_plans ADD COLUMN remark TEXT;

-- 扩展 payment_requests 表
-- 设计方案章节：9.12 付款申请表扩展
ALTER TABLE payment_requests ADD COLUMN payment_type TEXT DEFAULT 'material';
ALTER TABLE payment_requests ADD COLUMN purchase_order_id INTEGER;
ALTER TABLE payment_requests ADD COLUMN logistics_company_id INTEGER;
ALTER TABLE payment_requests ADD COLUMN logistics_document_url TEXT;
ALTER TABLE payment_requests ADD COLUMN driver_phone TEXT;
ALTER TABLE payment_requests ADD COLUMN cargo_weight REAL;
ALTER TABLE payment_requests ADD COLUMN transport_distance REAL;

-- 扩展 suppliers 表
-- 设计方案章节：9.13 供应商表扩展
ALTER TABLE suppliers ADD COLUMN supply_category TEXT;
ALTER TABLE suppliers ADD COLUMN country TEXT;
ALTER TABLE suppliers ADD COLUMN address TEXT;
ALTER TABLE suppliers ADD COLUMN phone TEXT;
ALTER TABLE suppliers ADD COLUMN email TEXT;
ALTER TABLE suppliers ADD COLUMN status TEXT DEFAULT 'active';

-- 扩展 purchase_requests 表
-- 设计方案章节：9.14 采购申请表扩展
ALTER TABLE purchase_requests ADD COLUMN expected_date DATE;

-- 创建新索引
CREATE INDEX IF NOT EXISTS idx_purchase_orders_project ON purchase_orders(project_id);
CREATE INDEX IF NOT EXISTS idx_purchase_orders_supplier_country ON purchase_orders(supplier_country);
CREATE INDEX IF NOT EXISTS idx_payment_requests_type ON payment_requests(payment_type);
CREATE INDEX IF NOT EXISTS idx_payment_requests_order ON payment_requests(purchase_order_id);
CREATE INDEX IF NOT EXISTS idx_suppliers_country ON suppliers(country);
CREATE INDEX IF NOT EXISTS idx_suppliers_status ON suppliers(status);
