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 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 4 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | import frappe |
ruthra kumar | 8d9b90f | 2023-10-27 12:37:37 +0530 | [diff] [blame] | 6 | from frappe import _, msgprint, qb, scrub |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 7 | from frappe.contacts.doctype.address.address import get_company_address, get_default_address |
Suraj Shetty | 7ed37ae | 2018-12-19 19:56:42 +0530 | [diff] [blame] | 8 | from frappe.core.doctype.user_permission.user_permission import get_permitted_documents |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 9 | from frappe.model.utils import get_fetch_values |
Gursheen Anand | 87df7ff | 2024-02-21 18:44:29 +0530 | [diff] [blame] | 10 | from frappe.query_builder.functions import Abs, Count, Date, Sum |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | from frappe.utils import ( |
| 12 | add_days, |
| 13 | add_months, |
| 14 | add_years, |
| 15 | cint, |
| 16 | cstr, |
| 17 | date_diff, |
| 18 | flt, |
| 19 | formatdate, |
| 20 | get_last_day, |
| 21 | get_timestamp, |
| 22 | getdate, |
| 23 | nowdate, |
| 24 | ) |
tunde | 4a263c7 | 2017-08-02 22:20:35 +0100 | [diff] [blame] | 25 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 26 | import erpnext |
| 27 | from erpnext import get_company_currency |
| 28 | from erpnext.accounts.utils import get_fiscal_year |
| 29 | from erpnext.exceptions import InvalidAccountCurrency, PartyDisabled, PartyFrozen |
Sagar Vora | 4205f56 | 2023-07-24 18:37:36 +0530 | [diff] [blame] | 30 | from erpnext.utilities.regional import temporary_flag |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 31 | |
Vishakh Desai | c2bda2c | 2023-11-09 18:26:34 +0530 | [diff] [blame] | 32 | PURCHASE_TRANSACTION_TYPES = { |
| 33 | "Supplier Quotation", |
| 34 | "Purchase Order", |
| 35 | "Purchase Receipt", |
| 36 | "Purchase Invoice", |
| 37 | } |
Sagar Vora | 5c06620 | 2023-03-13 14:02:50 +0000 | [diff] [blame] | 38 | SALES_TRANSACTION_TYPES = { |
| 39 | "Quotation", |
| 40 | "Sales Order", |
| 41 | "Delivery Note", |
| 42 | "Sales Invoice", |
| 43 | "POS Invoice", |
| 44 | } |
| 45 | TRANSACTION_TYPES = PURCHASE_TRANSACTION_TYPES | SALES_TRANSACTION_TYPES |
| 46 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 47 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | class DuplicatePartyAccountError(frappe.ValidationError): |
| 49 | pass |
| 50 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 52 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 53 | def get_party_details( |
| 54 | party=None, |
| 55 | account=None, |
| 56 | party_type="Customer", |
| 57 | company=None, |
| 58 | posting_date=None, |
| 59 | bill_date=None, |
| 60 | price_list=None, |
| 61 | currency=None, |
| 62 | doctype=None, |
| 63 | ignore_permissions=False, |
| 64 | fetch_payment_terms_template=True, |
| 65 | party_address=None, |
| 66 | company_address=None, |
| 67 | shipping_address=None, |
| 68 | pos_profile=None, |
| 69 | ): |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 70 | if not party: |
| 71 | return {} |
Nabin Hait | 5ef121b | 2015-06-08 12:26:52 +0530 | [diff] [blame] | 72 | if not frappe.db.exists(party_type, party): |
| 73 | frappe.throw(_("{0}: {1} does not exists").format(party_type, party)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 74 | return _get_party_details( |
| 75 | party, |
| 76 | account, |
| 77 | party_type, |
| 78 | company, |
| 79 | posting_date, |
| 80 | bill_date, |
| 81 | price_list, |
| 82 | currency, |
| 83 | doctype, |
| 84 | ignore_permissions, |
| 85 | fetch_payment_terms_template, |
| 86 | party_address, |
| 87 | company_address, |
| 88 | shipping_address, |
| 89 | pos_profile, |
| 90 | ) |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 91 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 92 | |
| 93 | def _get_party_details( |
| 94 | party=None, |
| 95 | account=None, |
| 96 | party_type="Customer", |
| 97 | company=None, |
| 98 | posting_date=None, |
| 99 | bill_date=None, |
| 100 | price_list=None, |
| 101 | currency=None, |
| 102 | doctype=None, |
| 103 | ignore_permissions=False, |
| 104 | fetch_payment_terms_template=True, |
| 105 | party_address=None, |
| 106 | company_address=None, |
| 107 | shipping_address=None, |
| 108 | pos_profile=None, |
| 109 | ): |
| 110 | party_details = frappe._dict( |
| 111 | set_account_and_due_date(party, account, party_type, company, posting_date, bill_date, doctype) |
| 112 | ) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 113 | party = party_details[party_type.lower()] |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 114 | party = frappe.get_doc(party_type, party) |
Ankush Menat | e84c9f7 | 2024-01-03 20:10:19 +0530 | [diff] [blame] | 115 | |
| 116 | if not ignore_permissions: |
| 117 | ptype = "select" if frappe.only_has_select_perm(party_type) else "read" |
| 118 | frappe.has_permission(party_type, ptype, party, throw=True) |
| 119 | |
Deepesh Garg | b72cdb4 | 2022-01-21 18:00:35 +0530 | [diff] [blame] | 120 | currency = party.get("default_currency") or currency or get_company_currency(company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 121 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 122 | party_address, shipping_address = set_address_details( |
| 123 | party_details, |
| 124 | party, |
| 125 | party_type, |
| 126 | doctype, |
| 127 | company, |
| 128 | party_address, |
| 129 | company_address, |
| 130 | shipping_address, |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 131 | ignore_permissions=ignore_permissions, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 132 | ) |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 133 | set_contact_details(party_details, party, party_type) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 134 | set_other_values(party_details, party, party_type) |
| 135 | set_price_list(party_details, party, party_type, price_list, pos_profile) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 136 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 137 | tax_template = set_taxes( |
| 138 | party.name, |
| 139 | party_type, |
| 140 | posting_date, |
| 141 | company, |
| 142 | customer_group=party_details.customer_group, |
| 143 | supplier_group=party_details.supplier_group, |
| 144 | tax_category=party_details.tax_category, |
| 145 | billing_address=party_address, |
| 146 | shipping_address=shipping_address, |
| 147 | ) |
Deepesh Garg | f12be30 | 2021-11-30 20:34:53 +0530 | [diff] [blame] | 148 | |
| 149 | if tax_template: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 150 | party_details["taxes_and_charges"] = tax_template |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 151 | |
Deepesh Garg | bcf56e6 | 2021-08-06 23:53:16 +0530 | [diff] [blame] | 152 | if cint(fetch_payment_terms_template): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 153 | 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] | 154 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 155 | if not party_details.get("currency"): |
| 156 | party_details["currency"] = currency |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 157 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 158 | # sales team |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | if party_type == "Customer": |
| 160 | party_details["sales_team"] = [ |
| 161 | { |
| 162 | "sales_person": d.sales_person, |
| 163 | "allocated_percentage": d.allocated_percentage or None, |
| 164 | "commission_rate": d.commission_rate, |
| 165 | } |
| 166 | for d in party.get("sales_team") |
| 167 | ] |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 168 | |
Zarrar | 7f8024c | 2018-08-01 17:45:05 +0530 | [diff] [blame] | 169 | # supplier tax withholding category |
| 170 | if party_type == "Supplier" and party: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 171 | 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] | 172 | |
Vishal Dhayagude | 7aafc90 | 2023-03-28 16:48:03 +0530 | [diff] [blame] | 173 | if not party_details.get("tax_category") and pos_profile: |
| 174 | party_details["tax_category"] = frappe.get_value("POS Profile", pos_profile, "tax_category") |
| 175 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 176 | return party_details |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 177 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 178 | |
| 179 | def set_address_details( |
| 180 | party_details, |
| 181 | party, |
| 182 | party_type, |
| 183 | doctype=None, |
| 184 | company=None, |
| 185 | party_address=None, |
| 186 | company_address=None, |
| 187 | shipping_address=None, |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 188 | *, |
Dany Robert | f34ffc2 | 2023-11-20 11:10:56 +0000 | [diff] [blame] | 189 | ignore_permissions=False, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 190 | ): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 191 | billing_address_field = "customer_address" if party_type == "Lead" else party_type.lower() + "_address" |
| 192 | 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] | 193 | if doctype: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 194 | party_details.update( |
| 195 | get_fetch_values(doctype, billing_address_field, party_details[billing_address_field]) |
| 196 | ) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 197 | # address display |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 198 | party_details.address_display = render_address( |
| 199 | party_details[billing_address_field], check_permissions=not ignore_permissions |
| 200 | ) |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 201 | # shipping address |
| 202 | if party_type in ["Customer", "Lead"]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 203 | party_details.shipping_address_name = shipping_address or get_party_shipping_address( |
| 204 | party_type, party.name |
| 205 | ) |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 206 | party_details.shipping_address = render_address( |
| 207 | party_details["shipping_address_name"], check_permissions=not ignore_permissions |
| 208 | ) |
Nabin Hait | 7eba1a3 | 2017-10-02 15:59:27 +0530 | [diff] [blame] | 209 | if doctype: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 210 | party_details.update( |
| 211 | get_fetch_values(doctype, "shipping_address_name", party_details.shipping_address_name) |
| 212 | ) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 213 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 214 | if company_address: |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 215 | party_details.company_address = company_address |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 216 | else: |
| 217 | party_details.update(get_company_address(company)) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 218 | |
Sagar Vora | 5c06620 | 2023-03-13 14:02:50 +0000 | [diff] [blame] | 219 | if doctype in SALES_TRANSACTION_TYPES and party_details.company_address: |
| 220 | party_details.update(get_fetch_values(doctype, "company_address", party_details.company_address)) |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 221 | |
Sagar Vora | 5c06620 | 2023-03-13 14:02:50 +0000 | [diff] [blame] | 222 | if doctype in PURCHASE_TRANSACTION_TYPES: |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 223 | if shipping_address: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 224 | party_details.update( |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 225 | shipping_address=shipping_address, |
Vishakh Desai | 45299fe | 2023-11-18 17:10:01 +0530 | [diff] [blame] | 226 | shipping_address_display=render_address( |
| 227 | shipping_address, check_permissions=not ignore_permissions |
| 228 | ), |
Dany Robert | f34ffc2 | 2023-11-20 11:10:56 +0000 | [diff] [blame] | 229 | **get_fetch_values(doctype, "shipping_address", shipping_address), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 230 | ) |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 231 | |
| 232 | if party_details.company_address: |
| 233 | # billing address |
| 234 | party_details.update( |
| 235 | billing_address=party_details.company_address, |
| 236 | billing_address_display=( |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 237 | party_details.company_address_display |
| 238 | or render_address(party_details.company_address, check_permissions=False) |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 239 | ), |
Dany Robert | f34ffc2 | 2023-11-20 11:10:56 +0000 | [diff] [blame] | 240 | **get_fetch_values(doctype, "billing_address", party_details.company_address), |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 241 | ) |
| 242 | |
| 243 | # shipping address - if not already set |
| 244 | if not party_details.shipping_address: |
| 245 | party_details.update( |
| 246 | shipping_address=party_details.billing_address, |
| 247 | shipping_address_display=party_details.billing_address_display, |
Dany Robert | f34ffc2 | 2023-11-20 11:10:56 +0000 | [diff] [blame] | 248 | **get_fetch_values(doctype, "shipping_address", party_details.billing_address), |
Sagar Vora | a3625b3 | 2022-07-29 12:41:40 +0530 | [diff] [blame] | 249 | ) |
| 250 | |
Sagar Vora | 5c06620 | 2023-03-13 14:02:50 +0000 | [diff] [blame] | 251 | party_address, shipping_address = ( |
| 252 | party_details.get(billing_address_field), |
| 253 | party_details.shipping_address_name, |
| 254 | ) |
| 255 | |
| 256 | party_details["tax_category"] = get_address_tax_category( |
| 257 | party.get("tax_category"), |
| 258 | party_address, |
| 259 | shipping_address if party_type != "Supplier" else party_address, |
| 260 | ) |
| 261 | |
| 262 | if doctype in TRANSACTION_TYPES: |
Sagar Vora | 4205f56 | 2023-07-24 18:37:36 +0530 | [diff] [blame] | 263 | with temporary_flag("company", company): |
| 264 | get_regional_address_details(party_details, doctype, company) |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 265 | |
Sagar Vora | 5c06620 | 2023-03-13 14:02:50 +0000 | [diff] [blame] | 266 | return party_address, shipping_address |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 267 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 268 | |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 269 | @erpnext.allow_regional |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 270 | def get_regional_address_details(party_details, doctype, company): |
Shreya Shah | 4fa600a | 2018-06-05 11:27:53 +0530 | [diff] [blame] | 271 | pass |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 272 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 273 | |
David Arnold | 462204f | 2024-03-25 20:12:44 +0100 | [diff] [blame] | 274 | def complete_contact_details(party_details): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 275 | if not party_details.contact_person: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 276 | party_details.update( |
| 277 | { |
| 278 | "contact_person": None, |
| 279 | "contact_display": None, |
| 280 | "contact_email": None, |
| 281 | "contact_mobile": None, |
| 282 | "contact_phone": None, |
| 283 | "contact_designation": None, |
| 284 | "contact_department": None, |
| 285 | } |
| 286 | ) |
Neil Trini Lasrado | fe2ffae | 2015-07-08 18:16:51 +0530 | [diff] [blame] | 287 | else: |
rohitwaghchaure | 2851a41 | 2023-10-18 16:31:35 +0530 | [diff] [blame] | 288 | fields = [ |
| 289 | "name as contact_person", |
| 290 | "full_name as contact_display", |
| 291 | "email_id as contact_email", |
| 292 | "mobile_no as contact_mobile", |
| 293 | "phone as contact_phone", |
| 294 | "designation as contact_designation", |
| 295 | "department as contact_department", |
| 296 | ] |
| 297 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 298 | contact_details = frappe.db.get_value("Contact", party_details.contact_person, fields, as_dict=True) |
rohitwaghchaure | 2851a41 | 2023-10-18 16:31:35 +0530 | [diff] [blame] | 299 | |
| 300 | party_details.update(contact_details) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 301 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 302 | |
David Arnold | 462204f | 2024-03-25 20:12:44 +0100 | [diff] [blame] | 303 | def set_contact_details(party_details, party, party_type): |
| 304 | party_details.contact_person = get_default_contact(party_type, party.name) |
| 305 | complete_contact_details(party_details) |
| 306 | |
| 307 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 308 | def set_other_values(party_details, party, party_type): |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 309 | # copy |
ChillarAnand | 9ace7d6 | 2022-03-07 16:53:59 +0530 | [diff] [blame] | 310 | if party_type == "Customer": |
saurabh | 8e49951 | 2016-02-19 11:08:45 +0530 | [diff] [blame] | 311 | to_copy = ["customer_name", "customer_group", "territory", "language"] |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 312 | else: |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 313 | to_copy = ["supplier_name", "supplier_group", "language"] |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 314 | for f in to_copy: |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 315 | party_details[f] = party.get(f) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 316 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 317 | # fields prepended with default in Customer doctype |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 318 | for f in ["currency"] + (["sales_partner", "commission_rate"] if party_type == "Customer" else []): |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 319 | if party.get("default_" + f): |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 320 | party_details[f] = party.get("default_" + f) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 321 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 322 | |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 323 | def get_default_price_list(party): |
| 324 | """Return default price list for party (Document object)""" |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 325 | if party.get("default_price_list"): |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 326 | return party.default_price_list |
| 327 | |
| 328 | if party.doctype == "Customer": |
Daizy Modi | 27df455 | 2022-11-03 13:38:48 +0530 | [diff] [blame] | 329 | return frappe.get_cached_value("Customer Group", party.customer_group, "default_price_list") |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 330 | |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 331 | |
Deepesh Garg | 6e2c13f | 2019-12-10 15:55:05 +0530 | [diff] [blame] | 332 | 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] | 333 | # price list |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 334 | price_list = get_permitted_documents("Price List") |
rohitwaghchaure | 5210761 | 2020-05-05 12:12:33 +0530 | [diff] [blame] | 335 | |
Faris Ansari | 64f01bd | 2020-04-14 11:51:48 +0530 | [diff] [blame] | 336 | # if there is only one permitted document based on user permissions, set it |
| 337 | if price_list and len(price_list) == 1: |
Suraj Shetty | dcc90e3 | 2018-05-04 17:52:57 +0530 | [diff] [blame] | 338 | price_list = price_list[0] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 339 | elif pos and party_type == "Customer": |
| 340 | customer_price_list = frappe.get_value("Customer", party.name, "default_price_list") |
deepeshgarg007 | c48efab | 2019-01-24 17:15:38 +0530 | [diff] [blame] | 341 | |
| 342 | if customer_price_list: |
| 343 | price_list = customer_price_list |
| 344 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 345 | pos_price_list = frappe.get_value("POS Profile", pos, "selling_price_list") |
deepeshgarg007 | 1f9a6fe | 2019-01-25 11:53:55 +0530 | [diff] [blame] | 346 | price_list = pos_price_list or given_price_list |
Suraj Shetty | dcc90e3 | 2018-05-04 17:52:57 +0530 | [diff] [blame] | 347 | else: |
| 348 | price_list = get_default_price_list(party) or given_price_list |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 349 | |
| 350 | if price_list: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 351 | party_details.price_list_currency = frappe.db.get_value( |
| 352 | "Price List", price_list, "currency", cache=True |
| 353 | ) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 354 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 355 | 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] | 356 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 357 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 358 | 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] | 359 | if doctype not in ["POS Invoice", "Sales Invoice", "Purchase Invoice"]: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 360 | # not an invoice |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 361 | return {party_type.lower(): party} |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 362 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 363 | if party: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 364 | account = get_party_account(party_type, party, company) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 365 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 366 | account_fieldname = "debit_to" if party_type == "Customer" else "credit_to" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 367 | out = { |
| 368 | party_type.lower(): party, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 369 | account_fieldname: account, |
| 370 | "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] | 371 | } |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 372 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 373 | return out |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 374 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 375 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 376 | @frappe.whitelist() |
Gursheen Anand | 7591f10 | 2023-06-12 11:06:03 +0530 | [diff] [blame] | 377 | def get_party_account(party_type, party=None, company=None, include_advance=False): |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 378 | """Returns the account for the given `party`. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 379 | Will first search in party (Customer / Supplier) record, if not found, |
| 380 | will search in group (Customer Group / Supplier Group), |
| 381 | finally will return default.""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 382 | if not company: |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 383 | frappe.throw(_("Please select a Company")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 384 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 385 | if not party and party_type in ["Customer", "Supplier"]: |
| 386 | default_account_name = ( |
| 387 | "default_receivable_account" if party_type == "Customer" else "default_payable_account" |
| 388 | ) |
Deepesh Garg | 8864857 | 2021-11-12 12:39:30 +0530 | [diff] [blame] | 389 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 390 | return frappe.get_cached_value("Company", company, default_account_name) |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 391 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 392 | account = frappe.db.get_value( |
| 393 | "Party Account", {"parenttype": party_type, "parent": party, "company": company}, "account" |
| 394 | ) |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 395 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 396 | if not account and party_type in ["Customer", "Supplier"]: |
| 397 | party_group_doctype = "Customer Group" if party_type == "Customer" else "Supplier Group" |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 398 | group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 399 | account = frappe.db.get_value( |
| 400 | "Party Account", |
| 401 | {"parenttype": party_group_doctype, "parent": group, "company": company}, |
| 402 | "account", |
| 403 | ) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 404 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 405 | if not account and party_type in ["Customer", "Supplier"]: |
| 406 | default_account_name = ( |
| 407 | "default_receivable_account" if party_type == "Customer" else "default_payable_account" |
| 408 | ) |
| 409 | account = frappe.get_cached_value("Company", company, default_account_name) |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 410 | |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 411 | existing_gle_currency = get_party_gle_currency(party_type, party, company) |
| 412 | if existing_gle_currency: |
| 413 | if account: |
Daizy Modi | 010bd9c | 2022-11-03 14:36:48 +0530 | [diff] [blame] | 414 | account_currency = frappe.get_cached_value("Account", account, "account_currency") |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 415 | if (account and account_currency != existing_gle_currency) or not account: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 416 | account = get_party_gle_account(party_type, party, company) |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 417 | |
RitvikSardana | d2f03c8 | 2023-09-05 12:00:54 +0530 | [diff] [blame] | 418 | if include_advance and party_type in ["Customer", "Supplier", "Student"]: |
Gursheen Anand | 7591f10 | 2023-06-12 11:06:03 +0530 | [diff] [blame] | 419 | advance_account = get_party_advance_account(party_type, party, company) |
Deepesh Garg | 92f845c | 2023-06-21 12:21:19 +0530 | [diff] [blame] | 420 | if advance_account: |
| 421 | return [account, advance_account] |
| 422 | else: |
| 423 | return [account] |
| 424 | |
Saurabh | 0f86d86 | 2017-11-10 19:21:09 +0530 | [diff] [blame] | 425 | return account |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 426 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 427 | |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 428 | def get_party_advance_account(party_type, party, company): |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 429 | account = frappe.db.get_value( |
Gursheen Anand | 4ee1637 | 2023-06-08 13:15:23 +0530 | [diff] [blame] | 430 | "Party Account", |
| 431 | {"parenttype": party_type, "parent": party, "company": company}, |
| 432 | "advance_account", |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 433 | ) |
| 434 | |
| 435 | if not account: |
| 436 | party_group_doctype = "Customer Group" if party_type == "Customer" else "Supplier Group" |
| 437 | group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype)) |
| 438 | account = frappe.db.get_value( |
| 439 | "Party Account", |
| 440 | {"parenttype": party_group_doctype, "parent": group, "company": company}, |
Gursheen Anand | 4ee1637 | 2023-06-08 13:15:23 +0530 | [diff] [blame] | 441 | "advance_account", |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 442 | ) |
Gursheen Anand | 4ee1637 | 2023-06-08 13:15:23 +0530 | [diff] [blame] | 443 | |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 444 | if not account: |
Gursheen Anand | a06017c | 2023-06-12 15:24:53 +0530 | [diff] [blame] | 445 | account_name = ( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 446 | "default_advance_received_account" if party_type == "Customer" else "default_advance_paid_account" |
Gursheen Anand | a06017c | 2023-06-12 15:24:53 +0530 | [diff] [blame] | 447 | ) |
| 448 | account = frappe.get_cached_value("Company", company, account_name) |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 449 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 450 | return account |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 451 | |
| 452 | |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 453 | @frappe.whitelist() |
| 454 | def get_party_bank_account(party_type, party): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 455 | return frappe.db.get_value("Bank Account", {"party_type": party_type, "party": party, "is_default": 1}) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 456 | |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 457 | |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 458 | def get_party_account_currency(party_type, party, company): |
| 459 | def generator(): |
| 460 | party_account = get_party_account(party_type, party, company) |
Daizy Modi | 010bd9c | 2022-11-03 14:36:48 +0530 | [diff] [blame] | 461 | return frappe.get_cached_value("Account", party_account, "account_currency") |
Anand Doshi | b20baf8 | 2015-09-25 16:17:50 +0530 | [diff] [blame] | 462 | |
| 463 | return frappe.local_cache("party_account_currency", (party_type, party, company), generator) |
| 464 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 465 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 466 | def get_party_gle_currency(party_type, party, company): |
| 467 | def generator(): |
ruthra kumar | 8d9b90f | 2023-10-27 12:37:37 +0530 | [diff] [blame] | 468 | gl = qb.DocType("GL Entry") |
| 469 | existing_gle_currency = ( |
| 470 | qb.from_(gl) |
| 471 | .select(gl.account_currency) |
| 472 | .where( |
| 473 | (gl.docstatus == 1) |
| 474 | & (gl.company == company) |
| 475 | & (gl.party_type == party_type) |
| 476 | & (gl.party == party) |
| 477 | & (gl.is_cancelled == 0) |
| 478 | ) |
| 479 | .limit(1) |
| 480 | .run() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 481 | ) |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 482 | |
| 483 | return existing_gle_currency[0][0] if existing_gle_currency else None |
| 484 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 485 | return frappe.local_cache( |
| 486 | "party_gle_currency", (party_type, party, company), generator, regenerate_if_none=True |
| 487 | ) |
| 488 | |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 489 | |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 490 | def get_party_gle_account(party_type, party, company): |
| 491 | def generator(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 492 | existing_gle_account = frappe.db.sql( |
| 493 | """select account from `tabGL Entry` |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 494 | where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 495 | limit 1""", |
| 496 | {"company": company, "party_type": party_type, "party": party}, |
| 497 | ) |
Nabin Hait | 16d6959 | 2016-06-28 16:15:58 +0530 | [diff] [blame] | 498 | |
| 499 | return existing_gle_account[0][0] if existing_gle_account else None |
| 500 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 501 | return frappe.local_cache( |
| 502 | "party_gle_account", (party_type, party, company), generator, regenerate_if_none=True |
| 503 | ) |
| 504 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 505 | |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 506 | 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] | 507 | """Validate party account currency with existing GL Entry's currency""" |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 508 | if not party_account_currency: |
| 509 | party_account_currency = get_party_account_currency(party_type, party, company) |
| 510 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 511 | existing_gle_currency = get_party_gle_currency(party_type, party, company) |
| 512 | |
| 513 | if existing_gle_currency and party_account_currency != existing_gle_currency: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 514 | frappe.throw( |
| 515 | _( |
| 516 | "{0} {1} has accounting entries in currency {2} for company {3}. Please select a receivable or payable account with currency {2}." |
| 517 | ).format( |
| 518 | frappe.bold(party_type), |
| 519 | frappe.bold(party), |
| 520 | frappe.bold(existing_gle_currency), |
| 521 | frappe.bold(company), |
| 522 | ), |
| 523 | InvalidAccountCurrency, |
| 524 | ) |
| 525 | |
Anand Doshi | 4945b94 | 2015-09-30 16:41:15 +0530 | [diff] [blame] | 526 | |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 527 | def validate_party_accounts(doc): |
Deepesh Garg | 5a2b571 | 2022-02-18 20:05:49 +0530 | [diff] [blame] | 528 | from erpnext.controllers.accounts_controller import validate_account_head |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 529 | |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 530 | companies = [] |
| 531 | |
| 532 | for account in doc.get("accounts"): |
| 533 | if account.company in companies: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 534 | frappe.throw( |
| 535 | _("There can only be 1 Account per Company in {0} {1}").format(doc.doctype, doc.name), |
| 536 | DuplicatePartyAccountError, |
| 537 | ) |
Anand Doshi | da98ab6 | 2015-09-28 15:13:38 +0530 | [diff] [blame] | 538 | else: |
| 539 | companies.append(account.company) |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 540 | |
Daizy Modi | 010bd9c | 2022-11-03 14:36:48 +0530 | [diff] [blame] | 541 | party_account_currency = frappe.get_cached_value("Account", account.account, "account_currency") |
hendrik | c257ce8 | 2019-09-05 16:44:09 +0700 | [diff] [blame] | 542 | if frappe.db.get_default("Company"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 543 | company_default_currency = frappe.get_cached_value( |
| 544 | "Company", frappe.db.get_default("Company"), "default_currency" |
| 545 | ) |
hendrik | c257ce8 | 2019-09-05 16:44:09 +0700 | [diff] [blame] | 546 | else: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 547 | company_default_currency = frappe.get_cached_value("Company", account.company, "default_currency") |
Anand Doshi | d8bc40d | 2015-10-22 17:54:50 +0530 | [diff] [blame] | 548 | |
Deepesh Garg | 1a2cf5c | 2020-03-28 21:26:16 +0530 | [diff] [blame] | 549 | 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] | 550 | |
rohitwaghchaure | ea092a7 | 2017-02-01 12:02:08 +0530 | [diff] [blame] | 551 | if doc.get("default_currency") and party_account_currency and company_default_currency: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 552 | if ( |
| 553 | doc.default_currency != party_account_currency |
| 554 | and doc.default_currency != company_default_currency |
| 555 | ): |
| 556 | frappe.throw( |
| 557 | _( |
| 558 | "Billing currency must be equal to either default company's currency or party account currency" |
| 559 | ) |
| 560 | ) |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 561 | |
Deepesh Garg | 5a2b571 | 2022-02-18 20:05:49 +0530 | [diff] [blame] | 562 | # validate if account is mapped for same company |
Gursheen Anand | 7461926 | 2023-06-02 17:13:51 +0530 | [diff] [blame] | 563 | if account.account: |
| 564 | validate_account_head(account.idx, account.account, account.company) |
| 565 | if account.advance_account: |
| 566 | validate_account_head(account.idx, account.advance_account, account.company) |
Deepesh Garg | 5a2b571 | 2022-02-18 20:05:49 +0530 | [diff] [blame] | 567 | |
rohitwaghchaure | 62fea03 | 2016-03-30 13:24:42 +0530 | [diff] [blame] | 568 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 569 | @frappe.whitelist() |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 570 | 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] | 571 | """Get due date from `Payment Terms Template`""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 572 | due_date = None |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 573 | if (bill_date or posting_date) and party: |
| 574 | due_date = bill_date or posting_date |
marination | fa085d7 | 2020-10-27 13:18:30 +0530 | [diff] [blame] | 575 | template_name = get_payment_terms_template(party, party_type, company) |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 576 | |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 577 | if template_name: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 578 | 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] | 579 | else: |
| 580 | if party_type == "Supplier": |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 581 | supplier_group = frappe.get_cached_value(party_type, party, "supplier_group") |
| 582 | template_name = frappe.get_cached_value("Supplier Group", supplier_group, "payment_terms") |
tunde | e5973e4 | 2017-09-05 18:11:58 +0100 | [diff] [blame] | 583 | if template_name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 584 | due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime( |
| 585 | "%Y-%m-%d" |
| 586 | ) |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 587 | # If due date is calculated from bill_date, check this condition |
| 588 | if getdate(due_date) < getdate(posting_date): |
| 589 | due_date = posting_date |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 590 | return due_date |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 591 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 592 | |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 593 | def get_due_date_from_template(template_name, posting_date, bill_date): |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 594 | """ |
| 595 | Inspects all `Payment Term`s from the a `Payment Terms Template` and returns the due |
| 596 | date after considering all the `Payment Term`s requirements. |
| 597 | :param template_name: Name of the `Payment Terms Template` |
| 598 | :return: String representing the calculated due date |
| 599 | """ |
Shreya Shah | 3a9eec2 | 2018-02-16 13:05:21 +0530 | [diff] [blame] | 600 | due_date = getdate(bill_date or posting_date) |
| 601 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 602 | template = frappe.get_doc("Payment Terms Template", template_name) |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 603 | |
| 604 | for term in template.terms: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 605 | if term.due_date_based_on == "Day(s) after invoice date": |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 606 | due_date = max(due_date, add_days(due_date, term.credit_days)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 607 | elif term.due_date_based_on == "Day(s) after the end of the invoice month": |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 608 | due_date = max(due_date, add_days(get_last_day(due_date), term.credit_days)) |
| 609 | else: |
developsessions | ce8a108 | 2023-02-03 11:30:29 +0100 | [diff] [blame] | 610 | due_date = max(due_date, get_last_day(add_months(due_date, term.credit_months))) |
tunde | 3b4bd37 | 2017-08-31 16:28:47 +0100 | [diff] [blame] | 611 | return due_date |
| 612 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 613 | |
Deepesh Garg | 5b15718 | 2024-01-04 10:48:01 +0530 | [diff] [blame] | 614 | def validate_due_date(posting_date, due_date, bill_date=None, template_name=None): |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 615 | if getdate(due_date) < getdate(posting_date): |
Rohit Waghchaure | d1a85a3 | 2018-11-26 18:42:29 +0530 | [diff] [blame] | 616 | frappe.throw(_("Due Date cannot be before Posting / Supplier Invoice Date")) |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 617 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 618 | if not template_name: |
| 619 | return |
rohitwaghchaure | 3ffe896 | 2018-07-13 17:40:48 +0530 | [diff] [blame] | 620 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 621 | default_due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime( |
| 622 | "%Y-%m-%d" |
| 623 | ) |
Saurabh | d7897f1 | 2018-07-18 17:08:16 +0530 | [diff] [blame] | 624 | |
Anand Doshi | 0ca587e | 2015-09-25 16:32:14 +0530 | [diff] [blame] | 625 | if not default_due_date: |
| 626 | return |
| 627 | |
Nabin Hait | 93d3c82 | 2015-07-10 17:11:58 +0530 | [diff] [blame] | 628 | if default_due_date != posting_date and getdate(due_date) > getdate(default_due_date): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 629 | is_credit_controller = ( |
| 630 | frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() |
| 631 | ) |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 632 | if is_credit_controller: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 633 | msgprint( |
| 634 | _("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)").format( |
| 635 | date_diff(due_date, default_due_date) |
| 636 | ) |
| 637 | ) |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 638 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 639 | frappe.throw( |
| 640 | _("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date)) |
| 641 | ) |
| 642 | |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 643 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 644 | @frappe.whitelist() |
Nabin Hait | 16a7ec9 | 2019-09-05 13:01:15 +0530 | [diff] [blame] | 645 | def get_address_tax_category(tax_category=None, billing_address=None, shipping_address=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 646 | addr_tax_category_from = frappe.db.get_single_value( |
| 647 | "Accounts Settings", "determine_address_tax_category_from" |
| 648 | ) |
Saif Ur Rehman | fd531a6 | 2018-12-29 01:49:11 +0500 | [diff] [blame] | 649 | if addr_tax_category_from == "Shipping Address": |
| 650 | if shipping_address: |
| 651 | tax_category = frappe.db.get_value("Address", shipping_address, "tax_category") or tax_category |
| 652 | else: |
| 653 | if billing_address: |
| 654 | tax_category = frappe.db.get_value("Address", billing_address, "tax_category") or tax_category |
| 655 | |
| 656 | return cstr(tax_category) |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 657 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 658 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 659 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 660 | def set_taxes( |
| 661 | party, |
| 662 | party_type, |
| 663 | posting_date, |
| 664 | company, |
| 665 | customer_group=None, |
| 666 | supplier_group=None, |
| 667 | tax_category=None, |
| 668 | billing_address=None, |
| 669 | shipping_address=None, |
| 670 | use_for_shopping_cart=None, |
| 671 | ): |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 672 | from erpnext.accounts.doctype.tax_rule.tax_rule import get_party_details, get_tax_template |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 673 | |
| 674 | args = {party_type.lower(): party, "company": company} |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 675 | |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 676 | if tax_category: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 677 | args["tax_category"] = tax_category |
Saif Ur Rehman | 6778668 | 2018-12-27 02:11:07 +0500 | [diff] [blame] | 678 | |
Rushabh Mehta | 3c14c5a | 2017-09-29 13:21:22 +0530 | [diff] [blame] | 679 | if customer_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 680 | args["customer_group"] = customer_group |
Rushabh Mehta | 3c14c5a | 2017-09-29 13:21:22 +0530 | [diff] [blame] | 681 | |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 682 | if supplier_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 683 | args["supplier_group"] = supplier_group |
Rushabh Mehta | 3c14c5a | 2017-09-29 13:21:22 +0530 | [diff] [blame] | 684 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 685 | if billing_address or shipping_address: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 686 | args.update( |
| 687 | get_party_details( |
| 688 | party, party_type, {"billing_address": billing_address, "shipping_address": shipping_address} |
| 689 | ) |
| 690 | ) |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 691 | else: |
| 692 | args.update(get_party_details(party, party_type)) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 693 | |
Nabin Hait | 47ce6de | 2023-06-06 15:19:04 +0530 | [diff] [blame] | 694 | if party_type in ("Customer", "Lead", "Prospect"): |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 695 | args.update({"tax_type": "Sales"}) |
Anand Doshi | 2f957f2 | 2016-04-08 17:36:10 +0530 | [diff] [blame] | 696 | |
Nabin Hait | 47ce6de | 2023-06-06 15:19:04 +0530 | [diff] [blame] | 697 | if party_type in ["Lead", "Prospect"]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 698 | args["customer"] = None |
Nabin Hait | 47ce6de | 2023-06-06 15:19:04 +0530 | [diff] [blame] | 699 | del args[frappe.scrub(party_type)] |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 700 | else: |
| 701 | args.update({"tax_type": "Purchase"}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 702 | |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 703 | if use_for_shopping_cart: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 704 | args.update({"use_for_shopping_cart": use_for_shopping_cart}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 705 | |
| 706 | return get_tax_template(posting_date, args) |
shreyas | 29b565f | 2016-01-25 17:30:49 +0530 | [diff] [blame] | 707 | |
tunde | 400d046 | 2017-08-31 13:44:57 +0100 | [diff] [blame] | 708 | |
| 709 | @frappe.whitelist() |
marination | fa085d7 | 2020-10-27 13:18:30 +0530 | [diff] [blame] | 710 | def get_payment_terms_template(party_name, party_type, company=None): |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 711 | if party_type not in ("Customer", "Supplier"): |
| 712 | return |
tunde | 400d046 | 2017-08-31 13:44:57 +0100 | [diff] [blame] | 713 | template = None |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 714 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 715 | if party_type == "Customer": |
| 716 | customer = frappe.get_cached_value( |
| 717 | "Customer", party_name, fieldname=["payment_terms", "customer_group"], as_dict=1 |
| 718 | ) |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 719 | template = customer.payment_terms |
| 720 | |
| 721 | if not template and customer.customer_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 722 | template = frappe.get_cached_value("Customer Group", customer.customer_group, "payment_terms") |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 723 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 724 | supplier = frappe.get_cached_value( |
| 725 | "Supplier", party_name, fieldname=["payment_terms", "supplier_group"], as_dict=1 |
| 726 | ) |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 727 | template = supplier.payment_terms |
Zlash65 | 8907078 | 2018-04-19 18:37:29 +0530 | [diff] [blame] | 728 | if not template and supplier.supplier_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 729 | template = frappe.get_cached_value("Supplier Group", supplier.supplier_group, "payment_terms") |
Nabin Hait | cfa9d1a | 2018-01-29 16:07:21 +0530 | [diff] [blame] | 730 | |
| 731 | if not template and company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 732 | template = frappe.get_cached_value("Company", company, fieldname="payment_terms") |
tunde | 400d046 | 2017-08-31 13:44:57 +0100 | [diff] [blame] | 733 | return template |
| 734 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 735 | |
shreyas | eba9ca4 | 2016-01-26 14:56:52 +0530 | [diff] [blame] | 736 | def validate_party_frozen_disabled(party_type, party_name): |
Subin Tom | b8845b9 | 2021-07-30 16:30:18 +0530 | [diff] [blame] | 737 | if frappe.flags.ignore_party_validation: |
| 738 | return |
| 739 | |
shreyas | 79872bf | 2016-01-26 13:57:06 +0530 | [diff] [blame] | 740 | if party_type and party_name: |
KanchanChauhan | fbaa619 | 2017-01-30 15:15:58 +0530 | [diff] [blame] | 741 | if party_type in ("Customer", "Supplier"): |
Nabin Hait | 9a33bc6 | 2018-08-09 10:47:09 +0530 | [diff] [blame] | 742 | 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] | 743 | if party.disabled: |
| 744 | frappe.throw(_("{0} {1} is disabled").format(party_type, party_name), PartyDisabled) |
| 745 | elif party.get("is_frozen"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 746 | frozen_accounts_modifier = frappe.db.get_single_value( |
| 747 | "Accounts Settings", "frozen_accounts_modifier" |
| 748 | ) |
barredterra | eb9ee3f | 2023-12-05 11:22:55 +0100 | [diff] [blame] | 749 | if frozen_accounts_modifier not in frappe.get_roles(): |
KanchanChauhan | fbaa619 | 2017-01-30 15:15:58 +0530 | [diff] [blame] | 750 | frappe.throw(_("{0} {1} is frozen").format(party_type, party_name), PartyFrozen) |
| 751 | |
| 752 | elif party_type == "Employee": |
Rucha Mahabal | a38147a | 2021-06-12 13:33:21 +0530 | [diff] [blame] | 753 | if frappe.db.get_value("Employee", party_name, "status") != "Active": |
Kanchan Chauhan | 95108ac | 2017-04-11 15:55:34 +0530 | [diff] [blame] | 754 | 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] | 755 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 756 | |
Rushabh Mehta | 15a7f21 | 2016-04-14 17:30:40 +0530 | [diff] [blame] | 757 | def get_timeline_data(doctype, name): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 758 | """returns timeline data for the past one year""" |
Rushabh Mehta | 15a7f21 | 2016-04-14 17:30:40 +0530 | [diff] [blame] | 759 | from frappe.desk.form.load import get_communication_data |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 760 | |
| 761 | out = {} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 762 | after = add_years(None, -1).strftime("%Y-%m-%d") |
Zarrar | 5820018 | 2018-05-30 11:56:23 +0530 | [diff] [blame] | 763 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 764 | data = get_communication_data( |
| 765 | doctype, |
| 766 | name, |
| 767 | after=after, |
Gursheen Anand | 87df7ff | 2024-02-21 18:44:29 +0530 | [diff] [blame] | 768 | group_by="group by communication_date", |
| 769 | fields="C.communication_date as communication_date, count(C.name)", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 770 | as_dict=False, |
| 771 | ) |
Zarrar | 5820018 | 2018-05-30 11:56:23 +0530 | [diff] [blame] | 772 | |
| 773 | # fetch and append data from Activity Log |
Gursheen Anand | 87df7ff | 2024-02-21 18:44:29 +0530 | [diff] [blame] | 774 | activity_log = frappe.qb.DocType("Activity Log") |
| 775 | data += ( |
| 776 | frappe.qb.from_(activity_log) |
| 777 | .select(activity_log.communication_date, Count(activity_log.name)) |
| 778 | .where( |
| 779 | ( |
| 780 | ((activity_log.reference_doctype == doctype) & (activity_log.reference_name == name)) |
| 781 | | ((activity_log.timeline_doctype == doctype) & (activity_log.timeline_name == name)) |
| 782 | | ( |
| 783 | (activity_log.reference_doctype.isin(["Quotation", "Opportunity"])) |
| 784 | & (activity_log.timeline_name == name) |
| 785 | ) |
| 786 | ) |
| 787 | & (activity_log.status != "Success") |
| 788 | & (activity_log.creation > after) |
| 789 | ) |
| 790 | .groupby(activity_log.communication_date) |
| 791 | .orderby(activity_log.communication_date, order=frappe.qb.desc) |
| 792 | ).run() |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 793 | |
| 794 | timeline_items = dict(data) |
| 795 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 796 | for date, count in timeline_items.items(): |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 797 | timestamp = get_timestamp(date) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 798 | out.update({timestamp: count}) |
mbauskar | e7b87ad | 2017-02-16 13:12:29 +0530 | [diff] [blame] | 799 | |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 800 | return out |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 801 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 802 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 803 | def get_dashboard_info(party_type, party, loyalty_program=None): |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 804 | current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 805 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 806 | doctype = "Sales Invoice" if party_type == "Customer" else "Purchase Invoice" |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 807 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 808 | companies = frappe.get_all( |
| 809 | doctype, filters={"docstatus": 1, party_type.lower(): party}, distinct=1, fields=["company"] |
| 810 | ) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 811 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 812 | company_wise_info = [] |
| 813 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 814 | company_wise_grand_total = frappe.get_all( |
| 815 | doctype, |
deepeshgarg007 | ee05e35 | 2018-11-29 16:24:28 +0530 | [diff] [blame] | 816 | filters={ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 817 | "docstatus": 1, |
deepeshgarg007 | ee05e35 | 2018-11-29 16:24:28 +0530 | [diff] [blame] | 818 | party_type.lower(): party, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 819 | "posting_date": ( |
| 820 | "between", |
| 821 | [current_fiscal_year.year_start_date, current_fiscal_year.year_end_date], |
| 822 | ), |
| 823 | }, |
| 824 | group_by="company", |
| 825 | fields=[ |
| 826 | "company", |
| 827 | "sum(grand_total) as grand_total", |
| 828 | "sum(base_grand_total) as base_grand_total", |
| 829 | ], |
| 830 | ) |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 831 | |
deepeshgarg007 | a1d015c | 2018-12-26 11:01:02 +0530 | [diff] [blame] | 832 | loyalty_point_details = [] |
| 833 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 834 | if party_type == "Customer": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 835 | loyalty_point_details = frappe._dict( |
| 836 | frappe.get_all( |
| 837 | "Loyalty Point Entry", |
| 838 | filters={ |
| 839 | "customer": party, |
| 840 | "expiry_date": (">=", getdate()), |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 841 | }, |
| 842 | group_by="company", |
| 843 | fields=["company", "sum(loyalty_points) as loyalty_points"], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 844 | as_list=1, |
| 845 | ) |
| 846 | ) |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 847 | |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 848 | company_wise_billing_this_year = frappe._dict() |
| 849 | |
| 850 | for d in company_wise_grand_total: |
| 851 | company_wise_billing_this_year.setdefault( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 852 | d.company, {"grand_total": d.grand_total, "base_grand_total": d.base_grand_total} |
| 853 | ) |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 854 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 855 | company_wise_total_unpaid = frappe._dict( |
| 856 | frappe.db.sql( |
| 857 | """ |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 858 | select company, sum(debit_in_account_currency) - sum(credit_in_account_currency) |
Nabin Hait | 24f0b13 | 2017-05-24 13:01:26 +0530 | [diff] [blame] | 859 | from `tabGL Entry` |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 860 | where party_type = %s and party=%s |
Deepesh Garg | 802e63a | 2021-06-28 11:24:32 +0530 | [diff] [blame] | 861 | and is_cancelled = 0 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 862 | group by company""", |
| 863 | (party_type, party), |
| 864 | ) |
| 865 | ) |
Nabin Hait | 2d79a64 | 2017-05-24 12:41:14 +0530 | [diff] [blame] | 866 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 867 | for d in companies: |
Daizy Modi | 27df455 | 2022-11-03 13:38:48 +0530 | [diff] [blame] | 868 | company_default_currency = frappe.get_cached_value("Company", d.company, "default_currency") |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 869 | party_account_currency = get_party_account_currency(party_type, party, d.company) |
Rushabh Mehta | 919a74a | 2017-06-22 16:37:04 +0530 | [diff] [blame] | 870 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 871 | if party_account_currency == company_default_currency: |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 872 | 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] | 873 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 874 | 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] | 875 | |
| 876 | total_unpaid = flt(company_wise_total_unpaid.get(d.company)) |
| 877 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 878 | if loyalty_point_details: |
| 879 | loyalty_points = loyalty_point_details.get(d.company) |
| 880 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 881 | info = {} |
| 882 | info["billing_this_year"] = flt(billing_this_year) if billing_this_year else 0 |
| 883 | info["currency"] = party_account_currency |
| 884 | info["total_unpaid"] = flt(total_unpaid) if total_unpaid else 0 |
| 885 | info["company"] = d.company |
| 886 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 887 | if party_type == "Customer" and loyalty_point_details: |
| 888 | info["loyalty_points"] = loyalty_points |
| 889 | |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 890 | if party_type == "Supplier": |
| 891 | info["total_unpaid"] = -1 * info["total_unpaid"] |
| 892 | |
| 893 | company_wise_info.append(info) |
| 894 | |
| 895 | return company_wise_info |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 896 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 897 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 898 | def get_party_shipping_address(doctype: str, name: str) -> str | None: |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 899 | """ |
| 900 | Returns an Address name (best guess) for the given doctype and name for which `address_type == 'Shipping'` is true. |
| 901 | and/or `is_shipping_address = 1`. |
| 902 | |
| 903 | It returns an empty string if there is no matching record. |
| 904 | |
| 905 | :param doctype: Party Doctype |
| 906 | :param name: Party name |
| 907 | :return: String |
| 908 | """ |
Raffael Meyer | b91bb17 | 2023-06-08 16:19:09 +0200 | [diff] [blame] | 909 | shipping_addresses = frappe.get_all( |
| 910 | "Address", |
| 911 | filters=[ |
| 912 | ["Dynamic Link", "link_doctype", "=", doctype], |
| 913 | ["Dynamic Link", "link_name", "=", name], |
| 914 | ["disabled", "=", 0], |
| 915 | ], |
| 916 | or_filters=[ |
| 917 | ["is_shipping_address", "=", 1], |
| 918 | ["address_type", "=", "Shipping"], |
| 919 | ], |
| 920 | pluck="name", |
| 921 | limit=1, |
| 922 | order_by="is_shipping_address DESC", |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 923 | ) |
Raffael Meyer | b91bb17 | 2023-06-08 16:19:09 +0200 | [diff] [blame] | 924 | |
| 925 | return shipping_addresses[0] if shipping_addresses else None |
Rohit Waghchaure | affeb3d | 2019-01-15 17:38:31 +0530 | [diff] [blame] | 926 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 927 | |
| 928 | def get_partywise_advanced_payment_amount( |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 929 | party_type, posting_date=None, future_payment=0, company=None, party=None |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 930 | ): |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 931 | ple = frappe.qb.DocType("Payment Ledger Entry") |
Gursheen Anand | e355dea | 2023-07-28 14:51:28 +0530 | [diff] [blame] | 932 | query = ( |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 933 | frappe.qb.from_(ple) |
| 934 | .select(ple.party, Abs(Sum(ple.amount).as_("amount"))) |
Gursheen Anand | e355dea | 2023-07-28 14:51:28 +0530 | [diff] [blame] | 935 | .where( |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 936 | (ple.party_type.isin(party_type)) |
| 937 | & (ple.amount < 0) |
| 938 | & (ple.against_voucher_no == ple.voucher_no) |
| 939 | & (ple.delinked == 0) |
Gursheen Anand | e355dea | 2023-07-28 14:51:28 +0530 | [diff] [blame] | 940 | ) |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 941 | .groupby(ple.party) |
Gursheen Anand | e355dea | 2023-07-28 14:51:28 +0530 | [diff] [blame] | 942 | ) |
Gursheen Anand | e355dea | 2023-07-28 14:51:28 +0530 | [diff] [blame] | 943 | |
Rohit Waghchaure | 4ed7d03 | 2019-03-25 15:37:25 +0530 | [diff] [blame] | 944 | if posting_date: |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 945 | if future_payment: |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 946 | query = query.where((ple.posting_date <= posting_date) | (Date(ple.creation) <= posting_date)) |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 947 | else: |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 948 | query = query.where(ple.posting_date <= posting_date) |
Deepesh Garg | ed2c180 | 2020-05-27 12:46:12 +0530 | [diff] [blame] | 949 | |
Saqib | 3980591 | 2020-05-01 18:15:13 +0530 | [diff] [blame] | 950 | if company: |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 951 | query = query.where(ple.company == company) |
Rohit Waghchaure | 4ed7d03 | 2019-03-25 15:37:25 +0530 | [diff] [blame] | 952 | |
Nabin Hait | 3504bf7 | 2023-05-29 09:23:40 +0530 | [diff] [blame] | 953 | if party: |
ruthra kumar | 0dc5e5c | 2023-08-21 08:46:55 +0530 | [diff] [blame] | 954 | query = query.where(ple.party == party) |
Nabin Hait | 3504bf7 | 2023-05-29 09:23:40 +0530 | [diff] [blame] | 955 | |
Dany Robert | f34ffc2 | 2023-11-20 11:10:56 +0000 | [diff] [blame] | 956 | if invoice_doctypes := frappe.get_hooks("invoice_doctypes"): |
| 957 | query = query.where(ple.voucher_type.notin(invoice_doctypes)) |
| 958 | |
ruthra kumar | 896b123 | 2023-08-20 21:17:27 +0530 | [diff] [blame] | 959 | data = query.run() |
Rohit Waghchaure | affeb3d | 2019-01-15 17:38:31 +0530 | [diff] [blame] | 960 | if data: |
hendrik | c257ce8 | 2019-09-05 16:44:09 +0700 | [diff] [blame] | 961 | return frappe._dict(data) |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 962 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 963 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 964 | def get_default_contact(doctype: str, name: str) -> str | None: |
Deepesh Garg | cf9347d | 2020-03-31 17:28:43 +0530 | [diff] [blame] | 965 | """ |
Raffael Meyer | b91bb17 | 2023-06-08 16:19:09 +0200 | [diff] [blame] | 966 | Returns contact name only if there is a primary contact for given doctype and name. |
| 967 | |
| 968 | Else returns None |
| 969 | |
| 970 | :param doctype: Party Doctype |
| 971 | :param name: Party name |
| 972 | :return: String |
Ronel Cabrera | fcb5476 | 2019-11-18 17:00:07 +0800 | [diff] [blame] | 973 | """ |
Raffael Meyer | b91bb17 | 2023-06-08 16:19:09 +0200 | [diff] [blame] | 974 | contacts = frappe.get_all( |
| 975 | "Contact", |
| 976 | filters=[ |
| 977 | ["Dynamic Link", "link_doctype", "=", doctype], |
| 978 | ["Dynamic Link", "link_name", "=", name], |
| 979 | ], |
| 980 | or_filters=[ |
| 981 | ["is_primary_contact", "=", 1], |
| 982 | ["is_billing_contact", "=", 1], |
| 983 | ], |
| 984 | pluck="name", |
| 985 | limit=1, |
| 986 | order_by="is_primary_contact DESC, is_billing_contact DESC", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 987 | ) |
Raffael Meyer | b91bb17 | 2023-06-08 16:19:09 +0200 | [diff] [blame] | 988 | |
| 989 | return contacts[0] if contacts else None |
Deepesh Garg | bc34737 | 2022-05-25 15:42:47 +0530 | [diff] [blame] | 990 | |
| 991 | |
| 992 | def add_party_account(party_type, party, company, account): |
| 993 | doc = frappe.get_doc(party_type, party) |
| 994 | account_exists = False |
| 995 | for d in doc.get("accounts"): |
| 996 | if d.account == account: |
| 997 | account_exists = True |
| 998 | |
| 999 | if not account_exists: |
| 1000 | accounts = {"company": company, "account": account} |
| 1001 | |
| 1002 | doc.append("accounts", accounts) |
| 1003 | |
| 1004 | doc.save() |
Ankush Menat | f4d7499 | 2023-10-17 22:11:56 +0530 | [diff] [blame] | 1005 | |
| 1006 | |
| 1007 | def render_address(address, check_permissions=True): |
| 1008 | try: |
| 1009 | from frappe.contacts.doctype.address.address import render_address as _render |
| 1010 | except ImportError: |
| 1011 | # Older frappe versions where this function is not available |
| 1012 | from frappe.contacts.doctype.address.address import get_address_display as _render |
| 1013 | |
| 1014 | return frappe.call(_render, address, check_permissions=check_permissions) |