Files
kulakpos_web/app/Services/PurchaseVatTransaction.php

71 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2026-05-14 11:55:22 +07:00
<?php
namespace App\Services;
use App\Models\Vat;
use App\Models\VatTransaction;
class PurchaseVatTransaction
{
/**
* Store VAT transactions for a purchase detail.
*/
public static function storeVatTransactions($business, $detail, $product, $party = null)
{
$vatAmountTotal = 0;
if ($product->vat_id) {
$vat = Vat::find($product->vat_id);
if ($vat) {
$vatRate = $vat->rate;
$qty = $detail->quantities;
$purchasePrice = $detail->productPurchasePrice;
// For purchases, it's often inclusive in the purchase price provided by supplier
// or on top. We'll follow the standard logic.
if ($product->vat_type === 'inclusive') {
$vatAmount = ($purchasePrice * $vatRate) / (100 + $vatRate);
} else {
$vatAmount = ($purchasePrice * $vatRate) / 100;
}
$vatAmountTotal = $vatAmount * $qty;
VatTransaction::create([
'business_id' => $business->id,
'vat_id' => $vat->id,
'vat_rate' => $vatRate,
'vat_amount' => $vatAmountTotal,
'vatable_id' => $detail->id,
'vatable_type' => get_class($detail),
]);
}
}
return $vatAmountTotal;
}
/**
* Adjust VAT on purchase return.
*/
public static function productReturnVatCalculation($detail, $requested_qty, $returnDetail, $totalQty)
{
$vatTransaction = VatTransaction::where('vatable_id', $detail->id)
->where('vatable_type', get_class($detail))
->first();
if ($vatTransaction) {
$returnVatAmount = ($vatTransaction->vat_amount / $totalQty) * $requested_qty;
VatTransaction::create([
'business_id' => $vatTransaction->business_id,
'vat_id' => $vatTransaction->vat_id,
'vat_rate' => $vatTransaction->vat_rate,
'vat_amount' => -$returnVatAmount,
'vatable_id' => $returnDetail->id,
'vatable_type' => get_class($returnDetail),
]);
}
}
}