2017-01-02 10:50:43 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2017-01-02 10:50:43 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.model.meta import get_field_precision
|
|
|
|
|
|
|
|
from erpnext import get_default_currency
|
2021-09-02 11:14:59 +00:00
|
|
|
|
|
|
|
|
2017-01-02 10:50:43 +00:00
|
|
|
def get_ordered_to_be_billed_data(args):
|
|
|
|
doctype, party = args.get("doctype"), args.get("party")
|
|
|
|
child_tab = doctype + " Item"
|
|
|
|
precision = (
|
|
|
|
get_field_precision(
|
|
|
|
frappe.get_meta(child_tab).get_field("billed_amt"), currency=get_default_currency()
|
|
|
|
)
|
|
|
|
or 2
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-04-08 10:59:59 +00:00
|
|
|
|
2017-01-02 10:50:43 +00:00
|
|
|
project_field = get_project_field(doctype, party)
|
|
|
|
|
|
|
|
return frappe.db.sql(
|
|
|
|
"""
|
|
|
|
Select
|
2020-09-09 10:54:11 +00:00
|
|
|
`{parent_tab}`.name, `{parent_tab}`.{date_field},
|
|
|
|
`{parent_tab}`.{party}, `{parent_tab}`.{party}_name,
|
|
|
|
`{child_tab}`.item_code,
|
|
|
|
`{child_tab}`.base_amount,
|
2019-04-08 10:59:59 +00:00
|
|
|
(`{child_tab}`.billed_amt * ifnull(`{parent_tab}`.conversion_rate, 1)),
|
2020-09-09 10:54:11 +00:00
|
|
|
(`{child_tab}`.base_rate * ifnull(`{child_tab}`.returned_qty, 0)),
|
|
|
|
(`{child_tab}`.base_amount -
|
|
|
|
(`{child_tab}`.billed_amt * ifnull(`{parent_tab}`.conversion_rate, 1)) -
|
|
|
|
(`{child_tab}`.base_rate * ifnull(`{child_tab}`.returned_qty, 0))),
|
|
|
|
`{child_tab}`.item_name, `{child_tab}`.description,
|
|
|
|
{project_field}, `{parent_tab}`.company
|
2017-01-02 10:50:43 +00:00
|
|
|
from
|
|
|
|
`{parent_tab}`, `{child_tab}`
|
|
|
|
where
|
2019-06-26 05:27:45 +00:00
|
|
|
`{parent_tab}`.name = `{child_tab}`.parent and `{parent_tab}`.docstatus = 1
|
|
|
|
and `{parent_tab}`.status not in ('Closed', 'Completed')
|
2020-09-09 10:54:11 +00:00
|
|
|
and `{child_tab}`.amount > 0
|
|
|
|
and (`{child_tab}`.base_amount -
|
|
|
|
round(`{child_tab}`.billed_amt * ifnull(`{parent_tab}`.conversion_rate, 1), {precision}) -
|
|
|
|
(`{child_tab}`.base_rate * ifnull(`{child_tab}`.returned_qty, 0))) > 0
|
2017-01-02 10:50:43 +00:00
|
|
|
order by
|
|
|
|
`{parent_tab}`.{order} {order_by}
|
|
|
|
""".format(
|
|
|
|
parent_tab="tab" + doctype,
|
|
|
|
child_tab="tab" + child_tab,
|
|
|
|
precision=precision,
|
|
|
|
party=party,
|
|
|
|
date_field=args.get("date"),
|
|
|
|
project_field=project_field,
|
|
|
|
order=args.get("order"),
|
|
|
|
order_by=args.get("order_by"),
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2017-01-02 10:50:43 +00:00
|
|
|
|
|
|
|
def get_project_field(doctype, party):
|
|
|
|
if party == "supplier":
|
|
|
|
doctype = doctype + " Item"
|
|
|
|
return "`tab%s`.project" % (doctype)
|