Files
crmPRO/autoload/Domain/Finances/FakturowniaApiClient.php
2026-04-02 12:00:38 +02:00

163 lines
4.1 KiB
PHP

<?php
namespace Domain\Finances;
class FakturowniaApiClient
{
private $baseUrl;
private $apiToken;
private $pageLimit;
private $timeout;
public function __construct( $baseUrl, $apiToken, $pageLimit = 100, $timeout = 20 )
{
$this -> baseUrl = rtrim( (string)$baseUrl, '/' );
$this -> apiToken = (string)$apiToken;
$this -> pageLimit = max( 1, (int)$pageLimit );
$this -> timeout = max( 5, (int)$timeout );
}
public function fetchSalesDocuments( $startDate, $page = 1 )
{
$query = [
'page' => (int)$page,
'per_page' => $this -> pageLimit
];
if ( $this -> canUseCurrentMonthPeriod( $startDate ) )
$query['period'] = 'this_month';
return $this -> requestList( '/invoices.json', $query );
}
public function fetchCostDocuments( $startDate, $page = 1 )
{
$queries = [
[
'page' => (int)$page,
'per_page' => $this -> pageLimit,
'income' => 'no'
]
];
if ( $this -> canUseCurrentMonthPeriod( $startDate ) )
$queries[0]['period'] = 'this_month';
$paths = [ '/costs.json', '/expenses.json', '/invoices.json' ];
foreach ( $paths as $path )
{
foreach ( $queries as $query )
{
$response = $this -> requestList( $path, $query, true );
if ( $response['ok'] )
return $response['data'];
}
}
throw new \RuntimeException( 'Nie udalo sie pobrac faktur kosztowych z API Fakturowni.' );
}
public function fetchInvoiceDetails( $invoiceId )
{
$invoiceId = (int)$invoiceId;
if ( $invoiceId <= 0 )
return null;
$result = $this -> request( '/invoices/' . $invoiceId . '.json', [] );
if ( $result['http_code'] >= 400 )
return null;
$data = json_decode( $result['body'], true );
return is_array( $data ) ? $data : null;
}
private function requestList( $path, $query, $softFail = false )
{
$result = $this -> request( $path, $query );
if ( $result['http_code'] >= 400 )
{
if ( $softFail )
return [ 'ok' => false, 'data' => [] ];
throw new \RuntimeException( 'Blad API Fakturowni: HTTP ' . $result['http_code'] . ' dla ' . $path );
}
$data = json_decode( $result['body'], true );
if ( !is_array( $data ) )
{
if ( $softFail )
return [ 'ok' => false, 'data' => [] ];
throw new \RuntimeException( 'API Fakturowni zwrocilo niepoprawny JSON.' );
}
$list = $this -> extractList( $data );
return $softFail ? [ 'ok' => true, 'data' => $list ] : $list;
}
private function request( $path, $query )
{
$query['api_token'] = $this -> apiToken;
$url = $this -> baseUrl . $path . '?' . http_build_query( $query );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this -> timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $this -> timeout );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
'Accept: application/json'
] );
$body = curl_exec( $ch );
$httpCode = (int)curl_getinfo( $ch, CURLINFO_HTTP_CODE );
$error = curl_error( $ch );
curl_close( $ch );
if ( $body === false )
throw new \RuntimeException( 'Blad polaczenia z API Fakturowni: ' . $error );
return [
'http_code' => $httpCode,
'body' => $body
];
}
private function extractList( $data )
{
if ( $this -> isList( $data ) )
return $data;
$keys = [ 'invoices', 'costs', 'expenses', 'data' ];
foreach ( $keys as $key )
{
if ( isset( $data[ $key ] ) && is_array( $data[ $key ] ) )
{
if ( $this -> isList( $data[ $key ] ) )
return $data[ $key ];
}
}
return [];
}
private function isList( $value )
{
if ( !is_array( $value ) )
return false;
if ( $value === [] )
return true;
return array_keys( $value ) === range( 0, count( $value ) - 1 );
}
private function canUseCurrentMonthPeriod( $startDate )
{
if ( !is_string( $startDate ) || !preg_match( '/^\d{4}-\d{2}-\d{2}$/', $startDate ) )
return false;
return $startDate === date( 'Y-m-01' );
}
}