Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 6 | import frappe |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 7 | import datetime |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 8 | from frappe import _, msgprint, scrub |
Anand Doshi | fab0904 | 2014-05-27 08:39:35 +0530 | [diff] [blame] | 9 | from frappe.defaults import get_user_permissions |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, getdate, formatdate, flt, get_first_day, date_diff, nowdate |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 11 | from erpnext.utilities.doctype.address.address import get_address_display |
| 12 | from erpnext.utilities.doctype.contact.contact import get_contact_details |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 14 | @frappe.whitelist() |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 15 | def get_party_details(party=None, account=None, party_type="Customer", company=None, |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 16 | posting_date=None, price_list=None, currency=None, doctype=None): |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 17 | |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 18 | if not party: |
| 19 | return {} |
Nabin Hait | 5ef121b | 2015-06-08 12:26:52 +0530 | [diff] [blame] | 20 | |
| 21 | if not frappe.db.exists(party_type, party): |
| 22 | frappe.throw(_("{0}: {1} does not exists").format(party_type, party)) |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 23 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 24 | return _get_party_details(party, account, party_type, |
| 25 | company, posting_date, price_list, currency, doctype) |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 26 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 27 | def _get_party_details(party=None, account=None, party_type="Customer", company=None, |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 28 | posting_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False): |
Nabin Hait | 5ef121b | 2015-06-08 12:26:52 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 30 | out = frappe._dict(set_account_and_due_date(party, account, party_type, company, posting_date, doctype)) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 31 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 32 | party = out[party_type.lower()] |
| 33 | |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 34 | if not ignore_permissions and not frappe.has_permission(party_type, "read", party): |
Rushabh Mehta | 8a40c13 | 2014-04-15 16:30:55 +0530 | [diff] [blame] | 35 | frappe.throw(_("Not permitted"), frappe.PermissionError) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 36 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 37 | party = frappe.get_doc(party_type, party) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 38 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 39 | set_address_details(out, party, party_type) |
| 40 | set_contact_details(out, party, party_type) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 41 | set_other_values(out, party, party_type) |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 42 | set_price_list(out, party, party_type, price_list) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 43 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 44 | if not out.get("currency"): |
| 45 | out["currency"] = currency |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 46 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 47 | # sales team |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 48 | if party_type=="Customer": |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 49 | out["sales_team"] = [{ |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 50 | "sales_person": d.sales_person, |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 51 | "sales_designation": d.sales_designation, |
| 52 | "allocated_percentage": d.allocated_percentage |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 53 | } for d in party.get("sales_team")] |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 54 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 55 | return out |
| 56 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 57 | def set_address_details(out, party, party_type): |
| 58 | billing_address_field = "customer_address" if party_type == "Lead" \ |
| 59 | else party_type.lower() + "_address" |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 60 | out[billing_address_field] = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 61 | {party_type.lower(): party.name, "is_primary_address":1}, "name") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 62 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 63 | # address display |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 64 | out.address_display = get_address_display(out[billing_address_field]) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 65 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 66 | # shipping address |
| 67 | if party_type in ["Customer", "Lead"]: |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 68 | out.shipping_address_name = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 69 | {party_type.lower(): party.name, "is_shipping_address":1}, "name") |
| 70 | out.shipping_address = get_address_display(out["shipping_address_name"]) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 71 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 72 | def set_contact_details(out, party, party_type): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 73 | out.contact_person = frappe.db.get_value("Contact", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 74 | {party_type.lower(): party.name, "is_primary_contact":1}, "name") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 75 | |
| 76 | if not out.contact_person: |
| 77 | return |
| 78 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 79 | out.update(get_contact_details(out.contact_person)) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 80 | |
| 81 | def set_other_values(out, party, party_type): |
| 82 | # copy |
| 83 | if party_type=="Customer": |
| 84 | to_copy = ["customer_name", "customer_group", "territory"] |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 85 | else: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 86 | to_copy = ["supplier_name", "supplier_type"] |
| 87 | for f in to_copy: |
| 88 | out[f] = party.get(f) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 89 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 90 | # fields prepended with default in Customer doctype |
| 91 | for f in ['currency', 'taxes_and_charges'] \ |
| 92 | + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []): |
| 93 | if party.get("default_" + f): |
| 94 | out[f] = party.get("default_" + f) |
| 95 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 96 | def set_price_list(out, party, party_type, given_price_list): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 97 | # price list |
Anand Doshi | a8ce570 | 2014-05-27 13:46:42 +0530 | [diff] [blame] | 98 | price_list = filter(None, get_user_permissions().get("Price List", [])) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 99 | if isinstance(price_list, list): |
Anand Doshi | a8ce570 | 2014-05-27 13:46:42 +0530 | [diff] [blame] | 100 | price_list = price_list[0] if len(price_list)==1 else None |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 101 | |
| 102 | if not price_list: |
| 103 | price_list = party.default_price_list |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 104 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 105 | if not price_list and party_type=="Customer": |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 106 | price_list = frappe.db.get_value("Customer Group", |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 107 | party.customer_group, "default_price_list") |
| 108 | |
| 109 | if not price_list: |
| 110 | price_list = given_price_list |
| 111 | |
| 112 | if price_list: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 113 | out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 114 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 115 | out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 116 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 117 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 118 | def set_account_and_due_date(party, account, party_type, company, posting_date, doctype): |
| 119 | if doctype not in ["Sales Invoice", "Purchase Invoice"]: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 120 | # not an invoice |
| 121 | return { |
| 122 | party_type.lower(): party |
| 123 | } |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 124 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 125 | if party: |
| 126 | account = get_party_account(company, party, party_type) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 127 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 128 | account_fieldname = "debit_to" if party_type=="Customer" else "credit_to" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 129 | |
| 130 | out = { |
| 131 | party_type.lower(): party, |
| 132 | account_fieldname : account, |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 133 | "due_date": get_due_date(posting_date, party_type, party, company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 134 | } |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 135 | return out |
| 136 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 137 | @frappe.whitelist() |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 138 | def get_party_account(company, party, party_type): |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 139 | """Returns the account for the given `party`. |
| 140 | Will first search in party (Customer / Supplier) record, if not found, |
| 141 | will search in group (Customer Group / Supplier Type), |
| 142 | finally will return default.""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 143 | if not company: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 144 | frappe.throw(_("Please select company first.")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 145 | |
| 146 | if party: |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 147 | account = frappe.db.get_value("Party Account", |
| 148 | {"parenttype": party_type, "parent": party, "company": company}, "account") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 149 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 150 | if not account: |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 151 | party_group_doctype = "Customer Group" if party_type=="Customer" else "Supplier Type" |
| 152 | group = frappe.db.get_value(party_type, party, scrub(party_group_doctype)) |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 153 | account = frappe.db.get_value("Party Account", |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 154 | {"parenttype": party_group_doctype, "parent": group, "company": company}, "account") |
| 155 | |
| 156 | if not account: |
| 157 | default_account_name = "default_receivable_account" if party_type=="Customer" else "default_payable_account" |
| 158 | account = frappe.db.get_value("Company", company, default_account_name) |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 159 | |
| 160 | return account |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 162 | @frappe.whitelist() |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 163 | def get_due_date(posting_date, party_type, party, company): |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 164 | """Set Due Date = Posting Date + Credit Days""" |
| 165 | due_date = None |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 166 | if posting_date and party: |
Nabin Hait | 6336ff7 | 2015-07-09 19:48:21 +0530 | [diff] [blame] | 167 | due_date = posting_date |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 168 | if party_type=="Customer": |
| 169 | credit_days_based_on, credit_days = get_credit_days(party_type, party, company) |
| 170 | if credit_days_based_on == "Fixed Days" and credit_days: |
| 171 | due_date = add_days(posting_date, credit_days) |
| 172 | elif credit_days_based_on == "Last Day of the Next Month": |
| 173 | due_date = (get_first_day(posting_date, 0, 2) + datetime.timedelta(-1)).strftime("%Y-%m-%d") |
| 174 | else: |
| 175 | credit_days = get_credit_days(party_type, party, company) |
| 176 | if credit_days: |
| 177 | due_date = add_days(posting_date, credit_days) |
| 178 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 179 | return due_date |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 180 | |
Nabin Hait | 3a40435 | 2014-08-27 17:43:55 +0530 | [diff] [blame] | 181 | def get_credit_days(party_type, party, company): |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 182 | if party_type and party: |
| 183 | if party_type == "Customer": |
| 184 | credit_days_based_on, credit_days, customer_group = \ |
| 185 | frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "customer_group"]) |
| 186 | |
| 187 | if not credit_days_based_on: |
| 188 | credit_days_based_on, credit_days = \ |
| 189 | frappe.db.get_value("Customer Group", customer_group, ["credit_days_based_on", "credit_days"]) \ |
| 190 | or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"]) |
| 191 | |
| 192 | return credit_days_based_on, credit_days |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 193 | else: |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 194 | credit_days, supplier_type = frappe.db.get_value(party_type, party, ["credit_days", "supplier_type"]) |
| 195 | if not credit_days: |
| 196 | credit_days = frappe.db.get_value("Supplier Type", supplier_type, "credit_days") \ |
| 197 | or frappe.db.get_value("Company", company, "credit_days") |
| 198 | |
| 199 | return credit_days |
| 200 | |
| 201 | def validate_due_date(posting_date, due_date, party_type, party, company): |
| 202 | if getdate(due_date) < getdate(posting_date): |
| 203 | frappe.throw(_("Due Date cannot be before Posting Date")) |
| 204 | else: |
| 205 | default_due_date = get_due_date(posting_date, party_type, party, company) |
Nabin Hait | 93d3c82 | 2015-07-10 17:11:58 +0530 | [diff] [blame] | 206 | if default_due_date != posting_date and getdate(due_date) > getdate(default_due_date): |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 207 | is_credit_controller = frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() |
| 208 | if is_credit_controller: |
| 209 | msgprint(_("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)") |
| 210 | .format(date_diff(due_date, default_due_date))) |
| 211 | else: |
| 212 | frappe.throw(_("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date))) |