82 lines
3.1 KiB
PHP
82 lines
3.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../src/config.php';
|
|
require_once __DIR__ . '/../src/auth.php';
|
|
require_once __DIR__ . '/../src/db.php';
|
|
require_once __DIR__ . '/../src/journal_functions.php';
|
|
require_login();
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$action = $_POST['action'] ?? $_GET['action'] ?? '';
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'save_entry':
|
|
$id = !empty($_POST['id']) ? (int)$_POST['id'] : null;
|
|
$data = [
|
|
'year_id' => (int)$_POST['year_id'],
|
|
'entry_date' => $_POST['entry_date'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'attachment_note' => $_POST['attachment_note'] ?? '',
|
|
'amount' => $_POST['amount'] ?? 0,
|
|
'supplier_id' => !empty($_POST['supplier_id']) ? (int)$_POST['supplier_id'] : null,
|
|
'sort_order' => (int)($_POST['sort_order'] ?? 0),
|
|
];
|
|
|
|
if (!$data['entry_date'] || !$data['description']) {
|
|
echo json_encode(['ok' => false, 'error' => 'Datum und Text sind Pflichtfelder.']);
|
|
exit;
|
|
}
|
|
|
|
$accounts = [];
|
|
$acct_types = $_POST['acct_type'] ?? [];
|
|
$acct_sides = $_POST['acct_side'] ?? [];
|
|
$acct_amounts = $_POST['acct_amount'] ?? [];
|
|
$acct_rev_ids = $_POST['acct_rev_id'] ?? [];
|
|
$acct_exp_ids = $_POST['acct_exp_id'] ?? [];
|
|
$acct_notes = $_POST['acct_note'] ?? [];
|
|
|
|
for ($i = 0; $i < count($acct_types); $i++) {
|
|
if (empty($acct_types[$i]) || (float)($acct_amounts[$i] ?? 0) == 0) continue;
|
|
$accounts[] = [
|
|
'account_type' => $acct_types[$i],
|
|
'side' => $acct_sides[$i] ?? 'soll',
|
|
'amount' => (float)($acct_amounts[$i] ?? 0),
|
|
'revenue_category_id' => !empty($acct_rev_ids[$i]) ? (int)$acct_rev_ids[$i] : null,
|
|
'expense_category_id' => !empty($acct_exp_ids[$i]) ? (int)$acct_exp_ids[$i] : null,
|
|
'note' => $acct_notes[$i] ?? '',
|
|
];
|
|
}
|
|
|
|
if (empty($accounts)) {
|
|
echo json_encode(['ok' => false, 'error' => 'Mindestens eine Kontenbuchung erforderlich.']);
|
|
exit;
|
|
}
|
|
|
|
$saved_id = save_journal_entry($id, $data, $accounts);
|
|
echo json_encode(['ok' => true, 'id' => $saved_id]);
|
|
break;
|
|
|
|
case 'delete_entry':
|
|
$del_id = (int)($_POST['id'] ?? 0);
|
|
if ($del_id) {
|
|
delete_journal_entry($del_id);
|
|
echo json_encode(['ok' => true]);
|
|
} else {
|
|
echo json_encode(['ok' => false, 'error' => 'Keine ID.']);
|
|
}
|
|
break;
|
|
|
|
case 'get_entry':
|
|
$get_id = (int)($_GET['id'] ?? 0);
|
|
$entry = get_journal_entry($get_id);
|
|
echo json_encode(['ok' => true, 'entry' => $entry]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['ok' => false, 'error' => 'Unbekannte Aktion.']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);
|
|
}
|