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), ]); } } }