Add Allegro import functionality and enhance product history management

This commit is contained in:
2025-01-01 23:34:52 +01:00
parent 33442a110a
commit f32f40bc50
7 changed files with 257 additions and 6 deletions

View File

@@ -29,8 +29,8 @@
},
"class.Cron.php": {
"type": "-",
"size": 18602,
"lmtime": 1733867229914,
"size": 18166,
"lmtime": 1734812301104,
"modified": false
},
"class.Products.php": {
@@ -67,8 +67,8 @@
},
"class.Products.php": {
"type": "-",
"size": 3709,
"lmtime": 1734386591635,
"size": 3708,
"lmtime": 1734812409871,
"modified": false
},
"class.Users.php": {

View File

@@ -13,6 +13,41 @@ class S
}
static public function to_decimal( $number = 0, $no_of_decimals = 2 ) {
$decimal_separator = '.';
$thousand_separator = '';
$number = is_null($number) ? 0 : $number;
$no_of_decimals = 2;
$negative_sign = "";
if ($number < 0)
{
$number = $number * -1;
$negative_sign = "-";
}
$currency_position = 'right';
if ($decimal_separator === ",")
{
if ($thousand_separator !== " ")
{
$thousand_separator = ".";
}
return $negative_sign . number_format($number, $no_of_decimals, ",", $thousand_separator);
}
else
{
if ($thousand_separator !== " ")
{
$thousand_separator = ",";
}
return $negative_sign . number_format($number, $no_of_decimals, ".", $thousand_separator);
}
}
static public function number_display( $value )
{
return number_format( $value, 2, ',', ' ' ) . ' zł';

View File

@@ -0,0 +1,177 @@
<?php
namespace controls;
class Allegro {
static public function main_view()
{
if ( \S::get_session( 'offers_added' ) )
{
$offers_added = \S::get_session( 'offers_added' );
\S::del_session( 'offers_added' );
}
if ( \S::get_session( 'history_added' ) )
{
$history_added = \S::get_session( 'history_added' );
\S::del_session( 'history_added' );
}
if ( \S::get_session( 'history_updated' ) )
{
$history_updated = \S::get_session( 'history_updated' );
\S::del_session( 'history_updated' );
}
return \Tpl::view( 'allegro/main_view', [
'clients' => \factory\Campaigns::get_clients(),
'offers_added' => $offers_added,
'history_added' => $history_added,
'history_updated' => $history_updated
] );
}
static public function import_data()
{
global $mdb;
$offers_added = 0;
$history_added = 0;
$history_updated = 0;
$file = $_FILES['file']['tmp_name'];
if ( ( $handle = fopen( $file, 'r' ) ) !== false )
{
// Pomiń pierwszy wiersz (nagłówki)
fgetcsv($handle, null, ";");
// Tablica asocjacyjna do przechowywania skumulowanych danych
$data = [];
while (($row_data = fgetcsv($handle, null, ";")) !== false)
{
// Upewnij się, że wiersz jest poprawnie wczytany
if ( is_array( $row_data ) && count( $row_data ) > 1 )
{
$client_name_part_1 = $row_data[0];
$client_name_part_2 = $row_data[1];
$offer_id = $row_data[3];
$date_add = date( 'Y-m-d', strtotime( $row_data[13] ) );
// Klucz składający się z offer_id i date_add, aby śledzić powtórzenia
$key = \S::seo( $client_name_part_1 ) . '_' . \S::seo( $client_name_part_2 ) . '_' . $offer_id . '_' . $date_add;
// Przygotowanie danych z wiersza CSV
$impressions = (int)$row_data[4];
$clicks = (int)$row_data[5];
$cost = \S::to_decimal( str_replace( [' PLN', ','], ['', '.'], $row_data[9] ) );
$conversions = (int)$row_data[11];
$conversions_value = \S::to_decimal(str_replace([' PLN', ','], ['', '.'], $row_data[12]));
// Jeśli ten klucz (offer_id + date_add) już istnieje, sumujemy dane
if (isset($data[$key]))
{
$data[$key]['campaign_name'] = $client_name_part_1;
$data[$key]['group_name'] = $client_name_part_2;
$data[$key]['impressions'] += $impressions;
$data[$key]['clicks'] += $clicks;
$data[$key]['cost'] += $cost;
$data[$key]['conversions'] += $conversions;
$data[$key]['conversions_value'] += $conversions_value;
}
else
{
// Jeśli klucz nie istnieje, dodajemy nowy wpis
$data[$key] = [
'campaign_name' => $client_name_part_1,
'group_name' => $client_name_part_2,
'offer_id' => $offer_id,
'offer_name' => $row_data[2],
'impressions' => $impressions,
'clicks' => $clicks,
'cost' => $cost,
'conversions' => $conversions,
'conversions_value' => $conversions_value,
'date_add' => $date_add
];
}
}
}
// Zamknij plik po przetworzeniu
fclose( $handle );
}
foreach ( $data as $offer )
{
$offer_data = [];
$campain_name = 'allegro.pl - ' . $offer['campaign_name'] . ' - ' . $offer['group_name'];
$client_id = $mdb -> get( 'clients', 'id', [ 'name' => $campain_name ] );
if ( !$client_id )
{
$mdb -> insert( 'clients', [ 'name' => $campain_name ] );
$client_id = $mdb -> id();
}
if ( !$mdb -> count( 'products', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] ) )
{
$offer_tmp['client_id'] = $client_id;
$offer_tmp['offer_id'] = $offer['offer_id'];
$offer_tmp['name'] = $offer['offer_name'];
if ( $mdb -> insert( 'products', $offer_tmp ) )
{
$product_id = $mdb -> id();
$offers_added++;
}
}
else
{
$product_id = $mdb -> get( 'products', 'id', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
$offer_current_name = $mdb -> get( 'products', 'name', [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
if ( $offer_current_name != $offer['offer_name'] and $offer['date_add'] == date( 'Y-m-d', strtotime( '-1 days', time() ) ) )
{
$mdb -> update( 'products', [ 'name' => $offer['offer_name'] ], [ 'AND' => [ 'client_id' => $client_id, 'offer_id' => $offer['offer_id'] ] ] );
$mdb -> insert( 'products_comments', [ 'product_id' => $product_id, 'comment' => 'Zmiana nazwy oferty na: ' . $offer['offer_name'], 'type' => 2, 'date_add' => $offer['date_add'] ] );
}
}
if ( $offer_id )
{
$offer_data['impressions'] = $offer['impressions'];
$offer_data['clicks'] = $offer['clicks'];
$offer_data['cost'] = $offer['cost'];
$offer_data['conversions'] = $offer['conversions'];
$offer_data['conversions_value'] = $offer['conversions_value'];
$offer_data['ctr'] = $offer['clicks'] ? round( $offer['impressions'] / $offer['clicks'], 4 ) : 0;
$offer_data['updated'] = 1;
if ( $mdb -> count( 'products_history', [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $offer['date_add'] ] ] ) )
{
$mdb -> update( 'products_history', $offer_data, [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $offer['date_add'] ] ] );
$history_updated++;
}
else
{
$offer_data['product_id'] = $product_id;
$offer_data['date_add'] = $offer['date_add'];
$mdb -> insert( 'products_history', $offer_data );
$history_added++;
}
}
}
\S::set_session( 'offers_added', $offers_added );
\S::set_session( 'history_added', $history_added );
\S::set_session( 'history_updated', $history_updated );
header( 'Location: /allegro/main_view/' );
exit;
}
}

View File

@@ -103,6 +103,7 @@ class Cron
// Zapisujemy każdy zsumowany wpis do offers_temp
$clicks_30 = \factory\Products::get_clicks_30( $offer_data['product_id'] );
$mdb -> insert( 'products_temp', [
'product_id' => $offer_data['product_id'],
'name' => $offer_data['name'],

View File

@@ -55,13 +55,13 @@ class Products
static public function get_impressions_30( $product_id )
{
global $mdb;
return $mdb -> query( 'SELECT SUM(impressions) FROM products_history WHERE product_id = \'' . $product_id . '\'' ) -> fetchColumn();
return $mdb -> query( 'SELECT SUM(impressions) FROM products_history WHERE product_id = \'' . $product_id . '\' AND date_add >= \'' . date( 'Y-m-d', strtotime( '-30 days', time() ) ) . '\'' ) -> fetchColumn();
}
static public function get_clicks_30( $product_id )
{
global $mdb;
return $mdb -> query( 'SELECT SUM(clicks) FROM products_history WHERE product_id = \'' . $product_id . '\'' ) -> fetchColumn();
return $mdb -> query( 'SELECT SUM(clicks) FROM products_history WHERE product_id = \'' . $product_id . '\' AND date_add >= \'' . date( 'Y-m-d', strtotime( '-30 days', time() ) ) . '\'' ) -> fetchColumn();
}
static public function add_product_comment( $product_id, $type, $comment )

View File

@@ -0,0 +1,35 @@
<form action="/allegro/import_data/" method="post" enctype="multipart/form-data">
<div class="admin-form theme-primary">
<div class="panel heading-border panel-primary">
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<? if ( $this -> offers_added ): ?>
<div class="alert alert-success" role="alert">
Dodano <?= $this -> offers_added; ?> ofert.
</div>
<? endif; ?>
<? if ( $this -> history_added ): ?>
<div class="alert alert-success" role="alert">
Dodano <?= $this -> history_added; ?> rekordów do historii.
</div>
<? endif; ?>
<? if ( $this -> history_updated ): ?>
<div class="alert alert-success" role="alert">
Zaktualizowano <?= $this -> history_updated; ?> rekordów w historii.
</div>
<? endif; ?>
</div>
</div>
<div class="row">
<div class="col-md-5">
<input type="file" id="file" name="file">
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary">Importuj</button>
</div>
</div>
</div>
</div>
</div>
</form>

View File

@@ -63,6 +63,9 @@
<li>
<a href="/products/main_view/">Produkty</a>
</li>
<li>
<a href="/allegro/main_view/">Allegro import</a>
</li>
</ul>
</div>
<div class="main">