Merge pull request #4685 from shreyasp/supplier-credit-fix

Added the credit_days_based_on field to Supplier and Supplier Type doctype
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index d1e2cdc..e761379 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -237,16 +237,11 @@
 	due_date = None
 	if posting_date and party:
 		due_date = posting_date
-		if party_type=="Customer":
-			credit_days_based_on, credit_days = get_credit_days(party_type, party, company)
-			if credit_days_based_on == "Fixed Days" and credit_days:
-				due_date = add_days(posting_date, credit_days)
-			elif credit_days_based_on == "Last Day of the Next Month":
-				due_date = (get_first_day(posting_date, 0, 2) + datetime.timedelta(-1)).strftime("%Y-%m-%d")
-		else:
-			credit_days = get_credit_days(party_type, party, company)
-			if credit_days:
-				due_date = add_days(posting_date, credit_days)
+		credit_days_based_on, credit_days = get_credit_days(party_type, party, company)
+		if credit_days_based_on == "Fixed Days" and credit_days:
+			due_date = add_days(posting_date, credit_days)
+		elif credit_days_based_on == "Last Day of the Next Month":
+			due_date = (get_first_day(posting_date, 0, 2) + datetime.timedelta(-1)).strftime("%Y-%m-%d")
 
 	return due_date
 
@@ -255,20 +250,21 @@
 		if party_type == "Customer":
 			credit_days_based_on, credit_days, customer_group = \
 				frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "customer_group"])
-
-			if not credit_days_based_on:
-				credit_days_based_on, credit_days = \
-					frappe.db.get_value("Customer Group", customer_group, ["credit_days_based_on", "credit_days"]) \
-					or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"])
-
-			return credit_days_based_on, credit_days
 		else:
-			credit_days, supplier_type = frappe.db.get_value(party_type, party, ["credit_days", "supplier_type"])
-			if not credit_days:
-				credit_days = frappe.db.get_value("Supplier Type", supplier_type, "credit_days") \
-					or frappe.db.get_value("Company", company, "credit_days")
+			credit_days_based_on, credit_days, supplier_type = \
+				frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "supplier_type"])
 
-			return credit_days
+	if not credit_days_based_on:
+		if party_type == "Customer":
+			credit_days_based_on, credit_days = \
+				frappe.db.get_value("Customer Group", customer_group, ["credit_days_based_on", "credit_days"]) \
+				or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"])
+		else:
+			credit_days_based_on, credit_days = \
+				frappe.db.get_value("Supplier Type", supplier_type, ["credit_days_based_on", "credit_days"])\
+				or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"] )
+
+	return credit_days_based_on, credit_days
 
 def validate_due_date(posting_date, due_date, party_type, party, company):
 	if getdate(due_date) < getdate(posting_date):
diff --git a/erpnext/buying/doctype/supplier/supplier.json b/erpnext/buying/doctype/supplier/supplier.json
index 9f78eab..b7697da 100644
--- a/erpnext/buying/doctype/supplier/supplier.json
+++ b/erpnext/buying/doctype/supplier/supplier.json
@@ -233,7 +233,57 @@
   {
    "allow_on_submit": 0, 
    "bold": 0, 
+   "collapsible": 1, 
+   "fieldname": "section_credit_limit", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Credit Limit", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
    "collapsible": 0, 
+   "fieldname": "credit_days_based_on", 
+   "fieldtype": "Select", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Credit Days Based On", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "\nFixed Days\nLast Day of the Next Month", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "depends_on": "eval:doc.credit_days_based_on == 'Fixed Days'", 
    "fieldname": "credit_days", 
    "fieldtype": "Int", 
    "hidden": 0, 
@@ -485,7 +535,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2016-01-06 02:35:18.285473", 
+ "modified": "2016-01-22 02:18:14.158258", 
  "modified_by": "Administrator", 
  "module": "Buying", 
  "name": "Supplier", 
@@ -635,5 +685,6 @@
  "read_only": 0, 
  "read_only_onload": 0, 
  "search_fields": "supplier_name, supplier_type", 
+ "sort_order": "ASC", 
  "title_field": "supplier_name"
 }
\ No newline at end of file
diff --git a/erpnext/buying/doctype/supplier/test_supplier.py b/erpnext/buying/doctype/supplier/test_supplier.py
index f747998..7a4a0cd 100644
--- a/erpnext/buying/doctype/supplier/test_supplier.py
+++ b/erpnext/buying/doctype/supplier/test_supplier.py
@@ -2,6 +2,53 @@
 # License: GNU General Public License v3. See license.txt
 from __future__ import unicode_literals
 
