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