brotherton-erpnext/erpnext/accounts/party.py

181 lines
5.9 KiB
Python
Raw Normal View History

# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
2014-02-14 10:17:51 +00:00
import frappe
from frappe import _
from frappe.defaults import get_restrictions
2014-02-19 12:13:24 +00:00
from frappe.utils import add_days
2014-02-03 10:44:56 +00:00
from erpnext.utilities.doctype.address.address import get_address_display
from erpnext.utilities.doctype.contact.contact import get_contact_details
2014-02-14 10:17:51 +00:00
@frappe.whitelist()
2014-02-03 10:44:56 +00:00
def get_party_details(party=None, account=None, party_type="Customer", company=None,
posting_date=None, price_list=None, currency=None):
2014-02-20 09:51:53 +00:00
return _get_party_details(party, account, party_type, company, posting_date, price_list, currency)
def _get_party_details(party=None, account=None, party_type="Customer", company=None,
posting_date=None, price_list=None, currency=None, ignore_permissions=False):
2014-02-14 10:17:51 +00:00
out = frappe._dict(set_account_and_due_date(party, account, party_type, company, posting_date))
2014-02-03 10:44:56 +00:00
party = out[party_type.lower()]
2014-02-20 09:51:53 +00:00
if not ignore_permissions and not frappe.has_permission(party_type, "read", party):
2014-02-14 10:17:51 +00:00
frappe.throw("Not Permitted", frappe.PermissionError)
2014-02-03 10:44:56 +00:00
2014-04-03 12:08:54 +00:00
party = frappe.get_doc(party_type, party)
2014-02-03 10:44:56 +00:00
2014-02-19 12:13:24 +00:00
set_address_details(out, party, party_type)
set_contact_details(out, party, party_type)
2014-02-03 10:44:56 +00:00
set_other_values(out, party, party_type)
2014-04-03 12:08:54 +00:00
set_price_list(out, party, party_type, price_list)
2014-02-03 10:44:56 +00:00
if not out.get("currency"):
out["currency"] = currency
2014-02-03 10:44:56 +00:00
# sales team
if party_type=="Customer":
2014-02-03 10:44:56 +00:00
out["sales_team"] = [{
"sales_person": d.sales_person,
"sales_designation": d.sales_designation
2014-04-03 12:08:54 +00:00
} for d in party.get("sales_team")]
2014-02-03 10:44:56 +00:00
return out
2014-02-19 12:13:24 +00:00
def set_address_details(out, party, party_type):
billing_address_field = "customer_address" if party_type == "Lead" \
else party_type.lower() + "_address"
2014-02-26 07:05:33 +00:00
out[billing_address_field] = frappe.db.get_value("Address",
2014-02-19 12:13:24 +00:00
{party_type.lower(): party.name, "is_primary_address":1}, "name")
2014-02-03 10:44:56 +00:00
# address display
2014-02-19 12:13:24 +00:00
out.address_display = get_address_display(out[billing_address_field])
2014-02-03 10:44:56 +00:00
2014-02-19 12:13:24 +00:00
# shipping address
if party_type in ["Customer", "Lead"]:
2014-02-26 07:05:33 +00:00
out.shipping_address_name = frappe.db.get_value("Address",
2014-02-19 12:13:24 +00:00
{party_type.lower(): party.name, "is_shipping_address":1}, "name")
out.shipping_address = get_address_display(out["shipping_address_name"])
def set_contact_details(out, party, party_type):
2014-02-26 07:05:33 +00:00
out.contact_person = frappe.db.get_value("Contact",
2014-02-19 12:13:24 +00:00
{party_type.lower(): party.name, "is_primary_contact":1}, "name")
2014-02-03 10:44:56 +00:00
2014-02-19 12:13:24 +00:00
out.update(get_contact_details(out.contact_person))
2014-02-03 10:44:56 +00:00
def set_other_values(out, party, party_type):
# copy
if party_type=="Customer":
to_copy = ["customer_name", "customer_group", "territory"]
else:
2014-02-03 10:44:56 +00:00
to_copy = ["supplier_name", "supplier_type"]
for f in to_copy:
out[f] = party.get(f)
# fields prepended with default in Customer doctype
for f in ['currency', 'taxes_and_charges'] \
+ (['sales_partner', 'commission_rate'] if party_type=="Customer" else []):
if party.get("default_" + f):
out[f] = party.get("default_" + f)
2014-04-03 12:08:54 +00:00
def set_price_list(out, party, party_type, given_price_list):
2014-02-03 10:44:56 +00:00
# price list
price_list = get_restrictions().get("Price List")
if isinstance(price_list, list):
price_list = None
if not price_list:
price_list = party.default_price_list
2014-04-03 12:08:54 +00:00
if not price_list and party_type=="Customer":
2014-02-26 07:05:33 +00:00
price_list = frappe.db.get_value("Customer Group",
2014-02-03 10:44:56 +00:00
party.customer_group, "default_price_list")
if not price_list:
price_list = given_price_list
if price_list:
2014-02-26 07:05:33 +00:00
out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency")
2014-02-03 10:44:56 +00:00
out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list
def set_account_and_due_date(party, account, party_type, company, posting_date):
if not posting_date:
# not an invoice
return {
party_type.lower(): party
}
if party:
account = get_party_account(company, party, party_type)
elif account:
2014-02-26 07:05:33 +00:00
party = frappe.db.get_value('Account', account, 'master_name')
account_fieldname = "debit_to" if party_type=="Customer" else "credit_to"
out = {
party_type.lower(): party,
account_fieldname : account,
"due_date": get_due_date(posting_date, party, party_type, account, company)
2014-02-03 10:44:56 +00:00
}
return out
def get_party_account(company, party, party_type):
if not company:
2014-02-14 10:17:51 +00:00
frappe.throw(_("Please select company first."))
if party:
2014-02-26 07:05:33 +00:00
acc_head = frappe.db.get_value("Account", {"master_name":party,
"master_type": party_type, "company": company})
if not acc_head:
create_party_account(party, party_type, company)
return acc_head
def get_due_date(posting_date, party, party_type, account, company):
"""Set Due Date = Posting Date + Credit Days"""
due_date = None
if posting_date:
credit_days = 0
2014-01-29 09:56:04 +00:00
if account:
2014-02-26 07:05:33 +00:00
credit_days = frappe.db.get_value("Account", account, "credit_days")
if party and not credit_days:
2014-02-26 07:05:33 +00:00
credit_days = frappe.db.get_value(party_type, party, "credit_days")
if company and not credit_days:
2014-02-26 07:05:33 +00:00
credit_days = frappe.db.get_value("Company", company, "credit_days")
due_date = add_days(posting_date, credit_days) if credit_days else posting_date
return due_date
def create_party_account(party, party_type, company):
if not company:
2014-02-14 10:17:51 +00:00
frappe.throw(_("Company is required"))
2014-02-26 07:05:33 +00:00
company_details = frappe.db.get_value("Company", company,
["abbr", "receivables_group", "payables_group"], as_dict=True)
2014-02-26 07:05:33 +00:00
if not frappe.db.exists("Account", (party + " - " + company_details.abbr)):
parent_account = company_details.receivables_group \
if party_type=="Customer" else company_details.payables_group
if not parent_account:
frappe.throw(_("Please enter Account Receivable/Payable group in company master"))
# create
2014-03-28 11:14:37 +00:00
account = frappe.get_doc({
"doctype": "Account",
'account_name': party,
'parent_account': parent_account,
'group_or_ledger':'Ledger',
'company': company,
'master_type': party_type,
'master_name': party,
"freeze_account": "No",
"report_type": "Balance Sheet"
}).insert(ignore_permissions=True)
frappe.msgprint(_("Account Created") + ": " + account.name)