In 1804 how did the spanish government almost go bankrupt?

The wars almost bankrupt Spain because it had massive war debts