This commit is contained in:
2026-04-15 01:24:42 +02:00
parent 6786665cbf
commit 596b7a995e
15 changed files with 1206 additions and 115 deletions

View File

@@ -18,43 +18,109 @@ class FakturowniaApiClient
public function fetchSalesDocuments( $startDate, $page = 1 )
{
$query = [
'page' => (int)$page,
'per_page' => $this -> pageLimit
];
if ( $this -> canUseCurrentMonthPeriod( $startDate ) )
$query['period'] = 'this_month';
// period=more + date_from: Fakturownia filtruje po stronie API po issue_date.
// Dzieki temu nie pobieramy historycznych faktur i paginacja konczy sie naturalnie.
$query = $this -> buildDateRangeQuery( $startDate, $page );
return $this -> requestList( '/invoices.json', $query );
}
public function fetchCostDocuments( $startDate, $page = 1 )
{
$queries = [
[
'page' => (int)$page,
'per_page' => $this -> pageLimit,
'income' => 'no'
]
// W Fakturowni "faktura kosztowa" moze byc zapisana jako:
// a) zwykla faktura z income=false (widoczna w /invoices.json?income=no, URL /invoices/ID) — primary source
// b) koszt w oddzielnym module /costs.json
// c) wydatek w /expenses.json (starsze API)
// Odpytujemy wszystkie trzy i merge'ujemy po id, zeby zadna faktura nie przepadla.
// Pusta odpowiedz HTTP 200 z ktoregokolwiek endpointu NIE konczy pobierania —
// idziemy dalej do kolejnych sciezek (to byl bug przed 05-04).
// period=more + date_from filtruje po stronie API, wiec paginacja konczy sie naturalnie.
$baseQuery = $this -> buildDateRangeQuery( $startDate, $page );
$attempts = [
[ 'path' => '/invoices.json', 'query' => array_merge( $baseQuery, [ 'income' => 'no' ] ) ],
[ 'path' => '/costs.json', 'query' => $baseQuery ],
[ 'path' => '/expenses.json', 'query' => $baseQuery ]
];
if ( $this -> canUseCurrentMonthPeriod( $startDate ) )
$queries[0]['period'] = 'this_month';
$merged = [];
$seenIds = [];
$hadAnySuccess = false;
$lastError = null;
$paths = [ '/costs.json', '/expenses.json', '/invoices.json' ];
foreach ( $paths as $path )
foreach ( $attempts as $attempt )
{
foreach ( $queries as $query )
try
{
$response = $this -> requestList( $path, $query, true );
if ( $response['ok'] )
return $response['data'];
$response = $this -> requestList( $attempt['path'], $attempt['query'], true );
}
catch ( \Throwable $e )
{
$lastError = $e;
continue;
}
if ( !$response['ok'] )
{
$lastError = new \RuntimeException( 'Blad HTTP dla ' . $attempt['path'] );
continue;
}
$hadAnySuccess = true;
foreach ( $response['data'] as $document )
{
$id = $this -> extractDocumentId( $document );
if ( $id === '' )
{
$merged[] = $document;
continue;
}
if ( isset( $seenIds[ $id ] ) )
continue;
$seenIds[ $id ] = true;
$merged[] = $document;
}
}
throw new \RuntimeException( 'Nie udalo sie pobrac faktur kosztowych z API Fakturowni.' );
if ( !$hadAnySuccess && $lastError !== null )
throw new \RuntimeException( 'Nie udalo sie pobrac faktur kosztowych z API Fakturowni: ' . $lastError -> getMessage() );
return $merged;
}
private function buildDateRangeQuery( $startDate, $page )
{
$query = [
'page' => (int)$page,
'per_page' => $this -> pageLimit
];
if ( is_string( $startDate ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $startDate ) )
{
$query['period'] = 'more';
$query['date_from'] = $startDate;
$query['date_to'] = date( 'Y-m-d' );
}
return $query;
}
private function extractDocumentId( $document )
{
if ( !is_array( $document ) )
return '';
if ( isset( $document['invoice'] ) && is_array( $document['invoice'] ) )
$document = $document['invoice'];
if ( isset( $document['id'] ) && (string)$document['id'] !== '' )
return (string)$document['id'];
return '';
}
public function fetchInvoiceDetails( $invoiceId )
@@ -96,7 +162,7 @@ class FakturowniaApiClient
return $softFail ? [ 'ok' => true, 'data' => $list ] : $list;
}
private function request( $path, $query )
protected function request( $path, $query )
{
$query['api_token'] = $this -> apiToken;
$url = $this -> baseUrl . $path . '?' . http_build_query( $query );
@@ -152,11 +218,4 @@ class FakturowniaApiClient
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' );
}
}