PHP code example of chuckbe / clearfacts-laravel-sdk
1. Go to this page and download the library: Download chuckbe/clearfacts-laravel-sdk 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/ */
chuckbe / clearfacts-laravel-sdk example snippets
use Clearfacts\Facades\Clearfacts;
// Set the token (typically in middleware or a tenant-aware service)
Clearfacts::setAccessToken($tenant->clearfacts_access_token);
Clearfacts::api()->resource->method($args);
use Clearfacts\Facades\Clearfacts;
// List all administrations
$administrations = Clearfacts::api()->administration->list();
// Get a single administration by company number
$admin = Clearfacts::api()->administration->get('0123456789');
echo $admin->name;
echo $admin->companyNumber;
echo $admin->companyType;
echo $admin->accountManager;
echo $admin->address->locality;
echo $admin->language;
use Clearfacts\Facades\Clearfacts;
use Clearfacts\Enums\InvoiceType;
use Clearfacts\Enums\ArchiveType;
// Upload a purchase invoice to an administration's inbox
$file = Clearfacts::api()->document->upload(
vatNumber: 'BE0123456789',
fileName: 'invoice_001.pdf',
invoiceType: InvoiceType::PURCHASE,
fileContent: file_get_contents('/path/to/invoice.pdf'),
);
echo $file->uuid;
echo $file->name;
echo $file->amountOfPages;
// Upload to the archive (various or permanent)
$file = Clearfacts::api()->document->uploadArchive(
vatNumber: 'BE0123456789',
fileName: 'contract.pdf',
type: ArchiveType::PERMANENT,
category: 'category-id-from-archiveCategory-list',
fileContent: file_get_contents('/path/to/contract.pdf'),
);
// Get a document by ID (returned after upload)
$doc = Clearfacts::api()->document->get('document-uuid');
echo $doc->type; // InvoiceType (PURCHASE, SALE, VARIOUS)
echo $doc->paymentState; // PAID or UNPAID
echo $doc->file->uuid;
use Clearfacts\Facades\Clearfacts;
// Get all customers for an administration (paginated, max 100 per request)
$customers = Clearfacts::api()->customer->list('0123456789');
foreach ($customers as $customer) {
echo $customer->name;
echo $customer->companyNumber;
echo $customer->email;
echo $customer->phone;
echo $customer->address->locality;
}
// Paginate with offset
$page2 = Clearfacts::api()->customer->list('0123456789', offset: 100);
use Clearfacts\Facades\Clearfacts;
// Get all journals for an administration
$journals = Clearfacts::api()->journal->list('0123456789');
foreach ($journals as $journal) {
echo $journal->id;
echo $journal->name;
echo $journal->type; // e.g. PURCHASE, SALE, VARIOUS
echo $journal->creditNote; // bool
echo $journal->default; // bool
echo $journal->lastDocumentNumber;
}
use Clearfacts\Facades\Clearfacts;
// Get archive categories for an administration
$categories = Clearfacts::api()->archiveCategory->list('BE0123456789');
// Various categories
foreach ($categories->various as $category) {
echo $category->id . ': ' . $category->name;
}
// Permanent categories
foreach ($categories->permanent as $category) {
echo $category->id . ': ' . $category->name;
}
use Clearfacts\Facades\Clearfacts;
// Get the accountant linked to the authenticated user
$accountant = Clearfacts::api()->accountant->get();
echo $accountant->name;
echo $accountant->companyNumber;
echo $accountant->email;
echo $accountant->platformName;
echo $accountant->address->locality;
use Clearfacts\Facades\Clearfacts;
use Clearfacts\Enums\AssociateType;
use Clearfacts\Enums\Language;
// List all associates
$associates = Clearfacts::api()->associate->list();
foreach ($associates as $associate) {
echo $associate->firstName . ' ' . $associate->lastName;
echo $associate->email;
echo $associate->type; // ADMIN, ASSOCIATE, SUPPORT
echo $associate->language;
echo $associate->active ? 'Active' : 'Inactive';
}
// List associate groups
$groups = Clearfacts::api()->associate->groups();
// Add a new associate
$associate = Clearfacts::api()->associate->add(
firstName: 'Jane',
lastName: 'Doe',
email: '[email protected]',
type: AssociateType::ASSOCIATE,
active: true,
language: Language::DUTCH,
sendActivationMail: true,
associateGroups: [['id' => 'group-uuid']],
);
// The plain password is only available once after creation
echo $associate->plainPassword;
// Edit an associate (only pass the fields you want to update)
$associate = Clearfacts::api()->associate->edit(
id: $associate->id,
fields: ['firstName' => 'Updated', 'active' => false],
);
use Clearfacts\Facades\Clearfacts;
// Get company statistics (AIR or processing) for a period
$stats = Clearfacts::api()->statistic->companyStatistics(
type: 'AIR', // or 'processing'
startPeriod: '2024-01-01',
endPeriod: '2024-12-31',
companyNumber: '0123456789', // optional: filter by company
invoicetype: 'PURCHASE', // optional: filter by invoice type
);
foreach ($stats as $companyStat) {
echo $companyStat->companyNumber;
foreach ($companyStat->items as $item) {
echo $item->period . ': ' . $item->value;
}
}
use Clearfacts\Facades\Clearfacts;
// Update the app info for a specific administration
$appInfo = Clearfacts::api()->appInfo->update(
vatnumber: 'BE0123456789',
emailaddress: '[email protected]',
badge: ['text' => '3', 'textColor' => '#FFFFFF', 'color' => '#FF0000'],
icon: ['type' => 'warning', 'color' => '#FFA500'],
imageUrl: 'https://example.com/logo.png',
iFrameUrl: 'https://example.com/embed',
);
// app/Http/Middleware/SetClearfactsToken.php
namespace App\Http\Middleware;
use Clearfacts\Facades\Clearfacts;
use Closure;
class SetClearfactsToken
{
public function handle($request, Closure $next)
{
$tenant = $request->user()->tenant;
if ($tenant->clearfacts_access_token) {
Clearfacts::setAccessToken($tenant->clearfacts_access_token);
}
return $next($request);
}
}