Skip to content

Commit

Permalink
feat(Catch Log Statistics): group by fish species (LAN-854)
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Sep 23, 2024
1 parent d33c04b commit f5ebe5c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
1 change: 1 addition & 0 deletions landa/translations/de.csv
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ Sum of Payments,Zahlungssumme,
Statement of Fees and Payments,Beitragsabrechnung,
This Statement of Fees and Payments already exists: {0},Diese Beitragsabrechnung existiert bereits: {0},
This Statement of Fees and Payments must be linked to an Organization,Diese Beitragsabrechnung muss mit einem Verein verknüpft sein,
Group By Fish Species,Nach Fischart gruppieren,
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ frappe.query_reports["Catch Log Statistics"] = {
fieldname: "extra_columns",
fieldtype: "MultiSelectList",
label: __("Extra Columns"),
depends_on: "eval: !doc.group_by_fish_species",
get_data: get_extra_columns,
},
{
fieldname: "group_by_fish_species",
fieldtype: "Check",
label: __("Group By Fish Species"),
}
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ def get_columns(
show_area_name: bool = False,
show_water_body_size: bool = False,
show_water_body_status: bool = False,
group_by_fish_species: bool = False,
):
columns = [
{
"fieldname": "water_body",
"fieldtype": "Link",
"label": _("Water Body"),
"options": "Water Body",
},
{
"fieldname": "water_body_title",
"fieldtype": "Data",
"label": _("Water Body Title"),
"width": 200,
},
]
columns = []

if not group_by_fish_species:
columns.extend(
[
{
"fieldname": "water_body",
"fieldtype": "Link",
"label": _("Water Body"),
"options": "Water Body",
},
{
"fieldname": "water_body_title",
"fieldtype": "Data",
"label": _("Water Body Title"),
"width": 200,
},
]
)

if show_water_body_status:
columns.append(
Expand Down Expand Up @@ -121,20 +127,19 @@ def get_data(
show_area_name: bool = False,
show_water_body_size: bool = False,
show_water_body_status: bool = False,
group_by_fish_species: bool = False,
):
entry = frappe.qb.DocType("Catch Log Entry")
child_table = frappe.qb.DocType("Catch Log Fish Table")
qb_filters = get_qb_filters(filters, entry, child_table)

query = (
frappe.qb.from_(entry)
.join(child_table)
.on(entry.name == child_table.parent)
.select(
query = frappe.qb.from_(entry).join(child_table).on(entry.name == child_table.parent)

if not group_by_fish_species:
query = query.select(
entry.water_body,
entry.water_body_title,
)
)

if show_water_body_status or show_water_body_size:
water_body = frappe.qb.DocType("Water Body")
Expand Down Expand Up @@ -191,7 +196,7 @@ def get_data(
.select(proportion_foreign.by_foreign_regional_org)
)

query = filter_and_group(query, entry, child_table, qb_filters)
query = filter_and_group(query, entry, child_table, qb_filters, group_by_fish_species)
return query.run()


Expand All @@ -210,10 +215,16 @@ def get_subquery(entry: Table, child_table: Table, qb_filters: List[Criterion]):
return filter_and_group(subquery, entry, child_table, qb_filters)


def filter_and_group(query, entry: Table, child_table: Table, qb_filters: List[Criterion]):
def filter_and_group(
query, entry: Table, child_table: Table, qb_filters: List[Criterion], group_by_fish_species=False
):
query = add_conditions(query, qb_filters)
query = add_or_filters(query, entry)
query = query.groupby(entry.water_body, entry.water_body_title, child_table.fish_species)
if group_by_fish_species:
query = query.groupby(child_table.fish_species)
else:
query = query.groupby(entry.water_body, entry.water_body_title, child_table.fish_species)

return query


Expand Down Expand Up @@ -298,22 +309,35 @@ def is_regional_or_state_employee():


def execute(filters=None):
group_by_fish_species = bool(filters.pop("group_by_fish_species", 0))
extra_columns = filters.pop("extra_columns", [])
show_by_foreign_regional_org = "by_foreign_regional_org" in extra_columns
show_area_name = "area_name" in extra_columns
show_water_body_size = "water_body_size" in extra_columns
show_water_body_status = "water_body_status" in extra_columns

if not group_by_fish_species:
show_by_foreign_regional_org = "by_foreign_regional_org" in extra_columns
show_area_name = "area_name" in extra_columns
show_water_body_size = "water_body_size" in extra_columns
show_water_body_status = "water_body_status" in extra_columns
else:
show_by_foreign_regional_org = False
show_area_name = False
show_water_body_size = False
show_water_body_status = False

return (
get_columns(
show_by_foreign_regional_org, show_area_name, show_water_body_size, show_water_body_status
show_by_foreign_regional_org,
show_area_name,
show_water_body_size,
show_water_body_status,
group_by_fish_species,
),
get_data(
filters,
show_by_foreign_regional_org,
show_area_name,
show_water_body_size,
show_water_body_status,
group_by_fish_species,
)
or [],
)

0 comments on commit f5ebe5c

Please sign in to comment.