is Payment table needed when you have an invoice table like this? - sql

this is my invoice table:
Invoice Table:
invoice_id
creation_date
due_date
payment_date
status enum('not paid','paid','expired')
user_id
total_price
I wonder if it's Useful to have a payment table in order to record user payments for invoices.
payment table can be like this:
payment_id
payment_date
invoice_id
price_paid
status enum('successful', 'not successful')

If you want to allow more than one payment for an invoices, then yes a payment table would be useful.
It's also a good idea to keep your database normalized as much as possible.

Yes. Sometimes they might not pay in full or overpay, or pay in multiple transactions. Having the separate table allows you to keep track of this.
With just one table, all you can see is the original amount and the amount remaining to be paid, but not the history of payments.

Could a payment span multiple invoices?
Could an invoice be payed via multiple payments?
My guess is you will need a payment table and probably an account table and some sort of transaction table as well.

Many accounting applications have invoice_header, invoice_line, account_ledger, ledger_application and account tables.
The account_ledger table typically contains the items on the customer/vendor accounts. These items could be invoices, payments, or whatever.
The ledger_application table holds the information on the many-to-many relationship between payments and invoices.
The invoice_header table contains all information associated with an invoice which does not belong in the account_ledger table.
The invoice_line table has the data pertaining to the individual items/quantities/amounts on the invoice.
The account table stores data about the customer/vendor such as address, contact info, etc.

Related

Whether to separate tables if part of a table has a relation to another

I’m learning SQL and have a basic question. I have a transactions table which contains payments and commissions.
A payment:
ID
Date
Amount
Type
Referrer ID
Balance
number
date
number
‘commission’ or ‘payment’
number
number
A commission has the same fields as payments, plus two additional fields: qualified and customerPaymentId
Qualified
Customer Payment ID
boolean (presumably 0 or 1)
number
Whenever a payment is made, the balance decreases, and vice versa whenever a commission occurs.
I have a user table too, where a user can have payments but not commissions. Should I separate the transactions table to be 2 tables (commissions and payments), or keep a single transactions table where the user table has a relation with it (rather than a relation to a separate payments table)?
Any advice or pointers would be great too, thank you.
UPDATE:
Sorry, I don’t think I have been very clear at all here. This is the transactions table, that contains both payments and commissions:
ID
Date
Amount
Type
Referrer ID
Balance
Qualified
Customer Payment Id
1
20/1/22
5.00
COMMISSION
5454
5.00
0
0001
2
28/1/22
4.00
COMMISSION
5454
9.00
0
0002
1
20/1/22
5.00
PAYMENT
5454
4.00
NULL
NULL
1
20/1/22
4.00
PAYMENT
5454
0.00
NULL
NULL
The type of ‘commission’ or ‘payment’ is there to determine whether a record is a commission or payment. A commission would have data for the ‘qualified’ and ‘customer payment id’ columns (but payments wouldn’t).
The Customer Payment ID column actually represents a different type of payment - a payment that a user makes for a product. Conversely, the transaction payment is a different payment that pays towards an affiliate’s balance.
The commission.balance and payment.balance represent an overall balance - commissions add to a user’s total balance while payments deduct from it.
To give some better context, this is for an affiliate system - when a customer buys a product, a payment (different - NOT a transaction payment; it’s from another table) is made, with a Customer Payment ID. An affiliate might have earned a commission from that sale, hence a commission record is made (in the transactions table). The amount they are owed is added to the affiliate’s balance. After 3 months, the commission becomes qualified, and the affiliate is paid their commission at the end of that month, hence a payment is made (a payment from the business to the affiliate), which deducts from the affiliate’s balance. It’s not simple to explain, but that is the gist of it if that makes somewhat sense :) I may not have constructed this the best way.
It's a little difficult to follow what's happening, but it seems you have a bunch of things which have some overlap in their fields but aren't actually the same. But sometimes you want to look at them all together.
I would suggest making separate tables for each kind of thing, and then a create a view to bring them all together.
For example...
create view transactions as
select created_on, amount, 'COMMISSION' as type, balance
from commissions
union all
select created_on, amount, 'PAYMENT' as type, balance
from payments
We use union instead of union all for performance. union removes duplicate rows which means the database has to do more work and operations are slower and more complicated. There are no duplicates in this instance.
Demonstration

How would I structure a database for taking sales orders?

