Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
| 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 |
| 7 | from frappe import _ |
| 8 | from frappe.defaults import get_restrictions |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 9 | from frappe.utils import add_days |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 10 | from erpnext.utilities.doctype.address.address import get_address_display |
| 11 | from erpnext.utilities.doctype.contact.contact import get_contact_details |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 12 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 13 | @frappe.whitelist() |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 14 | def get_party_details(party=None, account=None, party_type="Customer", company=None, |
| 15 | posting_date=None, price_list=None, currency=None): |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 16 | |
| 17 | return _get_party_details(party, account, party_type, company, posting_date, price_list, currency) |
| 18 | |
| 19 | def _get_party_details(party=None, account=None, party_type="Customer", company=None, |
| 20 | posting_date=None, price_list=None, currency=None, ignore_permissions=False): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 21 | out = frappe._dict(set_account_and_due_date(party, account, party_type, company, posting_date)) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 22 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 23 | party = out[party_type.lower()] |
| 24 | |
Anand Doshi | 201dbbd | 2014-02-20 15:21:53 +0530 | [diff] [blame] | 25 | if not ignore_permissions and not frappe.has_permission(party_type, "read", party): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 26 | frappe.throw("Not Permitted", frappe.PermissionError) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 27 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 28 | party_bean = frappe.bean(party_type, party) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 29 | party = party_bean.doc |
| 30 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 31 | set_address_details(out, party, party_type) |
| 32 | set_contact_details(out, party, party_type) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 33 | set_other_values(out, party, party_type) |
| 34 | set_price_list(out, party, price_list) |
| 35 | |
| 36 | if not out.get("currency"): |
| 37 | out["currency"] = currency |
| 38 | |
| 39 | # sales team |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 40 | if party_type=="Customer": |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 41 | out["sales_team"] = [{ |
| 42 | "sales_person": d.sales_person, |
| 43 | "sales_designation": d.sales_designation |
| 44 | } for d in party_bean.doclist.get({"doctype":"Sales Team"})] |
| 45 | |
| 46 | return out |
| 47 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 48 | def set_address_details(out, party, party_type): |
| 49 | billing_address_field = "customer_address" if party_type == "Lead" \ |
| 50 | else party_type.lower() + "_address" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 51 | out[billing_address_field] = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 52 | {party_type.lower(): party.name, "is_primary_address":1}, "name") |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 53 | |
| 54 | # address display |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 55 | out.address_display = get_address_display(out[billing_address_field]) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 56 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 57 | # shipping address |
| 58 | if party_type in ["Customer", "Lead"]: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 59 | out.shipping_address_name = frappe.db.get_value("Address", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 60 | {party_type.lower(): party.name, "is_shipping_address":1}, "name") |
| 61 | out.shipping_address = get_address_display(out["shipping_address_name"]) |
| 62 | |
| 63 | def set_contact_details(out, party, party_type): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 64 | out.contact_person = frappe.db.get_value("Contact", |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 65 | {party_type.lower(): party.name, "is_primary_contact":1}, "name") |
| 66 | |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 67 | out.update(get_contact_details(out.contact_person)) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 68 | |
| 69 | def set_other_values(out, party, party_type): |
| 70 | # copy |
| 71 | if party_type=="Customer": |
| 72 | to_copy = ["customer_name", "customer_group", "territory"] |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 73 | else: |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 74 | to_copy = ["supplier_name", "supplier_type"] |
| 75 | for f in to_copy: |
| 76 | out[f] = party.get(f) |
| 77 | |
| 78 | # fields prepended with default in Customer doctype |
| 79 | for f in ['currency', 'taxes_and_charges'] \ |
| 80 | + (['sales_partner', 'commission_rate'] if party_type=="Customer" else []): |
| 81 | if party.get("default_" + f): |
| 82 | out[f] = party.get("default_" + f) |
| 83 | |
| 84 | def set_price_list(out, party, given_price_list): |
| 85 | # price list |
| 86 | price_list = get_restrictions().get("Price List") |
| 87 | if isinstance(price_list, list): |
| 88 | price_list = None |
| 89 | |
| 90 | if not price_list: |
| 91 | price_list = party.default_price_list |
| 92 | |
| 93 | if not price_list and party.party_type=="Customer": |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 94 | price_list = frappe.db.get_value("Customer Group", |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 95 | party.customer_group, "default_price_list") |
| 96 | |
| 97 | if not price_list: |
| 98 | price_list = given_price_list |
| 99 | |
| 100 | if price_list: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 101 | out.price_list_currency = frappe.db.get_value("Price List", price_list, "currency") |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 102 | |
| 103 | out["selling_price_list" if party.doctype=="Customer" else "buying_price_list"] = price_list |
| 104 | |
| 105 | |
| 106 | def set_account_and_due_date(party, account, party_type, company, posting_date): |
| 107 | if not posting_date: |
| 108 | # not an invoice |
| 109 | return { |
| 110 | party_type.lower(): party |
| 111 | } |
| 112 | |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 113 | if party: |
| 114 | account = get_party_account(company, party, party_type) |
| 115 | elif account: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 116 | party = frappe.db.get_value('Account', account, 'master_name') |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 117 | |
| 118 | account_fieldname = "debit_to" if party_type=="Customer" else "credit_to" |
| 119 | |
| 120 | out = { |
| 121 | party_type.lower(): party, |
| 122 | account_fieldname : account, |
| 123 | "due_date": get_due_date(posting_date, party, party_type, account, company) |
Rushabh Mehta | cc008cc | 2014-02-03 16:14:56 +0530 | [diff] [blame] | 124 | } |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 125 | return out |
| 126 | |
| 127 | def get_party_account(company, party, party_type): |
| 128 | if not company: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 129 | frappe.throw(_("Please select company first.")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 130 | |
| 131 | if party: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 132 | acc_head = frappe.db.get_value("Account", {"master_name":party, |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 133 | "master_type": party_type, "company": company}) |
| 134 | |
| 135 | if not acc_head: |
| 136 | create_party_account(party, party_type, company) |
| 137 | |
| 138 | return acc_head |
| 139 | |
| 140 | def get_due_date(posting_date, party, party_type, account, company): |
| 141 | """Set Due Date = Posting Date + Credit Days""" |
| 142 | due_date = None |
| 143 | if posting_date: |
| 144 | credit_days = 0 |
Rushabh Mehta | 24da761 | 2014-01-29 15:26:04 +0530 | [diff] [blame] | 145 | if account: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 146 | credit_days = frappe.db.get_value("Account", account, "credit_days") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 147 | if party and not credit_days: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 148 | credit_days = frappe.db.get_value(party_type, party, "credit_days") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 149 | if company and not credit_days: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 150 | credit_days = frappe.db.get_value("Company", company, "credit_days") |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 151 | |
| 152 | due_date = add_days(posting_date, credit_days) if credit_days else posting_date |
| 153 | |
| 154 | return due_date |
| 155 | |
| 156 | def create_party_account(party, party_type, company): |
| 157 | if not company: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 158 | frappe.throw(_("Company is required")) |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 159 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 160 | company_details = frappe.db.get_value("Company", company, |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 161 | ["abbr", "receivables_group", "payables_group"], as_dict=True) |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 162 | if not frappe.db.exists("Account", (party + " - " + company_details.abbr)): |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 163 | parent_account = company_details.receivables_group \ |
| 164 | if party_type=="Customer" else company_details.payables_group |
| 165 | |
| 166 | # create |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 167 | account = frappe.bean({ |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 168 | "doctype": "Account", |
| 169 | 'account_name': party, |
| 170 | 'parent_account': parent_account, |
| 171 | 'group_or_ledger':'Ledger', |
| 172 | 'company': company, |
| 173 | 'master_type': party_type, |
| 174 | 'master_name': party, |
| 175 | "freeze_account": "No" |
| 176 | }).insert(ignore_permissions=True) |
| 177 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 178 | frappe.msgprint(_("Account Created") + ": " + account.doc.name) |