Merge pull request #1323 from nabinhait/hotfix

Billing status for zero value SO/PO
diff --git a/accounts/doctype/accounts_settings/accounts_settings.py b/accounts/doctype/accounts_settings/accounts_settings.py
index a6e9938..2475fda 100644
--- a/accounts/doctype/accounts_settings/accounts_settings.py
+++ b/accounts/doctype/accounts_settings/accounts_settings.py
@@ -6,6 +6,7 @@
 from __future__ import unicode_literals
 import webnotes
 from webnotes import _
+from webnotes.utils import cint
 
 class DocType:
 	def __init__(self, d, dl):
@@ -14,7 +15,12 @@
 	def on_update(self):
 		webnotes.conn.set_default("auto_accounting_for_stock", self.doc.auto_accounting_for_stock)
 		
-		if self.doc.auto_accounting_for_stock:
+		if cint(self.doc.auto_accounting_for_stock):
+			# set default perpetual account in company
+			for company in webnotes.conn.sql("select name from tabCompany"):
+				webnotes.bean("Company", company[0]).save()
+			
+			# Create account head for warehouses
 			warehouse_list = webnotes.conn.sql("select name, company from tabWarehouse", as_dict=1)
 			warehouse_with_no_company = [d.name for d in warehouse_list if not d.company]
 			if warehouse_with_no_company:
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 06b7a3a..fcd6846 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -302,6 +302,7 @@
 		self.make_gl_entries()
 		self.update_against_document_in_jv()
 		self.update_prevdoc_status()
+		self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
 
 	def make_gl_entries(self):
 		auto_accounting_for_stock = \
@@ -421,7 +422,7 @@
 		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_voucher")
 		
 		self.update_prevdoc_status()
-		
+		self.update_billing_status_for_zero_amount_refdoc("Purchase Order")
 		self.make_cancel_gl_entries()
 		
 	def on_update(self):
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index bfba30f..a39702b 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -88,6 +88,7 @@
 		
 		self.update_status_updater_args()
 		self.update_prevdoc_status()
+		self.update_billing_status_for_zero_amount_refdoc("Sales Order")
 		
 		# this sequence because outstanding may get -ve
 		self.make_gl_entries()
@@ -114,6 +115,7 @@
 
 		self.update_status_updater_args()
 		self.update_prevdoc_status()
+		self.update_billing_status_for_zero_amount_refdoc("Sales Order")
 		
 		self.make_cancel_gl_entries()
 		
diff --git a/controllers/status_updater.py b/controllers/status_updater.py
index a285c47..1743887 100644
--- a/controllers/status_updater.py
+++ b/controllers/status_updater.py
@@ -232,6 +232,43 @@
 								'Fully %(keyword)s', 'Partly %(keyword)s'))
 							where name='%(name)s'""" % args)
 							
+							
+	def update_billing_status_for_zero_amount_refdoc(self, ref_dt):
+		ref_fieldname = ref_dt.lower().replace(" ", "_")
+		zero_amount_refdoc = []
+		all_zero_amount_refdoc = webnotes.conn.sql_list("""select name from `tab%s` 
+			where docstatus=1 and net_total = 0""" % ref_dt)
+	
+		for item in self.doclist.get({"parentfield": "entries"}):
+			if item.fields.get(ref_fieldname) \
+				and item.fields.get(ref_fieldname) in all_zero_amount_refdoc \
+				and item.fields.get(ref_fieldname) not in zero_amount_refdoc:
+					zero_amount_refdoc.append(item.fields[ref_fieldname])
+		
+		if zero_amount_refdoc:
+			self.update_biling_status(zero_amount_refdoc, ref_dt, ref_fieldname)
+	
+	def update_biling_status(self, zero_amount_refdoc, ref_dt, ref_fieldname):
+		for ref_dn in zero_amount_refdoc:
+			ref_doc_qty = flt(webnotes.conn.sql("""select sum(ifnull(qty, 0)) from `tab%s Item` 
+				where parent=%s""" % (ref_dt, '%s'), (ref_dn))[0][0])
+			
+			billed_qty = flt(webnotes.conn.sql("""select sum(ifnull(qty, 0)) 
+				from `tab%s Item` where %s=%s and docstatus=1""" % 
+				(self.doc.doctype, ref_fieldname, '%s'), (ref_dn))[0][0])
+			
+			per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\
+				/ ref_doc_qty)*100
+			webnotes.conn.set_value(ref_dt, ref_dn, "per_billed", per_billed)
+			
+			from webnotes.model.meta import has_field
+			if has_field(ref_dt, "billing_status"):
+				if per_billed < 0.001: billing_status = "Not Billed"
+				elif per_billed >= 99.99: billing_status = "Fully Billed"
+				else: billing_status = "Partly Billed"
+			
+				webnotes.conn.set_value(ref_dt, ref_dn, "billing_status", billing_status)
+							
 def get_tolerance_for(item_code, item_tolerance={}, global_tolerance=None):
 	"""
 		Returns the tolerance for the item, if not set, returns global tolerance
