예제 #1
0
def get_reference_details(reference_doctype, reference_name,
                          party_account_currency):
    total_amount = outstanding_amount = exchange_rate = None
    ref_doc = frappe.get_doc(reference_doctype, reference_name)
    company_currency = ref_doc.get(
        "company_currency") or condos.get_company_currency(ref_doc.company)

    if reference_doctype == "Fees":
        total_amount = ref_doc.get("grand_total")
        exchange_rate = 1
        outstanding_amount = ref_doc.get("outstanding_amount")
    elif reference_doctype == "Journal Entry" and ref_doc.docstatus == 1:
        total_amount = ref_doc.get("total_amount")
        if ref_doc.multi_currency:
            exchange_rate = get_exchange_rate(party_account_currency,
                                              company_currency,
                                              ref_doc.posting_date)
        else:
            exchange_rate = 1
            outstanding_amount = get_outstanding_on_journal_entry(
                reference_name)
    elif reference_doctype != "Journal Entry":
        if party_account_currency == company_currency:
            if ref_doc.doctype == "Expense Claim":
                total_amount = ref_doc.total_sanctioned_amount
            elif ref_doc.doctype == "Employee Advance":
                total_amount = ref_doc.advance_amount
            else:
                total_amount = ref_doc.base_grand_total
            exchange_rate = 1
        else:
            total_amount = ref_doc.grand_total

            # Get the exchange rate from the original ref doc
            # or get it based on the posting date of the ref doc
            exchange_rate = ref_doc.get("conversion_rate") or \
             get_exchange_rate(party_account_currency, company_currency, ref_doc.posting_date)

        if reference_doctype in ("Sales Invoice", "Purchase Invoice",
                                 "Expense Claim"):
            outstanding_amount = ref_doc.get("outstanding_amount")
        elif reference_doctype == "Employee Advance":
            outstanding_amount = ref_doc.advance_amount - flt(
                ref_doc.paid_amount)
        else:
            outstanding_amount = flt(total_amount) - flt(ref_doc.advance_paid)
    else:
        # Get the exchange rate based on the posting date of the ref doc
        exchange_rate = get_exchange_rate(party_account_currency,
                                          company_currency,
                                          ref_doc.posting_date)

    return frappe._dict({
        "due_date": ref_doc.get("due_date"),
        "total_amount": total_amount,
        "outstanding_amount": outstanding_amount,
        "exchange_rate": exchange_rate
    })
예제 #2
0
	def validate_conversion_rate(self):
		# validate conversion rate
		company_currency = condos.get_company_currency(self.doc.company)
		if not self.doc.currency or self.doc.currency == company_currency:
			self.doc.currency = company_currency
			self.doc.conversion_rate = 1.0
		else:
			validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
				self.doc.meta.get_label("conversion_rate"), self.doc.company)

		self.doc.conversion_rate = flt(self.doc.conversion_rate)
예제 #3
0
def get_account_balance_and_party_type(account,
                                       date,
                                       company,
                                       debit=None,
                                       credit=None,
                                       exchange_rate=None):
    """Returns dict of account balance and party type to be set in Journal Entry on selection of account."""
    if not frappe.has_permission("Account"):
        frappe.msgprint(_("No Permission"), raise_exception=1)

    company_currency = condos.get_company_currency(company)
    account_details = frappe.db.get_value("Account",
                                          account,
                                          ["account_type", "account_currency"],
                                          as_dict=1)

    if not account_details:
        return

    if account_details.account_type == "Receivable":
        party_type = "Customer"
    elif account_details.account_type == "Payable":
        party_type = "Supplier"
    else:
        party_type = ""

    grid_values = {
        "balance":
        get_balance_on(account, date),
        "party_type":
        party_type,
        "account_type":
        account_details.account_type,
        "account_currency":
        account_details.account_currency or company_currency,

        # The date used to retreive the exchange rate here is the date passed in
        # as an argument to this function. It is assumed to be the date on which the balance is sought
        "exchange_rate":
        get_exchange_rate(date,
                          account,
                          account_details.account_currency,
                          company,
                          debit=debit,
                          credit=credit,
                          exchange_rate=exchange_rate)
    }

    # un-set party if not party type
    if not party_type:
        grid_values["party"] = ""

    return grid_values
