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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 6 | import frappe |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 7 | import datetime |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 8 | from frappe import _, msgprint, scrub |
Anand Doshi | fab0904 | 2014-05-27 08:39:35 +0530 | [diff] [blame] | 9 | from frappe.defaults import get_user_permissions |
Nabin Hait | 6aea1c0 | 2015-08-18 11:31:02 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, getdate, formatdate, get_first_day, date_diff |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 11 | from erpnext.utilities.doctype.address.address import get_address_display |
| 12 | from erpnext.utilities.doctype.contact.contact import get_contact_details |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 13 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 14 | class InvalidCurrency(frappe.ValidationError): pass |
| 15 | class InvalidAccountCurrency(frappe.ValidationError): pass |
| 16 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 17 | @frappe.whitelist() |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 18 | def get_party_details(party=None, account=None, party_type="Customer", company=None, |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 19 | posting_date=None, price_list=None, currency=None, doctype=None): |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 21 | if not party: |
| 22 | return {} |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 23 | |
Nabin Hait | 5ef121b | 2015-06-08 12:26:52 +0530 | [diff] [blame] | 24 | if not frappe.db.exists(party_type, party): |
| 25 | frappe.throw(_("{0}: {1} does not exists").format(party_type, party)) |
Rushabh Mehta | d7a5b73 | 2015-04-01 23:38:13 +0530 | [diff] [blame] | 26 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 27 | return _get_party_details(party, account, party_type, |
| 28 | company, posting_date, price_list, currency, doctype) |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 29 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 30 | def _get_party_details(party=None, account=None, party_type="Customer", company=None, |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 31 | posting_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False): |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 32 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 33 | out = frappe._dict(set_account_and_due_date(party, account, party_type, company, posting_date, doctype)) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 34 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 35 | party = out[party_type.lower()] |
| 36 | |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 37 | if not ignore_permissions and not frappe.has_permission(party_type, "read", party): |
Rushabh Mehta | 8a40c13 | 2014-04-15 16:30:55 +0530 | [diff] [blame] | 38 | frappe.throw(_("Not permitted"), frappe.PermissionError) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 39 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 40 | party = frappe.get_doc(party_type, party) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 41 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 42 | set_address_details(out, party, party_type) |
| 43 | set_contact_details(out, party, party_type) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 44 | set_other_values(out, party, party_type) |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 45 | set_price_list(out, party, party_type, price_list) |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 46 | out["taxes_and_charges"] = set_taxes(party.name, party_type, posting_date, company, out.customer_group, out.supplier_type) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 47 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 48 | if not out.get("currency"): |
| 49 | out["currency"] = currency |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 50 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 51 | # sales team |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 52 | if party_type=="Customer": |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 53 | out["sales_team"] = [{ |
Rushabh Mehta | 97c858a | 2015-09-21 10:03:38 +0530 | [diff] [blame] | 54 | "sales_person": d.sales_person |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 55 | } for d in party.get("sales_team")] |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 56 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 57 | return out |
| 58 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 59 | def set_address_details(out, party, party_type): |
| 60 | billing_address_field = "customer_address" if party_type == "Lead" \ |
| 61 | else party_type.lower() + "_address" |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 62 | out[billing_address_field] = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 63 | {party_type.lower(): party.name, "is_primary_address":1}, "name") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 64 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 65 | # address display |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 66 | out.address_display = get_address_display(out[billing_address_field]) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 67 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 68 | # shipping address |
| 69 | if party_type in ["Customer", "Lead"]: |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 70 | out.shipping_address_name = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 71 | {party_type.lower(): party.name, "is_shipping_address":1}, "name") |
| 72 | out.shipping_address = get_address_display(out["shipping_address_name"]) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 73 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 74 | def set_contact_details(out, party, party_type): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 75 | out.contact_person = frappe.db.get_value("Contact", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 76 | {party_type.lower(): party.name, "is_primary_contact":1}, "name") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 77 | |
| 78 | if not out.contact_person: |
Neil Trini Lasrado | 09a66c4 | 2015-07-07 16:41:37 +0530 | [diff] [blame] | 79 | out.update({ |
| 80 | "contact_person": None, |
| 81 | "contact_display": None, |
| 82 | "contact_email": None, |
| 83 | "contact_mobile": None, |
| 84 | "contact_phone": None, |
| 85 | "contact_designation": None, |
| 86 | "contact_department": None |
| 87 | }) |
Neil Trini Lasrado | fe2ffae | 2015-07-08 18:16:51 +0530 | [diff] [blame] | 88 | else: |
| 89 | out.update(get_contact_details(out.contact_person)) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 90 | |
| 91 | def set_other_values(out, party, party_type): |
| 92 | # copy |
| 93 | if party_type=="Customer": |
| 94 | to_copy = ["customer_name", "customer_group", "territory"] |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 95 | else: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 96 | to_copy = ["supplier_name", "supplier_type"] |
| 97 | for f in to_copy: |
| 98 | out[f] = party.get(f) |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 99 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 100 | # fields prepended with default in Customer doctype |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 101 | for f in ['currency'] \ |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 102 | + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []): |
| 103 | if party.get("default_" + f): |
| 104 | out[f] = party.get("default_" + f) |
| 105 | |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 106 | def get_default_price_list(party): |
| 107 | """Return default price list for party (Document object)""" |
| 108 | if party.default_price_list: |
| 109 | return party.default_price_list |
| 110 | |
| 111 | if party.doctype == "Customer": |
| 112 | price_list = frappe.db.get_value("Customer Group", |
| 113 | party.customer_group, "default_price_list") |
| 114 | if price_list: |
| 115 | return price_list |
| 116 | |
| 117 | return None |
| 118 | |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 119 | def set_price_list(out, party, party_type, given_price_list): |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 120 | # price list |
Anand Doshi | a8ce570 | 2014-05-27 13:46:42 +0530 | [diff] [blame] | 121 | price_list = filter(None, get_user_permissions().get("Price List", [])) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 122 | if isinstance(price_list, list): |
Anand Doshi | a8ce570 | 2014-05-27 13:46:42 +0530 | [diff] [blame] | 123 | price_list = price_list[0] if len(price_list)==1 else None |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 124 | |
| 125 | if not price_list: |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 126 | price_list = get_default_price_list(party) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 127 | |
| 128 | if not price_list: |
| 129 | price_list = given_price_list |
| 130 | |
| 131 | if price_list: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 132 | out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency") |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 133 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 134 | 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] | 135 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 136 | |
Nabin Hait | 4d7c4fc | 2014-06-18 16:40:27 +0530 | [diff] [blame] | 137 | def set_account_and_due_date(party, account, party_type, company, posting_date, doctype): |
| 138 | if doctype not in ["Sales Invoice", "Purchase Invoice"]: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 139 | # not an invoice |
| 140 | return { |
| 141 | party_type.lower(): party |
| 142 | } |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 143 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 144 | if party: |
| 145 | account = get_party_account(company, party, party_type) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 146 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 147 | account_fieldname = "debit_to" if party_type=="Customer" else "credit_to" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 148 | |
| 149 | out = { |
| 150 | party_type.lower(): party, |
| 151 | account_fieldname : account, |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 152 | "due_date": get_due_date(posting_date, party_type, party, company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 153 | } |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 154 | return out |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 155 | |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 156 | def validate_accounting_currency(party): |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 157 | party_account_currency_in_db = frappe.db.get_value(party.doctype, party.name, "party_account_currency") |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 158 | if party_account_currency_in_db != party.party_account_currency: |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 159 | existing_gle = frappe.db.get_value("GL Entry", {"party_type": party.doctype, |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 160 | "party": party.name}, ["name", "account_currency"], as_dict=1) |
| 161 | if existing_gle: |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 162 | if party_account_currency_in_db: |
| 163 | frappe.throw(_("Accounting Currency cannot be changed, as GL Entry exists for this {0}") |
| 164 | .format(party.doctype), InvalidCurrency) |
| 165 | else: |
| 166 | party.party_account_currency = existing_gle.account_currency |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 167 | |
| 168 | |
Nabin Hait | 6aea1c0 | 2015-08-18 11:31:02 +0530 | [diff] [blame] | 169 | def validate_party_account(party): |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 170 | company_currency = get_company_currency() |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 171 | if party.party_account_currency: |
| 172 | companies_with_different_currency = [] |
| 173 | for company, currency in company_currency.items(): |
| 174 | if currency != party.party_account_currency: |
| 175 | companies_with_different_currency.append(company) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 176 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 177 | for d in party.get("accounts"): |
| 178 | if d.company in companies_with_different_currency: |
| 179 | companies_with_different_currency.remove(d.company) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 180 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 181 | selected_account_currency = frappe.db.get_value("Account", d.account, "account_currency") |
| 182 | if selected_account_currency != party.party_account_currency: |
| 183 | frappe.throw(_("Account {0} is invalid, account currency must be {1}") |
| 184 | .format(d.account, selected_account_currency), InvalidAccountCurrency) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 185 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 186 | if companies_with_different_currency: |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 187 | frappe.msgprint(_("Please mention Default {0} Account for the following companies, as accounting currency is different from company's default currency: {1}") |
| 188 | .format( |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 189 | "Receivable" if party.doctype=="Customer" else "Payable", |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 190 | "\n" + "\n".join(companies_with_different_currency) |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 191 | ) |
| 192 | ) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 193 | |
Nabin Hait | 9809677 | 2015-09-03 10:28:08 +0530 | [diff] [blame] | 194 | def get_company_currency(): |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 195 | company_currency = frappe._dict() |
| 196 | for d in frappe.get_all("Company", fields=["name", "default_currency"]): |
| 197 | company_currency.setdefault(d.name, d.default_currency) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 198 | |
Nabin Hait | 1609933 | 2015-09-03 16:03:07 +0530 | [diff] [blame] | 199 | return company_currency |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 200 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 201 | @frappe.whitelist() |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 202 | def get_party_account(company, party, party_type): |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 203 | """Returns the account for the given `party`. |
| 204 | Will first search in party (Customer / Supplier) record, if not found, |
| 205 | will search in group (Customer Group / Supplier Type), |
| 206 | finally will return default.""" |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 207 | if not company: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 208 | frappe.throw(_("Please select company first.")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 209 | |
| 210 | if party: |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 211 | account = frappe.db.get_value("Party Account", |
| 212 | {"parenttype": party_type, "parent": party, "company": company}, "account") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 213 | |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 214 | if not account: |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 215 | party_group_doctype = "Customer Group" if party_type=="Customer" else "Supplier Type" |
| 216 | group = frappe.db.get_value(party_type, party, scrub(party_group_doctype)) |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 217 | account = frappe.db.get_value("Party Account", |
Rushabh Mehta | 364054a | 2015-02-21 14:39:35 +0530 | [diff] [blame] | 218 | {"parenttype": party_group_doctype, "parent": group, "company": company}, "account") |
| 219 | |
| 220 | if not account: |
| 221 | default_account_name = "default_receivable_account" if party_type=="Customer" else "default_payable_account" |
| 222 | account = frappe.db.get_value("Company", company, default_account_name) |
Nabin Hait | db030d1 | 2014-09-10 17:11:30 +0530 | [diff] [blame] | 223 | |
| 224 | return account |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 225 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 226 | @frappe.whitelist() |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 227 | def get_due_date(posting_date, party_type, party, company): |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 228 | """Set Due Date = Posting Date + Credit Days""" |
| 229 | due_date = None |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 230 | if posting_date and party: |
Nabin Hait | 6336ff7 | 2015-07-09 19:48:21 +0530 | [diff] [blame] | 231 | due_date = posting_date |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 232 | if party_type=="Customer": |
| 233 | credit_days_based_on, credit_days = get_credit_days(party_type, party, company) |
| 234 | if credit_days_based_on == "Fixed Days" and credit_days: |
| 235 | due_date = add_days(posting_date, credit_days) |
| 236 | elif credit_days_based_on == "Last Day of the Next Month": |
| 237 | due_date = (get_first_day(posting_date, 0, 2) + datetime.timedelta(-1)).strftime("%Y-%m-%d") |
| 238 | else: |
| 239 | credit_days = get_credit_days(party_type, party, company) |
| 240 | if credit_days: |
| 241 | due_date = add_days(posting_date, credit_days) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 242 | |
Anand Doshi | 13ae548 | 2014-04-10 18:40:57 +0530 | [diff] [blame] | 243 | return due_date |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 244 | |
Nabin Hait | 3a40435 | 2014-08-27 17:43:55 +0530 | [diff] [blame] | 245 | def get_credit_days(party_type, party, company): |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 246 | if party_type and party: |
| 247 | if party_type == "Customer": |
| 248 | credit_days_based_on, credit_days, customer_group = \ |
| 249 | frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "customer_group"]) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 250 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 251 | if not credit_days_based_on: |
| 252 | credit_days_based_on, credit_days = \ |
| 253 | frappe.db.get_value("Customer Group", customer_group, ["credit_days_based_on", "credit_days"]) \ |
| 254 | or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"]) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 255 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 256 | return credit_days_based_on, credit_days |
Nabin Hait | e9daefe | 2014-08-27 16:46:33 +0530 | [diff] [blame] | 257 | else: |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 258 | credit_days, supplier_type = frappe.db.get_value(party_type, party, ["credit_days", "supplier_type"]) |
| 259 | if not credit_days: |
| 260 | credit_days = frappe.db.get_value("Supplier Type", supplier_type, "credit_days") \ |
| 261 | or frappe.db.get_value("Company", company, "credit_days") |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 262 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 263 | return credit_days |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 264 | |
Nabin Hait | 4770a1a | 2015-07-09 16:35:46 +0530 | [diff] [blame] | 265 | def validate_due_date(posting_date, due_date, party_type, party, company): |
| 266 | if getdate(due_date) < getdate(posting_date): |
| 267 | frappe.throw(_("Due Date cannot be before Posting Date")) |
| 268 | else: |
| 269 | default_due_date = get_due_date(posting_date, party_type, party, company) |
Anand Doshi | 0ca587e | 2015-09-25 16:32:14 +0530 | [diff] [blame] | 270 | if not default_due_date: |
| 271 | return |
| 272 | |
Nabin Hait | 93d3c82 | 2015-07-10 17:11:58 +0530 | [diff] [blame] | 273 | 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] | 274 | is_credit_controller = frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() |
| 275 | if is_credit_controller: |
| 276 | msgprint(_("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)") |
| 277 | .format(date_diff(due_date, default_due_date))) |
| 278 | else: |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 279 | frappe.throw(_("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date))) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 280 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 281 | @frappe.whitelist() |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 282 | def set_taxes(party, party_type, posting_date, company, customer_group=None, supplier_type=None, |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 283 | billing_address=None, shipping_address=None, use_for_shopping_cart=None): |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 284 | from erpnext.accounts.doctype.tax_rule.tax_rule import get_tax_template, get_party_details |
| 285 | args = { |
Rushabh Mehta | 307978f | 2015-09-23 15:43:09 +0530 | [diff] [blame] | 286 | party_type.lower(): party, |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 287 | "customer_group": customer_group, |
| 288 | "supplier_type": supplier_type, |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 289 | "company": company |
| 290 | } |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 291 | |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 292 | if billing_address or shipping_address: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 293 | args.update(get_party_details(party, party_type, {"billing_address": billing_address, \ |
| 294 | "shipping_address": shipping_address })) |
Neil Trini Lasrado | 09f9c96 | 2015-08-26 10:41:31 +0530 | [diff] [blame] | 295 | else: |
| 296 | args.update(get_party_details(party, party_type)) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 297 | |
Neil Trini Lasrado | 810bd35 | 2015-08-21 15:04:57 +0530 | [diff] [blame] | 298 | if party_type=="Customer": |
| 299 | args.update({"tax_type": "Sales"}) |
| 300 | else: |
| 301 | args.update({"tax_type": "Purchase"}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 302 | |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 303 | if use_for_shopping_cart: |
Saurabh | 957e7a3 | 2015-09-11 15:44:06 +0530 | [diff] [blame] | 304 | args.update({"use_for_shopping_cart": use_for_shopping_cart}) |
Rushabh Mehta | 361df89 | 2015-09-18 12:59:51 +0530 | [diff] [blame] | 305 | |
| 306 | return get_tax_template(posting_date, args) |