37241-vm/api/bill_payments_post.php
2026-01-03 08:40:36 +00:00

99 lines
3.3 KiB
PHP

<?php
require_once __DIR__ . '/../includes/uuid.php';
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../includes/journal_helpers.php';
require_once __DIR__ . '/../includes/audit_helpers.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit();
}
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data['vendor_id']) || empty($data['payment_date']) || empty($data['payment_account_code']) || empty($data['amount']) || empty($data['bill_ids'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing required fields: vendor_id, payment_date, payment_account_code, amount, and bill_ids array.']);
exit();
}
$pdo = db();
try {
$pdo->beginTransaction();
$total_payment_amount = (float)$data['amount'];
$ap_account_code = '2100-AP'; // Accounts Payable
// 1. Create Journal Entry to settle AP
$journal_lines = [
// Debit Accounts Payable
[
'account_code' => $ap_account_code,
'type' => 'DEBIT',
'amount' => $total_payment_amount
],
// Credit the cash/bank account used for payment
[
'account_code' => $data['payment_account_code'],
'type' => 'CREDIT',
'amount' => $total_payment_amount
]
];
$journal_payload = [
'entry_date' => $data['payment_date'],
'description' => 'Payment for vendor ' . $data['vendor_id'],
'lines' => $journal_lines
];
$journal_result = post_journal_entry($pdo, $journal_payload);
if (empty($journal_result['journal_entry_id'])) {
throw new Exception('Failed to post journal entry: ' . ($journal_result['error'] ?? 'Unknown error'));
}
$journal_entry_id = $journal_result['journal_entry_id'];
// 2. Create the Payment Record
$payment_id = uuid_v4();
$stmt = $pdo->prepare(
"INSERT INTO payments (id, journal_entry_id, vendor_id, payment_date, amount, payment_method, reference_type) VALUES (?, ?, ?, ?, ?, ?, 'BILL')"
);
$stmt->execute([
$payment_id,
$journal_entry_id,
$data['vendor_id'],
$data['payment_date'],
$total_payment_amount,
$data['payment_method'] ?? 'Bank Transfer'
]);
// 3. Link payment to bills in payment_lines (and update bill statuses)
$stmt_line = $pdo->prepare("INSERT INTO payment_lines (id, payment_id, invoice_id) VALUES (?, ?, ?)"); // Reusing invoice_id for bill_id
$stmt_update_bill = $pdo->prepare("UPDATE bills SET status = 'PAID' WHERE bill_id = ?");
foreach($data['bill_ids'] as $bill_id) {
$stmt_line->execute([uuid_v4(), $payment_id, $bill_id]);
$stmt_update_bill->execute([$bill_id]);
}
// Log audit trail
log_audit_trail($pdo, 'pay_bill', ['payment_id' => $payment_id, 'vendor_id' => $data['vendor_id'], 'amount' => $total_payment_amount]);
$pdo->commit();
http_response_code(201);
echo json_encode([
'status' => 'success',
'payment_id' => $payment_id,
'journal_entry_id' => $journal_entry_id
]);
} catch (Exception $e) {
$pdo->rollBack();
http_response_code(500);
echo json_encode(['error' => 'Payment failed: ' . $e->getMessage()]);
}