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 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | import frappe |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 7 | from frappe import _, msgprint, scrub |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from frappe.contacts.doctype.address.address import ( |
| 9 | get_address_display, |
| 10 | get_company_address, |
| 11 | get_default_address, |
| 12 | ) |
| 13 | from frappe.contacts.doctype.contact.contact import get_contact_details |
Suraj Shetty | 7ed37ae | 2018-12-19 19:56:42 +0530 | [diff] [blame] | 14 | from frappe.core.doctype.user_permission.user_permission import get_permitted_documents |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 15 | from frappe.model.utils import get_fetch_values |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 16 | from frappe.utils import ( |
| 17 | add_days, |
| 18 | add_months, |
| 19 | add_years, |
| 20 | cint, |
| 21 | cstr, |
| 22 | date_diff, |
| 23 | flt, |
| 24 | formatdate, |
| 25 | get_last_day, |
| 26 | get_timestamp, |
| 27 | getdate, |
| 28 | nowdate, |
| 29 | ) |
| 30 | from six import iteritems |
tunde | 4a263c7 | 2017-08-02 22:20:35 +0100 | [diff] [blame] | 31 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 32 | import erpnext |
| 33 | from erpnext import get_company_currency |
| 34 | from erpnext.accounts.utils import get_fiscal_year |
| 35 | from erpnext.exceptions import InvalidAccountCurrency, PartyDisabled, PartyFrozen |
| 36 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 37 | |
Anand Doshi | 248c867 | 2015-09-28 15:24:00 +0530 | [diff] [blame] | 38 | class DuplicatePartyAccountError(frappe.ValidationError): pass |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 39 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 40 | @frappe.whitelist() |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 41 | def get_party_details(party=None, account=None, party_type="Customer", company=None, posting_date=None, |
deepeshgarg007 | c48efab | 2019-01-24 17:15:38 +0530 | [diff] [blame] | 42 | bill_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False, fetch_payment_terms_template=True, |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 43 | party_address=None, company_address=None, shipping_address=None, pos_profile=None): |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 44 | |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 45 | if not party: |
| 46 | return {} |
Nabin Hait | 5ef121b | 2015-06-08 12:26:52 +0530 | [diff] [blame] | 47 | if not frappe.db.exists(party_type, party): |
| 48 | frappe.throw(_("{0}: {1} does not exists").format(party_type, party)) |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 49 | return _get_party_details(party, account, party_type, |
deepeshgarg007 | c48efab | 2019-01-24 17:15:38 +0530 | [diff] [blame] | 50 | company, posting_date, bill_date, price_list, currency, doctype, ignore_permissions, |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 51 | fetch_payment_terms_template, party_address, company_address, shipping_address, pos_profile) |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 52 | |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 53 | def _get_party_details(party=None, account=None, party_type="Customer", company=None, posting_date=None, |
Shreya Shah | 5615cb4 | 2018-10-07 11:42:07 +0530 | [diff] [blame] | 54 | bill_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False, |
Deepesh Garg | 15ff6a5 | 2020-02-18 12:28:41 +0530 | [diff] [blame] | 55 | fetch_payment_terms_template=True, party_address=None, company_address=None, shipping_address=None, pos_profile=None): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 56 | party_details = frappe._dict(set_account_and_due_date(party, account, party_type, company, posting_date, bill_date, doctype)) |
| 57 | party = party_details[party_type.lower()] |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 58 | |
Saurabh | 9dcb286 | 2021-02-01 20:36:15 +0530 | [diff] [blame] | 59 | if not ignore_permissions and not (frappe.has_permission(party_type, "read", party) or frappe.has_permission(party_type, "select", party)): |
Rushabh Mehta | 2d70887 | 2015-11-27 11:35:35 +0530 | [diff] [blame] | 60 | frappe.throw(_("Not permitted for {0}").format(party), frappe.PermissionError) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 61 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 62 | party = frappe.get_doc(party_type, party) |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 63 | currency = party.default_currency if party.get("default_currency") else get_company_currency(company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 64 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 65 | party_address, shipping_address = set_address_details(party_details, party, party_type, doctype, company, party_address, company_address, shipping_address) |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 66 | set_contact_details(party_details, party, party_type) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 67 | set_other_values(party_details, party, party_type) |
| 68 | set_price_list(party_details, party, party_type, price_list, pos_profile) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 69 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 70 | party_details["tax_category"] = get_address_tax_category(party.get("tax_category"), |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 71 | party_address, shipping_address if party_type != "Supplier" else party_address) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 72 | |
| 73 | if not party_details.get("taxes_and_charges"): |
| 74 | party_details["taxes_and_charges"] = set_taxes(party.name, party_type, posting_date, company, |
| 75 | customer_group=party_details.customer_group, supplier_group=party_details.supplier_group, tax_category=party_details.tax_category, |
| 76 | billing_address=party_address, shipping_address=shipping_address) |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 77 | |
Deepesh Garg | bcf56e6 | 2021-08-06 23:53:16 +0530 | [diff] [blame] | 78 | if cint(fetch_payment_terms_template): |
marination | fa085d7 | 2020-10-27 13:18:30 +0530 | [diff] [blame] | 79 | party_details["payment_terms_template"] = get_payment_terms_template(party.name, party_type, company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 80 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 81 | if not party_details.get("currency"): |
| 82 | party_details["currency"] = currency |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 83 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 84 | # sales team |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 85 | if party_type=="Customer": |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 86 | party_details["sales_team"] = [{ |
Anand Doshi | 4b72d05 | 2015-10-12 16:15:52 +0530 | [diff] [blame] | 87 | "sales_person": d.sales_person, |
| 88 | "allocated_percentage": d.allocated_percentage or None |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 89 | } for d in party.get("sales_team")] |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 90 | |
Zarrar | 7f8024c | 2018-08-01 17:45:05 +0530 | [diff] [blame] | 91 | # supplier tax withholding category |
| 92 | if party_type == "Supplier" and party: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 93 | party_details["supplier_tds"] = frappe.get_value(party_type, party.name, "tax_withholding_category") |
Zarrar | 7f8024c | 2018-08-01 17:45:05 +0530 | [diff] [blame] | 94 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 95 | return party_details |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 96 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 97 | def set_address_details(party_details, party, party_type, doctype=None, company=None, party_address=None, company_address=None, shipping_address=None): |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 98 | billing_address_field = "customer_address" if party_type == "Lead" \ |
| 99 | else party_type.lower() + "_address" |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 100 | party_details[billing_address_field] = party_address or get_default_address(party_type, party.name) |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 101 | if doctype: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 102 | party_details.update(get_fetch_values(doctype, billing_address_field, party_details[billing_address_field])) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 103 | # address display |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 104 | party_details.address_display = get_address_display(party_details[billing_address_field]) |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 105 | # shipping address |
| 106 | if party_type in ["Customer", "Lead"]: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 107 | party_details.shipping_address_name = shipping_address or get_party_shipping_address(party_type, party.name) |
| 108 | party_details.shipping_address = get_address_display(party_details["shipping_address_name"]) |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 109 | if doctype: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 110 | party_details.update(get_fetch_values(doctype, 'shipping_address_name', party_details.shipping_address_name)) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 111 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 112 | if company_address: |
| 113 | party_details.update({'company_address': company_address}) |
| 114 | else: |
| 115 | party_details.update(get_company_address(company)) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 116 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 117 | if doctype and doctype in ['Delivery Note', 'Sales Invoice', 'Sales Order']: |
| 118 | if party_details.company_address: |
| 119 | party_details.update(get_fetch_values(doctype, 'company_address', party_details.company_address)) |
| 120 | get_regional_address_details(party_details, doctype, company) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 121 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 122 | elif doctype and doctype in ["Purchase Invoice", "Purchase Order", "Purchase Receipt"]: |
| 123 | if party_details.company_address: |
| 124 | party_details["shipping_address"] = shipping_address or party_details["company_address"] |
| 125 | party_details.shipping_address_display = get_address_display(party_details["shipping_address"]) |
| 126 | party_details.update(get_fetch_values(doctype, 'shipping_address', party_details.shipping_address)) |
| 127 | get_regional_address_details(party_details, doctype, company) |
| 128 | |
| 129 | return party_details.get(billing_address_field), party_details.shipping_address_name |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 130 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 131 | @erpnext.allow_regional |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 132 | def get_regional_address_details(party_details, doctype, company): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 133 | pass |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 134 | |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 135 | def set_contact_details(party_details, party, party_type): |
| 136 | party_details.contact_person = get_default_contact(party_type, party.name) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 137 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 138 | if not party_details.contact_person: |
| 139 | party_details.update({ |
Neil Trini Lasrado | 09a66c4 | 2015-07-07 16:41:37 +0530 | [diff] [blame] | 140 | "contact_person": None, |
| 141 | "contact_display": None, |
| 142 | "contact_email": None, |
| 143 | "contact_mobile": None, |
| 144 | "contact_phone": None, |
| 145 | "contact_designation": None, |
| 146 | "contact_department": None |
| 147 | }) |
Neil Trini Lasrado | fe2ffae | 2015-07-08 18:16:51 +0530 | [diff] [blame] | 148 | else: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 149 | party_details.update(get_contact_details(party_details.contact_person)) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 150 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 151 | def set_other_values(party_details, party, party_type): |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 152 | # copy |
| 153 | if party_type=="Customer": |
saurabh | 8e49951 | 2016-02-19 11:08:45 +0530 | [diff] [blame] | 154 | to_copy = ["customer_name", "customer_group", "territory", "language"] |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 155 | else: |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 156 | to_copy = ["supplier_name", "supplier_group", "language"] |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 157 | for f in to_copy: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 158 | party_details[f] = party.get(f) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 159 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 160 | # fields prepended with default in Customer doctype |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 161 | for f in ['currency'] \ |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 162 | + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []): |
| 163 | if party.get("default_" + f): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 164 | party_details[f] = party.get("default_" + f) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 165 | |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 166 | def get_default_price_list(party): |
| 167 | """Return default price list for party (Document object)""" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 168 | if party.get("default_price_list"): |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 169 | return party.default_price_list |
| 170 | |
| 171 | if party.doctype == "Customer": |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 172 | price_list = frappe.get_cached_value("Customer Group", |
| 173 | party.customer_group, "default_price_list") |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 174 | if price_list: |
| 175 | return price_list |
| 176 | |
| 177 | return None |
| 178 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 179 | def set_price_list(party_details, party, party_type, given_price_list, pos=None): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 180 | # price list |
Suraj Shetty | 7ed37ae | 2018-12-19 19:56:42 +0530 | [diff] [blame] | 181 | price_list = get_permitted_documents('Price List') |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 182 | |
Faris Ansari | 64f01bd | 2020-04-14 11:51:48 +0530 | [diff] [blame] | 183 | # if there is only one permitted document based on user permissions, set it |
| 184 | if price_list and len(price_list) == 1: |
Suraj Shetty | dcc90e3 | 2018-05-04 17:52:57 +0530 | [diff] [blame] | 185 | price_list = price_list[0] |
deepeshgarg007 | c48efab | 2019-01-24 17:15:38 +0530 | [diff] [blame] | 186 | elif pos and party_type == 'Customer': |
| 187 | customer_price_list = frappe.get_value('Customer', party.name, 'default_price_list') |
| 188 | |
| 189 | if customer_price_list: |
| 190 | price_list = customer_price_list |
| 191 | else: |
deepeshgarg007 | 8c84b7b | 2019-01-25 16:44:45 +0530 | [diff] [blame] | 192 | pos_price_list = frappe.get_value('POS Profile', pos, 'selling_price_list') |
deepeshgarg007 | 1f9a6fe | 2019-01-25 11:53:55 +0530 | [diff] [blame] | 193 | price_list = pos_price_list or given_price_list |
Suraj Shetty | dcc90e3 | 2018-05-04 17:52:57 +0530 | [diff] [blame] | 194 | else: |
| 195 | price_list = get_default_price_list(party) or given_price_list |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 196 | |
| 197 | if price_list: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 198 | party_details.price_list_currency = frappe.db.get_value("Price List", price_list, "currency", cache=True) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 199 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 200 | party_details["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] | 201 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 202 | |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 203 | def set_account_and_due_date(party, account, party_type, company, posting_date, bill_date, doctype): |
Saqib | a6f98d4 | 2020-07-23 18:51:26 +0530 | [diff] [blame] | 204 | if doctype not in ["POS Invoice", "Sales Invoice", "Purchase Invoice"]: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 205 | # not an invoice |
| 206 | return { |
| 207 | party_type.lower(): party |
| 208 | } |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 209 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 210 | if party: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 211 | account = get_party_account(party_type, party, company) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 212 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 213 | account_fieldname = "debit_to" if party_type=="Customer" else "credit_to" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 214 | out = { |
| 215 | party_type.lower(): party, |
| 216 | account_fieldname : account, |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 217 | "due_date": get_due_date(posting_date, party_type, party, company, bill_date) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 218 | } |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 219 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 220 | return out |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 221 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 222 | @frappe.whitelist() |
marination | e9b04dc | 2020-10-16 12:36:13 +0530 | [diff] [blame] | 223 | def get_party_account(party_type, party, company=None): |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 224 | """Returns the account for the given `party`. |
| 225 | Will first search in party (Customer / Supplier) record, if not found, |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 226 | will search in group (Customer Group / Supplier Group), |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 227 | finally will return default.""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 228 | if not company: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 229 | frappe.throw(_("Please select a Company")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 230 | |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 231 | if not party: |
Nabin Hait | 6605919 | 2017-11-13 15:06:35 +0530 | [diff] [blame] | 232 | return |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 233 | |
| 234 | account = frappe.db.get_value("Party Account", |
| 235 | {"parenttype": party_type, "parent": party, "company": company}, "account") |
| 236 | |
| 237 | if not account and party_type in ['Customer', 'Supplier']: |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 238 | party_group_doctype = "Customer Group" if party_type=="Customer" else "Supplier Group" |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 239 | group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype)) |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 240 | account = frappe.db.get_value("Party Account", |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 241 | {"parenttype": party_group_doctype, "parent": group, "company": company}, "account") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 242 | |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 243 | if not account and party_type in ['Customer', 'Supplier']: |
| 244 | default_account_name = "default_receivable_account" \ |
| 245 | if party_type=="Customer" else "default_payable_account" |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 246 | account = frappe.get_cached_value('Company', company, default_account_name) |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 247 | |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 248 | existing_gle_currency = get_party_gle_currency(party_type, party, company) |
| 249 | if existing_gle_currency: |
| 250 | if account: |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 251 | account_currency = frappe.db.get_value("Account", account, "account_currency", cache=True) |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 252 | if (account and account_currency != existing_gle_currency) or not account: |
| 253 | account = get_party_gle_account(party_type, party, company) |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 254 | |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 255 | return account |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 256 | |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 257 | @frappe.whitelist() |
| 258 | def get_party_bank_account(party_type, party): |
| 259 | return frappe.db.get_value('Bank Account', { |
| 260 | 'party_type': party_type, |
| 261 | 'party': party, |
| 262 | 'is_default': 1 |
| 263 | }) |
| 264 | |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 265 | def get_party_account_currency(party_type, party, company): |
| 266 | def generator(): |
| 267 | party_account = get_party_account(party_type, party, company) |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 268 | return frappe.db.get_value("Account", party_account, "account_currency", cache=True) |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 269 | |
| 270 | return frappe.local_cache("party_account_currency", (party_type, party, company), generator) |
| 271 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 272 | def get_party_gle_currency(party_type, party, company): |
| 273 | def generator(): |
| 274 | existing_gle_currency = frappe.db.sql("""select account_currency from `tabGL Entry` |
| 275 | where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s |
| 276 | limit 1""", { "company": company, "party_type": party_type, "party": party }) |
| 277 | |
| 278 | return existing_gle_currency[0][0] if existing_gle_currency else None |
| 279 | |
Anand Doshi | 0b93bdc | 2015-10-22 19:32:56 +0530 | [diff] [blame] | 280 | return frappe.local_cache("party_gle_currency", (party_type, party, company), generator, |
| 281 | regenerate_if_none=True) |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 282 | |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 283 | def get_party_gle_account(party_type, party, company): |
| 284 | def generator(): |
| 285 | existing_gle_account = frappe.db.sql("""select account from `tabGL Entry` |
| 286 | where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s |
| 287 | limit 1""", { "company": company, "party_type": party_type, "party": party }) |
| 288 | |
| 289 | return existing_gle_account[0][0] if existing_gle_account else None |
| 290 | |
| 291 | return frappe.local_cache("party_gle_account", (party_type, party, company), generator, |
| 292 | regenerate_if_none=True) |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 293 | |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 294 | 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] | 295 | """Validate party account currency with existing GL Entry's currency""" |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 296 | if not party_account_currency: |
| 297 | party_account_currency = get_party_account_currency(party_type, party, company) |
| 298 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 299 | existing_gle_currency = get_party_gle_currency(party_type, party, company) |
| 300 | |
| 301 | if existing_gle_currency and party_account_currency != existing_gle_currency: |
Deepesh Garg | c6b850e | 2020-03-28 21:17:58 +0530 | [diff] [blame] | 302 | frappe.throw(_("{0} {1} has accounting entries in currency {2} for company {3}. Please select a receivable or payable account with currency {2}.") |
Deepesh Garg | 56bc36f | 2020-03-30 15:05:20 +0530 | [diff] [blame] | 303 | .format(frappe.bold(party_type), frappe.bold(party), frappe.bold(existing_gle_currency), frappe.bold(company)), InvalidAccountCurrency) |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 304 | |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 305 | def validate_party_accounts(doc): |
Subin Tom | b8845b9 | 2021-07-30 16:30:18 +0530 | [diff] [blame] | 306 | |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 307 | companies = [] |
| 308 | |
| 309 | for account in doc.get("accounts"): |
| 310 | if account.company in companies: |
Nabin Hait | 2873f2e | 2015-10-16 13:07:45 +0530 | [diff] [blame] | 311 | frappe.throw(_("There can only be 1 Account per Company in {0} {1}") |
| 312 | .format(doc.doctype, doc.name), DuplicatePartyAccountError) |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 313 | else: |
| 314 | companies.append(account.company) |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 315 | |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 316 | party_account_currency = frappe.db.get_value("Account", account.account, "account_currency", cache=True) |
hendrik | c257ce8 | 2019-09-05 16:44:09 +0700 | [diff] [blame] | 317 | if frappe.db.get_default("Company"): |
| 318 | company_default_currency = frappe.get_cached_value('Company', |
| 319 | frappe.db.get_default("Company"), "default_currency") |
| 320 | else: |
| 321 | company_default_currency = frappe.db.get_value('Company', account.company, "default_currency") |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 322 | |
Deepesh Garg | 1a2cf5c | 2020-03-28 21:26:16 +0530 | [diff] [blame] | 323 | validate_party_gle_currency(doc.doctype, doc.name, account.company, party_account_currency) |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 324 | |
rohitwaghchaure | ea092a7 | 2017-02-01 12:02:08 +0530 | [diff] [blame] | 325 | if doc.get("default_currency") and party_account_currency and company_default_currency: |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 326 | if doc.default_currency != party_account_currency and doc.default_currency != company_default_currency: |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 327 | frappe.throw(_("Billing currency must be equal to either default company's currency or party account currency")) |
| 328 | |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 329 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 330 | @frappe.whitelist() |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 331 | def get_due_date(posting_date, party_type, party, company=None, bill_date=None): |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 332 | """Get due date from `Payment Terms Template`""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 333 | due_date = None |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 334 | if (bill_date or posting_date) and party: |
| 335 | due_date = bill_date or posting_date |
marination | fa085d7 | 2020-10-27 13:18:30 +0530 | [diff] [blame] | 336 | template_name = get_payment_terms_template(party, party_type, company) |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 337 | |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 338 | if template_name: |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 339 | due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime("%Y-%m-%d") |
tunde | ed86efb | 2017-09-05 01:15:31 +0100 | [diff] [blame] | 340 | else: |
| 341 | if party_type == "Supplier": |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 342 | supplier_group = frappe.get_cached_value(party_type, party, "supplier_group") |
| 343 | template_name = frappe.get_cached_value("Supplier Group", supplier_group, "payment_terms") |
tunde | e5973e4 | 2017-09-05 18:11:58 +0100 | [diff] [blame] | 344 | if template_name: |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 345 | due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime("%Y-%m-%d") |
| 346 | # If due date is calculated from bill_date, check this condition |
| 347 | if getdate(due_date) < getdate(posting_date): |
| 348 | due_date = posting_date |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 349 | return due_date |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 350 | |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 351 | def get_due_date_from_template(template_name, posting_date, bill_date): |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 352 | """ |
| 353 | Inspects all `Payment Term`s from the a `Payment Terms Template` and returns the due |
| 354 | date after considering all the `Payment Term`s requirements. |
| 355 | :param template_name: Name of the `Payment Terms Template` |
| 356 | :return: String representing the calculated due date |
| 357 | """ |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 358 | due_date = getdate(bill_date or posting_date) |
| 359 | |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 360 | template = frappe.get_doc('Payment Terms Template', template_name) |
| 361 | |
| 362 | for term in template.terms: |
| 363 | if term.due_date_based_on == 'Day(s) after invoice date': |
| 364 | due_date = max(due_date, add_days(due_date, term.credit_days)) |
| 365 | elif term.due_date_based_on == 'Day(s) after the end of the invoice month': |
| 366 | due_date = max(due_date, add_days(get_last_day(due_date), term.credit_days)) |
| 367 | else: |
| 368 | due_date = max(due_date, add_months(get_last_day(due_date), term.credit_months)) |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 369 | return due_date |
| 370 | |
Saurabh | d7897f1 | 2018-07-18 17:08:16 +0530 | [diff] [blame] | 371 | def validate_due_date(posting_date, due_date, party_type, party, company=None, bill_date=None, template_name=None): |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 372 | if getdate(due_date) < getdate(posting_date): |
Rohit Waghchaure | d1a85a3 | 2018-11-26 18:42:29 +0530 | [diff] [blame] | 373 | frappe.throw(_("Due Date cannot be before Posting / Supplier Invoice Date")) |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 374 | else: |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 375 | if not template_name: return |
| 376 | |
Saurabh | d7897f1 | 2018-07-18 17:08:16 +0530 | [diff] [blame] | 377 | default_due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime("%Y-%m-%d") |
| 378 | |
Anand Doshi | 0ca587e | 2015-09-25 16:32:14 +0530 | [diff] [blame] | 379 | if not default_due_date: |
| 380 | return |
| 381 | |
Nabin Hait | 93d3c82 | 2015-07-10 17:11:58 +0530 | [diff] [blame] | 382 | 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] | 383 | is_credit_controller = frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() |
| 384 | if is_credit_controller: |
| 385 | msgprint(_("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)") |
| 386 | .format(date_diff(due_date, default_due_date))) |
| 387 | else: |
Nabin Hait | 0551f7b | 2017-11-21 19:58:16 +0530 | [diff] [blame] | 388 | frappe.throw(_("Due / Reference Date cannot be after {0}") |
| 389 | .format(formatdate(default_due_date))) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 390 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 391 | @frappe.whitelist() |
Nabin Hait | 16a7ec9 | 2019-09-05 13:01:15 +0530 | [diff] [blame] | 392 | def get_address_tax_category(tax_category=None, billing_address=None, shipping_address=None): |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 393 | addr_tax_category_from = frappe.db.get_single_value("Accounts Settings", "determine_address_tax_category_from") |
| 394 | if addr_tax_category_from == "Shipping Address": |
| 395 | if shipping_address: |
| 396 | tax_category = frappe.db.get_value("Address", shipping_address, "tax_category") or tax_category |
| 397 | else: |
| 398 | if billing_address: |
| 399 | tax_category = frappe.db.get_value("Address", billing_address, "tax_category") or tax_category |
| 400 | |
| 401 | return cstr(tax_category) |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 402 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 403 | @frappe.whitelist() |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 404 | def set_taxes(party, party_type, posting_date, company, customer_group=None, supplier_group=None, tax_category=None, |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 405 | billing_address=None, shipping_address=None, use_for_shopping_cart=None): |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 406 | from erpnext.accounts.doctype.tax_rule.tax_rule import get_party_details, get_tax_template |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 407 | args = { |
Rushabh Mehta | 307978f | 2015-09-23 15:43:09 +0530 | [diff] [blame] | 408 | party_type.lower(): party, |
marination | e27b996 | 2020-07-03 22:03:25 +0530 | [diff] [blame] | 409 | "company": company |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 410 | } |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 411 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 412 | if tax_category: |
| 413 | args['tax_category'] = tax_category |
| 414 | |
Rushabh Mehta | 3c14c5a | 2017-09-29 13:21:22 +0530 | [diff] [blame] | 415 | if customer_group: |
| 416 | args['customer_group'] = customer_group |
| 417 | |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 418 | if supplier_group: |
| 419 | args['supplier_group'] = supplier_group |
Rushabh Mehta | 3c14c5a | 2017-09-29 13:21:22 +0530 | [diff] [blame] | 420 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 421 | if billing_address or shipping_address: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 422 | args.update(get_party_details(party, party_type, {"billing_address": billing_address, \ |
| 423 | "shipping_address": shipping_address })) |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 424 | else: |
| 425 | args.update(get_party_details(party, party_type)) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 426 | |
Anand Doshi | 2f957f2 | 2016-04-08 17:36:10 +0530 | [diff] [blame] | 427 | if party_type in ("Customer", "Lead"): |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 428 | args.update({"tax_type": "Sales"}) |
Anand Doshi | 2f957f2 | 2016-04-08 17:36:10 +0530 | [diff] [blame] | 429 | |
| 430 | if party_type=='Lead': |
| 431 | args['customer'] = None |
| 432 | del args['lead'] |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 433 | else: |
| 434 | args.update({"tax_type": "Purchase"}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 435 | |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 436 | if use_for_shopping_cart: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 437 | args.update({"use_for_shopping_cart": use_for_shopping_cart}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 438 | |
| 439 | return get_tax_template(posting_date, args) |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 440 | |
tunde | 400d046 | 2017-08-31 13:44:57 +0100 | [diff] [blame] | 441 | |
| 442 | @frappe.whitelist() |
marination | fa085d7 | 2020-10-27 13:18:30 +0530 | [diff] [blame] | 443 | def get_payment_terms_template(party_name, party_type, company=None): |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 444 | if party_type not in ("Customer", "Supplier"): |
| 445 | return |
tunde | 400d046 | 2017-08-31 13:44:57 +0100 | [diff] [blame] | 446 | template = None |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 447 | if party_type == 'Customer': |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 448 | customer = frappe.get_cached_value("Customer", party_name, |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 449 | fieldname=['payment_terms', "customer_group"], as_dict=1) |
| 450 | template = customer.payment_terms |
| 451 | |
| 452 | if not template and customer.customer_group: |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 453 | template = frappe.get_cached_value("Customer Group", |
| 454 | customer.customer_group, 'payment_terms') |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 455 | else: |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 456 | supplier = frappe.get_cached_value("Supplier", party_name, |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 457 | fieldname=['payment_terms', "supplier_group"], as_dict=1) |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 458 | template = supplier.payment_terms |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 459 | if not template and supplier.supplier_group: |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 460 | template = frappe.get_cached_value("Supplier Group", supplier.supplier_group, 'payment_terms') |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 461 | |
| 462 | if not template and company: |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 463 | template = frappe.get_cached_value('Company', company, fieldname='payment_terms') |
tunde | 400d046 | 2017-08-31 13:44:57 +0100 | [diff] [blame] | 464 | return template |
| 465 | |
shreyas | eba9ca4 | 2016-01-26 14:56:52 +0530 | [diff] [blame] | 466 | def validate_party_frozen_disabled(party_type, party_name): |
Subin Tom | b8845b9 | 2021-07-30 16:30:18 +0530 | [diff] [blame] | 467 | |
| 468 | if frappe.flags.ignore_party_validation: |
| 469 | return |
| 470 | |
shreyas | 79872bf | 2016-01-26 13:57:06 +0530 | [diff] [blame] | 471 | if party_type and party_name: |
KanchanChauhan | fbaa619 | 2017-01-30 15:15:58 +0530 | [diff] [blame] | 472 | if party_type in ("Customer", "Supplier"): |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 473 | party = frappe.get_cached_value(party_type, party_name, ["is_frozen", "disabled"], as_dict=True) |
deepeshgarg007 | 737ec6b | 2019-10-20 17:36:36 +0530 | [diff] [blame] | 474 | if party.disabled: |
| 475 | frappe.throw(_("{0} {1} is disabled").format(party_type, party_name), PartyDisabled) |
| 476 | elif party.get("is_frozen"): |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 477 | frozen_accounts_modifier = frappe.db.get_single_value( 'Accounts Settings', 'frozen_accounts_modifier') |
KanchanChauhan | fbaa619 | 2017-01-30 15:15:58 +0530 | [diff] [blame] | 478 | if not frozen_accounts_modifier in frappe.get_roles(): |
| 479 | frappe.throw(_("{0} {1} is frozen").format(party_type, party_name), PartyFrozen) |
| 480 | |
| 481 | elif party_type == "Employee": |
Rucha Mahabal | a38147a | 2021-06-12 13:33:21 +0530 | [diff] [blame] | 482 | if frappe.db.get_value("Employee", party_name, "status") != "Active": |
Kanchan Chauhan | 95108ac | 2017-04-11 15:55:34 +0530 | [diff] [blame] | 483 | 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] | 484 | |
| 485 | def get_timeline_data(doctype, name): |
| 486 | '''returns timeline data for the past one year''' |
| 487 | from frappe.desk.form.load import get_communication_data |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 488 | |
| 489 | out = {} |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 490 | fields = 'creation, count(*)' |
Zarrar | 5820018 | 2018-05-30 11:56:23 +0530 | [diff] [blame] | 491 | after = add_years(None, -1).strftime('%Y-%m-%d') |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 492 | group_by='group by Date(creation)' |
Zarrar | 5820018 | 2018-05-30 11:56:23 +0530 | [diff] [blame] | 493 | |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 494 | data = get_communication_data(doctype, name, after=after, group_by='group by creation', |
| 495 | fields='C.creation as creation, count(C.name)',as_dict=False) |
Zarrar | 5820018 | 2018-05-30 11:56:23 +0530 | [diff] [blame] | 496 | |
| 497 | # fetch and append data from Activity Log |
| 498 | data += frappe.db.sql("""select {fields} |
| 499 | from `tabActivity Log` |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 500 | where (reference_doctype=%(doctype)s and reference_name=%(name)s) |
| 501 | or (timeline_doctype in (%(doctype)s) and timeline_name=%(name)s) |
| 502 | or (reference_doctype in ("Quotation", "Opportunity") and timeline_name=%(name)s) |
Zarrar | 5820018 | 2018-05-30 11:56:23 +0530 | [diff] [blame] | 503 | and status!='Success' and creation > {after} |
| 504 | {group_by} order by creation desc |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 505 | """.format(fields=fields, group_by=group_by, after=after), { |
| 506 | "doctype": doctype, |
| 507 | "name": name |
| 508 | }, as_dict=False) |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 509 | |
| 510 | timeline_items = dict(data) |
| 511 | |
Achilles Rasquinha | 56b2e12 | 2018-02-13 14:42:40 +0530 | [diff] [blame] | 512 | for date, count in iteritems(timeline_items): |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 513 | timestamp = get_timestamp(date) |
| 514 | out.update({ timestamp: count }) |
| 515 | |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 516 | return out |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 517 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 518 | def get_dashboard_info(party_type, party, loyalty_program=None): |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 519 | current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 520 | |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 521 | doctype = "Sales Invoice" if party_type=="Customer" else "Purchase Invoice" |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 522 | |
deepeshgarg007 | ee05e35 | 2018-11-29 16:24:28 +0530 | [diff] [blame] | 523 | companies = frappe.get_all(doctype, filters={ |
| 524 | 'docstatus': 1, |
| 525 | party_type.lower(): party |
| 526 | }, distinct=1, fields=['company']) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 527 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 528 | company_wise_info = [] |
| 529 | |
deepeshgarg007 | ee05e35 | 2018-11-29 16:24:28 +0530 | [diff] [blame] | 530 | company_wise_grand_total = frappe.get_all(doctype, |
| 531 | filters={ |
| 532 | 'docstatus': 1, |
| 533 | party_type.lower(): party, |
| 534 | 'posting_date': ('between', [current_fiscal_year.year_start_date, current_fiscal_year.year_end_date]) |
| 535 | }, |
| 536 | group_by="company", |
| 537 | fields=["company", "sum(grand_total) as grand_total", "sum(base_grand_total) as base_grand_total"] |
| 538 | ) |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 539 | |
deepeshgarg007 | a1d015c | 2018-12-26 11:01:02 +0530 | [diff] [blame] | 540 | loyalty_point_details = [] |
| 541 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 542 | if party_type == "Customer": |
| 543 | loyalty_point_details = frappe._dict(frappe.get_all("Loyalty Point Entry", |
| 544 | filters={ |
| 545 | 'customer': party, |
| 546 | 'expiry_date': ('>=', getdate()), |
| 547 | }, |
| 548 | group_by="company", |
| 549 | fields=["company", "sum(loyalty_points) as loyalty_points"], |
| 550 | as_list =1 |
| 551 | )) |
| 552 | |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 553 | company_wise_billing_this_year = frappe._dict() |
| 554 | |
| 555 | for d in company_wise_grand_total: |
| 556 | company_wise_billing_this_year.setdefault( |
| 557 | d.company,{ |
| 558 | "grand_total": d.grand_total, |
| 559 | "base_grand_total": d.base_grand_total |
| 560 | }) |
| 561 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 562 | |
| 563 | company_wise_total_unpaid = frappe._dict(frappe.db.sql(""" |
| 564 | select company, sum(debit_in_account_currency) - sum(credit_in_account_currency) |
Nabin Hait | 24f0b13 | 2017-05-24 13:01:26 +0530 | [diff] [blame] | 565 | from `tabGL Entry` |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 566 | where party_type = %s and party=%s |
Deepesh Garg | 802e63a | 2021-06-28 11:24:32 +0530 | [diff] [blame] | 567 | and is_cancelled = 0 |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 568 | group by company""", (party_type, party))) |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 569 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 570 | for d in companies: |
| 571 | company_default_currency = frappe.db.get_value("Company", d.company, 'default_currency') |
| 572 | party_account_currency = get_party_account_currency(party_type, party, d.company) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 573 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 574 | if party_account_currency==company_default_currency: |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 575 | billing_this_year = flt(company_wise_billing_this_year.get(d.company,{}).get("base_grand_total")) |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 576 | else: |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 577 | billing_this_year = flt(company_wise_billing_this_year.get(d.company,{}).get("grand_total")) |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 578 | |
| 579 | total_unpaid = flt(company_wise_total_unpaid.get(d.company)) |
| 580 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 581 | if loyalty_point_details: |
| 582 | loyalty_points = loyalty_point_details.get(d.company) |
| 583 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 584 | info = {} |
| 585 | info["billing_this_year"] = flt(billing_this_year) if billing_this_year else 0 |
| 586 | info["currency"] = party_account_currency |
| 587 | info["total_unpaid"] = flt(total_unpaid) if total_unpaid else 0 |
| 588 | info["company"] = d.company |
| 589 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 590 | if party_type == "Customer" and loyalty_point_details: |
| 591 | info["loyalty_points"] = loyalty_points |
| 592 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 593 | if party_type == "Supplier": |
| 594 | info["total_unpaid"] = -1 * info["total_unpaid"] |
| 595 | |
| 596 | company_wise_info.append(info) |
| 597 | |
| 598 | return company_wise_info |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 599 | |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 600 | def get_party_shipping_address(doctype, name): |
| 601 | """ |
| 602 | Returns an Address name (best guess) for the given doctype and name for which `address_type == 'Shipping'` is true. |
| 603 | and/or `is_shipping_address = 1`. |
| 604 | |
| 605 | It returns an empty string if there is no matching record. |
| 606 | |
| 607 | :param doctype: Party Doctype |
| 608 | :param name: Party name |
| 609 | :return: String |
| 610 | """ |
| 611 | out = frappe.db.sql( |
| 612 | 'SELECT dl.parent ' |
| 613 | 'from `tabDynamic Link` dl join `tabAddress` ta on dl.parent=ta.name ' |
| 614 | 'where ' |
| 615 | 'dl.link_doctype=%s ' |
| 616 | 'and dl.link_name=%s ' |
| 617 | 'and dl.parenttype="Address" ' |
rohitwaghchaure | 5472fff | 2018-12-10 17:45:39 +0530 | [diff] [blame] | 618 | 'and ifnull(ta.disabled, 0) = 0 and' |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 619 | '(ta.address_type="Shipping" or ta.is_shipping_address=1) ' |
| 620 | 'order by ta.is_shipping_address desc, ta.address_type desc limit 1', |
| 621 | (doctype, name) |
| 622 | ) |
| 623 | if out: |
| 624 | return out[0][0] |
| 625 | else: |
| 626 | return '' |
Rohit Waghchaure | affeb3d | 2019-01-15 17:38:31 +0530 | [diff] [blame] | 627 | |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 628 | def get_partywise_advanced_payment_amount(party_type, posting_date = None, future_payment=0, company=None): |
Rohit Waghchaure | 4ed7d03 | 2019-03-25 15:37:25 +0530 | [diff] [blame] | 629 | cond = "1=1" |
| 630 | if posting_date: |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 631 | if future_payment: |
UrvashiKishnani | 810a361 | 2021-03-02 08:20:03 +0400 | [diff] [blame] | 632 | cond = "posting_date <= '{0}' OR DATE(creation) <= '{0}' """.format(posting_date) |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 633 | else: |
UrvashiKishnani | 810a361 | 2021-03-02 08:20:03 +0400 | [diff] [blame] | 634 | cond = "posting_date <= '{0}'".format(posting_date) |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 635 | |
Saqib | 3980591 | 2020-05-01 18:15:13 +0530 | [diff] [blame] | 636 | if company: |
UrvashiKishnani | 810a361 | 2021-03-02 08:20:03 +0400 | [diff] [blame] | 637 | cond += "and company = {0}".format(frappe.db.escape(company)) |
Rohit Waghchaure | 4ed7d03 | 2019-03-25 15:37:25 +0530 | [diff] [blame] | 638 | |
UrvashiKishnani | 810a361 | 2021-03-02 08:20:03 +0400 | [diff] [blame] | 639 | data = frappe.db.sql(""" SELECT party, sum({0}) as amount |
| 640 | FROM `tabGL Entry` |
Rohit Waghchaure | 1a6e015 | 2019-03-19 15:06:01 +0530 | [diff] [blame] | 641 | WHERE |
UrvashiKishnani | 810a361 | 2021-03-02 08:20:03 +0400 | [diff] [blame] | 642 | party_type = %s and against_voucher is null |
| 643 | and is_cancelled = 0 |
| 644 | and {1} GROUP BY party""" |
Rohit Waghchaure | 4ed7d03 | 2019-03-25 15:37:25 +0530 | [diff] [blame] | 645 | .format(("credit") if party_type == "Customer" else "debit", cond) , party_type) |
Rohit Waghchaure | affeb3d | 2019-01-15 17:38:31 +0530 | [diff] [blame] | 646 | |
| 647 | if data: |
hendrik | c257ce8 | 2019-09-05 16:44:09 +0700 | [diff] [blame] | 648 | return frappe._dict(data) |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 649 | |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 650 | def get_default_contact(doctype, name): |
| 651 | """ |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 652 | Returns default contact for the given doctype and name. |
| 653 | Can be ordered by `contact_type` to either is_primary_contact or is_billing_contact. |
| 654 | """ |
| 655 | out = frappe.db.sql(""" |
| 656 | SELECT dl.parent, c.is_primary_contact, c.is_billing_contact |
| 657 | FROM `tabDynamic Link` dl |
| 658 | INNER JOIN tabContact c ON c.name = dl.parent |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 659 | WHERE |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 660 | dl.link_doctype=%s AND |
| 661 | dl.link_name=%s AND |
| 662 | dl.parenttype = "Contact" |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 663 | ORDER BY is_primary_contact DESC, is_billing_contact DESC |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 664 | """, (doctype, name)) |
| 665 | if out: |
| 666 | try: |
| 667 | return out[0][0] |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 668 | except Exception: |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 669 | return None |
| 670 | else: |
Faris Ansari | 64f01bd | 2020-04-14 11:51:48 +0530 | [diff] [blame] | 671 | return None |