IO-2543 Schema changes for AR calculation.
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
- function:
|
||||
name: jobs_ar_summary
|
||||
schema: public
|
||||
- function:
|
||||
name: search_bills
|
||||
schema: public
|
||||
|
||||
@@ -2673,6 +2673,9 @@
|
||||
- table:
|
||||
name: ioevents
|
||||
schema: public
|
||||
- table:
|
||||
name: job_ar_schema
|
||||
schema: public
|
||||
- table:
|
||||
name: job_conversations
|
||||
schema: public
|
||||
@@ -3812,6 +3815,7 @@
|
||||
- referral_source
|
||||
- referral_source_extra
|
||||
- regie_number
|
||||
- remove_from_ar
|
||||
- ro_number
|
||||
- scheduled_completion
|
||||
- scheduled_delivery
|
||||
@@ -4093,6 +4097,7 @@
|
||||
- referral_source
|
||||
- referral_source_extra
|
||||
- regie_number
|
||||
- remove_from_ar
|
||||
- ro_number
|
||||
- scheduled_completion
|
||||
- scheduled_delivery
|
||||
|
||||
33
hasura/migrations/1705693552101_run_sql_migration/down.sql
Normal file
33
hasura/migrations/1705693552101_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,33 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF jobs
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.shopid,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- p.total_payments,
|
||||
-- j.clm_total - p.total_payments as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- sum(p.amount) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid ;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
31
hasura/migrations/1705693552101_run_sql_migration/up.sql
Normal file
31
hasura/migrations/1705693552101_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF jobs
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.shopid,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
p.total_payments,
|
||||
j.clm_total - p.total_payments as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
sum(p.amount) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid ;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE "public"."job_ar_schema";
|
||||
@@ -0,0 +1 @@
|
||||
CREATE TABLE "public"."job_ar_schema" ("id" uuid NOT NULL, "ro_number" text, "clm_total" numeric NOT NULL, "total_payments" numeric NOT NULL DEFAULT 0, "balance" numeric NOT NULL DEFAULT 0, PRIMARY KEY ("id") );
|
||||
34
hasura/migrations/1705693896379_run_sql_migration/down.sql
Normal file
34
hasura/migrations/1705693896379_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,34 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- DROP FUNCTION public.jobs_ar_summary;
|
||||
--
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- p.total_payments,
|
||||
-- j.clm_total - p.total_payments as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- sum(p.amount) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid ;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
32
hasura/migrations/1705693896379_run_sql_migration/up.sql
Normal file
32
hasura/migrations/1705693896379_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
DROP FUNCTION public.jobs_ar_summary;
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
p.total_payments,
|
||||
j.clm_total - p.total_payments as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
sum(p.amount) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid ;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
32
hasura/migrations/1705694146809_run_sql_migration/down.sql
Normal file
32
hasura/migrations/1705694146809_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- p.total_payments,
|
||||
-- j.clm_total - p.total_payments as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid ;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
30
hasura/migrations/1705694146809_run_sql_migration/up.sql
Normal file
30
hasura/migrations/1705694146809_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
p.total_payments,
|
||||
j.clm_total - p.total_payments as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid ;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
32
hasura/migrations/1705694176838_run_sql_migration/down.sql
Normal file
32
hasura/migrations/1705694176838_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- p.total_payments,
|
||||
-- j.clm_total - p.total_payments as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid ;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
30
hasura/migrations/1705694176838_run_sql_migration/up.sql
Normal file
30
hasura/migrations/1705694176838_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
p.total_payments,
|
||||
j.clm_total - p.total_payments as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid ;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
32
hasura/migrations/1705696451631_run_sql_migration/down.sql
Normal file
32
hasura/migrations/1705696451631_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- coalesce (p.total_payments,0) as total_payments,
|
||||
-- j.clm_total - coalesce (p.total_payments,0) as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid ;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
30
hasura/migrations/1705696451631_run_sql_migration/up.sql
Normal file
30
hasura/migrations/1705696451631_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
coalesce (p.total_payments,0) as total_payments,
|
||||
j.clm_total - coalesce (p.total_payments,0) as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid ;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- alter table "public"."jobs" add column "remove_from_ar" boolean
|
||||
-- not null default 'false';
|
||||
@@ -0,0 +1,2 @@
|
||||
alter table "public"."jobs" add column "remove_from_ar" boolean
|
||||
not null default 'false';
|
||||
33
hasura/migrations/1705698426997_run_sql_migration/down.sql
Normal file
33
hasura/migrations/1705698426997_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,33 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- coalesce (p.total_payments,0) as total_payments,
|
||||
-- j.clm_total - coalesce (p.total_payments,0) as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid
|
||||
-- where j.remove_from_ar = false;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
31
hasura/migrations/1705698426997_run_sql_migration/up.sql
Normal file
31
hasura/migrations/1705698426997_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
coalesce (p.total_payments,0) as total_payments,
|
||||
j.clm_total - coalesce (p.total_payments,0) as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid
|
||||
where j.remove_from_ar = false;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- alter table "public"."job_ar_schema" add column "date_invoiced" timestamptz
|
||||
-- null;
|
||||
@@ -0,0 +1,2 @@
|
||||
alter table "public"."job_ar_schema" add column "date_invoiced" timestamptz
|
||||
null;
|
||||
34
hasura/migrations/1705698534883_run_sql_migration/down.sql
Normal file
34
hasura/migrations/1705698534883_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,34 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- j.date_invoiced,
|
||||
-- coalesce (p.total_payments,0) as total_payments,
|
||||
-- j.clm_total - coalesce (p.total_payments,0) as balance
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid
|
||||
-- where j.remove_from_ar = false and j.date_invoiced is not null;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
32
hasura/migrations/1705698534883_run_sql_migration/up.sql
Normal file
32
hasura/migrations/1705698534883_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
j.date_invoiced,
|
||||
coalesce (p.total_payments,0) as total_payments,
|
||||
j.clm_total - coalesce (p.total_payments,0) as balance
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid
|
||||
where j.remove_from_ar = false and j.date_invoiced is not null;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
34
hasura/migrations/1705698593644_run_sql_migration/down.sql
Normal file
34
hasura/migrations/1705698593644_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,34 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- coalesce (p.total_payments,0) as total_payments,
|
||||
-- j.clm_total - coalesce (p.total_payments,0) as balance,
|
||||
-- j.date_invoiced
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid
|
||||
-- where j.remove_from_ar = false and j.date_invoiced is not null;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
32
hasura/migrations/1705698593644_run_sql_migration/up.sql
Normal file
32
hasura/migrations/1705698593644_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
coalesce (p.total_payments,0) as total_payments,
|
||||
j.clm_total - coalesce (p.total_payments,0) as balance,
|
||||
j.date_invoiced
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid
|
||||
where j.remove_from_ar = false and j.date_invoiced is not null;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
34
hasura/migrations/1705698876975_run_sql_migration/down.sql
Normal file
34
hasura/migrations/1705698876975_run_sql_migration/down.sql
Normal file
@@ -0,0 +1,34 @@
|
||||
-- Could not auto-generate a down migration.
|
||||
-- Please write an appropriate down migration for the SQL below:
|
||||
-- CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
-- RETURNS SETOF job_ar_schema
|
||||
-- LANGUAGE plpgsql
|
||||
-- STABLE
|
||||
-- AS $function$
|
||||
-- BEGIN
|
||||
--
|
||||
-- RETURN query
|
||||
-- select
|
||||
-- j.id,
|
||||
-- j.ro_number,
|
||||
-- j.clm_total,
|
||||
-- coalesce (p.total_payments,0) as total_payments,
|
||||
-- j.clm_total - coalesce (p.total_payments,0) as balance,
|
||||
-- j.date_invoiced
|
||||
-- from
|
||||
-- jobs j
|
||||
-- left join (
|
||||
-- select
|
||||
-- p.jobid,
|
||||
-- coalesce (sum(p.amount),0) as total_payments
|
||||
-- from
|
||||
-- payments p
|
||||
-- group by
|
||||
-- p.jobid
|
||||
-- ) p on
|
||||
-- j.id = p.jobid
|
||||
-- where j.remove_from_ar = false and j.date_invoiced is not null and balance > 0;
|
||||
--
|
||||
--
|
||||
-- END
|
||||
-- $function$;
|
||||
32
hasura/migrations/1705698876975_run_sql_migration/up.sql
Normal file
32
hasura/migrations/1705698876975_run_sql_migration/up.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
CREATE OR REPLACE FUNCTION public.jobs_ar_summary ()
|
||||
RETURNS SETOF job_ar_schema
|
||||
LANGUAGE plpgsql
|
||||
STABLE
|
||||
AS $function$
|
||||
BEGIN
|
||||
|
||||
RETURN query
|
||||
select
|
||||
j.id,
|
||||
j.ro_number,
|
||||
j.clm_total,
|
||||
coalesce (p.total_payments,0) as total_payments,
|
||||
j.clm_total - coalesce (p.total_payments,0) as balance,
|
||||
j.date_invoiced
|
||||
from
|
||||
jobs j
|
||||
left join (
|
||||
select
|
||||
p.jobid,
|
||||
coalesce (sum(p.amount),0) as total_payments
|
||||
from
|
||||
payments p
|
||||
group by
|
||||
p.jobid
|
||||
) p on
|
||||
j.id = p.jobid
|
||||
where j.remove_from_ar = false and j.date_invoiced is not null and balance > 0;
|
||||
|
||||
|
||||
END
|
||||
$function$;
|
||||
Reference in New Issue
Block a user