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

@@ -310,4 +310,146 @@ class CostRepository extends AbstractRepository {
return (float) $total;
}
// -------------------------------------------------------------------------
// Parcel cost assignments
// -------------------------------------------------------------------------
/**
* Get the parcel cost assignments table name.
*
* @return string
*/
private function assignment_table() {
return Schema::table( 'parcel_cost_assignments' );
}
/**
* Get cost entry IDs assigned to a specific parcel.
*
* @param int $parcel_id Parcel ID.
* @return int[]
*/
public function get_assigned_entry_ids( $parcel_id ) {
$parcel_id = absint( $parcel_id );
$table = $this->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' ) );
}
}
}