update
This commit is contained in:
@@ -8,6 +8,11 @@ class FinancesController
|
||||
return new \Domain\Finances\FinanceRepository();
|
||||
}
|
||||
|
||||
private static function importRepo()
|
||||
{
|
||||
return new \Domain\Finances\FakturowniaImportRepository();
|
||||
}
|
||||
|
||||
private static function requireAuth()
|
||||
{
|
||||
global $user;
|
||||
@@ -53,6 +58,8 @@ class FinancesController
|
||||
return false;
|
||||
|
||||
$repo = self::repo();
|
||||
$importRepo = self::importRepo();
|
||||
$importRepo -> ensureTables();
|
||||
|
||||
if ( \S::get( 'tag-clear' ) )
|
||||
unset( $_SESSION['finance-tag-id'] );
|
||||
@@ -93,10 +100,121 @@ class FinancesController
|
||||
'wallet_summary' => $repo -> walletSummary( $group_id ),
|
||||
'wallet_summary_this_month' => $repo -> walletSummaryThisMonth( $group_id ),
|
||||
'wallet_income_this_month' => $repo -> walletIncomeThisMonth( $group_id ),
|
||||
'wallet_expenses_this_month' => $repo -> walletExpensesThisMonth( $group_id )
|
||||
'wallet_expenses_this_month' => $repo -> walletExpensesThisMonth( $group_id ),
|
||||
'fakturownia_pending_clients' => self::preparePendingRows( $importRepo -> pendingClientMappings() ),
|
||||
'fakturownia_pending_items' => self::preparePendingRows( $importRepo -> pendingItemMappings() ),
|
||||
'fakturownia_crm_clients' => $repo -> clientsList(),
|
||||
'fakturownia_categories' => self::prepareCategoryOptions( $repo -> categoriesFlatList() ),
|
||||
'fakturownia_last_summary' => self::lastImportSummary( $importRepo )
|
||||
] );
|
||||
}
|
||||
|
||||
private static function preparePendingRows( $rows )
|
||||
{
|
||||
$output = [];
|
||||
|
||||
if ( !is_array( $rows ) )
|
||||
return $output;
|
||||
|
||||
foreach ( $rows as $row )
|
||||
{
|
||||
$payload = [];
|
||||
if ( isset( $row['payload_json'] ) && $row['payload_json'] )
|
||||
{
|
||||
$decoded = json_decode( $row['payload_json'], true );
|
||||
if ( is_array( $decoded ) )
|
||||
$payload = $decoded;
|
||||
}
|
||||
|
||||
$output[] = [
|
||||
'external_key' => (string)$row['external_key'],
|
||||
'external_name' => (string)$row['external_name'],
|
||||
'hits' => (int)$row['hits'],
|
||||
'last_seen_at' => (string)$row['last_seen_at'],
|
||||
'payload' => $payload
|
||||
];
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
private static function lastImportSummary( $importRepo )
|
||||
{
|
||||
$raw = $importRepo -> getState( 'last_import_summary' );
|
||||
if ( !$raw )
|
||||
return null;
|
||||
|
||||
$decoded = json_decode( $raw, true );
|
||||
return is_array( $decoded ) ? $decoded : null;
|
||||
}
|
||||
|
||||
private static function prepareCategoryOptions( $categories )
|
||||
{
|
||||
$options = [];
|
||||
if ( !is_array( $categories ) || empty( $categories ) )
|
||||
return $options;
|
||||
|
||||
$byId = [];
|
||||
foreach ( $categories as $category )
|
||||
{
|
||||
$id = isset( $category['id'] ) ? (int)$category['id'] : 0;
|
||||
if ( $id <= 0 )
|
||||
continue;
|
||||
|
||||
$byId[ $id ] = $category;
|
||||
}
|
||||
|
||||
foreach ( $categories as $category )
|
||||
{
|
||||
$id = isset( $category['id'] ) ? (int)$category['id'] : 0;
|
||||
if ( $id <= 0 )
|
||||
continue;
|
||||
|
||||
$label = self::buildCategoryPathLabel( $id, $byId );
|
||||
$options[] = [
|
||||
'id' => $id,
|
||||
'name' => $label,
|
||||
'group_id' => isset( $category['group_id'] ) ? (int)$category['group_id'] : 0
|
||||
];
|
||||
}
|
||||
|
||||
usort( $options, function( $left, $right )
|
||||
{
|
||||
return strcmp( $left['name'], $right['name'] );
|
||||
} );
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
private static function buildCategoryPathLabel( $categoryId, $byId )
|
||||
{
|
||||
$parts = [];
|
||||
$guard = 0;
|
||||
$currentId = (int)$categoryId;
|
||||
|
||||
while ( $currentId > 0 && isset( $byId[ $currentId ] ) )
|
||||
{
|
||||
$category = $byId[ $currentId ];
|
||||
$name = trim( (string)( $category['name'] ?? '' ) );
|
||||
if ( $name !== '' )
|
||||
array_unshift( $parts, $name );
|
||||
|
||||
$parentId = isset( $category['parent_id'] ) ? (int)$category['parent_id'] : 0;
|
||||
if ( $parentId <= 0 || $parentId === $currentId )
|
||||
break;
|
||||
|
||||
$currentId = $parentId;
|
||||
$guard++;
|
||||
if ( $guard > 20 )
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $parts ) )
|
||||
return 'Kategoria #' . (int)$categoryId;
|
||||
|
||||
return implode( ' > ', $parts );
|
||||
}
|
||||
|
||||
public static function operationEdit()
|
||||
{
|
||||
if ( !self::requireAuth() )
|
||||
@@ -230,4 +348,70 @@ class FinancesController
|
||||
'date_to' => $date_to
|
||||
] );
|
||||
}
|
||||
|
||||
public static function fakturowniaClientMappingSave()
|
||||
{
|
||||
if ( !self::requireAuth() )
|
||||
return false;
|
||||
|
||||
if ( !\S::csrf_verify() )
|
||||
{
|
||||
\S::alert( 'Nieprawidlowy token bezpieczenstwa. Odswiez strone i sproboj ponownie.' );
|
||||
header( 'Location: /finances/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$externalKey = trim( (string)\S::get( 'external_key' ) );
|
||||
$externalName = trim( (string)\S::get( 'external_name' ) );
|
||||
$crmClientId = (int)\S::get( 'crm_client_id' );
|
||||
$repo = self::repo();
|
||||
|
||||
if ( $externalKey === '' || $externalName === '' || $crmClientId <= 0 || !$repo -> clientExists( $crmClientId ) )
|
||||
{
|
||||
\S::alert( 'Nie udalo sie zapisac mapowania klienta. Uzupelnij wszystkie pola.' );
|
||||
header( 'Location: /finances/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$importRepo = self::importRepo();
|
||||
$importRepo -> ensureTables();
|
||||
$importRepo -> saveClientMapping( $externalKey, $externalName, $crmClientId );
|
||||
|
||||
\S::alert( 'Mapowanie klienta zostalo zapisane.' );
|
||||
header( 'Location: /finances/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function fakturowniaItemMappingSave()
|
||||
{
|
||||
if ( !self::requireAuth() )
|
||||
return false;
|
||||
|
||||
if ( !\S::csrf_verify() )
|
||||
{
|
||||
\S::alert( 'Nieprawidlowy token bezpieczenstwa. Odswiez strone i sproboj ponownie.' );
|
||||
header( 'Location: /finances/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$externalKey = trim( (string)\S::get( 'external_key' ) );
|
||||
$externalName = trim( (string)\S::get( 'external_name' ) );
|
||||
$financeCategoryId = (int)\S::get( 'finance_category_id' );
|
||||
$repo = self::repo();
|
||||
|
||||
if ( $externalKey === '' || $externalName === '' || $financeCategoryId <= 0 || !$repo -> categoryExists( $financeCategoryId ) )
|
||||
{
|
||||
\S::alert( 'Nie udalo sie zapisac mapowania pozycji. Uzupelnij wszystkie pola.' );
|
||||
header( 'Location: /finances/main_view/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
$importRepo = self::importRepo();
|
||||
$importRepo -> ensureTables();
|
||||
$importRepo -> saveItemMapping( $externalKey, $externalName, $financeCategoryId );
|
||||
|
||||
\S::alert( 'Mapowanie pozycji zostalo zapisane.' );
|
||||
header( 'Location: /finances/main_view/' );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user