diff --git a/hr/report/monthly_salary_register/monthly_salary_register.py b/hr/report/monthly_salary_register/monthly_salary_register.py
index 9b8a84f..42c62e4 100644
--- a/hr/report/monthly_salary_register/monthly_salary_register.py
+++ b/hr/report/monthly_salary_register/monthly_salary_register.py
@@ -50,9 +50,9 @@
 		where ifnull(d_modified_amount, 0) != 0 and parent in (%s)""" % 
 		(', '.join(['%s']*len(salary_slips))), tuple([d.name for d in salary_slips]))
 		
-	columns = columns + [(e + ":Link/Earning Type:120") for e in earning_types] + \
+	columns = columns + [(e + ":Currency:120") for e in earning_types] + \
 		["Arrear Amount:Currency:120", "Leave Encashment Amount:Currency:150", 
-		"Gross Pay:Currency:120"] + [(d + ":Link/Deduction Type:120") for d in ded_types] + \
+		"Gross Pay:Currency:120"] + [(d + ":Currency:120") for d in ded_types] + \
 		["Total Deduction:Currency:120", "Net Pay:Currency:120"]
 
 	return columns, earning_types, ded_types
@@ -102,6 +102,6 @@
 	ss_ded_map = {}
 	for d in ss_deductions:
 		ss_ded_map.setdefault(d.parent, webnotes._dict()).setdefault(d.d_type, [])
-		ss_ded_map[d.parent][d.e_type] = flt(d.d_modified_amount)
+		ss_ded_map[d.parent][d.d_type] = flt(d.d_modified_amount)
 	
 	return ss_ded_map
\ No newline at end of file
diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py
index 5c28d8d..f783faa 100644
--- a/utilities/transaction_base.py
+++ b/utilities/transaction_base.py
@@ -79,9 +79,10 @@
 		"""
 		customer_defaults = self.get_customer_defaults()
 					
-		customer_defaults["selling_price_list"] = customer_defaults.get("price_list") or \
-			webnotes.conn.get_value("Customer Group", self.doc.customer_group, "default_price_list") or \
-			self.doc.selling_price_list
+		customer_defaults["selling_price_list"] = self.get_user_default_price_list("Selling") or \
+			customer_defaults.get("price_list") or \
+			webnotes.conn.get_value("Customer Group", self.doc.customer_group, 
+				"default_price_list") or self.doc.selling_price_list
 			
 		for fieldname, val in customer_defaults.items():
 			if self.meta.get_field(fieldname):
@@ -90,6 +91,12 @@
 		if self.meta.get_field("sales_team") and self.doc.customer:
 			self.set_sales_team_for_customer()
 			
+	def get_user_default_price_list(self, price_list_for):
+		from webnotes.defaults import get_user_default_as_list
+		user_default_price_list = get_user_default_as_list("selling_price_list" 
+			if price_list_for=="Selling" else "buying_price_list")
+		return user_default_price_list[0] if len(user_default_price_list)==1 else ""
+			
 	def set_sales_team_for_customer(self):
 		from webnotes.model import default_fields
 		
@@ -120,8 +127,9 @@
 		out["supplier_name"] = supplier.supplier_name
 		if supplier.default_currency:
 			out["currency"] = supplier.default_currency
-		if supplier.default_price_list:
-			out["buying_price_list"] = supplier.default_price_list
+			
+		out["buying_price_list"] = self.get_user_default_price_list("Buying") or \
+			supplier.default_price_list or self.doc.buying_price_list
 		
 		return out