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