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

View File

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

View File

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