CREATE TABLE IF NOT EXISTS `receipts` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_id` BIGINT UNSIGNED NOT NULL,
  `config_id` INT UNSIGNED NOT NULL,
  `receipt_number` VARCHAR(64) NOT NULL,
  `issue_date` DATE NOT NULL,
  `sale_date` DATE NOT NULL,
  `seller_data_json` JSON NOT NULL,
  `buyer_data_json` JSON DEFAULT NULL,
  `items_json` JSON NOT NULL,
  `total_net` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `total_gross` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `order_reference_value` VARCHAR(128) DEFAULT NULL,
  `created_by` INT UNSIGNED DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `receipts_number_unique` (`receipt_number`),
  KEY `receipts_order_idx` (`order_id`),
  KEY `receipts_config_idx` (`config_id`),
  KEY `receipts_issue_date_idx` (`issue_date`),
  CONSTRAINT `receipts_order_fk` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `receipts_config_fk` FOREIGN KEY (`config_id`) REFERENCES `receipt_configs` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
