Skip to content

Commit

Permalink
Merge pull request #244 from scdanieli/lan-117-create-stocking-targets
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra authored Aug 22, 2023
2 parents 7dae28e + d23a594 commit 3f2e141
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions landa/translations/de.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 3f2e141

Please sign in to comment.