blob: c9ffd0e7a97eed607e7fe6e64601a589d5310272 [file] [log] [blame]
Rushabh Mehta49dd7be2014-01-28 17:43:10 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5
6import webnotes
7from webnotes import _
Rushabh Mehtacc008cc2014-02-03 16:14:56 +05308from webnotes.defaults import get_restrictions
9from erpnext.utilities.doctype.address.address import get_address_display
10from erpnext.utilities.doctype.contact.contact import get_contact_details
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053011
12@webnotes.whitelist()
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053013def get_party_details(party=None, account=None, party_type="Customer", company=None,
14 posting_date=None, price_list=None, currency=None):
15 out = webnotes._dict(set_account_and_due_date(party, account, party_type, company, posting_date))
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053016
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053017 party = out[party_type.lower()]
18
19 if not webnotes.has_permission(party_type, "read", party):
20 webnotes.throw("Not Permitted", webnotes.PermissionError)
21
22 party_bean = webnotes.bean(party_type, party)
23 party = party_bean.doc
24
25 set_address_and_contact(out, party, party_type)
26 set_other_values(out, party, party_type)
27 set_price_list(out, party, price_list)
28
29 if not out.get("currency"):
30 out["currency"] = currency
31
32 # sales team
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053033 if party_type=="Customer":
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053034 out["sales_team"] = [{
35 "sales_person": d.sales_person,
36 "sales_designation": d.sales_designation
37 } for d in party_bean.doclist.get({"doctype":"Sales Team"})]
38
39 return out
40
41def set_address_and_contact(out, party, party_type):
42 out.update({
43 party_type.lower() + "_address": webnotes.conn.get_value("Address",
44 {party_type.lower(): party.name, "is_primary_address":1}, "name"),
45 "contact_person": webnotes.conn.get_value("Contact",
46 {party_type.lower(): party.name, "is_primary_contact":1}, "name")
47 })
48
49 # address display
50 out.address_display = get_address_display(out[party_type.lower() + "_address"])
51
52 # primary contact details
53 out.update(get_contact_details(out.contact_person))
54
55
56def set_other_values(out, party, party_type):
57 # copy
58 if party_type=="Customer":
59 to_copy = ["customer_name", "customer_group", "territory"]
Rushabh Mehta49dd7be2014-01-28 17:43:10 +053060 else:
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053061 to_copy = ["supplier_name", "supplier_type"]
62 for f in to_copy:
63 out[f] = party.get(f)
64
65 # fields prepended with default in Customer doctype
66 for f in ['currency', 'taxes_and_charges'] \
67 + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []):
68 if party.get("default_" + f):
69 out[f] = party.get("default_" + f)
70
71def set_price_list(out, party, given_price_list):
72 # price list
73 price_list = get_restrictions().get("Price List")
74 if isinstance(price_list, list):
75 price_list = None
76
77 if not price_list:
78 price_list = party.default_price_list
79
80 if not price_list and party.party_type=="Customer":
81 price_list = webnotes.conn.get_value("Customer Group",
82 party.customer_group, "default_price_list")
83
84 if not price_list:
85 price_list = given_price_list
86
87 if price_list:
88 out.price_list_currency = webnotes.conn.get_value("Price List", price_list, "currency")
89
90 out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list
91
92
93def set_account_and_due_date(party, account, party_type, company, posting_date):
94 if not posting_date:
95 # not an invoice
96 return {
97 party_type.lower(): party
98 }
99
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530100 if party:
101 account = get_party_account(company, party, party_type)
102 elif account:
103 party = webnotes.conn.get_value('Account', account, 'master_name')
104
105 account_fieldname = "debit_to" if party_type=="Customer" else "credit_to"
106
107 out = {
108 party_type.lower(): party,
109 account_fieldname : account,
110 "due_date": get_due_date(posting_date, party, party_type, account, company)
Rushabh Mehtacc008cc2014-02-03 16:14:56 +0530111 }
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530112 return out
113
114def get_party_account(company, party, party_type):
115 if not company:
116 webnotes.throw(_("Please select company first."))
117
118 if party:
119 acc_head = webnotes.conn.get_value("Account", {"master_name":party,
120 "master_type": party_type, "company": company})
121
122 if not acc_head:
123 create_party_account(party, party_type, company)
124
125 return acc_head
126
127def get_due_date(posting_date, party, party_type, account, company):
128 """Set Due Date = Posting Date + Credit Days"""
129 due_date = None
130 if posting_date:
131 credit_days = 0
Rushabh Mehta24da7612014-01-29 15:26:04 +0530132 if account:
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530133 credit_days = webnotes.conn.get_value("Account", account, "credit_days")
134 if party and not credit_days:
135 credit_days = webnotes.conn.get_value(party_type, party, "credit_days")
136 if company and not credit_days:
137 credit_days = webnotes.conn.get_value("Company", company, "credit_days")
138
139 due_date = add_days(posting_date, credit_days) if credit_days else posting_date
140
141 return due_date
142
143def create_party_account(party, party_type, company):
144 if not company:
145 webnotes.throw(_("Company is required"))
146
147 company_details = webnotes.conn.get_value("Company", company,
148 ["abbr", "receivables_group", "payables_group"], as_dict=True)
Rushabh Mehta24da7612014-01-29 15:26:04 +0530149 if not webnotes.conn.exists("Account", (party + " - " + company_details.abbr)):
Rushabh Mehta49dd7be2014-01-28 17:43:10 +0530150 parent_account = company_details.receivables_group \
151 if party_type=="Customer" else company_details.payables_group
152
153 # create
154 account = webnotes.bean({
155 "doctype": "Account",
156 'account_name': party,
157 'parent_account': parent_account,
158 'group_or_ledger':'Ledger',
159 'company': company,
160 'master_type': party_type,
161 'master_name': party,
162 "freeze_account": "No"
163 }).insert(ignore_permissions=True)
164
Rushabh Mehta24da7612014-01-29 15:26:04 +0530165 webnotes.msgprint(_("Account Created") + ": " + account.doc.name)