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 |
Rushabh Mehta | 40b3a5b | 2016-05-20 11:20:59 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, getdate, formatdate, get_first_day, date_diff, add_years |
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 |
shreyas | f76853a | 2016-01-25 21:25:11 +0530 | [diff] [blame] | 13 | from erpnext.exceptions import PartyFrozen, InvalidCurrency, PartyDisabled, InvalidAccountCurrency |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 14 | |
Anand Doshi | 248c867 | 2015-09-28 15:24:00 +0530 | [diff] [blame] | 15 | class DuplicatePartyAccountError(frappe.ValidationError): pass |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 16 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 17 | @frappe.whitelist() |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 18 | def get_party_details(party=None, account=None, party_type="Customer", company=None, |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 19 | posting_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False): |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 21 | if not party: |
| 22 | return {} |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 23 | |
Nabin Hait | 5ef121b | 2015-06-08 12:26:52 +0530 | [diff] [blame] | 24 | if not frappe.db.exists(party_type, party): |
| 25 | frappe.throw(_("{0}: {1} does not exists").format(party_type, party)) |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 26 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 27 | return _get_party_details(party, account, party_type, |
rohitwaghchaure | a1064a6 | 2016-03-03 14:00:35 +0530 | [diff] [blame] | 28 | company, posting_date, price_list, currency, doctype, ignore_permissions) |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 29 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 30 | 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] | 31 | posting_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False): |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 32 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 33 | 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] | 34 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 35 | party = out[party_type.lower()] |
| 36 | |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 37 | if not ignore_permissions and not frappe.has_permission(party_type, "read", party): |
Rushabh Mehta | 2d70887 | 2015-11-27 11:35:35 +0530 | [diff] [blame] | 38 | frappe.throw(_("Not permitted for {0}").format(party), frappe.PermissionError) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 39 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 40 | party = frappe.get_doc(party_type, party) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 41 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 42 | set_address_details(out, party, party_type) |
| 43 | set_contact_details(out, party, party_type) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 44 | set_other_values(out, party, party_type) |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 45 | set_price_list(out, party, party_type, price_list) |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 46 | out["taxes_and_charges"] = set_taxes(party.name, party_type, posting_date, company, out.customer_group, out.supplier_type) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 47 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 48 | if not out.get("currency"): |
| 49 | out["currency"] = currency |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 50 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 51 | # sales team |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 52 | if party_type=="Customer": |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 53 | out["sales_team"] = [{ |
Anand Doshi | 4b72d05 | 2015-10-12 16:15:52 +0530 | [diff] [blame] | 54 | "sales_person": d.sales_person, |
| 55 | "allocated_percentage": d.allocated_percentage or None |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 56 | } for d in party.get("sales_team")] |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 57 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 58 | return out |
| 59 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 60 | def set_address_details(out, party, party_type): |
| 61 | billing_address_field = "customer_address" if party_type == "Lead" \ |
| 62 | else party_type.lower() + "_address" |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 63 | out[billing_address_field] = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 64 | {party_type.lower(): party.name, "is_primary_address":1}, "name") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 65 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 66 | # address display |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 67 | out.address_display = get_address_display(out[billing_address_field]) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 68 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 69 | # shipping address |
| 70 | if party_type in ["Customer", "Lead"]: |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 71 | out.shipping_address_name = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 72 | {party_type.lower(): party.name, "is_shipping_address":1}, "name") |
| 73 | out.shipping_address = get_address_display(out["shipping_address_name"]) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 74 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 75 | def set_contact_details(out, party, party_type): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 76 | out.contact_person = frappe.db.get_value("Contact", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 77 | {party_type.lower(): party.name, "is_primary_contact":1}, "name") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 78 | |
| 79 | if not out.contact_person: |
Neil Trini Lasrado | 09a66c4 | 2015-07-07 16:41:37 +0530 | [diff] [blame] | 80 | out.update({ |
| 81 | "contact_person": None, |
| 82 | "contact_display": None, |
| 83 | "contact_email": None, |
| 84 | "contact_mobile": None, |
| 85 | "contact_phone": None, |
| 86 | "contact_designation": None, |
| 87 | "contact_department": None |
| 88 | }) |
Neil Trini Lasrado | fe2ffae | 2015-07-08 18:16:51 +0530 | [diff] [blame] | 89 | else: |
| 90 | out.update(get_contact_details(out.contact_person)) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 91 | |
| 92 | def set_other_values(out, party, party_type): |
| 93 | # copy |
| 94 | if party_type=="Customer": |
saurabh | 8e49951 | 2016-02-19 11:08:45 +0530 | [diff] [blame] | 95 | to_copy = ["customer_name", "customer_group", "territory", "language"] |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 96 | else: |
saurabh | 8e49951 | 2016-02-19 11:08:45 +0530 | [diff] [blame] | 97 | to_copy = ["supplier_name", "supplier_type", "language"] |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 98 | for f in to_copy: |
| 99 | out[f] = party.get(f) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 100 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 101 | # fields prepended with default in Customer doctype |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 102 | for f in ['currency'] \ |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 103 | + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []): |
| 104 | if party.get("default_" + f): |
| 105 | out[f] = party.get("default_" + f) |
| 106 | |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 107 | def get_default_price_list(party): |
| 108 | """Return default price list for party (Document object)""" |
| 109 | if party.default_price_list: |
| 110 | return party.default_price_list |
| 111 | |
| 112 | if party.doctype == "Customer": |
| 113 | price_list = frappe.db.get_value("Customer Group", |
| 114 | party.customer_group, "default_price_list") |
| 115 | if price_list: |
| 116 | return price_list |
| 117 | |
| 118 | return None |
| 119 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 120 | def set_price_list(out, party, party_type, given_price_list): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 121 | # price list |
Anand Doshi | a8ce570 | 2014-05-27 13:46:42 +0530 | [diff] [blame] | 122 | price_list = filter(None, get_user_permissions().get("Price List", [])) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 123 | if isinstance(price_list, list): |
Anand Doshi | a8ce570 | 2014-05-27 13:46:42 +0530 | [diff] [blame] | 124 | price_list = price_list[0] if len(price_list)==1 else None |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 125 | |
| 126 | if not price_list: |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 127 | price_list = get_default_price_list(party) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 128 | |
| 129 | if not price_list: |
| 130 | price_list = given_price_list |
| 131 | |
| 132 | if price_list: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 133 | 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] | 134 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 135 | 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] | 136 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 137 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 138 | def set_account_and_due_date(party, account, party_type, company, posting_date, doctype): |
| 139 | if doctype not in ["Sales Invoice", "Purchase Invoice"]: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 140 | # not an invoice |
| 141 | return { |
| 142 | party_type.lower(): party |
| 143 | } |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 144 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 145 | if party: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 146 | account = get_party_account(party_type, party, company) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 147 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 148 | account_fieldname = "debit_to" if party_type=="Customer" else "credit_to" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 149 | |
| 150 | out = { |
| 151 | party_type.lower(): party, |
| 152 | account_fieldname : account, |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 153 | "due_date": get_due_date(posting_date, party_type, party, company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 154 | } |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 155 | return out |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 156 | |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 157 | def get_company_currency(): |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 158 | company_currency = frappe._dict() |
| 159 | for d in frappe.get_all("Company", fields=["name", "default_currency"]): |
| 160 | company_currency.setdefault(d.name, d.default_currency) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 162 | return company_currency |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 163 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 164 | @frappe.whitelist() |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 165 | def get_party_account(party_type, party, company): |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 166 | """Returns the account for the given `party`. |
| 167 | Will first search in party (Customer / Supplier) record, if not found, |
| 168 | will search in group (Customer Group / Supplier Type), |
| 169 | finally will return default.""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 170 | if not company: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 171 | frappe.throw(_("Please select a Company")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 172 | |
| 173 | if party: |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 174 | account = frappe.db.get_value("Party Account", |
| 175 | {"parenttype": party_type, "parent": party, "company": company}, "account") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 176 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 177 | if not account: |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 178 | party_group_doctype = "Customer Group" if party_type=="Customer" else "Supplier Type" |
| 179 | group = frappe.db.get_value(party_type, party, scrub(party_group_doctype)) |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 180 | account = frappe.db.get_value("Party Account", |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 181 | {"parenttype": party_group_doctype, "parent": group, "company": company}, "account") |
| 182 | |
| 183 | if not account: |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 184 | default_account_name = "default_receivable_account" \ |
| 185 | if party_type=="Customer" else "default_payable_account" |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 186 | account = frappe.db.get_value("Company", company, default_account_name) |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 187 | |
| 188 | existing_gle_currency = get_party_gle_currency(party_type, party, company) |
| 189 | if existing_gle_currency: |
| 190 | if account: |
| 191 | account_currency = frappe.db.get_value("Account", account, "account_currency") |
| 192 | if (account and account_currency != existing_gle_currency) or not account: |
| 193 | account = get_party_gle_account(party_type, party, company) |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 194 | |
| 195 | return account |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 196 | |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 197 | def get_party_account_currency(party_type, party, company): |
| 198 | def generator(): |
| 199 | party_account = get_party_account(party_type, party, company) |
| 200 | return frappe.db.get_value("Account", party_account, "account_currency") |
| 201 | |
| 202 | return frappe.local_cache("party_account_currency", (party_type, party, company), generator) |
| 203 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 204 | def get_party_gle_currency(party_type, party, company): |
| 205 | def generator(): |
| 206 | existing_gle_currency = frappe.db.sql("""select account_currency from `tabGL Entry` |
| 207 | where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s |
| 208 | limit 1""", { "company": company, "party_type": party_type, "party": party }) |
| 209 | |
| 210 | return existing_gle_currency[0][0] if existing_gle_currency else None |
| 211 | |
Anand Doshi | 0b93bdc | 2015-10-22 19:32:56 +0530 | [diff] [blame] | 212 | return frappe.local_cache("party_gle_currency", (party_type, party, company), generator, |
| 213 | regenerate_if_none=True) |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 214 | |
| 215 | def get_party_gle_account(party_type, party, company): |
| 216 | def generator(): |
| 217 | existing_gle_account = frappe.db.sql("""select account from `tabGL Entry` |
| 218 | where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s |
| 219 | limit 1""", { "company": company, "party_type": party_type, "party": party }) |
| 220 | |
| 221 | return existing_gle_account[0][0] if existing_gle_account else None |
| 222 | |
| 223 | return frappe.local_cache("party_gle_account", (party_type, party, company), generator, |
| 224 | regenerate_if_none=True) |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 225 | |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 226 | def validate_party_gle_currency(party_type, party, company, party_account_currency=None): |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 227 | """Validate party account currency with existing GL Entry's currency""" |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 228 | if not party_account_currency: |
| 229 | party_account_currency = get_party_account_currency(party_type, party, company) |
| 230 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 231 | existing_gle_currency = get_party_gle_currency(party_type, party, company) |
| 232 | |
| 233 | if existing_gle_currency and party_account_currency != existing_gle_currency: |
| 234 | frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}") |
| 235 | .format(party_type, party, existing_gle_currency), InvalidAccountCurrency) |
| 236 | |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 237 | def validate_party_accounts(doc): |
| 238 | companies = [] |
| 239 | |
| 240 | for account in doc.get("accounts"): |
| 241 | if account.company in companies: |
Nabin Hait | 2873f2e | 2015-10-16 13:07:45 +0530 | [diff] [blame] | 242 | frappe.throw(_("There can only be 1 Account per Company in {0} {1}") |
| 243 | .format(doc.doctype, doc.name), DuplicatePartyAccountError) |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 244 | else: |
| 245 | companies.append(account.company) |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 246 | |
Nabin Hait | 2873f2e | 2015-10-16 13:07:45 +0530 | [diff] [blame] | 247 | party_account_currency = frappe.db.get_value("Account", account.account, "account_currency") |
| 248 | existing_gle_currency = get_party_gle_currency(doc.doctype, doc.name, account.company) |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 249 | company_default_currency = frappe.db.get_value("Company", |
| 250 | frappe.db.get_default("Company"), "default_currency", cache=True) |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 251 | |
Nabin Hait | aa01590 | 2015-10-16 13:08:33 +0530 | [diff] [blame] | 252 | if existing_gle_currency and party_account_currency != existing_gle_currency: |
| 253 | frappe.throw(_("Accounting entries have already been made in currency {0} for company {1}. Please select a receivable or payable account with currency {0}.").format(existing_gle_currency, account.company)) |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 254 | |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 255 | if doc.default_currency and party_account_currency and company_default_currency: |
| 256 | if doc.default_currency != party_account_currency and doc.default_currency != company_default_currency: |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 257 | frappe.throw(_("Billing currency must be equal to either default comapany's currency or party account currency")) |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 258 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 259 | @frappe.whitelist() |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 260 | def get_due_date(posting_date, party_type, party, company): |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 261 | """Set Due Date = Posting Date + Credit Days""" |
| 262 | due_date = None |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 263 | if posting_date and party: |
Nabin Hait | 6336ff7 | 2015-07-09 19:48:21 +0530 | [diff] [blame] | 264 | due_date = posting_date |
shreyas | 28a6578 | 2016-01-25 12:44:14 +0530 | [diff] [blame] | 265 | credit_days_based_on, credit_days = get_credit_days(party_type, party, company) |
| 266 | if credit_days_based_on == "Fixed Days" and credit_days: |
| 267 | due_date = add_days(posting_date, credit_days) |
| 268 | elif credit_days_based_on == "Last Day of the Next Month": |
| 269 | due_date = (get_first_day(posting_date, 0, 2) + datetime.timedelta(-1)).strftime("%Y-%m-%d") |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 270 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 271 | return due_date |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 272 | |
Nabin Hait | 3a40435 | 2014-08-27 17:43:55 +0530 | [diff] [blame] | 273 | def get_credit_days(party_type, party, company): |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 274 | if party_type and party: |
| 275 | if party_type == "Customer": |
| 276 | credit_days_based_on, credit_days, customer_group = \ |
| 277 | frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "customer_group"]) |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 278 | else: |
shreyas | 28a6578 | 2016-01-25 12:44:14 +0530 | [diff] [blame] | 279 | credit_days_based_on, credit_days, supplier_type = \ |
| 280 | frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "supplier_type"]) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 281 | |
shreyas | 28a6578 | 2016-01-25 12:44:14 +0530 | [diff] [blame] | 282 | if not credit_days_based_on: |
| 283 | if party_type == "Customer": |
| 284 | credit_days_based_on, credit_days = \ |
| 285 | frappe.db.get_value("Customer Group", customer_group, ["credit_days_based_on", "credit_days"]) \ |
| 286 | or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"]) |
| 287 | else: |
| 288 | credit_days_based_on, credit_days = \ |
| 289 | frappe.db.get_value("Supplier Type", supplier_type, ["credit_days_based_on", "credit_days"])\ |
| 290 | or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"] ) |
| 291 | |
| 292 | return credit_days_based_on, credit_days |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 293 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 294 | def validate_due_date(posting_date, due_date, party_type, party, company): |
| 295 | if getdate(due_date) < getdate(posting_date): |
| 296 | frappe.throw(_("Due Date cannot be before Posting Date")) |
| 297 | else: |
| 298 | default_due_date = get_due_date(posting_date, party_type, party, company) |
Anand Doshi | 0ca587e | 2015-09-25 16:32:14 +0530 | [diff] [blame] | 299 | if not default_due_date: |
| 300 | return |
| 301 | |
Nabin Hait | 93d3c82 | 2015-07-10 17:11:58 +0530 | [diff] [blame] | 302 | 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] | 303 | is_credit_controller = frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() |
| 304 | if is_credit_controller: |
| 305 | msgprint(_("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)") |
| 306 | .format(date_diff(due_date, default_due_date))) |
| 307 | else: |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 308 | frappe.throw(_("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date))) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 309 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 310 | @frappe.whitelist() |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 311 | def set_taxes(party, party_type, posting_date, company, customer_group=None, supplier_type=None, |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 312 | billing_address=None, shipping_address=None, use_for_shopping_cart=None): |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 313 | from erpnext.accounts.doctype.tax_rule.tax_rule import get_tax_template, get_party_details |
| 314 | args = { |
Rushabh Mehta | 307978f | 2015-09-23 15:43:09 +0530 | [diff] [blame] | 315 | party_type.lower(): party, |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 316 | "customer_group": customer_group, |
| 317 | "supplier_type": supplier_type, |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 318 | "company": company |
| 319 | } |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 320 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 321 | if billing_address or shipping_address: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 322 | args.update(get_party_details(party, party_type, {"billing_address": billing_address, \ |
| 323 | "shipping_address": shipping_address })) |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 324 | else: |
| 325 | args.update(get_party_details(party, party_type)) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 326 | |
Anand Doshi | 2f957f2 | 2016-04-08 17:36:10 +0530 | [diff] [blame] | 327 | if party_type in ("Customer", "Lead"): |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 328 | args.update({"tax_type": "Sales"}) |
Anand Doshi | 2f957f2 | 2016-04-08 17:36:10 +0530 | [diff] [blame] | 329 | |
| 330 | if party_type=='Lead': |
| 331 | args['customer'] = None |
| 332 | del args['lead'] |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 333 | else: |
| 334 | args.update({"tax_type": "Purchase"}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 335 | |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 336 | if use_for_shopping_cart: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 337 | args.update({"use_for_shopping_cart": use_for_shopping_cart}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 338 | |
| 339 | return get_tax_template(posting_date, args) |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 340 | |
shreyas | eba9ca4 | 2016-01-26 14:56:52 +0530 | [diff] [blame] | 341 | def validate_party_frozen_disabled(party_type, party_name): |
shreyas | 79872bf | 2016-01-26 13:57:06 +0530 | [diff] [blame] | 342 | if party_type and party_name: |
| 343 | party = frappe.db.get_value(party_type, party_name, ["is_frozen", "disabled"], as_dict=True) |
shreyas | c8ac3b0 | 2016-01-26 13:23:38 +0530 | [diff] [blame] | 344 | if party.disabled: |
Nabin Hait | d9449e3 | 2016-03-29 13:22:31 +0530 | [diff] [blame] | 345 | frappe.throw(_("{0} {1} is disabled").format(party_type, party_name), PartyDisabled) |
shreyas | c8ac3b0 | 2016-01-26 13:23:38 +0530 | [diff] [blame] | 346 | elif party.is_frozen: |
| 347 | frozen_accounts_modifier = frappe.db.get_value( 'Accounts Settings', None,'frozen_accounts_modifier') |
| 348 | if not frozen_accounts_modifier in frappe.get_roles(): |
Nabin Hait | d9449e3 | 2016-03-29 13:22:31 +0530 | [diff] [blame] | 349 | frappe.throw(_("{0} {1} is frozen").format(party_type, party_name), PartyFrozen) |
Rushabh Mehta | 15a7f21 | 2016-04-14 17:30:40 +0530 | [diff] [blame] | 350 | |
| 351 | def get_timeline_data(doctype, name): |
| 352 | '''returns timeline data for the past one year''' |
| 353 | from frappe.desk.form.load import get_communication_data |
Rushabh Mehta | 40b3a5b | 2016-05-20 11:20:59 +0530 | [diff] [blame] | 354 | data = get_communication_data(doctype, name, |
| 355 | fields = 'unix_timestamp(date(creation)), count(name)', |
| 356 | after = add_years(None, -1).strftime('%Y-%m-%d'), |
Rushabh Mehta | 15a7f21 | 2016-04-14 17:30:40 +0530 | [diff] [blame] | 357 | group_by='group by date(creation)', as_dict=False) |
| 358 | return dict(data) |