From b41e3c7bb1c17adc37163aacad92dda0f4560a7a Mon Sep 17 00:00:00 2001 From: Ronny Grobel Date: Fri, 17 Apr 2026 17:20:51 +0200 Subject: [PATCH] Fix parcel statement cost assignment behavior --- includes/Admin/Admin.php | 47 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/includes/Admin/Admin.php b/includes/Admin/Admin.php index 4cdc486..31f829f 100644 --- a/includes/Admin/Admin.php +++ b/includes/Admin/Admin.php @@ -1715,22 +1715,28 @@ 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(); + // Load parcel-specific cost assignments so entries can either apply to + // all parcels (mandatory) or only to explicitly assigned parcels (manual). + $entries_with_assignments = $this->costs->get_entry_ids_with_assignments( $year ); + $subject_assigned_ids = array(); + + foreach ( array_map( 'intval', $parcel_ids ) as $parcel_id ) { + $subject_assigned_ids = array_merge( $subject_assigned_ids, $this->costs->get_assigned_entry_ids( $parcel_id ) ); + } + + $subject_assigned_ids = array_values( array_unique( array_map( 'intval', $subject_assigned_ids ) ) ); foreach ( $cost_entries as $entry ) { - $entry_id = (int) $entry->id; - $has_assignments = in_array( $entry_id, $entries_with_assignments, true ); + $entry_id = (int) $entry->id; + $has_assignments = in_array( $entry_id, $entries_with_assignments, true ); + $is_assigned = in_array( $entry_id, $subject_assigned_ids, true ); + $is_mandatory = ! isset( $entry->is_mandatory ) || (bool) $entry->is_mandatory; - // 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 ) ) { + if ( ! $is_mandatory && ! $is_assigned ) { + continue; + } + + if ( $is_mandatory && $has_assignments && ! $is_assigned ) { continue; } @@ -1908,15 +1914,16 @@ class Admin {

  -   - +   +