refactor!: Subcontract Order Summary

This commit is contained in:
Sagar Sharma 2022-05-13 14:40:14 +05:30
parent 3469560105
commit 8486bbf31a
3 changed files with 35 additions and 38 deletions

View File

@ -14,32 +14,32 @@ frappe.query_reports["Subcontract Order Summary"] = {
},
{
label: __("From Date"),
fieldname:"from_date",
fieldname: "from_date",
fieldtype: "Date",
default: frappe.datetime.add_months(frappe.datetime.get_today(), -1),
reqd: 1
},
{
label: __("To Date"),
fieldname:"to_date",
fieldname: "to_date",
fieldtype: "Date",
default: frappe.datetime.get_today(),
reqd: 1
},
{
label: __("Purchase Order"),
label: __("Subcontracting Order"),
fieldname: "name",
fieldtype: "Link",
options: "Purchase Order",
get_query: function() {
options: "Subcontracting Order",
get_query: function () {
return {
filters: {
docstatus: 1,
is_subcontracted: 1,
company: frappe.query_report.get_filter_value('company')
}
}
}
}
]
};
};

View File

@ -15,7 +15,7 @@
"name": "Subcontract Order Summary",
"owner": "Administrator",
"prepared_report": 0,
"ref_doctype": "Purchase Order",
"ref_doctype": "Subcontracting Order",
"report_name": "Subcontract Order Summary",
"report_type": "Script Report",
"roles": [

View File

@ -20,34 +20,33 @@ def get_data(report_filters):
if orders:
supplied_items = get_supplied_items(orders, report_filters)
po_details = prepare_subcontracted_data(orders, supplied_items)
get_subcontracted_data(po_details, data)
sco_details = prepare_subcontracted_data(orders, supplied_items)
get_subcontracted_data(sco_details, data)
return data
def get_subcontracted_orders(report_filters):
fields = [
"`tabPurchase Order Item`.`parent` as po_id",
"`tabPurchase Order Item`.`item_code`",
"`tabPurchase Order Item`.`item_name`",
"`tabPurchase Order Item`.`qty`",
"`tabPurchase Order Item`.`name`",
"`tabPurchase Order Item`.`received_qty`",
"`tabPurchase Order`.`status`",
"`tabSubcontracting Order Item`.`parent` as sco_id",
"`tabSubcontracting Order Item`.`item_code`",
"`tabSubcontracting Order Item`.`item_name`",
"`tabSubcontracting Order Item`.`qty`",
"`tabSubcontracting Order Item`.`name`",
"`tabSubcontracting Order Item`.`received_qty`",
"`tabSubcontracting Order`.`status`",
]
filters = get_filters(report_filters)
return frappe.get_all("Purchase Order", fields=fields, filters=filters) or []
return frappe.get_all("Subcontracting Order", fields=fields, filters=filters) or []
def get_filters(report_filters):
filters = [
["Purchase Order", "docstatus", "=", 1],
["Purchase Order", "is_subcontracted", "=", 1],
["Subcontracting Order", "docstatus", "=", 1],
[
"Purchase Order",
"Subcontracting Order",
"transaction_date",
"between",
(report_filters.from_date, report_filters.to_date),
@ -56,7 +55,7 @@ def get_filters(report_filters):
for field in ["name", "company"]:
if report_filters.get(field):
filters.append(["Purchase Order", field, "=", report_filters.get(field)])
filters.append(["Subcontracting Order", field, "=", report_filters.get(field)])
return filters
@ -71,16 +70,15 @@ def get_supplied_items(orders, report_filters):
"rm_item_code",
"required_qty",
"supplied_qty",
"returned_qty",
"total_supplied_qty",
"consumed_qty",
"reference_name",
]
filters = {"parent": ("in", [d.po_id for d in orders]), "docstatus": 1}
filters = {"parent": ("in", [d.sco_id for d in orders]), "docstatus": 1}
supplied_items = {}
for row in frappe.get_all("Purchase Order Item Supplied", fields=fields, filters=filters):
for row in frappe.get_all("Subcontracting Order Supplied Item", fields=fields, filters=filters):
new_key = (row.parent, row.reference_name, row.main_item_code)
supplied_items.setdefault(new_key, []).append(row)
@ -89,24 +87,24 @@ def get_supplied_items(orders, report_filters):
def prepare_subcontracted_data(orders, supplied_items):
po_details = {}
sco_details = {}
for row in orders:
key = (row.po_id, row.name, row.item_code)
if key not in po_details:
po_details.setdefault(key, frappe._dict({"po_item": row, "supplied_items": []}))
key = (row.sco_id, row.name, row.item_code)
if key not in sco_details:
sco_details.setdefault(key, frappe._dict({"sco_item": row, "supplied_items": []}))
details = po_details[key]
details = sco_details[key]
if supplied_items.get(key):
for supplied_item in supplied_items[key]:
details["supplied_items"].append(supplied_item)
return po_details
return sco_details
def get_subcontracted_data(po_details, data):
for key, details in po_details.items():
res = details.po_item
def get_subcontracted_data(sco_details, data):
for key, details in sco_details.items():
res = details.sco_item
for index, row in enumerate(details.supplied_items):
if index != 0:
res = {}
@ -118,10 +116,10 @@ def get_subcontracted_data(po_details, data):
def get_columns():
return [
{
"label": _("Purchase Order"),
"fieldname": "po_id",
"label": _("Subcontracting Order"),
"fieldname": "sco_id",
"fieldtype": "Link",
"options": "Purchase Order",
"options": "Subcontracting Order",
"width": 100,
},
{"label": _("Status"), "fieldname": "status", "fieldtype": "Data", "width": 80},
@ -144,5 +142,4 @@ def get_columns():
{"label": _("Required Qty"), "fieldname": "required_qty", "fieldtype": "Float", "width": 110},
{"label": _("Supplied Qty"), "fieldname": "supplied_qty", "fieldtype": "Float", "width": 110},
{"label": _("Consumed Qty"), "fieldname": "consumed_qty", "fieldtype": "Float", "width": 120},
{"label": _("Returned Qty"), "fieldname": "returned_qty", "fieldtype": "Float", "width": 110},
]