-- Receivables table
CREATE TABLE IF NOT EXISTS `receivables` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
  `order_id` VARCHAR(64) COMMENT '订单ID',
  `contract_id` VARCHAR(64) COMMENT '合同ID',
  `customer_id` VARCHAR(64) COMMENT '客户ID',
  `receivable_amount` DECIMAL(15,2) COMMENT '应收金额',
  `received_amount` DECIMAL(15,2) DEFAULT 0 COMMENT '已收金额',
  `due_date` DATE COMMENT '到期日期',
  `status` VARCHAR(32) DEFAULT 'pending' COMMENT '状态: pending/partial/completed',
  `description` VARCHAR(500) COMMENT '描述',
  `org_id` VARCHAR(64) COMMENT '组织ID',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_receivables_customer` (`customer_id`),
  KEY `idx_receivables_contract` (`contract_id`),
  KEY `idx_receivables_status` (`status`),
  KEY `idx_receivables_due_date` (`due_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='应收管理';

-- Receipts table
CREATE TABLE IF NOT EXISTS `receipts` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
  `customer_id` VARCHAR(64) COMMENT '客户ID',
  `total_amount` DECIMAL(15,2) COMMENT '收款总额',
  `receipt_date` DATE COMMENT '收款日期',
  `receipt_method` VARCHAR(32) COMMENT '收款方式',
  `status` VARCHAR(32) DEFAULT 'processed' COMMENT '状态',
  `org_id` VARCHAR(64) COMMENT '组织ID',
  `created_by` VARCHAR(64) COMMENT '创建人',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_receipts_customer` (`customer_id`),
  KEY `idx_receipts_date` (`receipt_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='收款记录';

-- Payments table
CREATE TABLE IF NOT EXISTS `payments` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
  `contract_id` VARCHAR(64) COMMENT '合同ID',
  `payment_amount` DECIMAL(15,2) COMMENT '支付金额',
  `payment_date` DATE COMMENT '支付日期',
  `payment_method` VARCHAR(32) COMMENT '支付方式',
  `status` VARCHAR(32) DEFAULT 'processed' COMMENT '状态',
  `org_id` VARCHAR(64) COMMENT '组织ID',
  `created_by` VARCHAR(64) COMMENT '创建人',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_payments_contract` (`contract_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支出记录';

-- Financial vouchers table
CREATE TABLE IF NOT EXISTS `financial_vouchers` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
  `voucher_type` VARCHAR(32) COMMENT '凭证类型',
  `contract_id` VARCHAR(64) COMMENT '合同ID',
  `order_id` VARCHAR(64) COMMENT '订单ID',
  `amount` DECIMAL(15,2) COMMENT '金额',
  `voucher_date` DATE COMMENT '凭证日期',
  `description` TEXT COMMENT '描述',
  `reference_id` VARCHAR(64) COMMENT '关联ID',
  `org_id` VARCHAR(64) COMMENT '组织ID',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_vouchers_contract` (`contract_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='财务凭证';

-- Receipt allocations table
CREATE TABLE IF NOT EXISTS `receipt_allocations` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键ID',
  `receipt_id` VARCHAR(64) COMMENT '收款ID',
  `receivable_id` VARCHAR(64) COMMENT '应收ID',
  `allocated_amount` DECIMAL(15,2) COMMENT '分配金额',
  `org_id` VARCHAR(64) COMMENT '组织ID',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_allocations_receipt` (`receipt_id`),
  KEY `idx_allocations_receivable` (`receivable_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='收款分配';
