PHP code example of superfaktura / apiclient

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

    

superfaktura / apiclient example snippets




= new \SuperFaktura\ApiClient\ApiClient(
    new \SuperFaktura\ApiClient\Authorization\SimpleProvider(
        '[email protected]',
        'YOUR_APIKEY',
        'Example s.r.o.',
        1,
    ),
    \SuperFaktura\ApiClient\MarketUri::SLOVAK,
);

$response = $api->invoices->create(
    invoice: [
        'name' => 'Test API',
        'variable' => '12345',
    ],
    items: [
        [
            'name' => 'item name',
            'description' => 'item description',
            'tax' => 20,
            'unit_price' => 10,
        ],
    ],
    client: [
        'name' => 'Client name',
        'ico' => '44981082',
        'comment' => 'Client comment',
        'update_addressbook' => 1,
    ],
);
var_dump($response->data);

try {
    $response = $api->invoices->create(
        invoice: [
            'name' => 'Test API',
            'variable' => '12345',
        ],
        items: [
            [
                'name' => 'item name',
                'description' => 'item description',
                'tax' => 20,
                'unit_price' => 10,
            ],
        ],
        client: [
            'name' => 'Client name',
            'ico' => '44981082',
            'comment' => 'Client comment',
            'update_addressbook' => 1,
        ],
    );
    
    var_dump($response->data);
} catch (\SuperFaktura\ApiClient\Contract\Invoice\CannotCreateInvoiceException $exception) {
    echo 'Cannot create invoice: ' . $exception->getMessage() . PHP_EOL;
} catch (\SuperFaktura\ApiClient\Request\CannotCreateRequestException $exception) {
    echo 'Cannot create request: ' . $exception->getMessage() . PHP_EOL;
}

$response = $api->bank_accounts->getAll();
var_dump($response->data);

$response = $api->bank_accounts->create([
    'bank_name' => 'NovaBanka',
    'iban' => 'SK000011112222333344',
    'swift' => 'suzuki',
    'default' => 1,
    'show' => 1,
    'show_account' => 1,
]);
var_dump($response->data);

$response = $api->bank_accounts->update(
    id: 1,
    bank_account: [
        'bank_name' => 'StaroNovaBanka',
        'swift' => '777777',
    ],
);
var_dump($response->data);

$api->bank_accounts->delete(1);

$response = $api->cash_registers->getAll();
var_dump($response->data);

$response = $api->cash_registers->getById(1);
var_dump($response->data);

$response = $api->cash_registers->items->create(
    cash_register_id: 1,
    data: [
        'amount' => 9.99,
        'currency' => \SuperFaktura\ApiClient\UseCase\Money\Currency::EURO,
    ],
);
var_dump($response->data);

$response = $api->clients->getAll(new \SuperFaktura\ApiClient\UseCase\Client\ClientsQuery(
    full_text: 'Test',
    created: new \SuperFaktura\ApiClient\Filter\TimePeriod(
        period: \SuperFaktura\ApiClient\Filter\TimePeriodEnum::THIS_YEAR
    ),
));
var_dump($response->data);

$response = $api->clients->getById(4);
var_dump($response->data);

$response = $api->clients->create([
    'name' => 'SuperFaktúra s.r.o',
]);
var_dump($response->data);

$response = $api->clients->update(
    id: 1,
    data: [
        'name' => 'SuperFaktúra s.r.o',
    ]
);
var_dump($response->data);

$api->clients->delete(1);

$response = $api->clients->contacts->getAllByClientId(1);
var_dump($response->data);

$response = $api->clients->contacts->create(
    client_id: 1,
    contact: [
        'name' => 'Joe Doe',
        'email' => '[email protected]',
    ],
);
var_dump($response->data);

$api->clients->contacts->delete(1);

$response = $api->countries->getAll();
var_dump($response->data);

$response = $api->expenses->getAll(
    new \SuperFaktura\ApiClient\UseCase\Expense\ExpensesQuery(
        full_text: 'SuperFaktura',
        type: \SuperFaktura\ApiClient\Contract\Expense\ExpenseType::INVOICE,
        created: new \SuperFaktura\ApiClient\Filter\TimePeriod(
            period: \SuperFaktura\ApiClient\Filter\TimePeriodEnum::THIS_YEAR
        ),
    )
);
var_dump($response->data);

