Feature: Parzellenspezifische Kostenpositionsenzuweisung

- Neue Tabelle wp_kgvvm_parcel_cost_assignments für 1:N Zuordnungen
- CostRepository erweitert: get_assigned_entry_ids(), get_entry_ids_with_assignments(), assign_to_parcel(), unassign_from_parcel(), delete_assignments_for_entry()
- Admin: Neue POST-Action toggle_parcel_cost_assignment() mit Nonce-Sicherung
- Jahresabrechnung Parzelle: Rechte Seitenleiste zeigt alle Kostenpositions mit Zuordnungsstatus (✓ zugeordnet, ✗ nicht zugeordnet, – alle Parzellen)
- Berechnung: Kostenposten mit Beschränkung werden nur berechnet wenn Parzelle zugeordnet ist
- DataTransfer.php: parcel_cost_assignments in table_keys integriert für Export/Import
- DELETE-Handler bereinigt Zuordnungen beim Löschen einer Kostenposition
This commit is contained in:
2026-04-17 16:55:09 +02:00
parent a1e6c52eaf
commit 1a6b1199cd
4 changed files with 287 additions and 2 deletions

View File

@@ -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;
}
?>
<style>
@media screen {
.kgvvm-statement-layout { display: flex; gap: 24px; align-items: flex-start; }
.kgvvm-statement-main { flex: 1 1 0; min-width: 0; }
.kgvvm-statement-sidebar { flex: 0 0 300px; width: 300px; }
}
@media print { .kgvvm-statement-sidebar { display: none !important; } }
.kgvvm-cost-assignment-list { list-style: none; margin: 0; padding: 0; }
.kgvvm-cost-assignment-list li { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid #f0f0f0; gap: 8px; }
.kgvvm-cost-assignment-list li:last-child { border-bottom: none; }
.kgvvm-cost-assignment-list .kgvvm-entry-name { flex: 1; }
</style>
<div class='wrap kgvvm-print-page'>
<h1><?php echo esc_html( $subject_label ); ?></h1>
<div class='kgvvm-print-actions'>
@@ -1722,6 +1784,9 @@ class Admin {
<a href='<?php echo esc_url( $this->admin_url( 'kgvvm-costs', array( 'year' => $year ) ) ); ?>' class='button-secondary'><?php echo esc_html__( 'Zurück', KGVVM_TEXT_DOMAIN ); ?></a>
</div>
<div class='kgvvm-statement-layout'>
<div class='kgvvm-statement-main'>
<?php if ( ! empty( $settings['pdf_club_name'] ) || ! empty( $settings['pdf_logo_url'] ) || ! empty( $settings['pdf_contact_block'] ) || ! empty( $settings['pdf_intro_text'] ) ) : ?>
<div class='kgvvm-card'>
<table style='width:100%; border-collapse:collapse;'>
@@ -1819,6 +1884,72 @@ class Admin {
<p><?php echo wp_kses_post( nl2br( esc_html( $settings['pdf_footer_text'] ) ) ); ?></p>
</div>
<?php endif; ?>
</div><!-- .kgvvm-statement-main -->
<?php if ( 'parcel' === $statement_type ) : ?>
<div class='kgvvm-statement-sidebar'>
<div class='kgvvm-card'>
<h3 style='margin-top:0;'><?php echo esc_html__( 'Kostenpositionen', KGVVM_TEXT_DOMAIN ); ?></h3>
<p class='description'><?php echo esc_html__( 'Hier legen Sie fest, welche Positionen dieser Parzelle berechnet werden. Positionen ohne Einschränkung gelten für alle Parzellen.', KGVVM_TEXT_DOMAIN ); ?></p>
<?php if ( empty( $cost_entries ) ) : ?>
<p><?php echo esc_html__( 'Für dieses Jahr sind keine Kostenposten hinterlegt.', KGVVM_TEXT_DOMAIN ); ?></p>
<?php else : ?>
<ul class='kgvvm-cost-assignment-list'>
<?php foreach ( $cost_entries as $entry ) :
$entry_id = (int) $entry->id;
$is_assigned = in_array( $entry_id, $parcel_assigned_ids, true );
$has_any = in_array( $entry_id, $entries_with_assignments, true );
?>
<li>
<span class='kgvvm-entry-name'>
<?php if ( $is_assigned ) : ?>
<span style='color:#007017;' title='<?php esc_attr_e( 'Diese Parzelle ist zugeordnet', KGVVM_TEXT_DOMAIN ); ?>'>&#10003;</span>
<?php elseif ( $has_any ) : ?>
<span style='color:#b32d2e;' title='<?php esc_attr_e( 'Diese Parzelle ist nicht zugeordnet', KGVVM_TEXT_DOMAIN ); ?>'>&#10007;</span>
<?php else : ?>
<span style='color:#999;' title='<?php esc_attr_e( 'Gilt für alle Parzellen', KGVVM_TEXT_DOMAIN ); ?>'>&#8212;</span>
<?php endif; ?>
<?php echo esc_html( $entry->name ); ?>
</span>
<?php if ( $is_assigned ) : ?>
<form method='post' action='<?php echo esc_url( admin_url( 'admin.php' ) ); ?>' style='display:inline;'>
<?php wp_nonce_field( 'kgvvm_toggle_parcel_cost' ); ?>
<input type='hidden' name='kgvvm_action' value='toggle_parcel_cost_assignment' />
<input type='hidden' name='parcel_id' value='<?php echo esc_attr( $subject_id ); ?>' />
<input type='hidden' name='cost_entry_id' value='<?php echo esc_attr( $entry_id ); ?>' />
<input type='hidden' name='assignment_mode' value='remove' />
<input type='hidden' name='year' value='<?php echo esc_attr( $year ); ?>' />
<input type='hidden' name='page' value='kgvvm-costs' />
<button type='submit' class='button button-small'><?php echo esc_html__( 'Entfernen', KGVVM_TEXT_DOMAIN ); ?></button>
</form>
<?php else : ?>
<form method='post' action='<?php echo esc_url( admin_url( 'admin.php' ) ); ?>' style='display:inline;'>
<?php wp_nonce_field( 'kgvvm_toggle_parcel_cost' ); ?>
<input type='hidden' name='kgvvm_action' value='toggle_parcel_cost_assignment' />
<input type='hidden' name='parcel_id' value='<?php echo esc_attr( $subject_id ); ?>' />
<input type='hidden' name='cost_entry_id' value='<?php echo esc_attr( $entry_id ); ?>' />
<input type='hidden' name='assignment_mode' value='add' />
<input type='hidden' name='year' value='<?php echo esc_attr( $year ); ?>' />
<input type='hidden' name='page' value='kgvvm-costs' />
<button type='submit' class='button button-small button-primary'><?php echo esc_html__( 'Hinzufügen', KGVVM_TEXT_DOMAIN ); ?></button>
</form>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<p class='description' style='margin-top:12px; font-size:11px;'>
<span style='color:#007017;'>&#10003;</span> <?php echo esc_html__( 'zugeordnet', KGVVM_TEXT_DOMAIN ); ?> &nbsp;
<span style='color:#b32d2e;'>&#10007;</span> <?php echo esc_html__( 'nicht zugeordnet', KGVVM_TEXT_DOMAIN ); ?> &nbsp;
<span style='color:#999;'>&#8212;</span> <?php echo esc_html__( 'alle Parzellen', KGVVM_TEXT_DOMAIN ); ?>
</p>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div><!-- .kgvvm-statement-layout -->
</div>
<?php
}