PHP code example of tawwfik / zakat-calculator

1. Go to this page and download the library: Download tawwfik/zakat-calculator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tawwfik / zakat-calculator example snippets


use Tawfik\ZakatCalculator\ZakatCalculator;

class ZakatController extends Controller
{
    protected $calculator;

    public function __construct(ZakatCalculator $calculator)
    {
        $this->calculator = $calculator;
    }

    public function calculate(Request $request)
    {
        $result = $this->calculator
            ->setCash($request->cash)
            ->setGoldItems([
                ['weight' => 100, 'karat' => 24],
                ['weight' => 50, 'karat' => 18]
            ])
            ->setSilverWeight(500)
            ->setBusinessAssets([
                'inventory' => 5000,
                'receivables' => 2000,
                'cash_at_bank' => 3000
            ])
            ->setAgriculturalProducts([
                ['value' => 10000, 'irrigated' => true],
                ['value' => 20000, 'irrigated' => false]
            ])
            ->calculate();

        return response()->json($result);
    }
}

[
    'total_assets' => [
        'cash' => float,
        'gold' => float,
        'silver' => float,
        'business' => float,
        'agricultural' => float
    ],
    'nisab_value' => float,
    'is_eligible' => boolean,
    'total_zakat' => float,
    'details' => [
        'cash' => [
            'amount' => float,
            'zakat' => float
        ],
        'gold' => [
            'items' => array,
            'total_weight' => float,
            'total_value' => float,
            'zakat' => float
        ],
        'silver' => [
            'weight' => float,
            'value' => float,
            'zakat' => float
        ],
        'business' => [
            'assets' => array,
            'total_value' => float,
            'zakat' => float
        ],
        'agricultural' => [
            'products' => array,
            'total_value' => float,
            'zakat' => float
        ]
    ],
    'calculation_method' => string
]

'default_prices' => [
    'gold'   => 0.00, // Set your default gold price per gram
    'silver' => 0.00, // Set your default silver price per gram
],

'nisab' => [
    'gold'   => 85,  // grams of gold
    'silver' => 595, // grams of silver
],

'currency' => [
    'code'     => 'SAR',    // Currency code
    'symbol'   => 'ر.س',      // Currency symbol
    'position' => 'before', // Symbol position: 'before' or 'after'
],

'weight_unit' => 'gram', // 'gram' or 'ounce'

'precision' => 2, // Number of decimal places

'supported_karats' => [24, 22, 21, 18, 14, 12, 10],

'cache' => [
    'enabled' => true,
    'ttl'     => 3600, // Cache time-to-live in seconds
],

'calculation_methods' => [
    'default' => 'hanafi', // Available: 'hanafi', 'shafi', 'maliki', 'hanbali'
],

'business' => [
    'inventory'    => true,    // Include inventory in calculation
    'receivables'  => true,    // Include receivables in calculation
    'cash_at_bank' => true,    // Include bank cash in calculation
    'cash_in_hand' => true,    // Include cash in hand in calculation
],

'agricultural' => [
    'irrigated_rate'     => 0.05, // 5% rate for irrigated crops
    'non_irrigated_rate' => 0.1,  // 10% rate for non-irrigated crops
],

$result = $calculator
    ->setGoldItems([
        ['weight' => 100, 'karat' => 24],
        ['weight' => 50, 'karat' => 18]
    ])
    ->setGoldPrice(100) // Price per gram
    ->calculateGoldZakat();

$result = $calculator
    ->setBusinessAssets([
        'inventory' => 5000,
        'receivables' => 2000,
        'cash_at_bank' => 3000,
        'cash_in_hand' => 1000
    ])
    ->calculateBusinessZakat();

$result = $calculator
    ->setAgriculturalProducts([
        ['value' => 10000, 'irrigated' => true],  // Irrigated crops (5%)
        ['value' => 20000, 'irrigated' => false]  // Non-irrigated crops (10%)
    ])
    ->calculateAgriculturalZakat();

$result = $calculator
    ->setCalculationMethod('shafi')
    ->setCash(10000)
    ->setGoldItems([['weight' => 100, 'karat' => 24]])
    ->calculate();

use Tawfik\ZakatCalculator\Exceptions\InvalidInputException;

try {
    $result = $calculator->setCash(-1000)->calculate();
} catch (InvalidInputException $e) {
    return response()->json(['error' => $e->getMessage()], 400);
}
bash
php artisan vendor:publish --provider="Tawfik\ZakatCalculator\ZakatServiceProvider" --tag="config"