$response = $api->expenses->getById(1);
var_dump($response->data);

$response = $api->expenses->getAllCategories();
var_dump($response->data);

$response = $api->expenses->create(
    expense: [
        'name' => 'Foo bar',
        'currency' => \SuperFaktura\ApiClient\UseCase\Money\Currency::EURO,
    ],
    items: [
        [
            'description' => 'description of item 1',
            'name' => 'item 1',
            'tax' => 20,
            'unit_price' => 10,
        ]
    ],
    client: [
        'ico' => '46655034',
    ],
    extra: ['vat_transfer' => 1],
    tags: [1, 2],
);
var_dump($response->data);

$response = $api->expenses->update(
    id: 1,
    expense: [
        'name' => 'Foo',
    ],
);
var_dump($response->data);

$api->expenses->delete(1);

$response = $api->expenses->payments->create(
    id: 95,
    payment: new \SuperFaktura\ApiClient\UseCase\Expense\Payment\Payment(
        amount: 10,
        currency: \SuperFaktura\ApiClient\UseCase\Money\Currency::EURO,
        payment_type: \SuperFaktura\ApiClient\Contract\PaymentType::CASH,
    ),
);
var_dump($response->data);

$api->expenses->payments->delete(1);

$response = $api->invoices->getById(1);
var_dump($response->data);

$response = $api->invoices->getByIds([1,2,3]);
var_dump($response->data);

$query = new \SuperFaktura\ApiClient\UseCase\Invoice\InvoicesQuery(
    sort: new \SuperFaktura\ApiClient\Filter\Sort('created', \SuperFaktura\ApiClient\Filter\SortDirection::DESC),
    client_id: 172,
    created: new \SuperFaktura\ApiClient\Filter\TimePeriod(
        \SuperFaktura\ApiClient\Filter\TimePeriodEnum::FROM_TO,
            new DateTimeImmutable('2023-11-01'),
            new DateTimeImmutable('2023-11-08'),
    ),
);
$response = $api->invoices->getAll($query);
var_dump($response->data);

$response = $api->invoices->downloadPdf(372, \SuperFaktura\ApiClient\Contract\Language::SLOVAK);
file_put_contents(__DIR__ . '/invoice.pdf', $response->data);

$response = $api->invoices->create(
    invoice: [
        'name' => 'Test API',
        'variable' => '12345',
    ],
    items: [
        [
            'name' => 'item name',
            'description' => 'item description',
            'tax' => 20,
            'unit_price' => 10,
        ],
    ],
    client: [
        'name' => 'Client name',
        'ico' => '44981082',
        'comment' => 'Client comment',
        'update_addressbook' => 1,
    ],
    settings: [
        'language' => \SuperFaktura\ApiClient\Contract\Language::SLOVAK,
        'signature' => true,
    ],
    extra: [
        'pickup_point_id' => 23,
    ],
    my_data: [
        'address' => 'Fiktivna 1',
        'business_register' => "-",
        'city' => 'Prague',
        'company_name' => 'MyData Inc.',
        'country_id' => 191,
        'dic' => 'SK99999999',
        'ic_dph' => 'ABCDE',
        'zip' => '999 88',
    ],
    tags: [4],
);
var_dump($response->data);

$response = $api->invoices->update(
    id: 1,
    invoice: [
        'name' => 'Test API updated',
        'variable' => '12345',
    ],
    items: [
        [
            'name' => 'item name',
            'description' => 'item description',
            'tax' => 20,
            'unit_price' => 10,
        ],
    ],
    client: [
        'name' => 'Client name',
        'ico' => '44981082',
        'comment' => 'Client comment',
        'update_addressbook' => 1,
    ],
    settings: [
        'language' => \SuperFaktura\ApiClient\Contract\Language::SLOVAK,
        'signature' => true,
    ],
    extra: [
        'pickup_point_id' => 23,
    ],
    my_data: [
        'address' => 'Fiktivna 1',
        'business_register' => "-",
        'city' => 'Prague',
        'company_name' => 'MyData Inc.',
        'country_id' => 191,
        'dic' => 'SK99999999',
        'ic_dph' => 'ABCDE',
        'zip' => '999 88',
    ],
    tags: [4],
);
var_dump($response->data);

