mirror of
https://github.com/getnamingo/registry.git
synced 2025-07-26 04:18:29 +02:00
Refund operations now show correctly in invoices
This commit is contained in:
parent
04b4e84741
commit
53516e01cb
5 changed files with 104 additions and 54 deletions
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
$c = require_once 'config.php';
|
$c = require_once 'config.php';
|
||||||
require_once 'helpers.php';
|
require_once 'helpers.php';
|
||||||
|
@ -18,32 +18,15 @@ try {
|
||||||
$log->error('DB Connection failed: ' . $e->getMessage());
|
$log->error('DB Connection failed: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name");
|
$settingsStmt = $pdo->query("
|
||||||
$stmt->execute(['name' => 'email']);
|
SELECT name, value FROM settings
|
||||||
$row = $stmt->fetch();
|
WHERE name IN ('email', 'phone', 'company_name')
|
||||||
if ($row) {
|
");
|
||||||
$supportEmail = $row['value'];
|
$settings = $settingsStmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||||
} else {
|
|
||||||
$supportEmail = 'default-support@example.com';
|
|
||||||
}
|
|
||||||
|
|
||||||
$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name");
|
$supportEmail = $settings['email'] ?? 'default-support@example.com';
|
||||||
$stmt->execute(['name' => 'phone']);
|
$supportPhoneNumber = $settings['phone'] ?? '+1.23456789';
|
||||||
$row = $stmt->fetch();
|
$registryName = $settings['company_name'] ?? 'Example Registry LLC';
|
||||||
if ($row) {
|
|
||||||
$supportPhoneNumber = $row['value'];
|
|
||||||
} else {
|
|
||||||
$supportPhoneNumber = '+1.23456789';
|
|
||||||
}
|
|
||||||
|
|
||||||
$stmt = $pdo->prepare("SELECT value FROM settings WHERE name = :name");
|
|
||||||
$stmt->execute(['name' => 'company_name']);
|
|
||||||
$row = $stmt->fetch();
|
|
||||||
if ($row) {
|
|
||||||
$registryName = $row['value'];
|
|
||||||
} else {
|
|
||||||
$registryName = 'Example Registry LLC';
|
|
||||||
}
|
|
||||||
|
|
||||||
$previous = date("Y-m", strtotime("first day of previous month"));
|
$previous = date("Y-m", strtotime("first day of previous month"));
|
||||||
|
|
||||||
|
@ -59,6 +42,7 @@ try {
|
||||||
|
|
||||||
foreach ($result as $row) {
|
foreach ($result as $row) {
|
||||||
$startDate = $previous . "-01";
|
$startDate = $previous . "-01";
|
||||||
|
$endDate = date("Y-m-d", strtotime("+1 month", strtotime($startDate)));
|
||||||
$combinedStmt = $pdo->prepare("
|
$combinedStmt = $pdo->prepare("
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(id) AS trans,
|
COUNT(id) AS trans,
|
||||||
|
@ -67,29 +51,41 @@ try {
|
||||||
statement
|
statement
|
||||||
WHERE
|
WHERE
|
||||||
registrar_id = :registrarId AND
|
registrar_id = :registrarId AND
|
||||||
date BETWEEN :startDate AND LAST_DAY(:startDate)
|
date >= :startDate AND date < :endDate
|
||||||
");
|
");
|
||||||
$combinedStmt->bindParam(':registrarId', $row['id'], PDO::PARAM_INT);
|
$combinedStmt->bindParam(':registrarId', $row['id'], PDO::PARAM_INT);
|
||||||
$combinedStmt->bindParam(':startDate', $startDate);
|
$combinedStmt->bindParam(':startDate', $startDate);
|
||||||
|
$combinedStmt->bindParam(':endDate', $endDate);
|
||||||
$combinedStmt->execute();
|
$combinedStmt->execute();
|
||||||
$combinedResult = $combinedStmt->fetch(PDO::FETCH_ASSOC);
|
$combinedResult = $combinedStmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$transactionsCount = $combinedResult['trans'] ?? 0;
|
$refundStmt = $pdo->prepare("
|
||||||
$totalAmount = $combinedResult['total'] ?? '0';
|
SELECT GROUP_CONCAT(description SEPARATOR '\n') AS refund_list,
|
||||||
|
SUM(amount) AS refund_total
|
||||||
|
FROM payment_history
|
||||||
|
WHERE registrar_id = :registrarId
|
||||||
|
AND date BETWEEN :startDate AND LAST_DAY(:startDate)
|
||||||
|
AND description LIKE '%provides a credit%'
|
||||||
|
");
|
||||||
|
$refundStmt->bindParam(':registrarId', $row['id'], PDO::PARAM_INT);
|
||||||
|
$refundStmt->bindParam(':startDate', $startDate);
|
||||||
|
$refundStmt->execute();
|
||||||
|
$refundRow = $refundStmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
$refundTotal = $refundRow['refund_total'] ?? 0;
|
||||||
|
$refundDetailsRaw = $refundRow['refund_list'] ?? '';
|
||||||
|
|
||||||
if ($transactionsCount > 0) {
|
$transactionsCount = $combinedResult['trans'] ?? 0;
|
||||||
|
$totalAmount = ($combinedResult['total'] ?? 0) - $refundTotal;
|
||||||
|
|
||||||
|
if ($transactionsCount > 0 && $totalAmount > 0) {
|
||||||
// Prepare and execute insert statement
|
// Prepare and execute insert statement
|
||||||
$insertStmt = $pdo->prepare("INSERT INTO invoices (registrar_id, billing_contact_id, issue_date, due_date, total_amount, payment_status, created_at) VALUES (:registrarId, :billingContactId, :issueDate, :dueDate, :totalAmount, :paymentStatus, :createdAt)");
|
$insertStmt = $pdo->prepare("INSERT INTO invoices (registrar_id, billing_contact_id, issue_date, due_date, total_amount, payment_status, created_at) VALUES (:registrarId, :billingContactId, :issueDate, :dueDate, :totalAmount, :paymentStatus, :createdAt)");
|
||||||
|
|
||||||
$currentDateTime = new DateTime(); // Current date and time
|
|
||||||
$currentDateTimeFormatted = $currentDateTime->format('Y-m-d H:i:s.u'); // Format with microseconds
|
|
||||||
|
|
||||||
$dueDateTime = (new DateTime())->modify('+30 days'); // Current date and time plus 30 days
|
$currentDateTime = new DateTimeImmutable();
|
||||||
$dueDateTimeFormatted = $dueDateTime->format('Y-m-d H:i:s.u'); // Format with microseconds
|
$dueDateTime = $currentDateTime->modify('+30 days');
|
||||||
|
|
||||||
// Truncate microseconds to milliseconds
|
$currentDateTimeMilliseconds = $currentDateTime->format('Y-m-d H:i:s.v');
|
||||||
$currentDateTimeMilliseconds = substr($currentDateTimeFormatted, 0, 23);
|
$dueDateTimeMilliseconds = $dueDateTime->format('Y-m-d H:i:s.v');
|
||||||
$dueDateTimeMilliseconds = substr($dueDateTimeFormatted, 0, 23);
|
|
||||||
|
|
||||||
$paymentStatus = 'unpaid';
|
$paymentStatus = 'unpaid';
|
||||||
|
|
||||||
|
@ -112,9 +108,16 @@ try {
|
||||||
$stmt->bindParam(':invoiceNumber', $invoiceNumber, PDO::PARAM_INT);
|
$stmt->bindParam(':invoiceNumber', $invoiceNumber, PDO::PARAM_INT);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
|
|
||||||
|
$log->info("Generated invoice {$invoiceIdFormatted} for registrar ID {$row['id']} ({$row['registrar_name']}) - Amount: {$totalAmount}");
|
||||||
|
|
||||||
$issueDate = date("Y-m-d");
|
$issueDate = date("Y-m-d");
|
||||||
$dueDate = date("Y-m-d", strtotime("+30 days"));
|
$dueDate = date("Y-m-d", strtotime("+30 days"));
|
||||||
|
|
||||||
|
if (empty($row['billing_email'])) {
|
||||||
|
$log->warning("Missing billing email for registrar ID {$row['id']}, skipping email.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare the email content
|
// Prepare the email content
|
||||||
$subject = "New Invoice Notification - " . $issueDate;
|
$subject = "New Invoice Notification - " . $issueDate;
|
||||||
$body = "Dear " . $row['registrar_name'] . ",\n\n" .
|
$body = "Dear " . $row['registrar_name'] . ",\n\n" .
|
||||||
|
@ -123,14 +126,21 @@ try {
|
||||||
"- Invoice Number: " . $invoiceIdFormatted . "\n" .
|
"- Invoice Number: " . $invoiceIdFormatted . "\n" .
|
||||||
"- Issue Date: " . $issueDate . "\n" .
|
"- Issue Date: " . $issueDate . "\n" .
|
||||||
"- Due Date: " . $dueDate . "\n" .
|
"- Due Date: " . $dueDate . "\n" .
|
||||||
"- Total Amount: " . $totalAmount . "\n\n" .
|
"- Total Amount: " . $totalAmount . "\n\n";
|
||||||
"The invoice is available in your account for review and payment. Please ensure that the payment is made by the due date to avoid any late fees or service interruptions.\n\n" .
|
|
||||||
"Should you have any questions or require further assistance, please do not hesitate to contact us at {$supportEmail}.\n\n" .
|
if ($refundTotal > 0) {
|
||||||
"Thank you for your prompt attention to this matter.\n\n" .
|
$body .= "- Credits This Period: -" . $refundTotal . "\n";
|
||||||
"Warm regards,\n\n" .
|
$body .= " (Details below)\n\n";
|
||||||
"{$registryName}\n" .
|
$body .= $refundDetailsRaw . "\n\n";
|
||||||
"{$supportEmail}\n" .
|
}
|
||||||
"{$supportPhoneNumber}";
|
|
||||||
|
$body .= "The invoice is available in your account for review and payment. Please ensure that the payment is made by the due date to avoid any late fees or service interruptions.\n\n" .
|
||||||
|
"Should you have any questions or require further assistance, please do not hesitate to contact us at {$supportEmail}.\n\n" .
|
||||||
|
"Thank you for your prompt attention to this matter.\n\n" .
|
||||||
|
"Warm regards,\n\n" .
|
||||||
|
"{$registryName}\n" .
|
||||||
|
"{$supportEmail}\n" .
|
||||||
|
"{$supportPhoneNumber}";
|
||||||
|
|
||||||
// Prepare the data array for the cURL request
|
// Prepare the data array for the cURL request
|
||||||
$data = [
|
$data = [
|
||||||
|
|
|
@ -166,8 +166,8 @@ class ApplicationsController extends Controller
|
||||||
|
|
||||||
// Validate that acceptedDate is before notAfter
|
// Validate that acceptedDate is before notAfter
|
||||||
try {
|
try {
|
||||||
$acceptedDate = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['accepted']);
|
$acceptedDate = DateTime::createFromFormat('Y-m-d\TH:i:s.v\Z', $data['accepted']);
|
||||||
$notAfterDate = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['notafter']);
|
$notAfterDate = DateTime::createFromFormat('Y-m-d\TH:i:s.v\Z', $data['notafter']);
|
||||||
|
|
||||||
if (!$acceptedDate || !$notAfterDate) {
|
if (!$acceptedDate || !$notAfterDate) {
|
||||||
$this->container->get('flash')->addMessage('error', "Invalid date format");
|
$this->container->get('flash')->addMessage('error', "Invalid date format");
|
||||||
|
|
|
@ -276,8 +276,8 @@ class DomainsController extends Controller
|
||||||
|
|
||||||
// Validate that acceptedDate is before notAfter
|
// Validate that acceptedDate is before notAfter
|
||||||
try {
|
try {
|
||||||
$acceptedDate = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['accepted']);
|
$acceptedDate = DateTime::createFromFormat('Y-m-d\TH:i:s.v\Z', $data['accepted']);
|
||||||
$notAfterDate = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $data['notafter']);
|
$notAfterDate = DateTime::createFromFormat('Y-m-d\TH:i:s.v\Z', $data['notafter']);
|
||||||
|
|
||||||
if (!$acceptedDate || !$notAfterDate) {
|
if (!$acceptedDate || !$notAfterDate) {
|
||||||
$this->container->get('flash')->addMessage('error', "Invalid date format");
|
$this->container->get('flash')->addMessage('error', "Invalid date format");
|
||||||
|
|
|
@ -17,12 +17,12 @@ class FinancialsController extends Controller
|
||||||
{
|
{
|
||||||
return view($response,'admin/financials/transactions.twig');
|
return view($response,'admin/financials/transactions.twig');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function overview(Request $request, Response $response)
|
public function overview(Request $request, Response $response)
|
||||||
{
|
{
|
||||||
return view($response,'admin/financials/overview.twig');
|
return view($response,'admin/financials/overview.twig');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function invoices(Request $request, Response $response)
|
public function invoices(Request $request, Response $response)
|
||||||
{
|
{
|
||||||
return view($response,'admin/financials/invoices.twig');
|
return view($response,'admin/financials/invoices.twig');
|
||||||
|
@ -73,6 +73,41 @@ class FinancialsController extends Controller
|
||||||
$statement = $db->select('SELECT * FROM statement WHERE date BETWEEN ? AND ? AND registrar_id = ?',
|
$statement = $db->select('SELECT * FROM statement WHERE date BETWEEN ? AND ? AND registrar_id = ?',
|
||||||
[ $firstDayPrevMonth, $lastDayPrevMonth, $invoice_details['registrar_id'] ]
|
[ $firstDayPrevMonth, $lastDayPrevMonth, $invoice_details['registrar_id'] ]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$refunds = $db->select("
|
||||||
|
SELECT
|
||||||
|
date,
|
||||||
|
description,
|
||||||
|
amount * -1 AS amount -- negate the refund to show as negative
|
||||||
|
FROM payment_history
|
||||||
|
WHERE registrar_id = ?
|
||||||
|
AND date BETWEEN ? AND ?
|
||||||
|
AND description LIKE '%provides a credit%'
|
||||||
|
", [
|
||||||
|
$invoice_details['registrar_id'],
|
||||||
|
$firstDayPrevMonth,
|
||||||
|
$lastDayPrevMonth
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($refunds as &$r) {
|
||||||
|
$r['domain_name'] = '(refund)';
|
||||||
|
$r['command'] = 'REFUND';
|
||||||
|
$r['type'] = 'credit';
|
||||||
|
|
||||||
|
if (preg_match('/domain ([a-z0-9.-]+\.[a-z]{2,})/i', $r['description'], $matchDomain)) {
|
||||||
|
$r['domain_name'] = $matchDomain[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/provides a credit (.*)$/i', $r['description'], $matchReason)) {
|
||||||
|
$r['reason'] = trim($matchReason[1]);
|
||||||
|
} else {
|
||||||
|
$r['reason'] = $r['description']; // fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($r);
|
||||||
|
|
||||||
|
$allTransactions = array_merge($statement, $refunds);
|
||||||
|
usort($allTransactions, fn($a, $b) => strtotime($a['date']) <=> strtotime($b['date']));
|
||||||
|
|
||||||
$vatCalculator = new VatCalculator();
|
$vatCalculator = new VatCalculator();
|
||||||
$vatCalculator->setBusinessCountryCode(strtoupper($cc));
|
$vatCalculator->setBusinessCountryCode(strtoupper($cc));
|
||||||
|
@ -107,7 +142,7 @@ class FinancialsController extends Controller
|
||||||
'billing' => $billing,
|
'billing' => $billing,
|
||||||
'billing_company' => $billing_company,
|
'billing_company' => $billing_company,
|
||||||
'billing_vat' => $billing_vat,
|
'billing_vat' => $billing_vat,
|
||||||
'statement' => $statement,
|
'statement' => $allTransactions,
|
||||||
'company_name' => $company_name,
|
'company_name' => $company_name,
|
||||||
'address' => $address,
|
'address' => $address,
|
||||||
'address2' => $address2,
|
'address2' => $address2,
|
||||||
|
|
|
@ -85,7 +85,12 @@
|
||||||
<tr>
|
<tr>
|
||||||
<td class="text-center">{{ loop.index }}</td>
|
<td class="text-center">{{ loop.index }}</td>
|
||||||
<td>
|
<td>
|
||||||
<p class="strong mb-1">{{ item.command }} {{ item.domain_name }}</p>
|
<p class="strong mb-1">
|
||||||
|
{{ item.command }} {{ item.domain_name }}
|
||||||
|
{% if item.type == 'credit' and item.reason is defined %}
|
||||||
|
<br><small class="text-muted">{{ item.reason }}</small>
|
||||||
|
{% endif %}
|
||||||
|
</p>
|
||||||
</td>
|
</td>
|
||||||
<td class="text-end">{{ item.amount }}</td>
|
<td class="text-end">{{ item.amount }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue