Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
-- Deploy numerus:update_expense_payment_status to pg
|
|
|
|
-- requires: roles
|
|
|
|
-- requires: schema_numerus
|
|
|
|
-- requires: expense
|
|
|
|
-- requires: payment
|
|
|
|
-- requires: expense_payment
|
|
|
|
-- requires: available_expense_status
|
|
|
|
-- requires: available_payment_status
|
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
|
|
set search_path to numerus, public;
|
|
|
|
|
|
|
|
create or replace function update_expense_payment_status(pid integer, eid integer, amount_cents integer) returns void as
|
|
|
|
$$
|
|
|
|
update payment
|
|
|
|
set payment_status = case when expense.amount > amount_cents or exists (select 1 from expense_payment as ep where ep.expense_id = expense.expense_id and payment_id <> pid) then 'partial' else 'complete' end
|
|
|
|
from expense
|
|
|
|
where expense.expense_id = eid
|
|
|
|
and payment_id = pid
|
|
|
|
;
|
|
|
|
|
|
|
|
update expense
|
2024-08-11 01:22:37 +00:00
|
|
|
set expense_status = case
|
|
|
|
when paid_amount >= expense.amount then 'paid'
|
|
|
|
when paid_amount = 0 then 'pending'
|
|
|
|
else 'partial' end
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
from (
|
2024-08-11 01:22:37 +00:00
|
|
|
select coalesce (sum(payment.amount), 0) as paid_amount
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
from expense_payment
|
|
|
|
join payment using (payment_id)
|
2024-08-11 01:22:37 +00:00
|
|
|
where expense_payment.expense_id = eid
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
) as payment
|
2024-08-11 01:22:37 +00:00
|
|
|
where expense.expense_id = eid
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
;
|
|
|
|
$$
|
|
|
|
language sql
|
|
|
|
;
|
|
|
|
|
|
|
|
revoke execute on function update_expense_payment_status(integer, integer, integer) from public;
|
|
|
|
grant execute on function update_expense_payment_status(integer, integer, integer) to invoicer;
|
|
|
|
grant execute on function update_expense_payment_status(integer, integer, integer) to admin;
|
|
|
|
|
|
|
|
commit;
|