I have a deal with one of my clients like this. Basically, I own the code, but they have the right to use the code as they wish provided they do not transfer or sell the software without my express authority.
I retain all rights to the codebase and may sell the software to others as I see fit.
Another clause in all my contracts is that if I should ever go out of business, each customer will receive a copy of the code and the database for maintenance purposes.
In your case I would consider a couple of different things.
Are you interested in selling to others? If so make sure you retain those rights, and give them rights to the code for maintenance purposes only.
If they are only interested in what happens if you go out of business, then you can make an addendum to the original contract that states that they will receive the code when you are no longer providing services.
Make sure when you sell the code you also get a maintenance contract for a decent term, like 5 yrs and then renews automatically each year that they do not decide to terminate the contract after that. Make sure also to add in an x% increase in maintenance fees, or some other way to increase the fees. I also put a cap on the number of hours for adding new features with a $/hr for anything that goes over that.
If you sell, make sure you get a good price for it. Something in the neighborhood of what you charged to create it would be a good starting point.
Get a lawyer involved, you can mock up the initial draft with something like rocketlaw.com, but have a lawyer go over it before presenting it to the client. I guarantee they will have their lawyer go over it. It's expensive so keep it in mind when you are negotiating the price.
You might want to put in some kind of clause that allows you to use portions of the code that are not business specific in other projects. I.E. if you created a really good drop down box you want to be able to use it in other projects, or maybe the security system etc.
Make sure any open source code you have hasn't already legally made the code public. Some of those licenses look like it's ok to use but there are clauses that make any code you add have to follow the same license (gpl, lgpl, creative commons etc) and release the code if asked.
Remember right now you have a lot of leverage, if he doesn't like the clauses, you can continue as is. You aren't trying to sell the code, he's trying to buy it. Each clause he takes out, increases the price you have to charge. For example if you can't use the security system, then the price increases by at least as much as it would take you to build another one from scratch.
It's going to cost a lot to buy and retrain their staff on a new system. And depending on how the original contract is written they may not even have rights to their data, or the format it's in. My contracts usually state that the data is the property of the client, but it does not state the format of that data, so if I want to produce it as a giant denormalized csv file, I can.
Some ideas anyway.