I am helping someone build a grocery deliver service. Very simple site and order process. The problem I am having is knowing how to come up with schema for the orders. The order would have contact information, but would also need to have all of the products they ordered. Since it can/will be different for each persons order, how would I go about designing this? Would I just have a products ordered field with a string list of all the items? Or would I need multiple linked tables?
Thanks!
You need three tables:
a) Products - Contains the product details
b) Orders - Contains the order header, contact information etc
c) Product_Orders - is a table with two columns: product_id and order_id, that bind the orders to the products, and a quantity field and unit price.
You could have a customers table, but ideally information like the delivery address, etc, would be attached to the order so that if the user changes his address it will not affect your order history.
For the same reason unit price must be in Product_Orders, so that if a product price changes it does not affect previous orders.
Product table
with name and price of all products.
Customer table
with name, address, phone etc...
Order table
with entries for each order: order id, cusomer id, date etc
Order Items table
with all ordered items, with link to id in ordertable, product table, quantity, price, etc...

splited payments type SQL

Hi i have this tables
Sales
id_sales (pk)
amount
payments
payment_id (pk)
payment_type_id (fk)
id_sales (fk)
payments_type
payment_type_id (pk)
detail (example:cash, debit card, credit card, direct credit)
how can i manage that on sale can have two types of payments, for example: a sale is $100.00 i pay $50.00 with cash an another $50.00 with debit card, and the tables record it.
do you think it's a better approach to put two payment_type_id on the table payments and have it like that and when is just one payment let the other in NULL
Add a new "Amount" column to the "payments" table. Now you can store the split amounts with the payments.
When you have a split amount, you insert 1 record to sales and 2+ records to payments, one for each payment per sales transaction. You don't need to put null records into payments, only a record for each legitimate payment. You can now even put multiple payments of the same type if someone paid with 2 different credit cards etc.
All the payments relate to 1 sales transaction (id_sales). Each payment has 1 payment type and you don't have to put in blank records or NULL into any column.

Select results get multiplied on joining

I'm using an SQL Server database which has Order table contains a foreign key called invoice_id, this attribute belongs to Invoice table. Also, I have another table called "Receiptwhich also includesinvoice_idandamount_paid` attributes.
Many items can be assigned to the same invoice (customer may make many orders at once), also many receipts can be assigned to the same invoice (customer may make different payments to the same invoice as for example they can pay 50% and then pay the rest later).
So the problem I'm facing is when I try to select the total paid amounts from the Receipt table taking the order_id as a condition, the result will be multiplied according to the number of orders that have the same invoice_id
For example, customer A placed three orders at once, each order cost is 100 USD, which should be 300 USD in the invoice and he already paid that invoice. Now if I query the Receipt table for the paid amounts, the result will be 900 USD (300 USD * 3 orders), which is obviously incorrect.
I'm stuck at this issue, I believe there are some mistakes in my database logic, so please provide me your suggestions to solve this problem and also what should do with the database if the logic is incorrect.
Below is the query i'm using to get the result:
select sum(r.amount_paid), o.invoice_id from RECEIPT r, INVOICE i, ORDER o
where r.invoice_id = i.invoice_id
and o.invoice_id = i.invoice_id
group by o.invoice_id;
Here's three answers to three slightly different questions you might ask of your database.
Amount paid per invoice
If you are trying to get the total amount paid per invoice, all you need is
SELECT SUM(Amount_Paid) Total_Paid,
Invoice_ID
FROM Receipt
GROUP BY Invoice_ID
Amount paid per order
If you want to know the total paid per order, this is not quite possible in your data model as you have described it. If a invoice has three orders on it, and the invoice is only partly paid, there is no way to tell which of the the orders is paid and which is not.
You need some additional data structure that indicates how payments are applied to orders within an invoice, e.g. an Allocation or Split table.
Amount paid on invoices that pertain to one or more orders
On the other hand, if you want to know how much payment has been received on invoices that contain one or more order IDs, you could write this:
SELECT SUM(Amount_Paid) Total_Paid,
Invoice_ID
FROM Receipt
WHERE Invoice_ID IN (SELECT Invoice_ID
FROM Order
WHERE Order_ID IN (1,2,3,4)) --Edit these IDs for your specific case
GROUP BY Invoice_ID
Notice none of the queries above required any joins, so no multiplying :)
Please, try this:
SELECT SUM(r.amount_paid), r.invoice_id FROM RECEIPT r
JOIN INVOICE i ON r.invoice_id = i.invoice_id
JOIN ORDER o ON r.invoice_id = o.invoice_id AND r.order_id = o.order_Id
GROUP BY r.invoice_id;

Create one invoice from many purchase orders

In Openerp v7, I have sent two (or more) purchase orders to the same supplier and he sent me one invoice for those orders.
Is there a way or a module to create one invoice grouping the two orders.
Note 1: I don't want to merge the orders, but just one unique invoice (as I received) related to both.
Note 2 : the orders concern service type product so, there are no pickings to be invoiced.
Thank you
Ok,
one solution is to set the invoice_method to "Based on Purchase Order lines" for this type or orders.
Then, when we receive the supplier invoice, we can select the concerned purchase order lines and create an invoice in accordance to the one received.
Not exactly group nor merge but it can do the trick