@@ -29,57 +29,63 @@ WHERE f.Disabled = 0
2929 AND NOT EXISTS (SELECT 1 FROM FundMetadata fm WHERE fm .FundCode = f .FundCode AND fm .MetadataKeyId = @govPayKeyId);
3030PRINT ' GOV.UK Pay API keys set'
3131
32- -- 2. Account holders (simple INSERT, no CTEs)
32+ -- 2. Account holders -- use actual fund codes from the database
3333PRINT ' === Seeding account holders ==='
3434
3535DECLARE @sysUser INT = 0 ;
3636DECLARE @now DATETIME = GETDATE ();
3737
38- IF NOT EXISTS (SELECT 1 FROM AccountHolders WHERE AccountReference = ' CT000001' )
38+ -- Get the first fund code that exists in the database
39+ DECLARE @firstFund VARCHAR (5 );
40+ SELECT TOP 1 @firstFund = FundCode FROM Funds WHERE Disabled = 0 ORDER BY FundCode;
41+ PRINT ' Using fund code: ' + ISNULL (@firstFund, ' NONE' );
42+
43+ IF @firstFund IS NOT NULL AND NOT EXISTS (SELECT 1 FROM AccountHolders WHERE AccountReference LIKE ' AH%' )
44+ BEGIN
45+ DECLARE @j INT = 1 ;
46+ DECLARE @names TABLE (Idx INT IDENTITY , Title VARCHAR (10 ), Forename VARCHAR (50 ), Surname VARCHAR (50 ), Addr1 VARCHAR (60 ), Postcode VARCHAR (10 ));
47+ INSERT INTO @names VALUES
48+ (' Mr' ,' James' ,' Thompson' ,' 12 High Street' ,' OX1 1AA' ),(' Mrs' ,' Sarah' ,' Williams' ,' 34 Church Lane' ,' CT1 2BB' ),
49+ (' Ms' ,' Emma' ,' Johnson' ,' 56 Oak Road' ,' GU1 3CC' ),(' Mr' ,' David' ,' Brown' ,' 78 Mill Lane' ,' SO1 4DD' ),
50+ (' Mrs' ,' Lisa' ,' Davies' ,' 90 Station Road' ,' CM1 5EE' ),(' Mr' ,' Robert' ,' Wilson' ,' 11 Park Avenue' ,' RG1 6FF' ),
51+ (' Ms' ,' Helen' ,' Taylor' ,' 23 King Street' ,' EX1 7GG' ),(' Mr' ,' Michael' ,' Anderson' ,' 45 Queens Road' ,' NR1 8HH' ),
52+ (' Mrs' ,' Claire' ,' Thomas' ,' 67 Bridge Street' ,' IP1 9JJ' ),(' Mr' ,' Andrew' ,' Jackson' ,' 89 Market Place' ,' DT1 1KK' ),
53+ (' Ms' ,' Rachel' ,' White' ,' 10 Victoria Road' ,' SN1 2LL' ),(' Mr' ,' Peter' ,' Harris' ,' 22 Albert Street' ,' BA1 3MM' ),
54+ (' Mrs' ,' Karen' ,' Martin' ,' 44 George Lane' ,' SY1 4NN' ),(' Mr' ,' Steven' ,' Clark' ,' 66 William Road' ,' NG1 5PP' ),
55+ (' Ms' ,' Angela' ,' Lewis' ,' 88 Elizabeth Way' ,' LE1 6QQ' ),(' Mr' ,' Mark' ,' Walker' ,' 15 Edward Close' ,' CV1 7RR' ),
56+ (' Mrs' ,' Julie' ,' Hall' ,' 27 Charles Ave' ,' GL1 8SS' ),(' Mr' ,' Paul' ,' Allen' ,' 39 Henry Lane' ,' ST1 9TT' ),
57+ (' Ms' ,' Nicola' ,' Young' ,' 51 Mary Street' ,' CB1 1UU' ),(' Mr' ,' Simon' ,' King' ,' 63 Anne Road' ,' LN1 2VV' );
58+
59+ DECLARE @nCount INT ;
60+ SELECT @nCount = COUNT (* ) FROM @names;
61+
62+ -- Create account holders using actual fund codes from the database
63+ DECLARE @funds TABLE (Idx INT IDENTITY , FundCode VARCHAR (5 ));
64+ INSERT INTO @funds (FundCode) SELECT TOP 10 FundCode FROM Funds WHERE Disabled = 0 ORDER BY FundCode;
65+ DECLARE @fTotal INT ;
66+ SELECT @fTotal = COUNT (* ) FROM @funds;
67+
68+ WHILE @j <= 40
69+ BEGIN
70+ DECLARE @ref VARCHAR (30 ) = ' AH' + RIGHT (' 000000' + CAST (@j AS VARCHAR ), 6 );
71+ DECLARE @fIdx INT = (@j % @fTotal) + 1 ;
72+ DECLARE @nIdx INT = (@j % @nCount) + 1 ;
73+ DECLARE @fund VARCHAR (5 );
74+ SELECT @fund = FundCode FROM @funds WHERE Idx = @fIdx;
75+
76+ DECLARE @t VARCHAR (10 ), @fn VARCHAR (50 ), @sn VARCHAR (50 ), @a1 VARCHAR (60 ), @pc VARCHAR (10 );
77+ SELECT @t= Title, @fn= Forename, @sn= Surname, @a1= Addr1, @pc= Postcode FROM @names WHERE Idx = @nIdx;
78+
79+ INSERT INTO AccountHolders (AccountReference, FundCode, Title, Forename, Surname, AddressLine1, Postcode, CurrentBalance, PeriodDebit, PeriodCredit, RecordType, CreatedAt, CreatedByUserId)
80+ VALUES (@ref, @fund, @t, @fn, @sn, @a1, @pc, CAST (100 + (@j * 37 % 1700 ) AS DECIMAL (18 ,2 )), 0 , 0 , ' D' , @now, @sysUser);
81+
82+ SET @j = @j + 1 ;
83+ END
84+ PRINT ' Inserted 40 account holders'
85+ END
86+ ELSE IF @firstFund IS NULL
3987BEGIN
40- INSERT INTO AccountHolders (AccountReference, FundCode, Title, Forename, Surname, AddressLine1, AddressLine2, AddressLine3, Postcode, CurrentBalance, PeriodDebit, PeriodCredit, RecordType, CreatedAt, CreatedByUserId) VALUES
41- (' CT000001' ,' 01' ,' Mr' ,' James' ,' Thompson' ,' 12 High Street' ,' ' ,' Anytown' ,' OX1 1AA' ,1250 .00 ,0 ,0 ,' D' ,@now,@sysUser),
42- (' CT000002' ,' 01' ,' Mrs' ,' Sarah' ,' Williams' ,' 34 Church Lane' ,' Flat 2' ,' Riverdale' ,' CT1 2BB' ,875 .50 ,0 ,0 ,' D' ,@now,@sysUser),
43- (' CT000003' ,' 01' ,' Ms' ,' Emma' ,' Johnson' ,' 56 Oak Road' ,' ' ,' Hillview' ,' GU1 3CC' ,1680 .00 ,0 ,0 ,' D' ,@now,@sysUser),
44- (' CT000004' ,' 01' ,' Mr' ,' David' ,' Brown' ,' 78 Mill Lane' ,' Unit 4' ,' Lakeside' ,' SO1 4DD' ,450 .25 ,0 ,0 ,' D' ,@now,@sysUser),
45- (' CT000005' ,' 01' ,' Mrs' ,' Lisa' ,' Davies' ,' 90 Station Road' ,' ' ,' Greenfield' ,' CM1 5EE' ,1100 .00 ,0 ,0 ,' D' ,@now,@sysUser),
46- (' CT000006' ,' 01' ,' Mr' ,' Robert' ,' Wilson' ,' 11 Park Avenue' ,' Apt 3' ,' Woodlands' ,' RG1 6FF' ,925 .75 ,0 ,0 ,' D' ,@now,@sysUser),
47- (' CT000007' ,' 01' ,' Ms' ,' Helen' ,' Taylor' ,' 23 King Street' ,' ' ,' Meadowbank' ,' EX1 7GG' ,1450 .00 ,0 ,0 ,' D' ,@now,@sysUser),
48- (' CT000008' ,' 01' ,' Mr' ,' Michael' ,' Anderson' ,' 45 Queens Road' ,' Floor 2' ,' Fairview' ,' NR1 8HH' ,780 .00 ,0 ,0 ,' D' ,@now,@sysUser),
49- (' CT000009' ,' 01' ,' Mrs' ,' Claire' ,' Thomas' ,' 67 Bridge Street' ,' ' ,' Westfield' ,' IP1 9JJ' ,1320 .50 ,0 ,0 ,' D' ,@now,@sysUser),
50- (' CT000010' ,' 01' ,' Mr' ,' Andrew' ,' Jackson' ,' 89 Market Place' ,' Suite 1' ,' Eastgate' ,' DT1 1KK' ,560 .00 ,0 ,0 ,' D' ,@now,@sysUser),
51- (' CT000011' ,' 01' ,' Ms' ,' Rachel' ,' White' ,' 10 Victoria Road' ,' ' ,' Northway' ,' SN1 2LL' ,1750 .00 ,0 ,0 ,' D' ,@now,@sysUser),
52- (' CT000012' ,' 01' ,' Mr' ,' Peter' ,' Harris' ,' 22 Albert Street' ,' Flat 5' ,' Southend' ,' BA1 3MM' ,890 .25 ,0 ,0 ,' D' ,@now,@sysUser),
53- (' CT000013' ,' 01' ,' Mrs' ,' Karen' ,' Martin' ,' 44 George Lane' ,' ' ,' Oldtown' ,' SY1 4NN' ,1100 .50 ,0 ,0 ,' D' ,@now,@sysUser),
54- (' CT000014' ,' 01' ,' Mr' ,' Steven' ,' Clark' ,' 66 William Road' ,' Block B' ,' Newbury' ,' NG1 5PP' ,675 .00 ,0 ,0 ,' D' ,@now,@sysUser),
55- (' CT000015' ,' 01' ,' Ms' ,' Angela' ,' Lewis' ,' 88 Elizabeth Way' ,' ' ,' Crossroads' ,' LE1 6QQ' ,1500 .00 ,0 ,0 ,' D' ,@now,@sysUser),
56- (' CT000016' ,' 01' ,' Mr' ,' Mark' ,' Walker' ,' 15 Edward Close' ,' Flat 1' ,' Springvale' ,' CV1 7RR' ,430 .75 ,0 ,0 ,' D' ,@now,@sysUser),
57- (' CT000017' ,' 01' ,' Mrs' ,' Julie' ,' Hall' ,' 27 Charles Ave' ,' ' ,' Riverside' ,' GL1 8SS' ,1200 .00 ,0 ,0 ,' D' ,@now,@sysUser),
58- (' CT000018' ,' 01' ,' Mr' ,' Paul' ,' Allen' ,' 39 Henry Lane' ,' Unit 7' ,' Bayview' ,' ST1 9TT' ,965 .50 ,0 ,0 ,' D' ,@now,@sysUser),
59- (' CT000019' ,' 01' ,' Ms' ,' Nicola' ,' Young' ,' 51 Mary Street' ,' ' ,' Clearwater' ,' CB1 1UU' ,1380 .00 ,0 ,0 ,' D' ,@now,@sysUser),
60- (' CT000020' ,' 01' ,' Mr' ,' Simon' ,' King' ,' 63 Anne Road' ,' Flat 9' ,' Stonegate' ,' LN1 2VV' ,720 .25 ,0 ,0 ,' D' ,@now,@sysUser);
61- -- Business Rates
62- INSERT INTO AccountHolders (AccountReference, FundCode, Title, Forename, Surname, AddressLine1, AddressLine2, AddressLine3, Postcode, CurrentBalance, PeriodDebit, PeriodCredit, RecordType, CreatedAt, CreatedByUserId) VALUES
63- (' BR000001' ,' 02' ,' Mr' ,' Richard' ,' Evans' ,' 1 Commerce Way' ,' ' ,' Business Park' ,' BS1 1AA' ,4500 .00 ,0 ,0 ,' D' ,@now,@sysUser),
64- (' BR000002' ,' 02' ,' Mrs' ,' Patricia' ,' Roberts' ,' 2 Trade Street' ,' Unit 12' ,' Industrial Estate' ,' LS1 2BB' ,3200 .00 ,0 ,0 ,' D' ,@now,@sysUser),
65- (' BR000003' ,' 02' ,' Mr' ,' Christopher' ,' Turner' ,' 3 Enterprise Road' ,' ' ,' Retail Park' ,' M1 3CC' ,8750 .00 ,0 ,0 ,' D' ,@now,@sysUser),
66- (' BR000004' ,' 02' ,' Ms' ,' Margaret' ,' Phillips' ,' 4 Market Way' ,' Floor 3' ,' Town Centre' ,' L1 4DD' ,2100 .00 ,0 ,0 ,' D' ,@now,@sysUser),
67- (' BR000005' ,' 02' ,' Mr' ,' Thomas' ,' Campbell' ,' 5 Factory Lane' ,' ' ,' Industrial Zone' ,' S1 5EE' ,5600 .00 ,0 ,0 ,' D' ,@now,@sysUser);
68- -- Housing Rents
69- INSERT INTO AccountHolders (AccountReference, FundCode, Title, Forename, Surname, AddressLine1, AddressLine2, AddressLine3, Postcode, CurrentBalance, PeriodDebit, PeriodCredit, RecordType, CreatedAt, CreatedByUserId) VALUES
70- (' HR000001' ,' 03' ,' Mr' ,' William' ,' Scott' ,' 1 Council Estate' ,' ' ,' Oakfield' ,' B1 1AA' ,520 .00 ,0 ,0 ,' D' ,@now,@sysUser),
71- (' HR000002' ,' 03' ,' Mrs' ,' Dorothy' ,' Green' ,' 2 Tower Block' ,' Flat 14' ,' Riverside' ,' E1 2BB' ,480 .50 ,0 ,0 ,' D' ,@now,@sysUser),
72- (' HR000003' ,' 03' ,' Mr' ,' George' ,' Baker' ,' 3 Sheltered Close' ,' ' ,' Hillside' ,' N1 3CC' ,395 .00 ,0 ,0 ,' D' ,@now,@sysUser),
73- (' HR000004' ,' 03' ,' Ms' ,' Susan' ,' Adams' ,' 4 Maisonette Row' ,' Ground Floor' ,' Lakewood' ,' W1 4DD' ,610 .00 ,0 ,0 ,' D' ,@now,@sysUser),
74- (' HR000005' ,' 03' ,' Mr' ,' Edward' ,' Nelson' ,' 5 Terrace Walk' ,' ' ,' Meadows' ,' SE1 5EE' ,445 .75 ,0 ,0 ,' D' ,@now,@sysUser);
75- -- Parking
76- INSERT INTO AccountHolders (AccountReference, FundCode, Title, Forename, Surname, AddressLine1, AddressLine2, AddressLine3, Postcode, CurrentBalance, PeriodDebit, PeriodCredit, RecordType, CreatedAt, CreatedByUserId) VALUES
77- (' PK000001' ,' 04' ,' Mr' ,' Daniel' ,' Mitchell' ,' 10 Elm Street' ,' ' ,' Parkside' ,' SW1 1AA' ,35 .00 ,0 ,0 ,' D' ,@now,@sysUser),
78- (' PK000002' ,' 04' ,' Ms' ,' Jennifer' ,' Carter' ,' 20 Birch Lane' ,' ' ,' Woodside' ,' WC1 2BB' ,70 .00 ,0 ,0 ,' D' ,@now,@sysUser),
79- (' PK000003' ,' 04' ,' Mr' ,' Matthew' ,' Morris' ,' 30 Pine Avenue' ,' ' ,' Hillcrest' ,' EC1 3CC' ,35 .00 ,0 ,0 ,' D' ,@now,@sysUser),
80- (' PK000004' ,' 04' ,' Mrs' ,' Laura' ,' Rogers' ,' 40 Cedar Close' ,' ' ,' Valleyview' ,' NW1 4DD' ,105 .00 ,0 ,0 ,' D' ,@now,@sysUser),
81- (' PK000005' ,' 04' ,' Mr' ,' Joseph' ,' Reed' ,' 50 Willow Way' ,' ' ,' Brookside' ,' N1 5EE' ,70 .00 ,0 ,0 ,' D' ,@now,@sysUser);
82- PRINT ' Inserted 35 account holders'
88+ PRINT ' WARNING: No funds found in database, skipping account holders'
8389END
8490ELSE
8591BEGIN
@@ -116,9 +122,11 @@ BEGIN
116122 LOWER (NEWID ()), LOWER (NEWID ()), ' S' ,
117123 DATEADD (MINUTE, - @i * 250 , GETDATE ()),
118124 DATEADD (MINUTE, - @i * 250 , GETDATE ()),
119- ' CT000001' , 1 , @fc, @mc,
125+ ISNULL ((SELECT TOP 1 AccountReference FROM AccountHolders WHERE FundCode = @fc), ' AH000001' ),
126+ 1 , @fc, @mc,
120127 CAST (10 + (@i * 7 % 190 ) AS DECIMAL (18 ,2 )),
121- 0 , ' E0' , ' Payment received' , 0 , 0 );
128+ 0 , ISNULL ((SELECT TOP 1 VatCode FROM Vat WHERE Disabled = 0 ), ' E0' ),
129+ ' Payment received' , 0 , 0 );
122130
123131 SET @i = @i + 1 ;
124132 END
0 commit comments