diff --git a/includes/Admin/Admin.php b/includes/Admin/Admin.php
index 762fe2a..2718eb1 100644
--- a/includes/Admin/Admin.php
+++ b/includes/Admin/Admin.php
@@ -249,6 +249,9 @@ class Admin {
case 'import_plugin_data':
$this->import_plugin_data();
break;
+ case 'toggle_parcel_cost_assignment':
+ $this->toggle_parcel_cost_assignment();
+ break;
}
}
@@ -313,6 +316,7 @@ class Admin {
$this->redirect_with_notice( 'kgvvm-costs', 'error', __( 'Der gewünschte Kostenposten wurde nicht gefunden.', KGVVM_TEXT_DOMAIN ), array( 'year' => $year ) );
}
$year = (int) $cost->entry_year;
+ $this->costs->delete_assignments_for_entry( $id );
$this->costs->delete( $id );
$this->redirect_with_notice( 'kgvvm-costs', 'success', __( 'Kostenposten wurde gelöscht.', KGVVM_TEXT_DOMAIN ), array( 'year' => $year ) );
break;
@@ -835,6 +839,34 @@ class Admin {
$this->redirect_with_notice( 'kgvvm-costs', 'success', __( 'Kostenposten wurde gespeichert.', KGVVM_TEXT_DOMAIN ), array( 'year' => $data['entry_year'] ) );
}
+ /**
+ * Add or remove a cost entry assignment for a specific parcel.
+ *
+ * @return void
+ */
+ private function toggle_parcel_cost_assignment() {
+ $this->require_cap( 'manage_kleingarten' );
+ check_admin_referer( 'kgvvm_toggle_parcel_cost' );
+
+ $parcel_id = absint( isset( $_POST['parcel_id'] ) ? $_POST['parcel_id'] : 0 );
+ $cost_entry_id = absint( isset( $_POST['cost_entry_id'] ) ? $_POST['cost_entry_id'] : 0 );
+ $mode = isset( $_POST['assignment_mode'] ) ? sanitize_key( wp_unslash( $_POST['assignment_mode'] ) ) : '';
+ $year = absint( isset( $_POST['year'] ) ? $_POST['year'] : 0 );
+
+ if ( $parcel_id < 1 || $cost_entry_id < 1 || ! in_array( $mode, array( 'add', 'remove' ), true ) ) {
+ $this->redirect_with_notice( 'kgvvm-costs', 'error', __( 'Ungültige Anfrage.', KGVVM_TEXT_DOMAIN ), array( 'view' => 'statement', 'statement_type' => 'parcel', 'subject_id' => $parcel_id, 'year' => $year ) );
+ }
+
+ if ( 'add' === $mode ) {
+ $this->costs->assign_to_parcel( $parcel_id, $cost_entry_id );
+ } else {
+ $this->costs->unassign_from_parcel( $parcel_id, $cost_entry_id );
+ }
+
+ wp_safe_redirect( $this->admin_url( 'kgvvm-costs', array( 'view' => 'statement', 'statement_type' => 'parcel', 'subject_id' => $parcel_id, 'year' => $year ) ) );
+ exit;
+ }
+
/**
* Render dashboard.
*
@@ -1673,7 +1705,25 @@ class Admin {
$fixed_items = array();
$fixed_total = 0.0;
+ // Load parcel-specific cost assignments so that entries with explicit
+ // assignments are only charged to the assigned parcels.
+ $entries_with_assignments = ( 'parcel' === $statement_type )
+ ? $this->costs->get_entry_ids_with_assignments( $year )
+ : array();
+ $parcel_assigned_ids = ( 'parcel' === $statement_type )
+ ? $this->costs->get_assigned_entry_ids( $subject_id )
+ : array();
+
foreach ( $cost_entries as $entry ) {
+ $entry_id = (int) $entry->id;
+ $has_assignments = in_array( $entry_id, $entries_with_assignments, true );
+
+ // For parcel statements: if this entry has any parcel restrictions,
+ // skip it unless this specific parcel is assigned.
+ if ( 'parcel' === $statement_type && $has_assignments && ! in_array( $entry_id, $parcel_assigned_ids, true ) ) {
+ continue;
+ }
+
$distribution_type = $this->get_cost_distribution_type( $entry );
$unit_amount = $this->get_cost_unit_amount( $entry, count( $active_parcels ), count( $active_members ) );
$subject_units = 'member' === $distribution_type ? $subject_member_count : $subject_parcel_count;
@@ -1714,6 +1764,18 @@ class Admin {
return;
}
?>
+
+
+
+
@@ -1819,6 +1884,72 @@ class Admin {
+
+
+
+
+
+
+
+
assignment_table();
+
+ if ( $parcel_id < 1 || '' === $table ) {
+ return array();
+ }
+
+ $ids = $this->wpdb->get_col( $this->wpdb->prepare( "SELECT cost_entry_id FROM {$table} WHERE parcel_id = %d", $parcel_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+
+ return array_map( 'intval', (array) $ids );
+ }
+
+ /**
+ * Get all cost entry IDs that have at least one parcel assignment.
+ *
+ * @param int $year Optional – filter by entry year (0 = all years).
+ * @return int[]
+ */
+ public function get_entry_ids_with_assignments( $year = 0 ) {
+ $table = $this->assignment_table();
+
+ if ( '' === $table ) {
+ return array();
+ }
+
+ $year = absint( $year );
+
+ if ( $year > 0 ) {
+ $ids = $this->wpdb->get_col( $this->wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ "SELECT DISTINCT a.cost_entry_id FROM {$table} a
+ INNER JOIN {$this->table} e ON e.id = a.cost_entry_id
+ WHERE e.entry_year = %d",
+ $year
+ ) );
+ } else {
+ $ids = $this->wpdb->get_col( "SELECT DISTINCT cost_entry_id FROM {$table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery
+ }
+
+ return array_map( 'intval', (array) $ids );
+ }
+
+ /**
+ * Assign a cost entry to a parcel.
+ *
+ * @param int $parcel_id Parcel ID.
+ * @param int $cost_entry_id Cost entry ID.
+ * @return bool
+ */
+ public function assign_to_parcel( $parcel_id, $cost_entry_id ) {
+ $parcel_id = absint( $parcel_id );
+ $cost_entry_id = absint( $cost_entry_id );
+ $table = $this->assignment_table();
+
+ if ( $parcel_id < 1 || $cost_entry_id < 1 || '' === $table ) {
+ return false;
+ }
+
+ $exists = (int) $this->wpdb->get_var( $this->wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ "SELECT COUNT(*) FROM {$table} WHERE parcel_id = %d AND cost_entry_id = %d",
+ $parcel_id,
+ $cost_entry_id
+ ) );
+
+ if ( $exists > 0 ) {
+ return true;
+ }
+
+ $result = $this->wpdb->insert(
+ $table,
+ array(
+ 'parcel_id' => $parcel_id,
+ 'cost_entry_id' => $cost_entry_id,
+ 'created_at' => $this->now(),
+ ),
+ array( '%d', '%d', '%s' )
+ );
+
+ return false !== $result;
+ }
+
+ /**
+ * Remove a cost entry assignment from a parcel.
+ *
+ * @param int $parcel_id Parcel ID.
+ * @param int $cost_entry_id Cost entry ID.
+ * @return bool
+ */
+ public function unassign_from_parcel( $parcel_id, $cost_entry_id ) {
+ $parcel_id = absint( $parcel_id );
+ $cost_entry_id = absint( $cost_entry_id );
+ $table = $this->assignment_table();
+
+ if ( $parcel_id < 1 || $cost_entry_id < 1 || '' === $table ) {
+ return false;
+ }
+
+ $result = $this->wpdb->delete(
+ $table,
+ array(
+ 'parcel_id' => $parcel_id,
+ 'cost_entry_id' => $cost_entry_id,
+ ),
+ array( '%d', '%d' )
+ );
+
+ return false !== $result;
+ }
+
+ /**
+ * Remove all parcel assignments for a cost entry (e.g. when the entry is deleted).
+ *
+ * @param int $cost_entry_id Cost entry ID.
+ * @return void
+ */
+ public function delete_assignments_for_entry( $cost_entry_id ) {
+ $table = $this->assignment_table();
+
+ if ( '' !== $table && absint( $cost_entry_id ) > 0 ) {
+ $this->wpdb->delete( $table, array( 'cost_entry_id' => absint( $cost_entry_id ) ), array( '%d' ) );
+ }
+ }
}
diff --git a/includes/Schema.php b/includes/Schema.php
index 56d99df..3925d2b 100644
--- a/includes/Schema.php
+++ b/includes/Schema.php
@@ -36,8 +36,9 @@ class Schema {
'cost_entries' => $wpdb->prefix . 'kgvvm_cost_entries',
'work_jobs' => $wpdb->prefix . 'kgvvm_work_jobs',
'work_year_config' => $wpdb->prefix . 'kgvvm_work_year_config',
- 'work_logs' => $wpdb->prefix . 'kgvvm_work_logs',
- 'work_log_members' => $wpdb->prefix . 'kgvvm_work_log_members',
+'work_logs' => $wpdb->prefix . 'kgvvm_work_logs',
+ 'work_log_members' => $wpdb->prefix . 'kgvvm_work_log_members',
+ 'parcel_cost_assignments' => $wpdb->prefix . 'kgvvm_parcel_cost_assignments',
);
return isset( $map[ $key ] ) ? $map[ $key ] : '';
@@ -254,6 +255,14 @@ class Schema {
KEY log_id (log_id)
) {$charset_collate};";
+ $sql[] = "CREATE TABLE " . self::table( 'parcel_cost_assignments' ) . " (
+ parcel_id BIGINT UNSIGNED NOT NULL,
+ cost_entry_id BIGINT UNSIGNED NOT NULL,
+ created_at DATETIME NOT NULL,
+ PRIMARY KEY (parcel_id, cost_entry_id),
+ KEY cost_entry_id (cost_entry_id)
+ ) {$charset_collate};";
+
foreach ( $sql as $statement ) {
dbDelta( $statement );
}
@@ -283,6 +292,7 @@ class Schema {
self::table( 'work_year_config' ),
self::table( 'work_logs' ),
self::table( 'work_log_members' ),
+ self::table( 'parcel_cost_assignments' ),
);
foreach ( $tables as $table ) {
@@ -301,6 +311,7 @@ class Schema {
global $wpdb;
$tables = array(
+ self::table( 'parcel_cost_assignments' ),
self::table( 'work_log_members' ),
self::table( 'work_logs' ),
self::table( 'work_year_config' ),