diff --git a/landa/translations/de.csv b/landa/translations/de.csv index d4104b86..9d22c636 100644 --- a/landa/translations/de.csv +++ b/landa/translations/de.csv @@ -37,3 +37,5 @@ There is no single address linked to {0}.,Es ist keine einzelne Adresse mit {0} Year must be between {0} and {1}.,Jahr muss zwischen {0} und {1} liegen., Create Stocking Measure,Neue Besatzmaßnahme, % In Progress,% in Bearbeitung, +Create Stocking Targets,Besatzplanungen erstellen, +"Based on your selection, Stocking Targets are created accordingly for the specified year.","Basierend auf Ihrer Auswahl werden für das angegebene Jahr entsprechende Besatzplanungen erstellt.", diff --git a/landa/water_body_management/doctype/stocking_measure/stocking_measure.py b/landa/water_body_management/doctype/stocking_measure/stocking_measure.py index 4aa95c87..6b46a183 100644 --- a/landa/water_body_management/doctype/stocking_measure/stocking_measure.py +++ b/landa/water_body_management/doctype/stocking_measure/stocking_measure.py @@ -19,3 +19,53 @@ def update_stocking_target(self): # saving a Stocking Target triggers validation, including a status update frappe.get_doc("Stocking Target", self.stocking_target).save() + + +@frappe.whitelist() +def create_stocking_targets(stocking_measure_names, year): + import json + + if isinstance(stocking_measure_names, str): + stocking_measure_names = json.loads(stocking_measure_names) + + stocking_measures = frappe.get_all( + "Stocking Measure", + filters={"name": ["in", stocking_measure_names]}, + fields=[ + "name", + "fish_species", + "fish_type_for_stocking", + "organization", + "water_body", + "weight", + "quantity", + ], + ) + + stocking_targets = {} + + for stocking_measure in stocking_measures: + primary_key = ( + stocking_measure["fish_species"], + stocking_measure["fish_type_for_stocking"], + stocking_measure["water_body"], + ) + + if primary_key not in stocking_targets: + stocking_targets[primary_key] = { + "year": int(year), + "organization": stocking_measure["organization"], + "water_body": stocking_measure["water_body"], + "fish_species": stocking_measure["fish_species"], + "fish_type_for_stocking": stocking_measure["fish_type_for_stocking"], + "weight": 0, + "quantity": 0, + } + + stocking_targets[primary_key]["weight"] += stocking_measure["weight"] + stocking_targets[primary_key]["quantity"] += stocking_measure["quantity"] + + for stocking_target in list(stocking_targets.values()): + doc = frappe.new_doc("Stocking Target") + doc.update(stocking_target) + doc.save() diff --git a/landa/water_body_management/doctype/stocking_measure/stocking_measure_list.js b/landa/water_body_management/doctype/stocking_measure/stocking_measure_list.js index 1ee98149..f001414f 100644 --- a/landa/water_body_management/doctype/stocking_measure/stocking_measure_list.js +++ b/landa/water_body_management/doctype/stocking_measure/stocking_measure_list.js @@ -1,10 +1,56 @@ frappe.listview_settings["Stocking Measure"] = { get_indicator: function (doc) { - var status_color = { + const status_color = { Draft: "red", "In Progress": "blue", Completed: "green", }; + return [__(doc.status), status_color[doc.status], "status,=," + doc.status]; }, + refresh: function (doc) { + doc.page.add_actions_menu_item( + __("Create Stocking Targets"), + () => showCreateStockingTargetsDialog(), + false + ); // Needs to be in refresh, otherwise it won't work in the Report View + }, }; + +function showCreateStockingTargetsDialog() { + const dialog = new frappe.ui.Dialog({ + title: __("Create Stocking Targets"), + fields: [ + { + label: __("Year"), + fieldname: "year", + fieldtype: "Int", + reqd: 1, + default: landa.utils.get_default_year(), + description: __( + "Based on your selection, Stocking Targets are created accordingly for the specified year." + ), + }, + ], + size: "small", + primary_action_label: __("Create"), + primary_action(values) { + const selected_measures = cur_list.get_checked_items(true); + + frappe.call({ + method: "landa.water_body_management.doctype.stocking_measure.stocking_measure.create_stocking_targets", + args: { + stocking_measure_names: selected_measures, + year: values.year, + }, + callback: function (response) { + frappe.set_route("List", "Stocking Target", "List"); + }, + }); + + dialog.hide(); + }, + }); + + dialog.show(); +}