$api->invoices->delete(1);

$api->invoices->changeLanguage(1, \SuperFaktura\ApiClient\Contract\Language::CZECH);

$api->invoices->markAsSent(1);

$api->invoices->markAsSentViaEmail(1, '[email protected]', 'Subject', 'Message');

$email = new \SuperFaktura\ApiClient\UseCase\Invoice\Email(
    email: '[email protected]',
    pdf_language: \SuperFaktura\ApiClient\Contract\Language::CZECH,
    bcc: ['[email protected]'],
    cc: ['[email protected]'],
    subject: 'Subject of email',
    message: 'Message',
);
$api->invoices->sendViaEmail(1, $email);

$address = new \SuperFaktura\ApiClient\UseCase\Invoice\Address(
    name: 'John Doe',
    address: 'Vymyslena 1',
    city: 'Bratislava',
    country_id: 191,
    state: 'Slovakia',
    zip: '99999',
);
$api->invoices->sendViaPostOffice(372, $address);

$api->invoices->items->delete(1, [1,2,3]);

$api->invoices->payments->markAsUnPayable(1);

$payment = new \SuperFaktura\ApiClient\UseCase\Invoice\Payment\Payment(
    amount: 6.00,
    currency: \SuperFaktura\ApiClient\UseCase\Money\Currency::EURO,
    payment_type: \SuperFaktura\ApiClient\Contract\PaymentType::CARD,
    document_number: '123',
    payment_date: new DateTimeImmutable('now'),
);
$api->invoices->payments->create(1, $payment);

$api->invoices->payments->delete(123);

$relation = new \SuperFaktura\ApiClient\UseCase\RelatedDocument\Relation(
    parent_id: 1,
    parent_type: \SuperFaktura\ApiClient\Contract\RelatedDocument\DocumentType::INVOICE,
    child_id: 1,
    child_type: \SuperFaktura\ApiClient\Contract\RelatedDocument\DocumentType::EXPENSE,
);
$response = $api->related_documents->link($relation);
var_dump($response->data);

$api->related_documents->unlink(1);

$response = $api->exports->exportInvoices(
    [1,2,3],
    \SuperFaktura\ApiClient\Contract\Export\Format::PDF,
    new \SuperFaktura\ApiClient\UseCase\Export\PdfExportOptions(
        language: \SuperFaktura\ApiClient\Contract\Language::SLOVAK,
        hide_signature: true,
    ),
);
var_dump($response->data);

$response = $api->exports->getStatus(1);
var_dump($response->data);

$response = $api->exports->download(1);
file_put_contents(__DIR__ . '/export.zip', $response->data);

$response = $api->stock_items->create([
    'name' => 'Cake - red velvet',
    'sku'  => 'CK-RV',
    'unit_price' => 30.99,
    'purchase_unit_price' => 39.99,
]);
var_dump($response->data);

$response = $api->stock_items->getById(1);
var_dump($response->data);

$query = new \SuperFaktura\ApiClient\Contract\Stock\ItemsQuery(
    price_from: 30.99,
    price_to: 39.99,
);
$response = $api->stock_items->getAll($query);
var_dump($response->data);

$response = $api->stock_items->update(1, [
    'name' => 'Updated name',
]);
var_dump($response->data);

$api->stock_items->delete(1);

$response = $api->stock_items->movements->create(1, [
    ['quantity' => 5, 'note' => 'Restocking the supplies'],
    ['quantity' => -5, 'note' => 'I must eat something for dinner'],
]);
var_dump($response->data);

$response = $api->stock_items->movements->createWithSku('CK-RV', [
    ['quantity' => 5, 'note' => 'Restocking the supplies'],
    ['quantity' => -5, 'note' => 'I must eat something for dinner'],
]);
var_dump($response->data);

$response = $api->tags->getAll();
var_dump($response->data);

$response = $api->tags->create('my-tag');
var_dump($response->data);

$response = $api->tags->update(1, 'my-updated-tag');
var_dump($response->data);

$api->tags->delete(1);

$api = new \SuperFaktura\ApiClient\ApiClient(
    new \SuperFaktura\ApiClient\Authorization\EnvFileProvider(__DIR__ . '/.env'),
    'https://moja.superfaktura.sk',
);