<commit_before>﻿CREATE PROCEDURE [levy].[ProcessDeclarationsTransactions]
AS

-- Create Declarations

INSERT INTO levy.TransactionLine
select mainUpdate.* from
	(
	select 
			x.AccountId,
			GetDate() as TransactionDate,
			x.SubmissionId,
			null as PaymentId,
			x.SubmissionDate,
			1 as TransactionType,
			(y.LevyDueYTD - isnull(LAG(y.LevyDueYTD) OVER(Partition by y.empref order by y.SubmissionDate asc, y.submissionId),0)) * y.EnglishFraction as Amount,
			x.EmpRef
		FROM 
			[levy].[GetLevyDeclarations] x
	
		left join 
			[levy].[GetLevyDeclarations] y on y.lastsubmission = 1 and y.id = x.id
		where
			y.LevyDueYTD is not null
	) mainUpdate
	inner join (
		select submissionId from levy.LevyDeclaration
	EXCEPT
		select SubmissionId from levy.TransactionLine where TransactionType = 1
	) dervx on dervx.submissionId = mainUpdate.SubmissionId

--create top up
	
INSERT INTO levy.TransactionLine
	select mainUpdate.* from
	(
select 
		x.AccountId,
		GetDate() as TransactionDate,
		x.SubmissionId,
		null as PaymentId,
		x.SubmissionDate,
		2 as TransactionType,
		(y.LevyDueYTD - isnull(LAG(y.LevyDueYTD) OVER(Partition by y.empref order by y.SubmissionDate asc, y.submissionId),0)) * y.TopUpPercentage as Amount,
		x.EmpRef
	FROM 
		[levy].[GetLevyDeclarations] x
	
	left join 
		[levy].[GetLevyDeclarations] y on y.lastsubmission = 1 and y.id = x.id
	where
		y.LevyDueYTD is not null
	) mainUpdate
	inner join (
		select submissionId from levy.LevyDeclaration
	EXCEPT
		select SubmissionId from levy.TransactionLine where TransactionType = 2
	) dervx on dervx.submissionId = mainUpdate.SubmissionId
<commit_msg>Correction to ProcessDeclarationsTransactions<commit_after>﻿CREATE PROCEDURE [levy].[ProcessDeclarationsTransactions]
AS

-- Create Declarations

INSERT INTO levy.TransactionLine
select mainUpdate.* from
	(
	select 
			x.AccountId,
			GetDate() as TransactionDate,
			x.SubmissionId,
			null as PaymentId,
			x.SubmissionDate,
			1 as TransactionType,
			(y.LevyDueYTD - isnull(LAG(y.LevyDueYTD) OVER(Partition by y.empref order by y.SubmissionDate asc, y.submissionId),0)) * y.EnglishFraction as Amount,
			x.EmpRef
		FROM 
			[levy].[GetLevyDeclarations] x
	
		left join 
			[levy].[GetLevyDeclarations] y on y.lastsubmission = 1 and y.id = x.id
		where
			y.LevyDueYTD is not null
	) mainUpdate
	inner join (
		select submissionId from levy.LevyDeclaration
	EXCEPT
		select SubmissionId from levy.TransactionLine where TransactionType = 1
	) dervx on dervx.submissionId = mainUpdate.SubmissionId

--create top up
	
INSERT INTO levy.TransactionLine
	select mainUpdate.* from
	(
select 
		x.AccountId,
		GetDate() as TransactionDate,
		x.SubmissionId,
		null as PaymentId,
		x.SubmissionDate,
		2 as TransactionType,
		((y.LevyDueYTD - isnull(LAG(y.LevyDueYTD) OVER(Partition by y.empref order by y.SubmissionDate asc, y.submissionId),0)) * y.EnglishFraction) * y.TopUpPercentage as Amount,
		x.EmpRef
	FROM 
		[levy].[GetLevyDeclarations] x
	
	left join 
		[levy].[GetLevyDeclarations] y on y.lastsubmission = 1 and y.id = x.id
	where
		y.LevyDueYTD is not null
	) mainUpdate
	inner join (
		select submissionId from levy.LevyDeclaration
	EXCEPT
		select SubmissionId from levy.TransactionLine where TransactionType = 2
	) dervx on dervx.submissionId = mainUpdate.SubmissionId

{'fraction_non_alphanumeric': 0.08171912832929783, 'fraction_numerical': 0.004842615012106538, 'mean_word_length': 4.11609907120743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6993278', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1153, 'n_words': 301}