CREATE TABLE `commercial_documents` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`type` text NOT NULL,
	`document_no` text NOT NULL,
	`date` text NOT NULL,
	`due_date` text DEFAULT '' NOT NULL,
	`client_id` integer,
	`project_id` integer,
	`subject` text NOT NULL,
	`items_json` text DEFAULT '[]' NOT NULL,
	`tax_mode` text DEFAULT 'exclude' NOT NULL,
	`tax_rate` real DEFAULT 11 NOT NULL,
	`discount_type` text DEFAULT 'percent' NOT NULL,
	`discount_value` real DEFAULT 0 NOT NULL,
	`subtotal` real DEFAULT 0 NOT NULL,
	`discount_amount` real DEFAULT 0 NOT NULL,
	`tax_amount` real DEFAULT 0 NOT NULL,
	`total` real DEFAULT 0 NOT NULL,
	`paid_amount` real DEFAULT 0 NOT NULL,
	`status` text DEFAULT 'Draft' NOT NULL,
	`source_document_id` integer,
	`terms` text DEFAULT '' NOT NULL,
	`notes` text DEFAULT '' NOT NULL,
	`created_at` text DEFAULT 'CURRENT_TIMESTAMP' NOT NULL,
	FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX `commercial_documents_document_no_unique` ON `commercial_documents` (`document_no`);