I need to create an sales invoice header and sales invoice lines programmatically, Any help would be greatly appreciated.

Sales Invoices ARE created automatically!

What you need is to create a the Sales Header and Sales Line records first.

If you want to see how this is done, then you could take a look at the function “Create Sales Invoice” - it’s report 1094 (Job Transfer to Sales Invoice) and is used to create invoices from job planning lines. Look at this report to get inspiration on how to create your own function.