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