Add Allegro import functionality and enhance product history management
This commit is contained in:
8
.vscode/ftp-kr.sync.cache.json
vendored
8
.vscode/ftp-kr.sync.cache.json
vendored
@@ -29,8 +29,8 @@
|
|||||||
},
|
},
|
||||||
"class.Cron.php": {
|
"class.Cron.php": {
|
||||||
"type": "-",
|
"type": "-",
|
||||||
"size": 18602,
|
"size": 18166,
|
||||||
"lmtime": 1733867229914,
|
"lmtime": 1734812301104,
|
||||||
"modified": false
|
"modified": false
|
||||||
},
|
},
|
||||||
"class.Products.php": {
|
"class.Products.php": {
|
||||||
@@ -67,8 +67,8 @@
|
|||||||
},
|
},
|
||||||
"class.Products.php": {
|
"class.Products.php": {
|
||||||
"type": "-",
|
"type": "-",
|
||||||
"size": 3709,
|
"size": 3708,
|
||||||
"lmtime": 1734386591635,
|
"lmtime": 1734812409871,
|
||||||
"modified": false
|
"modified": false
|
||||||
},
|
},
|
||||||
"class.Users.php": {
|
"class.Users.php": {
|
||||||
|
|||||||
@@ -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 )
|
static public function number_display( $value )
|
||||||
{
|
{
|
||||||
return number_format( $value, 2, ',', ' ' ) . ' zł';
|
return number_format( $value, 2, ',', ' ' ) . ' zł';
|
||||||
|
|||||||
177
autoload/controls/class.Allegro.php
Normal file
177
autoload/controls/class.Allegro.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -103,6 +103,7 @@ class Cron
|
|||||||
|
|
||||||
// Zapisujemy każdy zsumowany wpis do offers_temp
|
// Zapisujemy każdy zsumowany wpis do offers_temp
|
||||||
$clicks_30 = \factory\Products::get_clicks_30( $offer_data['product_id'] );
|
$clicks_30 = \factory\Products::get_clicks_30( $offer_data['product_id'] );
|
||||||
|
|
||||||
$mdb -> insert( 'products_temp', [
|
$mdb -> insert( 'products_temp', [
|
||||||
'product_id' => $offer_data['product_id'],
|
'product_id' => $offer_data['product_id'],
|
||||||
'name' => $offer_data['name'],
|
'name' => $offer_data['name'],
|
||||||
|
|||||||
@@ -55,13 +55,13 @@ class Products
|
|||||||
static public function get_impressions_30( $product_id )
|
static public function get_impressions_30( $product_id )
|
||||||
{
|
{
|
||||||
global $mdb;
|
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 )
|
static public function get_clicks_30( $product_id )
|
||||||
{
|
{
|
||||||
global $mdb;
|
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 )
|
static public function add_product_comment( $product_id, $type, $comment )
|
||||||
|
|||||||
35
templates/allegro/main_view.php
Normal file
35
templates/allegro/main_view.php
Normal 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>
|
||||||
@@ -63,6 +63,9 @@
|
|||||||
<li>
|
<li>
|
||||||
<a href="/products/main_view/">Produkty</a>
|
<a href="/products/main_view/">Produkty</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/allegro/main_view/">Allegro import</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="main">
|
<div class="main">
|
||||||
|
|||||||
Reference in New Issue
Block a user