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