74 lines
2.3 KiB
PHP
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.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|