Hi,
VOFM Routines are noting but modifications to SAP codes. They are exits. Generally used in SD module for Pricing, Formula Calculation, Output Control etc.
Steps to create VOFM Pricing Riutineare shown here
http://sapdocs.info/sap/sd-related-topics/routines-in-sd/
Coding Advice -
Once you get the routine created and configured, put a break point and see all the variable present in the global data tab of debugger. You will have structures KOMKBV1 and KOMPBV1 which will have Sales Order Header and Item Values. Now play with these to suit your needs.
BR.