-
 import frappe
-test_records = frappe.get_test_records('Supplier')
\ No newline at end of file
+import unittest
+from erpnext.accounts.party import get_due_date
+
+test_records = frappe.get_test_records('Supplier')
+
+class TestSupplier(unittest.TestCase):
+    def test_supplier_due_date_against_supplier_credit_limit(self):
+        # Set Credit Limit based on Fixed days
+        frappe.db.set_value("Supplier", "_Test Supplier", "credit_days_based_on", "Fixed Days")
+        frappe.db.set_value("Supplier", "_Test Supplier", "credit_days", 10)
+
+        due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        self.assertEqual(due_date, "2016-02-01")
+
+        # Set Credit Limit based on Last day next month
+        frappe.db.set_value("Supplier", "_Test Supplier", "credit_days", 0)
+        frappe.db.set_value("Supplier", "_Test Supplier", "credit_days_based_on",
+                            "Last Day of the Next Month")
+
+        # Leap year
+        due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        self.assertEqual(due_date, "2016-02-29")
+        # Non Leap year
+        due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        self.assertEqual(due_date, "2017-02-28")
+
+        frappe.db.set_value("Supplier", "_Test Supplier", "credit_days_based_on", "")
+
+        # Set credit limit for the supplier type instead of supplier and evaluate the due date
+        # based on Fixed days
+        frappe.db.set_value("Supplier Type", "_Test Supplier Type", "credit_days_based_on",
+                            "Fixed Days")
+        frappe.db.set_value("Supplier Type", "_Test Supplier Type", "credit_days", 10)
+
+        due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        self.assertEqual(due_date, "2016-02-01")
+
+        # Set credit limit for the supplier type instead of supplier and evaluate the due date
+        # based on Last day of next month
+        frappe.db.set_value("Supplier", "_Test Supplier Type", "credit_days", 0)
+        frappe.db.set_value("Supplier Type", "_Test Supplier Type", "credit_days_based_on",
+                            "Last Day of the Next Month")
+
+        # Leap year
+        due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        self.assertEqual(due_date, "2016-02-29")
+        # Non Leap year
+        due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier", "_Test Company")
+        self.assertEqual(due_date, "2017-02-28")
diff --git a/erpnext/setup/doctype/supplier_type/supplier_type.json b/erpnext/setup/doctype/supplier_type/supplier_type.json
index c6849b7..8d34022 100644
--- a/erpnext/setup/doctype/supplier_type/supplier_type.json
+++ b/erpnext/setup/doctype/supplier_type/supplier_type.json
@@ -26,6 +26,7 @@
    "oldfieldtype": "Data", 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
    "report_hide": 0, 
    "reqd": 1, 
@@ -36,7 +37,56 @@
   {
    "allow_on_submit": 0, 
    "bold": 0, 
+   "collapsible": 1, 
+   "fieldname": "section_credit_limit", 
+   "fieldtype": "Section Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Credit Limit", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
    "collapsible": 0, 
+   "fieldname": "credit_days_based_on", 
+   "fieldtype": "Select", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "in_filter": 0, 
+   "in_list_view": 0, 
+   "label": "Credit Days Based On", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "\nFixed Days\nLast Day of the Next Month", 
+   "permlevel": 1, 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "depends_on": "eval:doc.credit_days_based_on=='Fixed Days'", 
    "fieldname": "credit_days", 
    "fieldtype": "Int", 
    "hidden": 0, 
@@ -46,8 +96,10 @@
    "label": "Credit Days", 
    "length": 0, 
    "no_copy": 0, 
-   "permlevel": 1, 
+   "permlevel": 0, 
+   "precision": "", 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
@@ -70,6 +122,7 @@
    "no_copy": 0, 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
@@ -95,6 +148,7 @@
    "options": "Party Account", 
    "permlevel": 0, 
    "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
    "read_only": 0, 
    "report_hide": 0, 
    "reqd": 0, 
@@ -113,7 +167,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2015-11-16 06:29:58.970888", 
+ "modified": "2016-01-25 02:09:49.846022", 
  "modified_by": "Administrator", 
  "module": "Setup", 
  "name": "Supplier Type", 
@@ -241,5 +295,6 @@
   }
  ], 
  "read_only": 0, 
- "read_only_onload": 0
+ "read_only_onload": 0, 
+ "sort_order": "ASC"
 }
\ No newline at end of file