feat: 增值税计算,sql,添加状态筛选的功能

This commit is contained in:
bai.jingfeng 2024-03-29 16:05:42 +08:00
parent 38fdd9c332
commit c2604631a8

View File

@ -6,14 +6,16 @@ WITH RECURSIVE
SELECT SELECT
purchaser_id AS company_id, -- 发票进项取购买方公司id,作为本公司id标识 purchaser_id AS company_id, -- 发票进项取购买方公司id,作为本公司id标识
authentication_date AS "date", -- 发票进项取认证日期,作为计算月份的来源 authentication_date AS "date", -- 发票进项取认证日期,作为计算月份的来源
tax_amount tax_amount,
"state"
FROM FROM
invoice_input invoice_input
UNION ALL UNION ALL
SELECT SELECT
purchaser_id AS company_id, -- 完税凭证,同发票进项字段 purchaser_id AS company_id, -- 完税凭证,同发票进项字段
authentication_date AS "date", -- 完税凭证,同发票进项字段 authentication_date AS "date", -- 完税凭证,同发票进项字段
tax_amount tax_amount,
"state"
FROM FROM
invoice_receipt invoice_receipt
), ),
@ -22,11 +24,12 @@ WITH RECURSIVE
SELECT SELECT
seller_id AS company_id, -- 发票销项取销售方id,作为本公司id标识 seller_id AS company_id, -- 发票销项取销售方id,作为本公司id标识
"Invoicing_date" AS "date", -- 发票销项取开票日期,作为计算月份的来源 "Invoicing_date" AS "date", -- 发票销项取开票日期,作为计算月份的来源
tax_amount tax_amount,
"state"
FROM FROM
invoice_output invoice_output
), ),
-- 聚合A_INPUT: 1. 筛选出日期在'2022-01-01'之后的数据 2. 按照 company_id 和 date 聚合计算后的月份 "MONTH" 3. 获取对应月份的进项税额 input_tax_amount -- 聚合A_INPUT: 1. 按照 company_id 和 date 聚合计算后的月份 "MONTH" 2. 获取对应月份的进项税额 input_tax_amount
B_INPUT AS ( B_INPUT AS (
SELECT SELECT
company_id, company_id,
@ -35,12 +38,13 @@ WITH RECURSIVE
FROM FROM
A_INPUT A_INPUT
WHERE WHERE
"date" >= '2022-01-01' "date" >= '2022-01-01' -- 筛选出日期在'2022-01-01'之后的数据
AND "state" = '1' -- 筛选出所有状态为正常的记录, 正常-1,作废-2
GROUP BY GROUP BY
company_id, company_id,
"MONTH" "MONTH"
), ),
-- 聚合B_OUTPUT: 1.筛选出日期在'2022-01-01'之后的数据 2. 按照 company_id 和 date 聚合计算后的月份 "MONTH" 3. 获取对应月份的销项税额 output_tax_amount -- 聚合B_OUTPUT: 1. 按照 company_id 和 date 聚合计算后的月份 "MONTH" 2. 获取对应月份的销项税额 output_tax_amount
B_OUTPUT AS ( B_OUTPUT AS (
SELECT SELECT
company_id, company_id,
@ -49,7 +53,8 @@ WITH RECURSIVE
FROM FROM
A_OUTPUT A_OUTPUT
WHERE WHERE
"date" >= '2022-01-01' "date" >= '2022-01-01' -- 筛选出日期在'2022-01-01'之后的数据
AND "state" = '1' -- 筛选出所有状态为正常的记录, 正常-1,作废-2
GROUP BY GROUP BY
company_id, company_id,
"MONTH" "MONTH"
@ -66,7 +71,7 @@ WITH RECURSIVE
FULL JOIN B_OUTPUT ON B_INPUT.company_id = B_OUTPUT.company_id FULL JOIN B_OUTPUT ON B_INPUT.company_id = B_OUTPUT.company_id
AND B_INPUT."MONTH" = B_OUTPUT."MONTH" AND B_INPUT."MONTH" = B_OUTPUT."MONTH"
), ),
-- JOIN 两个表,B_INPUT, B_OUTPUT: 1. 聚合两个表,保留全部记录 2. 聚合后的表,如果某个表的对应取值字段为null,则置为0 -- 计算出中间值 excessive_tax_amount, 用于之后的计算
D_INTIAL_TAX AS ( D_INTIAL_TAX AS (
SELECT SELECT
company_id, company_id,