| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client1/seaside.pacim.de/web/ |
Upload File : |
<?php
/**
* invoice-actions.php – Rechnungsnummern-Vergabe & Zahlungs-Revision (PaCIM CRM). Nur Admins.
*
* POST action=create_single invoice_id -> vergibt EINE PAS-Nummer (nur falls Zahlungseingang, kein AIDA/Storno, noch keine Nummer)
* POST action=create_day date -> vergibt Nummern für ALLE Entwürfe (Anreisen des Tages mit Zahlungseingang)
* POST action=payment_revise invoice_id -> löscht alle HEUTE erfassten Zahlungen dieser Buchung und setzt den Status auf „offen"
*
* Rechnungsnummern werden bewusst NICHT mehr automatisch (bei Zahlung/Check-in) vergeben,
* sondern erst hier – vor der Tagesabrechnung bzw. einzeln aus der Buchungsansicht.
*/
require_once( __DIR__ . '/includes/config.inc.php' );
@ini_set( 'display_errors', '0' );
error_reporting( 0 );
function ia_json( $a ) { while ( ob_get_level() > 0 ) ob_end_clean(); header( 'Content-Type: application/json; charset=utf-8' ); echo json_encode( $a ); exit(); }
function ia_fail( $m ) { ia_json( array( 'ok' => false, 'error' => $m ) ); }
if ( ! function_exists( 'is_admin' ) || ! is_admin() ) { http_response_code( 403 ); ia_fail( 'Nicht berechtigt.' ); }
if ( $_SERVER['REQUEST_METHOD'] !== 'POST' ) ia_fail( 'Methode nicht erlaubt.' );
$action = (string) ( $_POST['action'] ?? '' );
$db = new MysqliDb( MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB );
/** Eine Rechnung nummerieren (mit allen Bedingungen). Gibt die neue Nummer zurück oder ''. */
function ia_assign_number( $db, $iid ) {
$iid = (int) $iid;
$row = $db->rawQueryOne(
"SELECT i.invoice_id, i.invoice_no, i.invoice_payment, i.invoice_status, i.invoice_date, c.customer_source
FROM pacim_invoice i JOIN pacim_customer c ON c.customer_id = i.invoice_customer
WHERE i.invoice_id = ?", array( $iid ) );
if ( ! $row ) return array( false, 'Rechnung nicht gefunden.' );
if ( strcasecmp( trim( (string) $row['customer_source'] ), 'AIDA' ) === 0 ) return array( false, 'AIDA-Buchung – keine PAS-Nummer.' );
if ( (int) $row['invoice_status'] === 3 ) return array( false, 'Storno – keine Nummer.' );
if ( trim( (string) $row['invoice_no'] ) !== '' ) return array( true, (string) $row['invoice_no'] ); // schon vorhanden
if ( ! in_array( (int) $row['invoice_payment'], array( 1, 2, 3 ), true ) ) return array( false, 'Kein Zahlungseingang.' );
$no = bookingservice::nextInvoiceNo( $db );
$upd = array( 'invoice_no' => $no );
// Rechnungsdatum sicherstellen (Zahlungs-/Erstellungstag), falls noch leer.
if ( trim( (string) $row['invoice_date'] ) === '' || (int) substr( (string) $row['invoice_date'], 0, 4 ) < 2000 )
$upd['invoice_date'] = date( 'Y-m-d' );
$db->where( 'invoice_id', $iid );
if ( ! $db->update( 'pacim_invoice', $upd ) ) return array( false, 'Konnte Nummer nicht speichern.' );
return array( true, $no );
}
if ( $action === 'create_single' ) {
$iid = (int) ( $_POST['invoice_id'] ?? 0 );
if ( $iid <= 0 ) ia_fail( 'Ungültige Rechnung.' );
list( $ok, $res ) = ia_assign_number( $db, $iid );
if ( ! $ok ) ia_fail( $res );
ia_json( array( 'ok' => true, 'invoice_no' => $res ) );
}
if ( $action === 'create_day' ) {
$date = substr( (string) ( $_POST['date'] ?? '' ), 0, 10 );
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date ) ) ia_fail( 'Ungültiges Datum.' );
// Alle Entwürfe (Anreisen des Tages mit Zahlungseingang, kein AIDA/Storno, ohne Nummer).
$rows = (array) $db->rawQuery(
"SELECT DISTINCT i.invoice_id
FROM pacim_invoice i
JOIN pacim_customer c ON c.customer_id = i.invoice_customer
WHERE i.invoice_status != 3
AND c.customer_source != 'AIDA'
AND i.invoice_payment IN (1,2,3)
AND ( i.invoice_no = '' OR i.invoice_no IS NULL )
AND EXISTS ( SELECT 1 FROM pacim_booking b WHERE b.booking_invoice = i.invoice_id AND DATE(b.booking_begin) = ? )
ORDER BY i.invoice_id ASC", array( $date ) );
$created = 0; $errors = 0; $items = array();
foreach ( $rows as $r ) {
$iid = (int) $r['invoice_id'];
list( $ok, $res ) = ia_assign_number( $db, $iid );
if ( $ok ) { $created++; $items[] = array( 'invoice_id' => $iid, 'invoice_no' => (string) $res ); } else $errors++;
}
ia_json( array( 'ok' => true, 'created' => $created, 'errors' => $errors, 'items' => $items ) );
}
if ( $action === 'payment_revise' ) {
$iid = (int) ( $_POST['invoice_id'] ?? 0 );
if ( $iid <= 0 ) ia_fail( 'Ungültige Rechnung.' );
$today = date( 'Y-m-d' );
// Alle HEUTE erfassten Zahlungen dieser Buchung löschen.
$db->rawQuery( "DELETE FROM pacim_payments WHERE payment_invoice = ? AND DATE(payment_date) = ?", array( $iid, $today ) );
// Verbleiben keine Zahlungen mehr -> Status auf „offen" zurücksetzen.
$rest = $db->rawQueryOne( "SELECT COUNT(*) AS n FROM pacim_payments WHERE payment_invoice = ?", array( $iid ) );
if ( ! $rest || (int) $rest['n'] === 0 ) {
$db->where( 'invoice_id', $iid );
$db->update( 'pacim_invoice', array( 'invoice_payment' => 0, 'invoice_paydate' => null, 'invoice_coupon' => null ) );
}
ia_json( array( 'ok' => true ) );
}
ia_fail( 'Unbekannte Aktion.' );