Files
kulakpos_web/app/Services/VatTransactionService.php
eko54r 05fd3230b8
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 5m14s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 17s
Build, Push and Deploy / deploy-staging (push) Successful in 41s
Build, Push and Deploy / deploy-production (push) Has been skipped
update marketing
2026-05-14 11:55:22 +07:00

74 lines
2.3 KiB
PHP

<?php
namespace App\Services;
use App\Models\Vat;
use App\Models\VatTransaction;
class VatTransactionService
{
/**
* Store VAT transactions for a sale detail.
*/
public static function storeVatTransactions($business, $detail, $product, $actualPrice, $party = null)
{
$vatAmountTotal = 0;
if ($product->vat_id) {
$vat = Vat::find($product->vat_id);
if ($vat) {
// Calculation logic (usually tax is on top or inclusive)
// Based on controller logic, we need to store it
$vatRate = $vat->rate;
$qty = $detail->quantities;
// If inclusive
if ($product->vat_type === 'inclusive') {
$vatAmount = ($actualPrice * $vatRate) / (100 + $vatRate);
} else {
$vatAmount = ($actualPrice * $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 product 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),
]);
// Adjust the original transaction amount if needed,
// but usually we just add a new record for the return.
}
}
}