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