2021年Mysql个税计算公式,自定义函数

1、目的

根据最新的新个税计算,自定义mysql函数,实现根据传入参数计算出当月应缴的税额。

2、代码

CREATE DEFINER=`root`@`%` FUNCTION `TaxFunc`( 
	-- 应发工资
	salary decimal(14,4), 
	-- 起征税额
	base decimal(14,4), 	 
	-- 保险
	insurance DECIMAL(14,4), 
	-- 公积金  
	housing_fund DECIMAL(14,4), 
	-- 专项扣除
	special_item DECIMAL(14,4), 
	-- 月份
	mon int) RETURNS decimal(14,4)
BEGIN
	DECLARE Jan DECIMAL(14,4);
	DECLARE Feb DECIMAL(14,4);
	DECLARE Mar DECIMAL(14,4);
	DECLARE Apr DECIMAL(14,4);
	DECLARE May DECIMAL(14,4);
	DECLARE Jun DECIMAL(14,4);
	DECLARE Jul DECIMAL(14,4);
	DECLARE Aug DECIMAL(14,4);
	DECLARE Sep DECIMAL(14,4);
	DECLARE Oct DECIMAL(14,4);
	DECLARE Nov DECIMAL(14,4);
	DECLARE Dece DECIMAL(14,4);
	DECLARE income DECIMAL(14,4);
	
	-- 1月
	-- 所得额 = (工资 - 起征税额 - 社保 - 公积金 - 专项扣除)* 月份
	set income = (salary-base-insurance-housing_fund-special_item) * 1;
	set Jan = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920))))));
	set Jan = IF(salary <= base, 0, Jan);

	-- 2月
	set income = (salary-base-insurance-housing_fund-special_item) * 2;
	set Feb = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan;
	set Feb = IF(salary <= base, 0, Feb);
	
	-- 3月
	set income = (salary-base-insurance-housing_fund-special_item) * 3;
	set Mar = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb;
	set Mar = IF(salary <= base, 0, Mar);
	
	-- 4月
	set income = (salary-base-insurance-housing_fund-special_item) * 4;
	set Apr = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar;
	set Apr = IF(salary <= base, 0, Apr);
	
	-- 5月
	set income = (salary-base-insurance-housing_fund-special_item) * 5;
	set May = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr;
	set May = IF(salary <= base, 0, May);
	
	-- 6月
	set income = (salary-base-insurance-housing_fund-special_item) * 6;
	set Jun = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May;
	set Jun = IF(salary <= base, 0, Jun);
	
	-- 7月
	set income = (salary-base-insurance-housing_fund-special_item) * 7;
	set Jul = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May - Jun;
	set Jul = IF(salary <= base, 0, Jul);
	
	-- 8月
	set income = (salary-base-insurance-housing_fund-special_item) * 8;
	set Aug = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May - Jun - Jul;
	set Aug = IF(salary <= base, 0, Aug);
	
	-- 9月
	set income = (salary-base-insurance-housing_fund-special_item) * 9;
	set Sep = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May - Jun - Jul - Aug;
	set Sep = IF(salary <= base, 0, Sep);
	
	-- 10月
	set income = (salary-base-insurance-housing_fund-special_item) * 10;
	set Oct = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May - Jun - Jul - Aug - Sep;
	set Oct = IF(salary <= base, 0, Oct);
	
	-- 11月
	set income = (salary-base-insurance-housing_fund-special_item) * 11;
	set Nov = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May - Jun - Jul - Aug - Sep - Oct;
	set Nov = IF(salary <= base, 0, Nov);
	
	-- 12月
	set income = (salary-base-insurance-housing_fund-special_item) * 12;
	set Dece = IF(income <= 36000,income*0.03,IF(income <=144000,income*0.1-2520,IF(income<=300000,income*0.2-16920,IF(income<=420000,income*0.25-31920,IF(income<=660000,income*0.3-52920,IF(income<=960000,income*0.35-85920,income*0.45-181920)))))) - Jan - Feb - Mar - Apr - May - Jun - Jul - Aug - Sep - Oct - Nov;
	set Dece = IF(salary <= base, 0, Dece);
	
	
RETURN IF(mon = 1,Jan,IF(mon=2,Feb,IF(mon=3,Mar,IF(mon=4,Apr,IF(mon=5,May,IF(mon=6,Jun,IF(mon=7,Jul,IF(mon=8,Aug,IF(mon=9,Sep,IF(mon=10,Oct,IF(mon=11,Nov,Dece)))))))))));
END

3、测试

如果:一个员工每个月是32000元工资,保险扣3000元,公积金扣1500元,专项附加扣2200元。那么1月到4月的每个月应缴个税金额:

SELECT
	TaxFunc ( 32000, 5000, 3000, 1500, 2200, 1 ) 一月,
	TaxFunc ( 32000, 5000, 3000, 1500, 2200, 2 ) 二月,
	TaxFunc ( 32000, 5000, 3000, 1500, 2200, 3 ) 三月,
	TaxFunc ( 32000, 5000, 3000, 1500, 2200, 4 ) 四月;

结果:

posted @ 2021-04-06 14:25  直角漫步  阅读(675)  评论(0编辑  收藏  举报