I am developing a program for a MLM based company

I have a the following tables

1. tbl_member

fields
membercode,Membername,sponsorcode and other details

2. tbl_sponsor

fields
membercode,sponsorcode

3. tbl_level

fields
levelid,levelname,

4. tbl_bill
billno,totalbv,totalpv,billmonth,membercode

5. tbl_monthlypayouts

fields
membercode,totalbv,totalpv,level,commission


Now i want to calculate the payouts on monthly basis for all the membercode who have bills on the bill table and insert in the tbl_monthlypayouts automatically. Also i want to split up the commission according to the level

Levels will be like this

level name totalbv
3% 1000
6% 5000
9% 10000
12% 20000
15% 40000
18% 70000
21% 100000

The final payouts should be calculated according to the sponsorlevel and the downline commissions also shold be get deducted from the lower level.

if some one can help me to find a solution for this i will be grateful..