update marketing
All checks were successful
All checks were successful
This commit is contained in:
@@ -15,6 +15,7 @@ class Vat extends Model
|
||||
'status',
|
||||
'sub_vat',
|
||||
'business_id',
|
||||
'manage_state',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -27,5 +28,6 @@ class Vat extends Model
|
||||
'sub_vat' => 'json',
|
||||
'status' => 'boolean',
|
||||
'business_id' => 'integer',
|
||||
'manage_state' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
27
app/Models/VatStateItem.php
Normal file
27
app/Models/VatStateItem.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class VatStateItem extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'parent_vat_id',
|
||||
'child_vat_id',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function parentVat()
|
||||
{
|
||||
return $this->belongsTo(Vat::class, 'parent_vat_id');
|
||||
}
|
||||
|
||||
public function childVat()
|
||||
{
|
||||
return $this->belongsTo(Vat::class, 'child_vat_id');
|
||||
}
|
||||
}
|
||||
35
app/Models/VatTransaction.php
Normal file
35
app/Models/VatTransaction.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class VatTransaction extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'business_id',
|
||||
'vat_id',
|
||||
'vat_rate',
|
||||
'vat_amount',
|
||||
'vatable_id',
|
||||
'vatable_type',
|
||||
];
|
||||
|
||||
public function vatable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function vat()
|
||||
{
|
||||
return $this->belongsTo(Vat::class);
|
||||
}
|
||||
|
||||
public function business()
|
||||
{
|
||||
return $this->belongsTo(Business::class);
|
||||
}
|
||||
}
|
||||
70
app/Services/PurchaseVatTransaction.php
Normal file
70
app/Services/PurchaseVatTransaction.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
73
app/Services/VatTransactionService.php
Normal file
73
app/Services/VatTransactionService.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?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.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user