How to configure periods dynamically according to number of persons?

Viewed 70

I'm making a database for my expense tracker/splitter (offers possibility to add/edit/remove Transactions). Depending on number of Persons, when you add a new transaction you must specify who paid and to what amount. Then it creates Payments row for each person within Transactions.

You must configure each person's salary then it will calculate the pro-rata for each person for unpaid expenses (IsPayed flag). Once every person is ready to settle they must create a period (start and end date). All transactions between those dates will be tagged as paid (IsPayed = true) and the application will assume they are settle and frozen in time. There will be a FK between Transaction and Period to specify from which period a transaction was paid.

I want to add a relation between Period(Id) and SalaryConfiguration(IdGroup); it would specify SalaryConfiguration for said period, but I can't since IdGroup is not a primary key. I do not know how a period can be dynamic on the number of persons and specific configurations. How can I link SalaryConfiguration and Periods?

Database DDL:

CREATE TABLE "Transactions" (
    "Id"    INTEGER NOT NULL UNIQUE,
    "FkIdCategories"    INTEGER NOT NULL,
    "Date"  TEXT NOT NULL,
    "Name"  TEXT NOT NULL,
    "Description"   TEXT,
    "Cost"  INTEGER NOT NULL,
    "IsPayed"   INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY("Id" AUTOINCREMENT),
    CONSTRAINT "Fk_Categories_Transactions" FOREIGN KEY("FkIdCategories") REFERENCES "Categories"("Id")
);

CREATE TABLE "Categories" (
    "Id"    INTEGER NOT NULL UNIQUE,
    "Name"  TEXT NOT NULL UNIQUE,
    PRIMARY KEY("Id" AUTOINCREMENT)
);

CREATE TABLE "Payments" (
    "Id"    INTEGER NOT NULL UNIQUE,
    "FkIdPerson"    INTEGER NOT NULL,
    "FkIdTransaction"   INTEGER NOT NULL,
    "Amount"    INTEGER NOT NULL,
    CONSTRAINT "Fk_Persons_Payments" FOREIGN KEY("FkIdPerson") REFERENCES "Persons"("Id"),
    CONSTRAINT "Fk_Transactions_Payments" FOREIGN KEY("FkIdTransaction") REFERENCES "Transactions"("Id"),
    PRIMARY KEY("Id" AUTOINCREMENT)
);

CREATE TABLE "Persons" (
    "Id"    INTEGER NOT NULL UNIQUE,
    "Name"  TEXT NOT NULL,
    PRIMARY KEY("Id" AUTOINCREMENT)
);

CREATE TABLE "SalaryConfigurations" (
    "Id"    INTEGER NOT NULL UNIQUE,
    "IdGroup"   INTEGER NOT NULL,
    "FkIdPerson"    INTEGER NOT NULL,
    "Salary"    INTEGER NOT NULL,
    "SaveDate"  TEXT NOT NULL,
    CONSTRAINT "Fk_Persons_SalaryConfiguration" FOREIGN KEY("FkIdPerson") REFERENCES "Persons"("Id"),
    PRIMARY KEY("Id" AUTOINCREMENT)
);

CREATE TABLE "Periods" (
    "Id"    INTEGER NOT NULL UNIQUE,
    "StartDate" TEXT NOT NULL,
    "EndDate"   TEXT NOT NULL,
    "FkIDSalaryConfigurationGroup"  INTEGER NOT NULL,
    PRIMARY KEY("Id" AUTOINCREMENT)
);

enter image description here

1 Answers

You can link the two tables together with another table that contains SalaryConfigurations(Id) and Periods(Id).

Related