I don't know if this help you, but I try. I have a similar scenario. I have openser, which deals with sip registers. If a sip phone make a call, I send the request to asterisk. Asterisk dispatch to PSTN or go back to openser (if this call is for sip). Asterisk deal the dialplan and cdr-csv and it's a gateway to PSTN. This is an idea. If you want i can post the config files. Bye.