Yazılım Geliştirme

Laravel için Kolay ve Esnek KDV Hesaplayıcı: VatCalculator

📅 Feb 02, 2026
Laravel projelerinde KDV hesaplamak bazen kafa karıştırıcı olabilir: netten brüt fiyat bulmak, brütten net çıkarmak, KDV tutarını hesaplamak, farklı oranları uygulamak… VatCalculator sınıfı bu işleri kolaylaştırıyor.

Bu sınıf ile:

  • Netten brüt fiyat bulabilir,
  • Brüt fiyatın içindeki KDV’yi hesaplayabilir,
  • Farklı KDV oranlarını ardışık olarak uygulayabilir,
  • Kolayca formatlanmış fiyatlar elde edebilirsiniz.

Kısaca, hem temiz kod hem de hassas KDV hesaplama sağlar. Laravel projelerinde helper, facade veya servis sağlayıcı aracılığıyla global kullanılabilir.

                                     <?php


namespace App\Support\Helpers;


/**
* KdvCalculator
*
* Genel amaçlı KDV hesaplayıcı sınıfı.
* - Net -> KDV'li (brüt)
* - Brüt -> Net
* - Net'ten KDV tutarı
* - Brüt'ten KDV tutarı (içinden ayrıştırma)
* - Farklı formatlama/yuvarlama seçenekleri
*
* Kullanım: Laravel servis sağlayıcısı ile global'e bağlanabilir veya helper/facade ile kullanılabilir.
*/
class VatCalculator
{
protected float $rate;

public const DEFAULT_VAT= 20.0;


public function __construct(float $rate = self::DEFAULT_VAT)
{
$this->rate = $rate;
}


/** Set KDV oranı (yüzde) */
public function setRate(float $rate): self
{
$this->rate = $rate;
return $this;
}


public function getRate(): float
{
return $this->rate;
}


/** KDV faktörü: 1 + rate/100 */
public function factor(): float
{
return 1 + ($this->rate / 100.0);
}


/** Net -> Brüt (KDV dahil) */
public function netToGross(float $net, int $precision = 2): float
{
return $this->round($net * $this->factor(), $precision);
}


/** Net'ten KDV tutarını hesapla */
public function netToVatAmount(float $net, int $precision = 2): float
{
$vat = $net * ($this->rate / 100.0);
return $this->round($vat, $precision);
}


/** Brüt -> Net (KDV hariç) */
public function grossToNet(float $gross, int $precision = 2): float
{
return $this->round($gross / $this->factor(), $precision);
}


/** Brüt fiyatın içindeki KDV tutarını hesapla */
public function grossToVatAmount(float $gross, int $precision = 2): float
{
$net = $this->grossToNet($gross, 10); // ara hassasiyet
$vat = $gross - $net;
return $this->round($vat, $precision);
}
/** Brüt fiyatın içindeki KDV tutarını hesapla */
public function splitGross(float $gross, int $precision = 2): array
{
$net = $this->grossToNet($gross, $precision);
$vat = $this->round($gross - $net, $precision);


return [
'net' => $net,
'vat' => $vat,
'gross' => $this->round($gross, $precision),
'rate' => $this->rate,
];
}


/** Net'i rate'ler listesi ile sıralı uygulayıp brüt bulur (ör: farklı vergiler) */
public function applyMultipleRates(float $net, array $rates, int $precision = 2): array
{
$current = $net;
$details = [];
foreach ($rates as $r) {
$vat = $this->round($current * ($r / 100.0), $precision);
$current = $this->round($current + $vat, $precision);
$details[] = ['rate' => $r, 'vat' => $vat, 'subtotal' => $current];
}
return [
'net' => $net,
'gross' => $current,
'details' => $details,
];
}


/** genel split: ister net ver ister brüt */
public function split(float $amount, bool $amountIsGross = false, int $precision = 2): array
{
if ($amountIsGross) {
return $this->splitGross($amount, $precision);
}


$vat = $this->netToVatAmount($amount, $precision);
$gross = $this->netToGross($amount, $precision);
return [
'net' => $this->round($amount, $precision),
'vat' => $vat,
'gross' => $gross,
'rate' => $this->rate,
];
}


/** Para biçimlendirme (basit) */
public function format(float $amount, int $decimals = 2, string $dec_point = '.', string $thousands_sep = ','): string
{
return number_format($amount, $decimals, $dec_point, $thousands_sep);
}


/** Esnek yuvarlama helper'i. Şu an PHP round kullanılıyor; gerekiyorsa banker/tip yuvarlama ekleyebilirsiniz. */
protected function round(float $value, int $precision = 2): float
{
return (float) round($value, $precision);
}


/** Statik yardımcı: tek satırda hesaplama isterseniz */
public static function calculateFromNet(float $net, float $rate = self::DEFAULT_VAT, int $precision = 2): array
{
$calc = new self($rate);
return $calc->split($net, false, $precision);
}


public static function calculateFromGross(float $gross, float $rate = self::DEFAULT_VAT, int $precision = 2): array
{
$calc = new self($rate);
return $calc->split($gross, true, $precision);
}
}
                                     <?php

$data = VatCalculator::calculateFromGross(200)

// [
// "net" => 166.67,
// "vat" => 33.33,
// "gross" => 200.0,
// "rate" => 20.0,
// ];
📌 Yazı Bilgileri
  • Hedef: Net ve brüt fiyatlardan KDV hesaplamalarını hızlı ve güvenilir şekilde yönetmek
  • Amaç: Laravel projelerinde tekrar eden KDV hesaplama işlerini tek sınıfta toplamak ve kodu temiz, okunabilir ve yeniden kullanılabilir hâle getirmek
🏷 Etiketler
Laravel KDV Finans Muhasebe Helper Ödeme