Added new pricing calculation method; testing needed

This commit is contained in:
Pinga 2023-12-15 07:06:36 +02:00
parent a07c407543
commit a8a4c44590
10 changed files with 250 additions and 92 deletions

View file

@ -192,12 +192,9 @@ class DomainsController extends Controller
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "create" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $date_add, 'create');
$price = $returnValue['price'];
if (!$price) {
return view($response, 'admin/domains/createDomain.twig', [
@ -1472,12 +1469,9 @@ class DomainsController extends Controller
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $date_add, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -1773,12 +1767,9 @@ class DomainsController extends Controller
]
);
if ($addPeriod_id) {
$priceColumn = "m" . $addPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "create" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $addPeriod, 'create');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
return $response->withHeader('Location', '/domains')->withStatus(302);
@ -1901,12 +1892,9 @@ class DomainsController extends Controller
]
);
if ($autoRenewPeriod_id) {
$priceColumn = "m" . $autoRenewPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $autoRenewPeriod, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
return $response->withHeader('Location', '/domains')->withStatus(302);
@ -1931,11 +1919,8 @@ class DomainsController extends Controller
]
);
if ($renewPeriod_id) {
$priceColumn = "m" . $renewPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $renewPeriod, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -1961,12 +1946,9 @@ class DomainsController extends Controller
]
);
if ($transferPeriod_id) {
$priceColumn = "m" . $transferPeriod;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "renew" LIMIT 1',
[$tld_id]
);
$returnValue = getDomainPrice($db, $domainName, $tld_id, $transferPeriod, 'renew');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
return $response->withHeader('Location', '/domains')->withStatus(302);
@ -2149,12 +2131,9 @@ class DomainsController extends Controller
$result = $db->selectRow('SELECT accountBalance, creditLimit FROM registrar WHERE id = ?', [$clid]);
$registrar_balance = $result['accountBalance'];
$creditLimit = $result['creditLimit'];
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "transfer" LIMIT 1',
[$tldid]
);
$returnValue = getDomainPrice($db, $domainName, $tldid, $date_add, 'transfer');
$price = $returnValue['price'];
if (!$price) {
$this->container->get('flash')->addMessage('error', 'The price, period and currency for such TLD are not declared');
@ -2381,12 +2360,9 @@ class DomainsController extends Controller
]
);
$priceColumn = "m" . $date_add;
$price = $db->selectValue(
'SELECT ' . $db->quoteIdentifier($priceColumn) . ' FROM domain_price WHERE tldid = ? AND command = "transfer" LIMIT 1',
[$tldid]
);
$returnValue = getDomainPrice($db, $domainName, $tldid, $date_add, 'transfer');
$price = $returnValue['price'];
if (($registrar_balance + $creditLimit) < $price) {
$this->container->get('flash')->addMessage('error', 'The registrar who took over this domain has no money to pay the renewal period that resulted from the transfer request');
return $response->withHeader('Location', '/transfers')->withStatus(302);

View file

@ -307,4 +307,64 @@ function extractDomainAndTLD($urlString) {
$tld = $result->suffix()->toString();
return ['domain' => $sld, 'tld' => $tld];
}
function getDomainPrice($db, $domain_name, $tld_id, $date_add = 12, $command = 'create') {
// Check if the domain is a premium domain
$premiumDomain = $db->selectRow(
'SELECT c.category_price
FROM premium_domain_pricing p
JOIN premium_domain_categories c ON p.category_id = c.category_id
WHERE p.domain_name = ? AND p.tld_id = ?',
[$domain_name, $tld_id]
);
if ($premiumDomain) {
return ['type' => 'premium', 'price' => $premiumDomain['category_price']];
}
// Check if there is a promotion for the domain
$currentDate = date('Y-m-d');
$promo = $db->selectRow(
"SELECT discount_percentage, discount_amount
FROM promotion_pricing
WHERE tld_id = ?
AND promo_type = 'full'
AND status = 'active'
AND start_date <= ?
AND end_date >= ?",
[$tld_id, $currentDate, $currentDate]
);
$discount = null;
if ($promo) {
if (!empty($promo['discount_percentage'])) {
$discount = $promo['discount_percentage']; // Percentage discount
} elseif (!empty($promo['discount_amount'])) {
$discount = $promo['discount_amount']; // Fixed amount discount
}
}
// Get regular price for the specified period
$priceColumn = "m" . $date_add;
$regularPrice = $db->selectValue(
"SELECT $priceColumn FROM domain_price WHERE tldid = ? AND command = ? LIMIT 1",
[$tld_id, $command]
);
if ($regularPrice !== false) {
if ($discount !== null) {
if (isset($promo['discount_percentage'])) {
$discountAmount = $regularPrice * ($promo['discount_percentage'] / 100);
} else {
$discountAmount = $discount;
}
$price = $regularPrice - $discountAmount;
return ['type' => 'promotion', 'price' => $price];
}
return ['type' => 'regular', 'price' => $regularPrice];
}
return ['type' => 'not_found', 'price' => 0];
}