简体   繁体   中英

Need some suggestions for database design for Invoices in a multi-tenant app

I need some guidance on designing the schema for invoices in a multi-tenant application. I have a table called EmployeePay which holds all the information required to generate an invoice. The invoice table would have the invoice number, invoice created date and VAT rate. I am thinking to create a Sequence object for each Tenant to generate an invoice number.

EmployeePay Table: EmployeeID, Hours, Rate, InvoiceID (FK)

Invoice Table: InvoiceID (PK) (Identity), InvoiceNumber, InvoiceDate, VATRate, TenantID

Is it okay to have hundreds of Sequence objects in a database, as I'll have to create one for each tenant? I'll also have to create same amount of stored procedures which returns the next invoice number (I prefer a separate stored procedure for each tenant rather than having one large stored procedure with hundreds of choices in a select case statement).

Another concern is, is it theoretical to insert into the master table ( Invoice ) based on the transaction table ( EmployeePay ) and then use its primary key( InvoiceID ) to update the transaction table?

Thanks in advance.

First make sure the relationship either this is one to many or many to many. If you are considering one employee that will have many invoices then its one to many relationship and you can create your table as under:

EmployeePay Table: EmployeeID (PK) (Identity), Hours, Rate

Invoice Table: InvoiceID (PK) (Identity), EmployeeID (FK), InvoiceNumber, InvoiceDate, VATRate, TenantID

EDIT:

I don't know which database you are using but for increment sequence check:

  1. for MySQL check this LINK .
  2. If you are using Oracle then check this LINK

I would suggest you to create another table can be called as InvoiceNumber, this will contain InvoiceNumberId(Int) , TenantId (Fk) , CurrentSequenceNumber(Int) .

Significance of CurrentSequenceNumber is that it will be simple integer number which can be used to generate next Invoicenumber. InvoiceNumberId will be a Identity columns for Primary key purpose (you may or may not have it).

Structure of the Table will look like below.

在此处输入图片说明

Now you need to create only One Stored Procedure which will take input parameter as TenantId and will have responsiblity to generate next Invoice number by reading CurrentSequenceNumber from above table.

For example if we need to generate new Invoice Id for Tenant with id as 15 then SP will have your Business logic I am assuming Just creating a String with "Inv-" as prefix with incremented value of CurrentSequenceNumber so output of Procedure will be.

Inv-0009

Then after generation of this number SP will increment value to 9 for InvoiceNumberId 3.

So everything will be managed by Single table and Single procedure only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM