DROP TABLE IF EXISTS `projectFinanceMonthly`;

CREATE TABLE IF NOT EXISTS `projectFinanceMonthly` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `projectId` INT NOT NULL,
    `month` DATE NOT NULL,
    `opening_balance` DECIMAL(15, 2) DEFAULT 0,
    `payment` DECIMAL(15, 2) DEFAULT 0,
    `wip` DECIMAL(15, 2) DEFAULT 0,
    `expenses` DECIMAL(15, 2) DEFAULT 0,
    `cost` DECIMAL(15, 2) DEFAULT 0,
    `nsr` DECIMAL(15, 2) DEFAULT 0,
    `margin` DECIMAL(15, 5) DEFAULT 0,
    `is_confirmed` TINYINT(1) NOT NULL DEFAULT 0,
    `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updatedAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (`projectId`) REFERENCES `projects`(`id`) ON DELETE CASCADE,
    UNIQUE KEY `unique_project_month` (`projectId`, `month`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;