blob: f0c29bc36ef0b63316b35156d48dcb5d6dc199d7 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehta49dd7be2014-01-28 17:43:10 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306import frappe
Nabin Hait4770a1a2015-07-09 16:35:46 +05307import datetime
Nabin Haitdb030d12014-09-10 17:11:30 +05308from frappe import _, msgprint, scrub
Anand Doshifab09042014-05-27 08:39:35 +05309from frappe.defaults import get_user_permissions
mbauskare7b87ad2017-02-16 13:12:29 +053010from frappe.utils import add_days, getdate, formatdate, get_first_day, date_diff, add_years, get_timestamp
Rushabh Mehtab92087c2017-01-13 18:53:11 +053011from frappe.geo.doctype.address.address import get_address_display, get_default_address
12from frappe.email.doctype.contact.contact import get_contact_details, get_default_contact
shreyasf76853a2016-01-25 21:25:11 +053013from erpnext.exceptions import PartyFrozen, InvalidCurrency, PartyDisabled, InvalidAccountCurrency
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053014
Anand Doshi248c8672015-09-28 15:24:00 +053015class DuplicatePartyAccountError(frappe.ValidationError): pass
Nabin Hait16099332015-09-03 16:03:07 +053016
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053017@frappe.whitelist()
Anand Doshi13ae5482014-04-10 18:40:57 +053018def get_party_details(party=None, account=None, party_type="Customer", company=None,
rohitwaghchaurea1064a62016-03-03 14:00:35 +053019 posting_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False):
Anand Doshi201dbbd2014-02-20 15:21:53 +053020
Rushabh Mehtad7a5b732015-04-01 23:38:13 +053021 if not party:
22 return {}
Rushabh Mehta361df892015-09-18 12:59:51 +053023
Nabin Hait5ef121b2015-06-08 12:26:52 +053024 if not frappe.db.exists(party_type, party):
25 frappe.throw(_("{0}: {1} does not exists").format(party_type, party))
Rushabh Mehtad7a5b732015-04-01 23:38:13 +053026
Nabin Hait4d7c4fc2014-06-18 16:40:27 +053027 return _get_party_details(party, account, party_type,
rohitwaghchaurea1064a62016-03-03 14:00:35 +053028 company, posting_date, price_list, currency, doctype, ignore_permissions)
Anand Doshi201dbbd2014-02-20 15:21:53 +053029
Anand Doshi13ae5482014-04-10 18:40:57 +053030def _get_party_details(party=None, account=None, party_type="Customer", company=None,
Nabin Hait4d7c4fc2014-06-18 16:40:27 +053031 posting_date=None, price_list=None, currency=None, doctype=None, ignore_permissions=False):
Rushabh Mehta361df892015-09-18 12:59:51 +053032
Nabin Hait4d7c4fc2014-06-18 16:40:27 +053033 out = frappe._dict(set_account_and_due_date(party, account, party_type, company, posting_date, doctype))
Anand Doshi13ae5482014-04-10 18:40:57 +053034
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053035 party = out[party_type.lower()]
36
Anand Doshi201dbbd2014-02-20 15:21:53 +053037 if not ignore_permissions and not frappe.has_permission(party_type, "read", party):
Rushabh Mehta2d708872015-11-27 11:35:35 +053038 frappe.throw(_("Not permitted for {0}").format(party), frappe.PermissionError)
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053039
Nabin Hait365ae272014-04-03 17:38:54 +053040 party = frappe.get_doc(party_type, party)
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053041
Nabin Hait9d1f0772014-02-19 17:43:24 +053042 set_address_details(out, party, party_type)
43 set_contact_details(out, party, party_type)
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053044 set_other_values(out, party, party_type)
Nabin Hait365ae272014-04-03 17:38:54 +053045 set_price_list(out, party, party_type, price_list)
Neil Trini Lasrado09f9c962015-08-26 10:41:31 +053046 out["taxes_and_charges"] = set_taxes(party.name, party_type, posting_date, company, out.customer_group, out.supplier_type)
Anand Doshi13ae5482014-04-10 18:40:57 +053047
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053048 if not out.get("currency"):
49 out["currency"] = currency
Anand Doshi13ae5482014-04-10 18:40:57 +053050
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053051 # sales team
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053052 if party_type=="Customer":
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053053 out["sales_team"] = [{
Anand Doshi4b72d052015-10-12 16:15:52 +053054 "sales_person": d.sales_person,
55 "allocated_percentage": d.allocated_percentage or None
Nabin Hait365ae272014-04-03 17:38:54 +053056 } for d in party.get("sales_team")]
Anand Doshi13ae5482014-04-10 18:40:57 +053057
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053058 return out
59
Nabin Hait9d1f0772014-02-19 17:43:24 +053060def set_address_details(out, party, party_type):
61 billing_address_field = "customer_address" if party_type == "Lead" \
62 else party_type.lower() + "_address"
Rushabh Mehtab92087c2017-01-13 18:53:11 +053063 out[billing_address_field] = get_default_address(party_type, party.name)
Anand Doshi13ae5482014-04-10 18:40:57 +053064
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053065 # address display
Nabin Hait9d1f0772014-02-19 17:43:24 +053066 out.address_display = get_address_display(out[billing_address_field])
Anand Doshi13ae5482014-04-10 18:40:57 +053067
Nabin Hait9d1f0772014-02-19 17:43:24 +053068 # shipping address
69 if party_type in ["Customer", "Lead"]:
Rushabh Mehtab92087c2017-01-13 18:53:11 +053070 out.shipping_address_name = get_default_address(party_type, party.name, 'is_shipping_address')
Nabin Hait9d1f0772014-02-19 17:43:24 +053071 out.shipping_address = get_address_display(out["shipping_address_name"])
Anand Doshi13ae5482014-04-10 18:40:57 +053072
Nabin Hait9d1f0772014-02-19 17:43:24 +053073def set_contact_details(out, party, party_type):
Rushabh Mehtab92087c2017-01-13 18:53:11 +053074 out.contact_person = get_default_contact(party_type, party.name)
Anand Doshi13ae5482014-04-10 18:40:57 +053075
76 if not out.contact_person:
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +053077 out.update({
78 "contact_person": None,
79 "contact_display": None,
80 "contact_email": None,
81 "contact_mobile": None,
82 "contact_phone": None,
83 "contact_designation": None,
84 "contact_department": None
85 })
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +053086 else:
87 out.update(get_contact_details(out.contact_person))
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053088
89def set_other_values(out, party, party_type):
90 # copy
91 if party_type=="Customer":
saurabh8e499512016-02-19 11:08:45 +053092 to_copy = ["customer_name", "customer_group", "territory", "language"]
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053093 else:
saurabh8e499512016-02-19 11:08:45 +053094 to_copy = ["supplier_name", "supplier_type", "language"]
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053095 for f in to_copy:
96 out[f] = party.get(f)
Anand Doshi13ae5482014-04-10 18:40:57 +053097
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053098 # fields prepended with default in Customer doctype
Neil Trini Lasrado810bd352015-08-21 15:04:57 +053099 for f in ['currency'] \
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530100 + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []):
101 if party.get("default_" + f):
102 out[f] = party.get("default_" + f)
103
Rushabh Mehta72fbf902015-09-17 18:29:44 +0530104def get_default_price_list(party):
105 """Return default price list for party (Document object)"""
106 if party.default_price_list:
107 return party.default_price_list
108
109 if party.doctype == "Customer":
110 price_list = frappe.db.get_value("Customer Group",
111 party.customer_group, "default_price_list")
112 if price_list:
113 return price_list
114
115 return None
116
Nabin Hait365ae272014-04-03 17:38:54 +0530117def set_price_list(out, party, party_type, given_price_list):
Anand Doshi13ae5482014-04-10 18:40:57 +0530118 # price list
Anand Doshia8ce5702014-05-27 13:46:42 +0530119 price_list = filter(None, get_user_permissions().get("Price List", []))
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530120 if isinstance(price_list, list):
Anand Doshia8ce5702014-05-27 13:46:42 +0530121 price_list = price_list[0] if len(price_list)==1 else None
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530122
123 if not price_list:
Rushabh Mehta72fbf902015-09-17 18:29:44 +0530124 price_list = get_default_price_list(party)
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530125
126 if not price_list:
127 price_list = given_price_list
128
129 if price_list:
Anand Doshie9baaa62014-02-26 12:35:33 +0530130 out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency")
Anand Doshi13ae5482014-04-10 18:40:57 +0530131
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530132 out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list
Anand Doshi13ae5482014-04-10 18:40:57 +0530133
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530134
Nabin Hait4d7c4fc2014-06-18 16:40:27 +0530135def set_account_and_due_date(party, account, party_type, company, posting_date, doctype):
136 if doctype not in ["Sales Invoice", "Purchase Invoice"]:
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530137 # not an invoice
138 return {
139 party_type.lower(): party
140 }
Anand Doshi13ae5482014-04-10 18:40:57 +0530141
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530142 if party:
Anand Doshib20baf82015-09-25 16:17:50 +0530143 account = get_party_account(party_type, party, company)
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530144
Anand Doshi13ae5482014-04-10 18:40:57 +0530145 account_fieldname = "debit_to" if party_type=="Customer" else "credit_to"
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530146
147 out = {
148 party_type.lower(): party,
149 account_fieldname : account,
Nabin Haite9daefe2014-08-27 16:46:33 +0530150 "due_date": get_due_date(posting_date, party_type, party, company)
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530151 }
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530152 return out
Rushabh Mehta361df892015-09-18 12:59:51 +0530153
Nabin Haitdb030d12014-09-10 17:11:30 +0530154@frappe.whitelist()
Anand Doshib20baf82015-09-25 16:17:50 +0530155def get_party_account(party_type, party, company):
Rushabh Mehta364054a2015-02-21 14:39:35 +0530156 """Returns the account for the given `party`.
157 Will first search in party (Customer / Supplier) record, if not found,
158 will search in group (Customer Group / Supplier Type),
159 finally will return default."""
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530160 if not company:
Anand Doshib20baf82015-09-25 16:17:50 +0530161 frappe.throw(_("Please select a Company"))
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530162
163 if party:
Rushabh Mehta364054a2015-02-21 14:39:35 +0530164 account = frappe.db.get_value("Party Account",
165 {"parenttype": party_type, "parent": party, "company": company}, "account")
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530166
rohitwaghchaureea092a72017-02-01 12:02:08 +0530167 if not account and party_type in ['Customer', 'Supplier']:
Rushabh Mehta364054a2015-02-21 14:39:35 +0530168 party_group_doctype = "Customer Group" if party_type=="Customer" else "Supplier Type"
169 group = frappe.db.get_value(party_type, party, scrub(party_group_doctype))
Nabin Haitdb030d12014-09-10 17:11:30 +0530170 account = frappe.db.get_value("Party Account",
Rushabh Mehta364054a2015-02-21 14:39:35 +0530171 {"parenttype": party_group_doctype, "parent": group, "company": company}, "account")
172
rohitwaghchaureea092a72017-02-01 12:02:08 +0530173 if not account and party_type in ['Customer', 'Supplier']:
Nabin Hait16d69592016-06-28 16:15:58 +0530174 default_account_name = "default_receivable_account" \
175 if party_type=="Customer" else "default_payable_account"
Rushabh Mehta364054a2015-02-21 14:39:35 +0530176 account = frappe.db.get_value("Company", company, default_account_name)
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530177
Nabin Hait16d69592016-06-28 16:15:58 +0530178 existing_gle_currency = get_party_gle_currency(party_type, party, company)
179 if existing_gle_currency:
180 if account:
181 account_currency = frappe.db.get_value("Account", account, "account_currency")
182 if (account and account_currency != existing_gle_currency) or not account:
183 account = get_party_gle_account(party_type, party, company)
Nabin Haitdb030d12014-09-10 17:11:30 +0530184
185 return account
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530186
Anand Doshib20baf82015-09-25 16:17:50 +0530187def get_party_account_currency(party_type, party, company):
188 def generator():
189 party_account = get_party_account(party_type, party, company)
190 return frappe.db.get_value("Account", party_account, "account_currency")
191
192 return frappe.local_cache("party_account_currency", (party_type, party, company), generator)
193
Anand Doshi4945b942015-09-30 16:41:15 +0530194def get_party_gle_currency(party_type, party, company):
195 def generator():
196 existing_gle_currency = frappe.db.sql("""select account_currency from `tabGL Entry`
197 where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s
198 limit 1""", { "company": company, "party_type": party_type, "party": party })
199
200 return existing_gle_currency[0][0] if existing_gle_currency else None
201
Anand Doshi0b93bdc2015-10-22 19:32:56 +0530202 return frappe.local_cache("party_gle_currency", (party_type, party, company), generator,
203 regenerate_if_none=True)
Rushabh Mehtab92087c2017-01-13 18:53:11 +0530204
Nabin Hait16d69592016-06-28 16:15:58 +0530205def get_party_gle_account(party_type, party, company):
206 def generator():
207 existing_gle_account = frappe.db.sql("""select account from `tabGL Entry`
208 where docstatus=1 and company=%(company)s and party_type=%(party_type)s and party=%(party)s
209 limit 1""", { "company": company, "party_type": party_type, "party": party })
210
211 return existing_gle_account[0][0] if existing_gle_account else None
212
213 return frappe.local_cache("party_gle_account", (party_type, party, company), generator,
214 regenerate_if_none=True)
Anand Doshi4945b942015-09-30 16:41:15 +0530215
Anand Doshid8bc40d2015-10-22 17:54:50 +0530216def validate_party_gle_currency(party_type, party, company, party_account_currency=None):
Anand Doshi4945b942015-09-30 16:41:15 +0530217 """Validate party account currency with existing GL Entry's currency"""
Anand Doshid8bc40d2015-10-22 17:54:50 +0530218 if not party_account_currency:
219 party_account_currency = get_party_account_currency(party_type, party, company)
220
Anand Doshi4945b942015-09-30 16:41:15 +0530221 existing_gle_currency = get_party_gle_currency(party_type, party, company)
222
223 if existing_gle_currency and party_account_currency != existing_gle_currency:
224 frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}")
225 .format(party_type, party, existing_gle_currency), InvalidAccountCurrency)
226
Anand Doshida98ab62015-09-28 15:13:38 +0530227def validate_party_accounts(doc):
228 companies = []
229
230 for account in doc.get("accounts"):
231 if account.company in companies:
Nabin Hait2873f2e2015-10-16 13:07:45 +0530232 frappe.throw(_("There can only be 1 Account per Company in {0} {1}")
233 .format(doc.doctype, doc.name), DuplicatePartyAccountError)
Anand Doshida98ab62015-09-28 15:13:38 +0530234 else:
235 companies.append(account.company)
Anand Doshid8bc40d2015-10-22 17:54:50 +0530236
Nabin Hait2873f2e2015-10-16 13:07:45 +0530237 party_account_currency = frappe.db.get_value("Account", account.account, "account_currency")
238 existing_gle_currency = get_party_gle_currency(doc.doctype, doc.name, account.company)
rohitwaghchaure62fea032016-03-30 13:24:42 +0530239 company_default_currency = frappe.db.get_value("Company",
240 frappe.db.get_default("Company"), "default_currency", cache=True)
Anand Doshid8bc40d2015-10-22 17:54:50 +0530241
Nabin Haitaa015902015-10-16 13:08:33 +0530242 if existing_gle_currency and party_account_currency != existing_gle_currency:
243 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 Doshida98ab62015-09-28 15:13:38 +0530244
rohitwaghchaureea092a72017-02-01 12:02:08 +0530245 if doc.get("default_currency") and party_account_currency and company_default_currency:
rohitwaghchaure62fea032016-03-30 13:24:42 +0530246 if doc.default_currency != party_account_currency and doc.default_currency != company_default_currency:
Nabin Hait56548cb2016-07-01 15:58:39 +0530247 frappe.throw(_("Billing currency must be equal to either default comapany's currency or party account currency"))
rohitwaghchaure62fea032016-03-30 13:24:42 +0530248
Nabin Hait4770a1a2015-07-09 16:35:46 +0530249@frappe.whitelist()
Nabin Haite9daefe2014-08-27 16:46:33 +0530250def get_due_date(posting_date, party_type, party, company):
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530251 """Set Due Date = Posting Date + Credit Days"""
252 due_date = None
Nabin Hait4770a1a2015-07-09 16:35:46 +0530253 if posting_date and party:
Nabin Hait6336ff72015-07-09 19:48:21 +0530254 due_date = posting_date
shreyas28a65782016-01-25 12:44:14 +0530255 credit_days_based_on, credit_days = get_credit_days(party_type, party, company)
256 if credit_days_based_on == "Fixed Days" and credit_days:
257 due_date = add_days(posting_date, credit_days)
258 elif credit_days_based_on == "Last Day of the Next Month":
259 due_date = (get_first_day(posting_date, 0, 2) + datetime.timedelta(-1)).strftime("%Y-%m-%d")
Rushabh Mehta361df892015-09-18 12:59:51 +0530260
Anand Doshi13ae5482014-04-10 18:40:57 +0530261 return due_date
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530262
Nabin Hait3a404352014-08-27 17:43:55 +0530263def get_credit_days(party_type, party, company):
Nabin Hait4770a1a2015-07-09 16:35:46 +0530264 if party_type and party:
265 if party_type == "Customer":
266 credit_days_based_on, credit_days, customer_group = \
267 frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "customer_group"])
Nabin Haite9daefe2014-08-27 16:46:33 +0530268 else:
shreyas28a65782016-01-25 12:44:14 +0530269 credit_days_based_on, credit_days, supplier_type = \
270 frappe.db.get_value(party_type, party, ["credit_days_based_on", "credit_days", "supplier_type"])
Rushabh Mehta361df892015-09-18 12:59:51 +0530271
shreyas28a65782016-01-25 12:44:14 +0530272 if not credit_days_based_on:
273 if party_type == "Customer":
274 credit_days_based_on, credit_days = \
275 frappe.db.get_value("Customer Group", customer_group, ["credit_days_based_on", "credit_days"]) \
276 or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"])
277 else:
278 credit_days_based_on, credit_days = \
279 frappe.db.get_value("Supplier Type", supplier_type, ["credit_days_based_on", "credit_days"])\
280 or frappe.db.get_value("Company", company, ["credit_days_based_on", "credit_days"] )
281
282 return credit_days_based_on, credit_days
Rushabh Mehta361df892015-09-18 12:59:51 +0530283
Nabin Hait4770a1a2015-07-09 16:35:46 +0530284def validate_due_date(posting_date, due_date, party_type, party, company):
285 if getdate(due_date) < getdate(posting_date):
286 frappe.throw(_("Due Date cannot be before Posting Date"))
287 else:
288 default_due_date = get_due_date(posting_date, party_type, party, company)
Anand Doshi0ca587e2015-09-25 16:32:14 +0530289 if not default_due_date:
290 return
291
Nabin Hait93d3c822015-07-10 17:11:58 +0530292 if default_due_date != posting_date and getdate(due_date) > getdate(default_due_date):
Nabin Hait4770a1a2015-07-09 16:35:46 +0530293 is_credit_controller = frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles()
294 if is_credit_controller:
295 msgprint(_("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)")
296 .format(date_diff(due_date, default_due_date)))
297 else:
Neil Trini Lasrado810bd352015-08-21 15:04:57 +0530298 frappe.throw(_("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date)))
Rushabh Mehta361df892015-09-18 12:59:51 +0530299
Neil Trini Lasrado09f9c962015-08-26 10:41:31 +0530300@frappe.whitelist()
Rushabh Mehta361df892015-09-18 12:59:51 +0530301def set_taxes(party, party_type, posting_date, company, customer_group=None, supplier_type=None,
Saurabh957e7a32015-09-11 15:44:06 +0530302 billing_address=None, shipping_address=None, use_for_shopping_cart=None):
Neil Trini Lasrado810bd352015-08-21 15:04:57 +0530303 from erpnext.accounts.doctype.tax_rule.tax_rule import get_tax_template, get_party_details
304 args = {
Rushabh Mehta307978f2015-09-23 15:43:09 +0530305 party_type.lower(): party,
Neil Trini Lasrado09f9c962015-08-26 10:41:31 +0530306 "customer_group": customer_group,
307 "supplier_type": supplier_type,
Neil Trini Lasrado810bd352015-08-21 15:04:57 +0530308 "company": company
309 }
Rushabh Mehta361df892015-09-18 12:59:51 +0530310
Neil Trini Lasrado09f9c962015-08-26 10:41:31 +0530311 if billing_address or shipping_address:
Saurabh957e7a32015-09-11 15:44:06 +0530312 args.update(get_party_details(party, party_type, {"billing_address": billing_address, \
313 "shipping_address": shipping_address }))
Neil Trini Lasrado09f9c962015-08-26 10:41:31 +0530314 else:
315 args.update(get_party_details(party, party_type))
Rushabh Mehta361df892015-09-18 12:59:51 +0530316
Anand Doshi2f957f22016-04-08 17:36:10 +0530317 if party_type in ("Customer", "Lead"):
Neil Trini Lasrado810bd352015-08-21 15:04:57 +0530318 args.update({"tax_type": "Sales"})
Anand Doshi2f957f22016-04-08 17:36:10 +0530319
320 if party_type=='Lead':
321 args['customer'] = None
322 del args['lead']
Neil Trini Lasrado810bd352015-08-21 15:04:57 +0530323 else:
324 args.update({"tax_type": "Purchase"})
Rushabh Mehta361df892015-09-18 12:59:51 +0530325
Saurabh957e7a32015-09-11 15:44:06 +0530326 if use_for_shopping_cart:
Saurabh957e7a32015-09-11 15:44:06 +0530327 args.update({"use_for_shopping_cart": use_for_shopping_cart})
Rushabh Mehta361df892015-09-18 12:59:51 +0530328
329 return get_tax_template(posting_date, args)
shreyas29b565f2016-01-25 17:30:49 +0530330
shreyaseba9ca42016-01-26 14:56:52 +0530331def validate_party_frozen_disabled(party_type, party_name):
shreyas79872bf2016-01-26 13:57:06 +0530332 if party_type and party_name:
KanchanChauhanfbaa6192017-01-30 15:15:58 +0530333 if party_type in ("Customer", "Supplier"):
334 party = frappe.db.get_value(party_type, party_name, ["is_frozen", "disabled"], as_dict=True)
335 if party.disabled:
336 frappe.throw(_("{0} {1} is disabled").format(party_type, party_name), PartyDisabled)
337 elif party.get("is_frozen"):
338 frozen_accounts_modifier = frappe.db.get_value( 'Accounts Settings', None,'frozen_accounts_modifier')
339 if not frozen_accounts_modifier in frappe.get_roles():
340 frappe.throw(_("{0} {1} is frozen").format(party_type, party_name), PartyFrozen)
341
342 elif party_type == "Employee":
343 if frappe.db.get_value("Employee", party_name, "status") == "Left":
Kanchan Chauhan95108ac2017-04-11 15:55:34 +0530344 frappe.msgprint(_("{0} {1} is not active").format(party_type, party_name), alert=True)
Rushabh Mehta15a7f212016-04-14 17:30:40 +0530345
346def get_timeline_data(doctype, name):
347 '''returns timeline data for the past one year'''
348 from frappe.desk.form.load import get_communication_data
mbauskare7b87ad2017-02-16 13:12:29 +0530349
350 out = {}
Rushabh Mehta40b3a5b2016-05-20 11:20:59 +0530351 data = get_communication_data(doctype, name,
mbauskare7b87ad2017-02-16 13:12:29 +0530352 fields = 'date(creation), count(name)',
Rushabh Mehta40b3a5b2016-05-20 11:20:59 +0530353 after = add_years(None, -1).strftime('%Y-%m-%d'),
Rushabh Mehta15a7f212016-04-14 17:30:40 +0530354 group_by='group by date(creation)', as_dict=False)
mbauskare7b87ad2017-02-16 13:12:29 +0530355
356 timeline_items = dict(data)
357
358 for date, count in timeline_items.iteritems():
359 timestamp = get_timestamp(date)
360 out.update({ timestamp: count })
361
362 return out