예제 #4
0
def get_outstanding(args):
    if not frappe.has_permission("Account"):
        frappe.msgprint(_("No Permission"), raise_exception=1)

    if isinstance(args, basestring):
        args = json.loads(args)

    company_currency = condos.get_company_currency(args.get("company"))

    if args.get("doctype") == "Journal Entry":
        condition = " and party=%(party)s" if args.get("party") else ""

        against_jv_amount = frappe.db.sql(
            """
			select sum(debit_in_account_currency) - sum(credit_in_account_currency)
			from `tabJournal Entry Account` where parent=%(docname)s and account=%(account)s {0}
			and (reference_type is null or reference_type = '')""".format(condition),
            args)

        against_jv_amount = flt(
            against_jv_amount[0][0]) if against_jv_amount else 0
        amount_field = "credit_in_account_currency" if against_jv_amount > 0 else "debit_in_account_currency"
        return {amount_field: abs(against_jv_amount)}
    elif args.get("doctype") in ("Sales Invoice", "Purchase Invoice"):
        party_type = "Customer" if args.get(
            "doctype") == "Sales Invoice" else "Supplier"
        invoice = frappe.db.get_value(
            args["doctype"],
            args["docname"],
            ["outstanding_amount", "conversion_rate",
             scrub(party_type)],
            as_dict=1)

        exchange_rate = invoice.conversion_rate if (
            args.get("account_currency") != company_currency) else 1

        if args["doctype"] == "Sales Invoice":
            amount_field = "credit_in_account_currency" \
             if flt(invoice.outstanding_amount) > 0 else "debit_in_account_currency"
        else:
            amount_field = "debit_in_account_currency" \
             if flt(invoice.outstanding_amount) > 0 else "credit_in_account_currency"

        return {
            amount_field: abs(flt(invoice.outstanding_amount)),
            "exchange_rate": exchange_rate,
            "party_type": party_type,
            "party": invoice.get(scrub(party_type))
        }
예제 #5
0
def _get_party_details(party=None,
                       account=None,
                       party_type="Customer",
                       company=None,
                       posting_date=None,
                       price_list=None,
                       currency=None,
                       doctype=None,
                       ignore_permissions=False):

    out = frappe._dict(
        set_account_and_due_date(party, account, party_type, company,
                                 posting_date, doctype))

    party = out[party_type.lower()]

    if not ignore_permissions and not frappe.has_permission(
            party_type, "read", party):
        frappe.throw(
            _("Not permitted for {0}").format(party), frappe.PermissionError)

    party = frappe.get_doc(party_type, party)
    currency = party.default_currency if party.default_currency else get_company_currency(
        company)

    set_address_details(out, party, party_type, doctype, company)
    set_contact_details(out, party, party_type)
    set_other_values(out, party, party_type)
    set_price_list(out, party, party_type, price_list)
    out["taxes_and_charges"] = set_taxes(party.name, party_type, posting_date,
                                         company, out.customer_group,
                                         out.supplier_type)
    out["payment_terms_template"] = get_pyt_term_template(
        party.name, party_type, company)

    if not out.get("currency"):
        out["currency"] = currency

    # sales team
    if party_type == "Customer":
        out["sales_team"] = [{
            "sales_person":
            d.sales_person,
            "allocated_percentage":
            d.allocated_percentage or None
        } for d in party.get("sales_team")]

    return out
예제 #6
0
def get_exchange_rate(posting_date,
                      account=None,
                      account_currency=None,
                      company=None,
                      reference_type=None,
                      reference_name=None,
                      debit=None,
                      credit=None,
                      exchange_rate=None):
    from condos.setup.utils import get_exchange_rate
    account_details = frappe.db.get_value(
        "Account",
        account, ["account_type", "root_type", "account_currency", "company"],
        as_dict=1)

    if not account_details:
        frappe.throw(_("Please select correct account"))

    if not company:
        company = account_details.company

    if not account_currency:
        account_currency = account_details.account_currency

    company_currency = condos.get_company_currency(company)

    if account_currency != company_currency:
        if reference_type in ("Sales Invoice",
                              "Purchase Invoice") and reference_name:
            exchange_rate = frappe.db.get_value(reference_type, reference_name,
                                                "conversion_rate")

        # The date used to retreive the exchange rate here is the date passed
        # in as an argument to this function.
        elif (not exchange_rate
              or exchange_rate == 1) and account_currency and posting_date:
            exchange_rate = get_exchange_rate(account_currency,
                                              company_currency, posting_date)
    else:
        exchange_rate = 1

    # don't return None or 0 as it is multipled with a value and that value could be lost
    return exchange_rate or 1
예제 #7
0
	def company_currency(self):
		if not hasattr(self, "__company_currency"):
			self.__company_currency = condos.get_company_currency(self.company)

		return self.__company_currency