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