test cases and minor changes
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index f75a9af..2e38927 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -85,7 +85,7 @@
 		self.validate_multiple_billing("Delivery Note", "dn_detail", "amount", "items")
 		self.update_packing_list()
 		self.set_billing_hours_and_amount()
-		self.calculate_billing_amount_from_timesheet()
+		self.update_timesheet_billing_for_project()
 
 	def before_save(self):
 		set_account_for_mode_of_payment(self)
@@ -467,13 +467,11 @@
 			if not timesheet.billing_amount and ts_doc.total_billing_amount:
 				timesheet.billing_amount = ts_doc.total_billing_amount
 
-	def calculate_billing_amount_from_timesheet(self):
-		total_billing_amount = 0.0
-		for data in self.timesheets:
-			if data.billing_amount:
-				total_billing_amount += data.billing_amount
-
-		self.total_billing_amount = total_billing_amount
+	def update_timesheet_billing_for_project(self):
+		if not self.timesheets and self.project:
+			self.add_timesheet_data()
+		else:
+			self.calculate_billing_amount_for_timesheet()
 
 	def add_timesheet_data(self):
 		self.set('timesheets', [])
@@ -486,7 +484,15 @@
 						'timesheet_detail': data.name
 					})
 
-			self.calculate_billing_amount_from_timesheet()
+			self.calculate_billing_amount_for_timesheet()
+
+	def calculate_billing_amount_for_timesheet(self):
+		total_billing_amount = 0.0
+		for data in self.timesheets:
+			if data.billing_amount:
+				total_billing_amount += data.billing_amount
+
+		self.total_billing_amount = total_billing_amount
 
 	def get_warehouse(self):
 		user_pos_profile = frappe.db.sql("""select name, warehouse from `tabPOS Profile`
diff --git a/erpnext/projects/doctype/timesheet/test_timesheet.py b/erpnext/projects/doctype/timesheet/test_timesheet.py
index 8e7e562..1e7be41 100644
--- a/erpnext/projects/doctype/timesheet/test_timesheet.py
+++ b/erpnext/projects/doctype/timesheet/test_timesheet.py
@@ -9,11 +9,12 @@
 from frappe.utils import now_datetime, nowdate
 from erpnext.projects.doctype.timesheet.timesheet import OverlapError
 from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice
+from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
 
 class TestTimesheet(unittest.TestCase):
 	def test_timesheet_billing_amount(self):
 		salary_structure = make_salary_structure("_T-Employee-0001")
-		timesheet = make_timesheet("_T-Employee-0001", True)
+		timesheet = make_timesheet("_T-Employee-0001", simulate = True, billable=1)
 
 		self.assertEquals(timesheet.total_hours, 2)
 		self.assertEquals(timesheet.total_billing_hours, 2)
@@ -22,7 +23,7 @@
 
 	def test_salary_slip_from_timesheet(self):
 		salary_structure = make_salary_structure("_T-Employee-0001")
-		timesheet = make_timesheet("_T-Employee-0001", simulate = True)
+		timesheet = make_timesheet("_T-Employee-0001", simulate = True, billable=1)
 		salary_slip = make_salary_slip(timesheet.name)
 		salary_slip.submit()
 
@@ -51,11 +52,20 @@
 		item.rate = 100
 
 		sales_invoice.submit()
-		
 		timesheet = frappe.get_doc('Timesheet', timesheet.name)
 		self.assertEquals(sales_invoice.total_billing_amount, 100)
 		self.assertEquals(timesheet.status, 'Billed')
 
+	def test_timesheet_billing_based_on_project(self):
+		timesheet = make_timesheet("_T-Employee-0001", simulate=True, billable=1, project = '_Test Project', company='_Test Company')
+		sales_invoice = create_sales_invoice(do_not_save=True)
+		sales_invoice.project = '_Test Project'
+		sales_invoice.submit()
+
+		ts = frappe.get_doc('Timesheet', timesheet.name)
+		self.assertEquals(ts.per_billed, 100)
+		self.assertEquals(ts.time_logs[0].sales_invoice, sales_invoice.name)
+
 def make_salary_structure(employee):
 	name = frappe.db.get_value('Salary Structure Employee', {'employee': employee}, 'parent')
 	if name:
@@ -93,7 +103,7 @@
 
 	return salary_structure
 
-def make_timesheet(employee, simulate=False, billable = 0, activity_type="_Test Activity Type", project=None, task=None):
+def make_timesheet(employee, simulate=False, billable = 0, activity_type="_Test Activity Type", project=None, task=None, company=None):
 	update_activity_type(activity_type)
 	timesheet = frappe.new_doc("Timesheet")
 	timesheet.employee = employee
@@ -105,6 +115,7 @@
 	timesheet_detail.to_time = timesheet_detail.from_time + datetime.timedelta(hours= timesheet_detail.hours)
 	timesheet_detail.project = project
 	timesheet_detail.task = task
+	timesheet_detail.company = company or '_Test Company'
 
 	for data in timesheet.get('time_logs'):
 		if simulate:
diff --git a/erpnext/projects/doctype/timesheet/timesheet.js b/erpnext/projects/doctype/timesheet/timesheet.js
index c4f92f3..c591ccb 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.js
+++ b/erpnext/projects/doctype/timesheet/timesheet.js
@@ -21,6 +21,14 @@
 				}
 			}
 		}
+
+		frm.fields_dict['time_logs'].grid.get_field('project').get_query = function() {
+			return{
+				filters: {
+					'status': frm.doc.company
+				}
+			}
+		}
 	},
 
 	onload: function(frm){
diff --git a/erpnext/projects/doctype/timesheet/timesheet.py b/erpnext/projects/doctype/timesheet/timesheet.py
index df87ed1..9d0371e 100644
--- a/erpnext/projects/doctype/timesheet/timesheet.py
+++ b/erpnext/projects/doctype/timesheet/timesheet.py
@@ -255,7 +255,7 @@
 					data.billing_amount = data.billing_rate * hours
 					data.costing_amount = data.costing_rate * hours
 
-@frappe.whitelist()	
+@frappe.whitelist()
 def get_projectwise_timesheet_data(project, parent=None):
 	cond = ''
 	if parent: