You get the dividend on all 8,000 shares; you are selling 4k of shares owned for 6 months - well beyond the 45 day rule
The points being missed are:
No CGT discount on the the shares bought 6 months ago - (if in a SMSF in pension phase, not an issue)
The issue is that you sold ANZ your 4,000 original shares at a profit; but the new 4,000 has increased your cost base
The only time such a strategy works is if the price drop ex-div is less than the dividend amount
Like your thoughts and approach; in theory, looks good - but remember, by buying the new 4k and selling 4k of your original holding, you have increased your cost base
Very happy to be corrected if I have mis-understood something