Merge pull request #2327 from nabinhait/stock_reco

Fixes in credit note and quotation
diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
index f9f6df1..c6299ed 100644
--- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py
@@ -289,26 +289,25 @@
 
 	def set_print_format_fields(self):
 		for d in self.get('entries'):
-			result = frappe.db.get_value("Account", d.account,
-				["account_type", "master_type"])
+			acc = frappe.db.get_value("Account", d.account, ["account_type", "master_type"], as_dict=1)
 
-			if not result:
-				continue
+			if not acc: continue
 
-			account_type, master_type = result
-
-			if master_type in ['Supplier', 'Customer']:
+			if acc.master_type in ['Supplier', 'Customer']:
 				if not self.pay_to_recd_from:
-					self.pay_to_recd_from = frappe.db.get_value(master_type,
-						' - '.join(d.account.split(' - ')[:-1]),
-						master_type == 'Customer' and 'customer_name' or 'supplier_name')
+					self.pay_to_recd_from = frappe.db.get_value(acc.master_type, ' - '.join(d.account.split(' - ')[:-1]),
+						acc.master_type == 'Customer' and 'customer_name' or 'supplier_name')
+				if self.voucher_type in ["Credit Note", "Debit Note"]:
+					self.set_total_amount(d.debit or d.credit)
 
-			if account_type in ['Bank', 'Cash']:
-				company_currency = get_company_currency(self.company)
-				amt = flt(d.debit) and d.debit or d.credit
-				self.total_amount = fmt_money(amt, currency=company_currency)
-				from frappe.utils import money_in_words
-				self.total_amount_in_words = money_in_words(amt, company_currency)
+			if acc.account_type in ['Bank', 'Cash']:
+				self.set_total_amount(d.debit or d.credit)
+
+	def set_total_amount(self, amt):
+		company_currency = get_company_currency(self.company)
+		self.total_amount = fmt_money(amt, currency=company_currency)
+		from frappe.utils import money_in_words
+		self.total_amount_in_words = money_in_words(amt, company_currency)
 
 	def check_credit_days(self):
 		date_diff = 0
diff --git a/erpnext/accounts/print_format/credit_note/credit_note.json b/erpnext/accounts/print_format/credit_note/credit_note.json
index 31507b4..ac0de75 100644
--- a/erpnext/accounts/print_format/credit_note/credit_note.json
+++ b/erpnext/accounts/print_format/credit_note/credit_note.json
@@ -4,9 +4,9 @@
  "doc_type": "Journal Voucher", 
  "docstatus": 0, 
  "doctype": "Print Format", 
- "html": "{%- from \"templates/print_formats/standard_macros.html\" import add_header -%}\n\n<div class=\"page-break\">\n    {%- if not doc.get(\"print_heading\") and not doc.get(\"select_print_heading\") \n        and doc.set(\"select_print_heading\", _(\"Credit Note\")) -%}{%- endif -%}\n    {{ add_header(0, 1, doc, letter_head, no_letterhead) }}\n\n    {%- for label, value in (\n        (_(\"Credit To\"), doc.pay_to_recd_from),\n        (_(\"Date\"), frappe.utils.formatdate(doc.voucher_date)),\n        (_(\"Amount\"), \"<strong>\" + doc.total_amount + \"</strong><br>\" + (doc.total_amount_in_words or \"\") + \"<br>\"),\n        (_(\"Remarks\"), doc.remark)\n    ) -%}\n\n    <div class=\"row\">\n        <div class=\"col-sm-3\"><label class=\"text-right\">{{ label }}</label></div>\n        <div class=\"col-sm-9\">{{ value }}</div>\n    </div>\n\n    {%- endfor -%}\n\n    <hr>\n    <br>\n    <p class=\"strong\">\n        {{ _(\"For\") }} {{ doc.company }},<br>\n        <br>\n        <br>\n        <br>\n        {{ _(\"Authorized Signatory\") }}\n    </p>\n</div>\n\n\n", 
+ "html": "{%- from \"templates/print_formats/standard_macros.html\" import add_header -%}\n\n<div class=\"page-break\">\n    {%- if not doc.get(\"print_heading\") and not doc.get(\"select_print_heading\") \n        and doc.set(\"select_print_heading\", _(\"Credit Note\")) -%}{%- endif -%}\n    {{ add_header(0, 1, doc, letter_head, no_letterhead) }}\n\n    {%- for label, value in (\n        (_(\"Credit To\"), doc.pay_to_recd_from),\n        (_(\"Date\"), frappe.utils.formatdate(doc.voucher_date)),\n        (_(\"Amount\"), \"<strong>\" + frappe.utils.cstr(doc.total_amount) + \"</strong><br>\" + (doc.total_amount_in_words or \"\") + \"<br>\"),\n        (_(\"Remarks\"), doc.remark)\n    ) -%}\n\n    <div class=\"row\">\n        <div class=\"col-sm-3\"><label class=\"text-right\">{{ label }}</label></div>\n        <div class=\"col-sm-9\">{{ value }}</div>\n    </div>\n\n    {%- endfor -%}\n\n    <hr>\n    <br>\n    <p class=\"strong\">\n        {{ _(\"For\") }} {{ doc.company }},<br>\n        <br>\n        <br>\n        <br>\n        {{ _(\"Authorized Signatory\") }}\n    </p>\n</div>\n\n\n", 
  "idx": 2, 
- "modified": "2014-08-29 13:20:15.789533", 
+ "modified": "2014-10-17 17:20:02.740340", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Credit Note", 
diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py
index ab6e4ba..f12f396 100644
--- a/erpnext/selling/doctype/quotation/quotation.py
+++ b/erpnext/selling/doctype/quotation/quotation.py
@@ -23,7 +23,7 @@
 		self.validate_order_type()
 		self.validate_for_items()
 		self.validate_uom_is_integer("stock_uom", "qty")
-		self.quotation_to = "Customer" if self.customer else "Lead"
+		self.validate_quotation_to()
 
 	def has_sales_order(self):
 		return frappe.db.get_value("Sales Order Item", {"prevdoc_docname": self.name, "docstatus": 1})
@@ -54,6 +54,13 @@
 				if is_sales_item == 'No':
 					frappe.throw(_("Item {0} must be Sales Item").format(d.item_code))
 
+	def validate_quotation_to(self):
+		if self.customer:
+			self.quotation_to = "Customer"
+			self.lead = None
+		elif self.lead:
+			self.quotation_to = "Lead"
+
 	def update_opportunity(self):
 		for opportunity in list(set([d.prevdoc_docname for d in self.get("quotation_details")])):
 			if opportunity:
@@ -139,8 +146,8 @@
 	return doclist
 
 def _make_customer(source_name, ignore_permissions=False):
-	quotation = frappe.db.get_value("Quotation", source_name, ["lead", "order_type"])
-	if quotation and quotation[0]:
+	quotation = frappe.db.get_value("Quotation", source_name, ["lead", "order_type", "customer"])
+	if quotation and quotation[0] and not quotation[2]:
 		lead_name = quotation[0]
 		customer_name = frappe.db.get_value("Customer", {"lead_name": lead_name},
 			["name", "customer_name"], as_dict=True)