Skip to content

Commit

Permalink
email reports: add gavebrev status
Browse files Browse the repository at this point in the history
  • Loading branch information
maximbaz committed Dec 17, 2024
1 parent 9f3f7a5 commit 03ef7ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ members_confirmed as (
and d.recipient = 'Giv Effektivt'
and c.created_at <@ tstzrange(year_from, year_to, '[)')
),
active_gavebrev as (
select
tin
from
const
cross join gavebrev g
inner join donor_with_sensitive_info p on g.donor_id = p.id
where
started_at <= year_from
and stopped_at > year_from
group by
tin
),
email_to_tin_guess as (
select distinct on (email)
p.email,
Expand Down Expand Up @@ -93,6 +106,7 @@ select
and d.tin is not null as is_tin_guessed,
length(coalesce(a.tin, b.tin, d.tin, '')) = 8 as is_company,
e.tin is not null as is_member,
f.tin is not null as has_gavebrev,
coalesce(a.transfer_id, b.transfer_id, c.transfer_id) as transfer_id,
a.total as amount_tax_deductible,
nullif(coalesce(b.total, 0) + coalesce(c.total, 0), 0) as amount_not_tax_deductible,
Expand All @@ -106,6 +120,7 @@ from
and coalesce(a.transfer_id, b.transfer_id) is not distinct from c.transfer_id
left join email_to_tin_guess d on coalesce(a.email, b.email, c.email) = d.email
left join members_confirmed e on coalesce(a.tin, b.tin, d.tin) = e.tin
left join active_gavebrev f on coalesce(a.tin, b.tin) = f.tin
order by
email,
tin,
Expand Down
49 changes: 29 additions & 20 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,25 @@ CREATE VIEW giveffektivt.donor_with_sensitive_info AS
WHERE (deleted_at IS NULL);


--
-- Name: gavebrev; Type: VIEW; Schema: giveffektivt; Owner: -
--

CREATE VIEW giveffektivt.gavebrev AS
SELECT id,
donor_id,
status,
type,
amount,
minimal_income,
started_at,
stopped_at,
created_at,
updated_at
FROM giveffektivt._gavebrev
WHERE (deleted_at IS NULL);


--
-- Name: annual_email_report; Type: VIEW; Schema: giveffektivt; Owner: -
--
Expand Down Expand Up @@ -478,6 +497,13 @@ CREATE VIEW giveffektivt.annual_email_report AS
JOIN giveffektivt.donation d_1 ON ((d_1.donor_id = p.id)))
JOIN giveffektivt.charge c_1 ON ((c_1.donation_id = d_1.id)))
WHERE ((c_1.status = 'charged'::giveffektivt.charge_status) AND (d_1.recipient = 'Giv Effektivt'::giveffektivt.donation_recipient) AND (c_1.created_at <@ tstzrange(const.year_from, const.year_to, '[)'::text)))
), active_gavebrev AS (
SELECT p.tin
FROM ((const
CROSS JOIN giveffektivt.gavebrev g)
JOIN giveffektivt.donor_with_sensitive_info p ON ((g.donor_id = p.id)))
WHERE ((g.started_at <= const.year_from) AND (g.stopped_at > const.year_from))
GROUP BY p.tin
), email_to_tin_guess AS (
SELECT DISTINCT ON (p.email) p.email,
p.tin
Expand Down Expand Up @@ -517,15 +543,17 @@ CREATE VIEW giveffektivt.annual_email_report AS
((COALESCE(a.tin, b.tin) IS NULL) AND (d.tin IS NOT NULL)) AS is_tin_guessed,
(length(COALESCE(a.tin, b.tin, d.tin, ''::text)) = 8) AS is_company,
(e.tin IS NOT NULL) AS is_member,
(f.tin IS NOT NULL) AS has_gavebrev,
COALESCE(a.transfer_id, b.transfer_id, c.transfer_id) AS transfer_id,
a.total AS amount_tax_deductible,
NULLIF((COALESCE(b.total, (0)::numeric) + COALESCE(c.total, (0)::numeric)), (0)::numeric) AS amount_not_tax_deductible,
((COALESCE(a.total, (0)::numeric) + COALESCE(b.total, (0)::numeric)) + COALESCE(c.total, (0)::numeric)) AS amount_total
FROM ((((with_tax a
FROM (((((with_tax a
FULL JOIN with_tin_no_tax b ON (((NOT (a.tin IS DISTINCT FROM b.tin)) AND (a.email = b.email) AND (NOT (a.transfer_id IS DISTINCT FROM b.transfer_id)))))
FULL JOIN with_no_tin_no_tax c ON (((COALESCE(a.email, b.email) = c.email) AND (NOT (COALESCE(a.transfer_id, b.transfer_id) IS DISTINCT FROM c.transfer_id)))))
LEFT JOIN email_to_tin_guess d ON ((COALESCE(a.email, b.email, c.email) = d.email)))
LEFT JOIN members_confirmed e ON ((COALESCE(a.tin, b.tin, d.tin) = e.tin)))
LEFT JOIN active_gavebrev f ON ((COALESCE(a.tin, b.tin) = f.tin)))
ORDER BY COALESCE(a.email, b.email, c.email), COALESCE(a.tin, b.tin, d.tin), COALESCE(a.transfer_id, b.transfer_id, c.transfer_id);


Expand Down Expand Up @@ -554,25 +582,6 @@ CREATE VIEW giveffektivt.annual_tax_report_current_payments AS
GROUP BY p.tin;


--
-- Name: gavebrev; Type: VIEW; Schema: giveffektivt; Owner: -
--

CREATE VIEW giveffektivt.gavebrev AS
SELECT id,
donor_id,
status,
type,
amount,
minimal_income,
started_at,
stopped_at,
created_at,
updated_at
FROM giveffektivt._gavebrev
WHERE (deleted_at IS NULL);


--
-- Name: annual_tax_report_gavebrev_all_payments; Type: VIEW; Schema: giveffektivt; Owner: -
--
Expand Down

0 comments on commit 03ef7ad

Please sign in to comment.