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 | |
| 6 | import webnotes |
| 7 | from webnotes import _ |
| 8 | |
| 9 | @webnotes.whitelist() |
Rushabh Mehta | 24da761 | 2014-01-29 15:26:04 +0530 | [diff] [blame] | 10 | def get_party_details(party=None, account=None, party_type="Customer", company=None, posting_date=None): |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 11 | if not webnotes.has_permission(party_type, "read", party): |
| 12 | webnotes.throw("No Permission") |
| 13 | |
| 14 | if party_type=="Customer": |
| 15 | get_party_details = webnotes.get_attr("erpnext.selling.doctype.customer.customer.get_customer_details") |
| 16 | else: |
| 17 | get_party_details = webnotes.get_attr("erpnext.buying.doctype.supplier.supplier.get_supplier_details") |
| 18 | |
| 19 | if party: |
| 20 | account = get_party_account(company, party, party_type) |
| 21 | elif account: |
| 22 | party = webnotes.conn.get_value('Account', account, 'master_name') |
| 23 | |
| 24 | account_fieldname = "debit_to" if party_type=="Customer" else "credit_to" |
| 25 | |
| 26 | out = { |
| 27 | party_type.lower(): party, |
| 28 | account_fieldname : account, |
| 29 | "due_date": get_due_date(posting_date, party, party_type, account, company) |
| 30 | } |
| 31 | out.update(get_party_details(party)) |
| 32 | return out |
| 33 | |
| 34 | def get_party_account(company, party, party_type): |
| 35 | if not company: |
| 36 | webnotes.throw(_("Please select company first.")) |
| 37 | |
| 38 | if party: |
| 39 | acc_head = webnotes.conn.get_value("Account", {"master_name":party, |
| 40 | "master_type": party_type, "company": company}) |
| 41 | |
| 42 | if not acc_head: |
| 43 | create_party_account(party, party_type, company) |
| 44 | |
| 45 | return acc_head |
| 46 | |
| 47 | def get_due_date(posting_date, party, party_type, account, company): |
| 48 | """Set Due Date = Posting Date + Credit Days""" |
| 49 | due_date = None |
| 50 | if posting_date: |
| 51 | credit_days = 0 |
Rushabh Mehta | 24da761 | 2014-01-29 15:26:04 +0530 | [diff] [blame] | 52 | if account: |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 53 | credit_days = webnotes.conn.get_value("Account", account, "credit_days") |
| 54 | if party and not credit_days: |
| 55 | credit_days = webnotes.conn.get_value(party_type, party, "credit_days") |
| 56 | if company and not credit_days: |
| 57 | credit_days = webnotes.conn.get_value("Company", company, "credit_days") |
| 58 | |
| 59 | due_date = add_days(posting_date, credit_days) if credit_days else posting_date |
| 60 | |
| 61 | return due_date |
| 62 | |
| 63 | def create_party_account(party, party_type, company): |
| 64 | if not company: |
| 65 | webnotes.throw(_("Company is required")) |
| 66 | |
| 67 | company_details = webnotes.conn.get_value("Company", company, |
| 68 | ["abbr", "receivables_group", "payables_group"], as_dict=True) |
Rushabh Mehta | 24da761 | 2014-01-29 15:26:04 +0530 | [diff] [blame] | 69 | if not webnotes.conn.exists("Account", (party + " - " + company_details.abbr)): |
Rushabh Mehta | 49dd7be | 2014-01-28 17:43:10 +0530 | [diff] [blame] | 70 | parent_account = company_details.receivables_group \ |
| 71 | if party_type=="Customer" else company_details.payables_group |
| 72 | |
| 73 | # create |
| 74 | account = webnotes.bean({ |
| 75 | "doctype": "Account", |
| 76 | 'account_name': party, |
| 77 | 'parent_account': parent_account, |
| 78 | 'group_or_ledger':'Ledger', |
| 79 | 'company': company, |
| 80 | 'master_type': party_type, |
| 81 | 'master_name': party, |
| 82 | "freeze_account": "No" |
| 83 | }).insert(ignore_permissions=True) |
| 84 | |
Rushabh Mehta | 24da761 | 2014-01-29 15:26:04 +0530 | [diff] [blame] | 85 | webnotes.msgprint(_("Account Created") + ": " + account.doc.name) |