PHP code example of supla / energy-cost-calculator

1. Go to this page and download the library: Download supla/energy-cost-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/ */

    

supla / energy-cost-calculator example snippets


use Supla\EnergyCostCalculator\Engine\CostCalculator;
use Supla\EnergyCostCalculator\Engine\CalculationOptions;
use Supla\EnergyCostCalculator\Model\TimeRange;

$calculator = new CostCalculator(
    $energyDeltaSource,
    $referenceDataSource,
);

$result = $calculator->calculate(
    meterId: (string)$channelId,
    range: new TimeRange($from, $to),
    definition: $jsonDefinition,
    options: new CalculationOptions(

use Supla\EnergyCostCalculator\Preset\TariffPresetCatalog;

$catalog = new TariffPresetCatalog();
$summaries = $catalog->presets();
$preset = $catalog->get('PL.TAURON_DYSTRYBUCJA.G12.2026');

$preset->document; // Complete preset document.
$preset->revision; // SHA-256 of the deterministically encoded JSON document.

use Supla\EnergyCostCalculator\Plan\CostPlanCompiler;

$plan = [
    'version' => 2,
    'currency' => 'PLN',
    'timezone' => 'Europe/Warsaw',
    'priceBasis' => 'NET',
    'billingCycles' => [[
        'validFrom' => '2026-01-01T00:00:00+01:00',
        'validTo' => '2027-01-01T00:00:00+01:00',
        'anchor' => '2026-01-15',
        'length' => 1,
        'unit' => 'MONTH',
    ]],
    'periods' => [[
        'validFrom' => '2026-01-01T00:00:00+01:00',
        'validTo' => '2027-01-01T00:00:00+01:00',
        'components' => [
            ['kind' => 'ENERGY_PURCHASE', 'presetId' => 'PL.TAURON_SPRZEDAZ.G12.2026', 'componentId' => 'energy-purchase', 'values' => ['energy.DAY' => '0.98', 'energy.NIGHT' => '0.62']],
            ['kind' => 'DISTRIBUTION_VARIABLE', 'presetId' => 'PL.TAURON_DYSTRYBUCJA.G12.2026', 'componentId' => 'distribution-variable', 'values' => []],
        ],
    ]],
];

$definition = (new CostPlanCompiler())->compile($plan);
json
{
  "billingCycles": [
    {
      "validFrom": null,
      "validTo": "2026-07-01T00:00:00+02:00",
      "anchor": "2026-01-15",
      "length": 1,
      "unit": "MONTH"
    },
    {
      "validFrom": "2026-07-01T00:00:00+02:00",
      "validTo": null,
      "anchor": "2026-07-01",
      "length": 1,
      "unit": "MONTH"
    }
  ]
}
text
PL_PUBLIC_HOLIDAYS