first commit
This commit is contained in:
71
autoload/shop/class.Basket.php
Normal file
71
autoload/shop/class.Basket.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class Basket implements \ArrayAccess
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static public function validate_basket( $basket )
|
||||
{
|
||||
if ( !is_array( $basket ) )
|
||||
return $basket = array();
|
||||
else
|
||||
return $basket;
|
||||
}
|
||||
|
||||
// sprawdzanie czy ilość produktu w koszyku nie jest większa niż stan magazynowy
|
||||
public static function check_product_quantity_in_stock( $basket, bool $message = false )
|
||||
{
|
||||
$result = false;
|
||||
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
$quantity_options = \shop\Product::get_product_permutation_quantity_options( $val['parent_id'] ? $val['parent_id'] : $val['product-id'], $val['attributes'][0] );
|
||||
|
||||
if ( ( $val[ 'quantity' ] > $quantity_options['quantity'] ) and !$quantity_options['stock_0_buy'] )
|
||||
{
|
||||
$basket[ $key ][ 'quantity' ] = $quantity_options['quantity'];
|
||||
if ( $message )
|
||||
\S::error( 'Ilość jednego lub więcej produktów została zmniejszona z powodu niestarczających stanów magazynowych. Sprawdź proszę koszyk.' );
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
\S::set_session( 'basket', $basket );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __get( $variable )
|
||||
{
|
||||
if ( array_key_exists( $variable, $this -> data ) )
|
||||
return $this -> $variable;
|
||||
}
|
||||
|
||||
public function __set( $variable, $value )
|
||||
{
|
||||
$this -> $variable = $value;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
84
autoload/shop/class.Category.php
Normal file
84
autoload/shop/class.Category.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class Category implements \ArrayAccess
|
||||
{
|
||||
public function __construct( int $category_id, $lang_id = null )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static public function get_category_products_id( int $category_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> select( 'pp_shop_products_categories', 'product_id', [ 'category_id' => $category_id ] );
|
||||
}
|
||||
|
||||
static public function get_category_name( int $category_id, $lang_id = null )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$lang_id )
|
||||
$lang_id = \front\factory\Languages::default_language();
|
||||
|
||||
return $mdb -> get( 'pp_shop_categories_langs', 'title', [ 'AND' => [ 'category_id' => $category_id, 'lang_id' => $lang_id ] ] );
|
||||
}
|
||||
|
||||
public function __get( $variable )
|
||||
{
|
||||
if ( array_key_exists( $variable, $this -> data ) )
|
||||
return $this -> $variable;
|
||||
}
|
||||
|
||||
public function __set( $variable, $value )
|
||||
{
|
||||
$this -> $variable = $value;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
|
||||
static public function get_subcategory_by_category( int $category_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\Category::get_subcategory_by_category:$category_id";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$shop_categories = $mdb -> select( 'pp_shop_categories', '*', ['parent_id' => $category_id] );
|
||||
$shop_categories_langs = array();
|
||||
foreach ($shop_categories as $shop_categorie) {
|
||||
array_push($shop_categories_langs, $mdb -> get( 'pp_shop_categories_langs', '*', ['category_id' => $shop_categorie['id']] ));
|
||||
}
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $shop_categories_langs );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
}
|
||||
|
||||
return $shop_categories_langs;
|
||||
}
|
||||
}
|
||||
77
autoload/shop/class.Coupon.php
Normal file
77
autoload/shop/class.Coupon.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?
|
||||
namespace shop;
|
||||
class Coupon implements \ArrayAccess
|
||||
{
|
||||
public function __construct( int $element_id )
|
||||
{
|
||||
global $mdb;
|
||||
if ( $element_id )
|
||||
{
|
||||
$result = $mdb -> get( 'pp_shop_coupon', '*', [ 'id' => $element_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
}
|
||||
|
||||
public function load_from_db_by_name( string $name )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_coupon', '*', [ 'name' => $name ] );
|
||||
if ( is_array( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
|
||||
public function is_one_time() {
|
||||
// return $this -> bean -> one_time;
|
||||
}
|
||||
|
||||
public function is_available()
|
||||
{
|
||||
if ( !$this -> id )
|
||||
return false;
|
||||
|
||||
if ( !$this -> status )
|
||||
return false;
|
||||
|
||||
return !$this -> used;
|
||||
}
|
||||
|
||||
public function set_as_used()
|
||||
{
|
||||
// $this -> bean -> used = 1;
|
||||
// $this -> bean -> date_used = date( 'Y-m-d H:i:s' );
|
||||
// \R::store( $this -> bean );
|
||||
}
|
||||
|
||||
public function __get( $variable )
|
||||
{
|
||||
if ( array_key_exists( $variable, $this -> data ) )
|
||||
return $this -> $variable;
|
||||
}
|
||||
|
||||
public function __set( $variable, $value )
|
||||
{
|
||||
$this -> $variable = $value;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
148
autoload/shop/class.Dashboard.php
Normal file
148
autoload/shop/class.Dashboard.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class Dashboard implements \ArrayAccess
|
||||
{
|
||||
static public function summary_orders()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
try
|
||||
{
|
||||
$redis = \RedisConnection::getInstance() -> getConnection();
|
||||
$objectData = $redis -> get( "summary_ordersd" );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$summary = $mdb -> count( 'pp_shop_orders', [ 'status' => 6 ] );
|
||||
$redis -> setex( "summary_ordersd", 60 * 5, serialize( $summary ) );
|
||||
}
|
||||
else
|
||||
$summary = unserialize( $objectData );
|
||||
}
|
||||
catch ( \RedisException $e )
|
||||
{
|
||||
$summary = $mdb -> count( 'pp_shop_orders', [ 'status' => 6 ] );
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
static public function summary_sales()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
try
|
||||
{
|
||||
$redis = \RedisConnection::getInstance() -> getConnection();
|
||||
$objectData = $redis -> get( "summary_salesd" );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$summary = $mdb -> sum( 'pp_shop_orders', 'summary', [ 'status' => 6 ] ) - $mdb -> sum( 'pp_shop_orders', 'transport_cost', [ 'status' => 6 ] );
|
||||
$redis -> setex( "summary_salesd", 60 * 5, serialize( $summary ) );
|
||||
}
|
||||
else
|
||||
$summary = unserialize( $objectData );
|
||||
}
|
||||
catch ( \RedisException $e )
|
||||
{
|
||||
$summary = $mdb -> sum( 'pp_shop_orders', 'summary', [ 'status' => 6 ] ) - $mdb -> sum( 'pp_shop_orders', 'transport_cost', [ 'status' => 6 ] );
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
static public function sales_grid()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$rows = $mdb -> select( 'pp_shop_orders', [ 'id', 'date_order' ], [ 'status' => 6 ] );
|
||||
if ( \S::is_array_fix( $rows ) ) foreach ( $rows as $row )
|
||||
{
|
||||
if ( date( 'N', strtotime( $row['date_order'] ) ) )
|
||||
$grid[ date( 'N', strtotime( $row['date_order'] ) ) ][ date( 'G', strtotime($row['date_order'] ) ) ] += 1;
|
||||
}
|
||||
|
||||
return $grid;
|
||||
}
|
||||
|
||||
static public function most_view_products()
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> query( 'SELECT '
|
||||
. 'id, SUM(visits) AS visits '
|
||||
. 'FROM '
|
||||
. 'pp_shop_products AS psop '
|
||||
. 'GROUP BY '
|
||||
. 'id '
|
||||
. 'ORDER BY '
|
||||
. 'visits DESC '
|
||||
. 'LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
}
|
||||
|
||||
static public function best_sales_products()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
return $mdb -> query( 'SELECT parent_product_id, SUM(quantity) AS quantity_summary, SUM(price_brutto_promo * quantity) AS sales FROM pp_shop_order_products AS psop INNER JOIN pp_shop_orders AS pso ON pso.id = psop.order_id WHERE pso.status = 6 GROUP BY parent_product_id ORDER BY sales DESC LIMIT 10' ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
}
|
||||
|
||||
static public function last_24_months_sales()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$monthsBack = 24;
|
||||
|
||||
$sales = [ [ 'date' => date( 'Y-m' ) ] ];
|
||||
$previousMonthDate = new \DateTime();
|
||||
for ( $monthInterval = 0; $monthInterval < $monthsBack; $monthInterval++)
|
||||
{
|
||||
$previousMonthDate -> sub( new \DateInterval( "P1M" ) );
|
||||
array_push( $sales, [ 'date' => $previousMonthDate -> format( 'Y-m' ) ] );
|
||||
}
|
||||
|
||||
for ( $i = 0; $i < 24; $i++ )
|
||||
{
|
||||
$date_start = date( 'Y-m-1', strtotime( $sales[$i]['date'] ) );
|
||||
$date_end = date( 'Y-m-t', strtotime( $sales[$i]['date'] ) );
|
||||
$sales[$i]['sales'] = $mdb -> sum( 'pp_shop_orders', 'summary', [ 'AND' => [ 'status' => 6, 'date_order[>=]' => $date_start, 'date_order[<=]' => $date_end ] ] ) - $mdb -> sum( 'pp_shop_orders', 'transport_cost', [ 'AND' => [ 'status' => 6, 'date_order[>=]' => $date_start, 'date_order[<=]' => $date_end ] ] );
|
||||
}
|
||||
|
||||
return $sales;
|
||||
}
|
||||
|
||||
static public function last_orders()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
return $mdb -> query( 'SELECT '
|
||||
. 'id, number, date_order, CONCAT( client_name, \' \', client_surname ) AS client, client_email, CONCAT( client_street, \', \', client_postal_code, \' \', client_city ) AS address, status, client_phone, summary '
|
||||
. 'FROM '
|
||||
. 'pp_shop_orders AS pso '
|
||||
. 'ORDER BY '
|
||||
. 'date_order DESC '
|
||||
. 'LIMIT '
|
||||
. '10' ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
480
autoload/shop/class.Order.php
Normal file
480
autoload/shop/class.Order.php
Normal file
@@ -0,0 +1,480 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class Order implements \ArrayAccess
|
||||
{
|
||||
public $id;
|
||||
public $products;
|
||||
public $statuses;
|
||||
public $status;
|
||||
public $client_email;
|
||||
public $sellasist_order_id;
|
||||
public $summary;
|
||||
public $baselinker_order_id;
|
||||
public $apilo_order_id;
|
||||
public $date_order;
|
||||
public $payment_method_id;
|
||||
public $transport_cost;
|
||||
public $number;
|
||||
|
||||
public function __construct( int $order_id, $hash = '', $przelewy24_hash = '' )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( $order_id )
|
||||
{
|
||||
$result = $mdb -> get( 'pp_shop_orders', '*', [ 'id' => $order_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
|
||||
if ( $hash )
|
||||
{
|
||||
$result = $mdb -> get( 'pp_shop_orders', '*', [ 'hash' => $hash ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
|
||||
if ( $przelewy24_hash )
|
||||
{
|
||||
$result = $mdb -> get( 'pp_shop_orders', '*', [ 'przelewy24_hash' => $przelewy24_hash ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
|
||||
$this -> products = $mdb -> select( 'pp_shop_order_products', '*', [ 'order_id' => $order_id ] );
|
||||
$this -> statuses = $mdb -> select( 'pp_shop_order_statuses', '*', [ 'order_id' => $order_id, 'ORDER' => [ 'id' => 'DESC' ] ] );
|
||||
}
|
||||
|
||||
static public function notes_save( int $order_id, $notes )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_shop_orders', [ 'notes' => $notes ], [ 'id' => $order_id ] );
|
||||
}
|
||||
|
||||
public static function order_statuses()
|
||||
{
|
||||
global $mdb;
|
||||
$results = $mdb -> select( 'pp_shop_statuses', [ 'id', 'status' ], [ 'ORDER' => [ 'o' => 'ASC' ] ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
$statuses[ (int)$row['id'] ] = $row['status'];
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
public function update_baselinker_order_status_date( $date )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_shop_orders', [ 'baselinker_order_status_date' => $date ], [ 'id' => $this -> id ] );
|
||||
}
|
||||
|
||||
public function update_aplio_order_status_date( $date )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_shop_orders', [ 'apilo_order_status_date' => $date ], [ 'id' => $this -> id ] );
|
||||
}
|
||||
|
||||
// set_as_unpaid
|
||||
public function set_as_unpaid() {
|
||||
global $mdb;
|
||||
|
||||
$sellasist_settings = \admin\factory\Integrations::sellasist_settings();
|
||||
if ( $sellasist_settings['enabled'] and $sellasist_settings['api_code'] and $sellasist_settings['sync_orders'] ) {
|
||||
if ( $this -> sellasist_order_id ) {
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://projectpro.sellasist.pl/api/v1/orders/" . $this -> sellasist_order_id );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( [
|
||||
'paid' => 0,
|
||||
'payment_status' => 'unpaid'
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
|
||||
"accept: application/json",
|
||||
"apiKey: " . $sellasist_settings['api_code'],
|
||||
"Content-Type: application/json"
|
||||
) );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec( $ch );
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
$mdb -> update( 'pp_shop_orders', [ 'paid' => 0 ], [ 'id' => $this -> id ] );
|
||||
return true;
|
||||
}
|
||||
|
||||
// set_as_paid
|
||||
public function set_as_paid( $send_email = false )
|
||||
{
|
||||
global $mdb, $config;
|
||||
|
||||
$sellasist_settings = \admin\factory\Integrations::sellasist_settings();
|
||||
if ( $sellasist_settings['enabled'] and $sellasist_settings['api_code'] and $sellasist_settings['sync_orders'] )
|
||||
{
|
||||
if ( $this -> sellasist_order_id )
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://projectpro.sellasist.pl/api/v1/orders/" . $this -> sellasist_order_id );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( [
|
||||
'paid' => str_replace( ',', '.', $this -> summary ),
|
||||
'payment_status' => 'paid'
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
|
||||
"accept: application/json",
|
||||
"apiKey: " . $sellasist_settings['api_code'],
|
||||
"Content-Type: application/json"
|
||||
) );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec( $ch );
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
$baselinker_settings = \admin\factory\Integrations::baselinker_settings();
|
||||
if ( $baselinker_settings['enabled'] and $baselinker_settings['api_code'] and $baselinker_settings['sync_orders'] )
|
||||
{
|
||||
if ( $this -> baselinker_order_id )
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://api.baselinker.com/connector.php" );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( [
|
||||
'token' => $baselinker_settings['api_code'],
|
||||
'method' => 'setOrderPayment',
|
||||
'parameters' => json_encode( [
|
||||
'order_id' => $this -> baselinker_order_id,
|
||||
'payment_done' => $this -> summary,
|
||||
'payment_date' => time( 'Y-m-d H:i:s' )
|
||||
] )
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec( $ch );
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
// apilo
|
||||
$apilo_settings = \admin\factory\Integrations::apilo_settings();
|
||||
if ( $apilo_settings['enabled'] and $apilo_settings['access-token'] and $apilo_settings['sync_orders'] )
|
||||
{
|
||||
// put data to file
|
||||
if ( $config['debug']['apilo'] )
|
||||
{
|
||||
file_put_contents( $_SERVER['DOCUMENT_ROOT'] . '/logs/apilo.txt', date( 'Y-m-d H:i:s' ) . " --- SET AS PAID\n\n", FILE_APPEND );
|
||||
file_put_contents( $_SERVER['DOCUMENT_ROOT'] . '/logs/apilo.txt', print_r( $this, true ) . "\n\n", FILE_APPEND );
|
||||
}
|
||||
|
||||
if ( $this -> apilo_order_id )
|
||||
{
|
||||
$payment_date = new \DateTime( $this -> date_order );
|
||||
$access_token = \admin\factory\Integrations::apilo_get_access_token();
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://projectpro.apilo.com/rest/api/orders/" . $this -> apilo_order_id . '/payment/' );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( [
|
||||
'amount' => str_replace( ',', '.', $this -> summary ),
|
||||
'paymentDate' => $payment_date -> format('Y-m-d\TH:i:s\Z'),
|
||||
'type' => 1
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $access_token,
|
||||
"Accept: application/json"
|
||||
] );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$apilo_response = curl_exec( $ch );
|
||||
|
||||
// put response to log
|
||||
if ( $config['debug']['apilo'] )
|
||||
file_put_contents( $_SERVER['DOCUMENT_ROOT'] . '/logs/apilo.txt', print_r( $apilo_response, true ) . "\n\n", FILE_APPEND );
|
||||
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
$mdb -> update( 'pp_shop_orders', [ 'paid' => 1 ], [ 'id' => $this -> id ] );
|
||||
$this -> update_status( 1, $send_email );
|
||||
|
||||
$dir_logs = $_SERVER['DOCUMENT_ROOT'] . '/logs/';
|
||||
if ( !is_dir( $dir_logs ) )
|
||||
mkdir( $dir_logs, 0777, true );
|
||||
|
||||
$file = $dir_logs . date( 'Y-m-d' ) . '-order-set-as-paid.txt';
|
||||
$content = date( 'Y-m-d H:i:s' ) . ' | ' . $this -> id . ' | ' . $this -> number . ' | ' . $this -> summary . ' | ' . $this -> client_email . "\n";
|
||||
$content .= print_r( $apilo_response, true ) . "\n\n";
|
||||
file_put_contents( $file, $content, FILE_APPEND );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// zmiana statusu zamówienia
|
||||
public function update_status( int $status, int $email_send )
|
||||
{
|
||||
global $mdb, $config;
|
||||
|
||||
if ( $this -> status == $status )
|
||||
return false;
|
||||
|
||||
if ( $mdb -> update( 'pp_shop_orders', [ 'status' => $status ], [ 'id' => $this -> id ] ) )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_order_statuses', [
|
||||
'order_id' => $this -> id,
|
||||
'status_id' => $status,
|
||||
'mail' => $email_send
|
||||
] );
|
||||
|
||||
if ( $email_send )
|
||||
{
|
||||
$this -> status = $status;
|
||||
$response['email'] = $this -> send_status_change_email();
|
||||
}
|
||||
|
||||
$response['result'] = true;
|
||||
|
||||
$sellasist_settings = \admin\factory\Integrations::sellasist_settings();
|
||||
if ( $sellasist_settings['enabled'] and $sellasist_settings['api_code'] and $sellasist_settings['sync_orders'] )
|
||||
{
|
||||
if ( $this -> sellasist_order_id )
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://projectpro.sellasist.pl/api/v1/orders/" . $this -> sellasist_order_id );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( [
|
||||
'status' => \front\factory\ShopStatuses::get_sellasist_status_id( $status )
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
|
||||
"accept: application/json",
|
||||
"apiKey: " . $sellasist_settings['api_code'],
|
||||
"Content-Type: application/json"
|
||||
) );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec( $ch );
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
$baselinker_settings = \admin\factory\Integrations::baselinker_settings();
|
||||
if ( $baselinker_settings['enabled'] and $baselinker_settings['api_code'] and $baselinker_settings['sync_orders'] )
|
||||
{
|
||||
if ( $this -> baselinker_order_id )
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://api.baselinker.com/connector.php" );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( [
|
||||
'token' => $baselinker_settings['api_code'],
|
||||
'method' => 'setOrderStatus',
|
||||
'parameters' => json_encode( [
|
||||
'order_id' => $this -> baselinker_order_id,
|
||||
'status_id' => \shop\ShopStatus::get_baselinker_status_by_shop_status( $status )
|
||||
] )
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$curl_result = curl_exec( $ch );
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
// apilo
|
||||
$apilo_settings = \admin\factory\Integrations::apilo_settings();
|
||||
if ( $apilo_settings['enabled'] and $apilo_settings['access-token'] and $apilo_settings['sync_orders'] )
|
||||
{
|
||||
// put data to file
|
||||
if ( $config['debug']['apilo'] )
|
||||
{
|
||||
file_put_contents( $_SERVER['DOCUMENT_ROOT'] . '/logs/apilo.txt', date( 'Y-m-d H:i:s' ) . " --- UPDATE STATUS\n\n", FILE_APPEND );
|
||||
file_put_contents( $_SERVER['DOCUMENT_ROOT'] . '/logs/apilo.txt', print_r( $this, true ) . "\n\n", FILE_APPEND );
|
||||
}
|
||||
|
||||
if ( $this -> apilo_order_id )
|
||||
{
|
||||
$access_token = \admin\factory\Integrations::apilo_get_access_token();
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt( $ch, CURLOPT_URL, "https://projectpro.apilo.com/rest/api/orders/" . $this -> apilo_order_id . '/status/' );
|
||||
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( [
|
||||
'id' => $this -> apilo_order_id,
|
||||
'status' => (int)\front\factory\ShopStatuses::get_apilo_status_id( $status )
|
||||
] ) );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
|
||||
"Authorization: Bearer " . $access_token,
|
||||
"Accept: application/json",
|
||||
"Content-Type: application/json"
|
||||
] );
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$apilo_result = curl_exec( $ch );
|
||||
|
||||
// put response to log
|
||||
if ( $config['debug']['apilo'] )
|
||||
file_put_contents( $_SERVER['DOCUMENT_ROOT'] . '/logs/apilo.txt', print_r( $apilo_result, true ) . "\n\n", FILE_APPEND );
|
||||
|
||||
curl_close( $ch );
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ponowne wysłanie maila o złożonym zamówieniu
|
||||
public function order_resend_confirmation_email()
|
||||
{
|
||||
global $settings;
|
||||
|
||||
$order = \front\factory\ShopOrder::order_details( $this -> id );
|
||||
|
||||
$mail_order = \Tpl::view( 'shop-order/mail-summary', [
|
||||
'settings' => $settings,
|
||||
'order' => $order
|
||||
] );
|
||||
|
||||
$settings[ 'ssl' ] ? $base = 'https' : $base = 'http';
|
||||
|
||||
$regex = "-(<img[^>]+src\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
|
||||
$mail_order = preg_replace( $regex, "$1" . $base . "://" . $_SERVER[ 'SERVER_NAME' ] . "$2$4", $mail_order );
|
||||
|
||||
$regex = "-(<a[^>]+href\s*=\s*['\"])(((?!'|\"|https?://).)*)(['\"][^>]*>)-i";
|
||||
$mail_order = preg_replace( $regex, "$1" . $base . "://" . $_SERVER[ 'SERVER_NAME' ] . "$2$4", $mail_order );
|
||||
|
||||
\S::send_email( $this -> client_email, \S::lang( 'potwierdzenie-zamowienia-ze-sklepu' ) . ' ' . $settings[ 'firm_name' ], $mail_order );
|
||||
\S::send_email( $settings[ 'contact_email' ], 'Nowe zamówienie / ' . $settings[ 'firm_name' ] . ' / ' . $order['number'] . ' - ' . $order['client_surname'] . ' ' . $order['client_name'], $mail_order );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// wysłanie maila o zmianie statusu
|
||||
public function send_status_change_email()
|
||||
{
|
||||
if ( !$this -> client_email )
|
||||
return false;
|
||||
|
||||
$order_statuses = self::order_statuses();
|
||||
|
||||
$firm_name = \front\factory\Settings::get_single_settings_value( 'firm_name' );
|
||||
|
||||
switch( $this -> status ):
|
||||
case 0:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało złożone' );
|
||||
break;
|
||||
case 1:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało opłacone' );
|
||||
break;
|
||||
case 2:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - płatność za zamówienie [NUMER] została odrzucona' );
|
||||
break;
|
||||
case 3:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - płatność za zamówienie [NUMER] jest sprawdzania ręcznie' );
|
||||
break;
|
||||
case 4:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało przyjęte do realizacji' );
|
||||
break;
|
||||
case 5:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało wysłane' );
|
||||
break;
|
||||
case 6:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało zrealizowane' );
|
||||
break;
|
||||
case 7:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało przygotowane go wysłania' );
|
||||
break;
|
||||
case 8:
|
||||
$subject = str_replace( '[NUMER]', $this -> number, $firm_name . ' - zamówienie [NUMER] zostało anulowane' );
|
||||
break;
|
||||
endswitch;
|
||||
|
||||
$email = new \Email( 0 );
|
||||
$email -> load_by_name( '#sklep-zmiana-statusu-zamowienia' );
|
||||
|
||||
$email -> text = str_replace( '[NUMER_ZAMOWIENIA]', $this -> number, $email -> text );
|
||||
$email -> text = str_replace( '[DATA_ZAMOWIENIA]', date( 'Y/m/d', strtotime( $this -> date_order ) ), $email -> text );
|
||||
$email -> text = str_replace( '[STATUS]', $order_statuses[ $this -> status ], $email -> text );
|
||||
|
||||
return $email -> send( $this -> client_email, $subject, true );
|
||||
}
|
||||
|
||||
// wyliczanie wartości zamówienia podczas edycji zamówienia
|
||||
static public function calculate_order_summary_by_admin( int $order_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$rows = $mdb -> select( 'pp_shop_order_products', '*', [ 'order_id' => $order_id ] );
|
||||
if ( \S::is_array_fix( $rows ) ) foreach ( $rows as $row )
|
||||
{
|
||||
if ( $row['price_brutto_promo'] )
|
||||
$summary += $row['price_brutto_promo'] * $row['quantity'];
|
||||
else
|
||||
$summary += $row['price_brutto'] * $row['quantity'];
|
||||
}
|
||||
|
||||
return $summary + $mdb -> get( 'pp_shop_orders', 'transport_cost', [ 'id' => $order_id ] );
|
||||
}
|
||||
|
||||
// ADMIN - usunięcie zamówienia
|
||||
static public function order_delete( int $order_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> delete( 'pp_shop_orders', [ 'id' => $order_id ] );
|
||||
}
|
||||
|
||||
// ADMIN - zmiana zamówienia
|
||||
static public function order_save_by_admin( int $order_id, string $client_name, string $client_surname, string $client_firm, string $client_street, string $client_postal_code, string $client_city, string $client_email,
|
||||
int $transport_id, string $inpost_paczkomat, int $payment_method_id )
|
||||
{
|
||||
global $mdb, $user;
|
||||
|
||||
$mdb -> update( 'pp_shop_orders', [
|
||||
'client_name' => $client_name,
|
||||
'client_surname' => $client_surname,
|
||||
'client_firm' => $client_firm,
|
||||
'client_street' => $client_street,
|
||||
'client_postal_code' => $client_postal_code,
|
||||
'client_city' => $client_city,
|
||||
'client_email' => $client_email,
|
||||
'transport_id' => $transport_id,
|
||||
'transport' => $mdb -> get( 'pp_shop_transports', 'name_visible', [ 'id' => $transport_id ] ),
|
||||
'transport_cost' => $mdb -> get( 'pp_shop_transports', 'cost', [ 'id' => $transport_id ] ),
|
||||
'transport_description' => $mdb -> get( 'pp_shop_transports', 'description', [ 'id' => $transport_id ] ),
|
||||
'inpost_paczkomat' => $inpost_paczkomat,
|
||||
'payment_method_id' => $payment_method_id,
|
||||
'payment_method' => $mdb -> get( 'pp_shop_payment_methods', 'name', [ 'id' => $payment_method_id ] ),
|
||||
], [
|
||||
'id' => $order_id
|
||||
] );
|
||||
|
||||
$mdb -> update( 'pp_shop_orders', [
|
||||
'summary' => \shop\Order::calculate_order_summary_by_admin( $order_id )
|
||||
], [
|
||||
'id' => $order_id
|
||||
] );
|
||||
|
||||
\Log::save_log( 'Zamówienie zmienione przez administratora | ID: ' . $order_id, $user['id'] );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
38
autoload/shop/class.PaymentMethod.php
Normal file
38
autoload/shop/class.PaymentMethod.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?
|
||||
namespace shop;
|
||||
class PaymentMethod implements \ArrayAccess
|
||||
{
|
||||
// lista dostępnych form płatności
|
||||
static public function method_list()
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> select( 'pp_shop_payment_methods', '*', [ 'status' => 1 ] );
|
||||
}
|
||||
|
||||
// get_apilo_payment_method_id
|
||||
static public function get_apilo_payment_method_id( $payment_method_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_payment_methods', 'apilo_payment_type_id', [ 'id' => $payment_method_id ] );
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
77
autoload/shop/class.Producer.php
Normal file
77
autoload/shop/class.Producer.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?
|
||||
namespace shop;
|
||||
class Producer implements \ArrayAccess
|
||||
{
|
||||
public function __construct( int $producer_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_producer', '*', [ 'id' => $producer_id ] );
|
||||
foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
|
||||
$rows = $mdb -> select( 'pp_shop_producer_lang', '*', [ 'producer_id' => $producer_id ] );
|
||||
foreach ( $rows as $row )
|
||||
{
|
||||
$languages[ $row['lang_id'] ]['description'] = $row['description'];
|
||||
$languages[ $row['lang_id'] ]['meta_title'] = $row['meta_title'];
|
||||
}
|
||||
|
||||
$this -> languages = $languages;
|
||||
}
|
||||
|
||||
static public function producer_products( $producer_id, $lang_id, $bs )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$count = $mdb -> count( 'pp_shop_products', [ 'AND' => [ 'producer_id' => $producer_id, 'status' => 1 ] ] );
|
||||
$ls = ceil( $count / 12 );
|
||||
|
||||
if ( $bs < 1 )
|
||||
$bs = 1;
|
||||
else if ( $bs > $ls )
|
||||
$bs = $ls;
|
||||
|
||||
$from = 12 * ( $bs - 1 );
|
||||
|
||||
if ( $from < 0 )
|
||||
$from = 0;
|
||||
|
||||
$results['products'] = $mdb -> select( 'pp_shop_products', 'id', [ 'AND' => [ 'producer_id' => $producer_id, 'status' => 1 ], 'LIMIT' => [ $from, 12 ] ] );
|
||||
|
||||
$results['ls'] = $ls;
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function __get( $variable )
|
||||
{
|
||||
if ( array_key_exists( $variable, $this -> data ) )
|
||||
return $this -> $variable;
|
||||
}
|
||||
|
||||
public function __set( $variable, $value )
|
||||
{
|
||||
$this -> $variable = $value;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
938
autoload/shop/class.Product.php
Normal file
938
autoload/shop/class.Product.php
Normal file
@@ -0,0 +1,938 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
use shop\Shop;
|
||||
use S;
|
||||
class Product implements \ArrayAccess
|
||||
{
|
||||
public function __construct( int $product_id, $lang_id = null, $permutation_hash = null )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$lang_id )
|
||||
$lang_id = \front\factory\Languages::default_language();
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_products', '*', [ 'id' => $product_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
|
||||
// kombinacja produktu
|
||||
if ( $this -> parent_id )
|
||||
{
|
||||
// pobranie wartości z produktu głównego
|
||||
if ( !$this -> price_netto or !$this -> price_brutto )
|
||||
{
|
||||
$result = $mdb -> get( 'pp_shop_products', [ 'price_netto', 'price_brutto', 'price_netto_promo', 'price_brutto_promo', 'vat', 'wp' ], [ 'id' => $this -> parent_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_products_langs', '*', [ 'AND' => [ 'product_id' => $this -> parent_id, 'lang_id' => $lang_id ] ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
{
|
||||
if ( $row[ 'copy_from' ] )
|
||||
{
|
||||
$results2 = $mdb -> select( 'pp_shop_products_langs', '*', [ 'AND' => [ 'product_id' => $this -> parent_id, 'lang_id' => $row[ 'copy_from' ] ] ] );
|
||||
if ( is_array( $results2 ) )
|
||||
foreach ( $results2 as $row2 )
|
||||
$this -> language = $row2;
|
||||
}
|
||||
else
|
||||
$this -> language = $row;
|
||||
}
|
||||
|
||||
$this -> images = $mdb -> select( 'pp_shop_products_images', '*', [ 'product_id' => $this -> parent_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] );
|
||||
$this -> files = $mdb -> select( 'pp_shop_products_files', '*', [ 'product_id' => $this -> parent_id] );
|
||||
$this -> categories = $mdb -> select( 'pp_shop_products_categories', 'category_id', [ 'product_id' => $this -> parent_id ] );
|
||||
|
||||
$this -> products_related = $mdb -> select( 'pp_shop_products_related', 'product_related_id', [ 'product_id' => $this -> parent_id ] );
|
||||
$this -> products_sets = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'AND' => [ 'set_id' => $this -> set_id, 'product_id[!]' => $this -> parent_id ] ] );
|
||||
}
|
||||
else
|
||||
{
|
||||
$results = $mdb -> select( 'pp_shop_products_langs', '*', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $lang_id ] ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
{
|
||||
if ( $row[ 'copy_from' ] )
|
||||
{
|
||||
$results2 = $mdb -> select( 'pp_shop_products_langs', '*', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $row[ 'copy_from' ] ] ] );
|
||||
if ( is_array( $results2 ) )
|
||||
foreach ( $results2 as $row2 )
|
||||
$this -> language = $row2;
|
||||
}
|
||||
else
|
||||
$this -> language = $row;
|
||||
}
|
||||
|
||||
$this -> images = $mdb -> select( 'pp_shop_products_images', '*', [ 'product_id' => $product_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] );
|
||||
$this -> files = $mdb -> select( 'pp_shop_products_files', '*', [ 'product_id' => $product_id ] );
|
||||
$this -> categories = $mdb -> select( 'pp_shop_products_categories', 'category_id', [ 'product_id' => $product_id ] );
|
||||
|
||||
$this -> products_related = $mdb -> select( 'pp_shop_products_related', 'product_related_id', [ 'product_id' => $product_id ] );
|
||||
$this -> products_sets = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'AND' => [ 'set_id' => $this -> set_id, 'product_id[!]' => $product_id ] ] );
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_products', 'id', [ 'parent_id' => $product_id ] );
|
||||
if ( \S::is_array_fix( $results ) )
|
||||
{
|
||||
foreach ( $results as $row )
|
||||
$product_combinations[] = \shop\Product::getFromCache( $row, $lang_id );
|
||||
|
||||
$this -> product_combinations = $product_combinations;
|
||||
}
|
||||
}
|
||||
|
||||
$this -> producer = $mdb -> get( 'pp_shop_producer', '*', [ 'id' => (int) $this -> producer_id ] );
|
||||
|
||||
if ( $permutation_hash )
|
||||
{
|
||||
$permutation_price = $mdb -> get( 'pp_shop_products', [ 'price_netto', 'price_brutto', 'price_netto_promo', 'price_brutto_promo' ], [ 'AND' => [ 'permutation_hash' => $permutation_hash, 'parent_id' => $product_id ] ] );
|
||||
if ( is_array( $permutation_price ) )
|
||||
{
|
||||
if ( $permutation_price[ 'price_netto' ] != null )
|
||||
$this -> price_netto = $permutation_price[ 'price_netto' ];
|
||||
|
||||
if ( $permutation_price[ 'price_brutto' ] != null )
|
||||
$this -> price_brutto = $permutation_price[ 'price_brutto' ];
|
||||
|
||||
if ( $permutation_price[ 'price_netto_promo' ] != null )
|
||||
$this -> price_netto_promo = $permutation_price[ 'price_netto_promo' ];
|
||||
|
||||
if ( $permutation_price[ 'price_brutto_promo' ] != null )
|
||||
$this -> price_brutto_promo = $permutation_price[ 'price_brutto_promo' ];
|
||||
}
|
||||
}
|
||||
|
||||
$this -> custom_fields = $mdb -> select( 'pp_shop_products_custom_fields', '*', [ 'id_product' => $product_id ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a product object from cache or creates a new instance if not found.
|
||||
*
|
||||
* @param int $product_id The ID of the product.
|
||||
* @param int $lang_id The ID of the language.
|
||||
* @param string $permutation_hash The permutation hash of the product.
|
||||
* @return \shop\Product The product object.
|
||||
*/
|
||||
public static function getFromCache( $product_id, $lang_id, $permutation_hash = null )
|
||||
{
|
||||
// Check if Redis extension is loaded
|
||||
if ( class_exists( 'Redis' ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the Redis connection instance
|
||||
$redis = \RedisConnection::getInstance() -> getConnection();
|
||||
|
||||
// Try to retrieve the serialized product object from cache
|
||||
$objectData = $redis -> get( "shop\product:$product_id:$lang_id:$permutation_hash" );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
// Product not found in cache, create a new instance and store it in cache
|
||||
$object = new self( $product_id, $lang_id, $permutation_hash );
|
||||
$redis->setex( "shop\product:$product_id:$lang_id:$permutation_hash", 60 * 60 * 24, serialize( $object ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Product found in cache, unserialize it
|
||||
$object = unserialize( $objectData );
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
// Log the exception if needed
|
||||
$object = new self( $product_id, $lang_id, $permutation_hash );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Redis extension not loaded, create a new instance
|
||||
$object = new self( $product_id, $lang_id, $permutation_hash );
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function getDefaultCombinationPrices()
|
||||
{
|
||||
$permutation_hash = '';
|
||||
|
||||
$attributes = \shop\Product::get_product_attributes( $this -> product_combinations );
|
||||
foreach ( $attributes as $attribute )
|
||||
{
|
||||
foreach ( $attribute['values'] as $value )
|
||||
{
|
||||
if ( $value['is_default'] )
|
||||
{
|
||||
if ( $permutation_hash )
|
||||
$permutation_hash .= '|';
|
||||
$permutation_hash .= $attribute['id'] . '-' . $value['id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $this -> product_combinations as $product_combination )
|
||||
{
|
||||
if ( $product_combination -> permutation_hash == $permutation_hash )
|
||||
{
|
||||
$prices['price_netto'] = $product_combination -> price_netto;
|
||||
$prices['price_brutto'] = $product_combination -> price_brutto;
|
||||
$prices['price_netto_promo'] = $product_combination -> price_netto_promo;
|
||||
$prices['price_brutto_promo'] = $product_combination -> price_brutto_promo;
|
||||
}
|
||||
}
|
||||
|
||||
return $prices;
|
||||
}
|
||||
|
||||
public function generateSubtitleFromAttributes( $permutation_hash )
|
||||
{
|
||||
global $lang_id;
|
||||
|
||||
$subtitle = '';
|
||||
|
||||
$attributes = explode( '|', $permutation_hash );
|
||||
foreach ( $attributes as $attribute )
|
||||
{
|
||||
$attribute = explode( '-', $attribute );
|
||||
|
||||
if ( $subtitle )
|
||||
$subtitle .= ', ';
|
||||
|
||||
$subtitle .= \shop\ProductAttribute::getAttributeName( $attribute[0], $lang_id ) . ': ' . \shop\ProductAttribute::get_value_name( $attribute[1], $lang_id );
|
||||
}
|
||||
|
||||
return $subtitle;
|
||||
}
|
||||
|
||||
public function getProductDataBySelectedAttributes( $selected_attribute )
|
||||
{
|
||||
global $settings;
|
||||
|
||||
foreach ( $this -> product_combinations as $product_combination )
|
||||
{
|
||||
if ( $product_combination -> permutation_hash == $selected_attribute )
|
||||
{
|
||||
if ( $product_combination -> quantity !== null or $product_combination -> stock_0_buy )
|
||||
{
|
||||
$result['quantity'] = $product_combination -> quantity;
|
||||
$result['stock_0_buy'] = $product_combination -> stock_0_buy;
|
||||
$result['price_netto'] = Shop::shortPrice( $product_combination -> price_netto );
|
||||
$result['price_brutto'] = Shop::shortPrice( $product_combination -> price_brutto );
|
||||
$result['price_netto_promo'] = $product_combination -> price_netto_promo ? Shop::shortPrice( $product_combination -> price_netto_promo ) : null;
|
||||
$result['price_brutto_promo'] = $product_combination -> price_brutto_promo ? Shop::shortPrice( $product_combination -> price_brutto_promo ) : null;
|
||||
}
|
||||
else
|
||||
{
|
||||
$result['quantity'] = $this -> quantity;
|
||||
$result['stock_0_buy'] = $this -> stock_0_buy;
|
||||
$result['price_netto'] = Shop::shortPrice( $this -> price_netto );
|
||||
$result['price_brutto'] = Shop::shortPrice( $this -> price_brutto );
|
||||
$result['price_netto_promo'] = $this -> price_netto_promo ? Shop::shortPrice( $this -> price_netto_promo ) : null;
|
||||
$result['price_brutto_promo'] = $this -> price_brutto_promo ? Shop::shortPrice( $this -> price_brutto_promo ) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
$result['messages']['warehouse_message_zero'] = $this -> language['warehouse_message_zero'] ? $this -> language['warehouse_message_zero'] : $settings['warehouse_message_zero_pl'];
|
||||
$result['messages']['warehouse_message_nonzero'] = $this -> language['warehouse_message_nonzero'] ? $this -> language['warehouse_message_nonzero'] : $settings['warehouse_message_nonzero_pl'];
|
||||
$result['permutation_hash'] = $selected_attribute;
|
||||
return $result;
|
||||
}
|
||||
|
||||
// sprawdź czy produkt jest na promocji
|
||||
static public function is_product_on_promotion( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( $mdb -> get( 'pp_shop_products', 'price_netto_promo', [ 'id' => $product_id ] ) != null )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// pobierz id produktu z Baselinker
|
||||
static public function get_baselinker_product_id( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_products', [ 'parent_id', 'baselinker_product_id' ], [ 'id' => $product_id ] );
|
||||
if ( $result['baselinker_product_id'] )
|
||||
return $result['baselinker_product_id'];
|
||||
else
|
||||
return $mdb -> get( 'pp_shop_products', 'baselinker_product_id', [ 'id' => $result['parent_id'] ] );
|
||||
}
|
||||
|
||||
// pobierz kod SKU
|
||||
static public function get_product_sku( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_products', [ 'parent_id', 'sku' ], [ 'id' => $product_id ] );
|
||||
if ( $result['sku'] )
|
||||
return $result['sku'];
|
||||
else
|
||||
return $mdb -> get( 'pp_shop_products', 'sku', [ 'id' => $result['parent_id'] ] );
|
||||
}
|
||||
|
||||
// pobierz cenę produktu
|
||||
static public function get_product_price( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$prices = $mdb -> get( 'pp_shop_products', [ 'price_brutto', 'price_brutto_promo' ], [ 'id' => $product_id ] );
|
||||
|
||||
if ( $prices['price_brutto_promo'] != '' and $prices['price_brutto_promo'] < $prices['price_brutto'] )
|
||||
return $prices['price_brutto_promo'];
|
||||
else
|
||||
return $prices['price_brutto'];
|
||||
}
|
||||
|
||||
// pobierz nazwę produktu
|
||||
static public function get_product_name( int $product_id, string $lang_id = null )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$lang_id )
|
||||
$lang_id = \front\factory\Languages::default_language();
|
||||
|
||||
return $mdb -> get( 'pp_shop_products_langs', 'name', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $lang_id ] ] );
|
||||
}
|
||||
|
||||
// pobierz i wyświetl produktu do zestawu po dodaniu do koszyka
|
||||
static public function product_sets_when_add_to_basket( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\Product::product_sets_when_add_to_basket:$product_id";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
if ( $product_id_tmp = $mdb -> get( 'pp_shop_products', 'parent_id', [ 'id' => $product_id ] ) )
|
||||
$set_id = $mdb -> get( 'pp_shop_products', 'set_id', [ 'id' => $product_id_tmp ] );
|
||||
else
|
||||
$set_id = $mdb -> get( 'pp_shop_products', 'set_id', [ 'id' => $product_id ] );
|
||||
|
||||
$products = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'set_id' => $set_id ] );
|
||||
if ( !$products )
|
||||
{
|
||||
$products_intersection = $mdb -> select( 'pp_shop_orders_products_intersection', [ 'product_1_id', 'product_2_id' ], [ 'OR' => [ 'product_1_id' => $product_id, 'product_2_id' => $product_id ], 'ORDER' => [ 'count' => 'DESC' ], 'LIMIT' => 5 ] );
|
||||
if ( !count( $products_intersection ) )
|
||||
{
|
||||
$product_id = $mdb -> get( 'pp_shop_products', 'parent_id', [ 'id' => $product_id ] );
|
||||
$products_intersection = $mdb -> select( 'pp_shop_orders_products_intersection', [ 'product_1_id', 'product_2_id' ], [ 'OR' => [ 'product_1_id' => $product_id, 'product_2_id' => $product_id ], 'ORDER' => [ 'count' => 'DESC' ], 'LIMIT' => 5 ] );
|
||||
}
|
||||
|
||||
foreach ( $products_intersection as $product_intersection )
|
||||
{
|
||||
if ( $product_intersection['product_1_id'] != $product_id )
|
||||
$products[] = $product_intersection['product_1_id'];
|
||||
else
|
||||
$products[] = $product_intersection['product_2_id'];
|
||||
}
|
||||
array_unique( $products );
|
||||
}
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $products );
|
||||
}
|
||||
else
|
||||
{
|
||||
$products = unserialize( $objectData );
|
||||
}
|
||||
|
||||
foreach ( $products as $product_id )
|
||||
{
|
||||
if ( !\front\factory\ShopProduct::is_product_active( $product_id ) )
|
||||
$products = array_diff( $products, [ $product_id ] );
|
||||
}
|
||||
|
||||
return \Tpl::view( 'shop-basket/alert-product-sets', [
|
||||
'products' => $products
|
||||
] );
|
||||
}
|
||||
|
||||
// dodaje 1 do licznika odwiedziń produktu
|
||||
static public function add_visit( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_shop_products', [ 'visits[+]' => 1 ], [ 'id' => $product_id ] );
|
||||
}
|
||||
|
||||
//FIX:ME - do poprawy nazwa
|
||||
// pobierz zdjęcie główne
|
||||
static public function getProductImg( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_products_images', 'src', [ 'product_id' => $product_id, 'ORDER' => [ 'o' => 'ASC' ] ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
return $row;
|
||||
}
|
||||
|
||||
//FIX:ME - do poprawy nazwa
|
||||
// pobierz bezpośredni url produktu
|
||||
static public function getProductUrl( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$lang_id = \front\factory\Languages::default_language();
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_products_langs', '*', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $lang_id ] ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
{
|
||||
if ( $row[ 'copy_from' ] )
|
||||
{
|
||||
$results2 = $mdb -> select( 'pp_shop_products_langs', '*', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $row[ 'copy_from' ] ] ] );
|
||||
if ( is_array( $results2 ) )
|
||||
foreach ( $results2 as $row2 )
|
||||
$language = $row2;
|
||||
}
|
||||
else
|
||||
$language = $row;
|
||||
}
|
||||
|
||||
$language['seo_link'] ? $url = '/' . $language['seo_link'] : $url = '/p-' . $product_id . '-' . \S::seo( $language['name'] );
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
// pobierz ilość wyników na podstawie wyszukiwanej nazwy
|
||||
static public function searchProductsByNameCount( $query, $lang_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$results = $mdb -> query( 'SELECT COUNT(0) AS c FROM ( '
|
||||
. 'SELECT psp.id, '
|
||||
. '( CASE '
|
||||
. 'WHEN copy_from IS NULL THEN name '
|
||||
. 'WHEN copy_from IS NOT NULL THEN ( '
|
||||
. 'SELECT '
|
||||
. 'name '
|
||||
. 'FROM '
|
||||
. 'pp_shop_products_langs '
|
||||
. 'WHERE '
|
||||
. 'lang_id = pspl.copy_from AND product_id = psp.id '
|
||||
. ') '
|
||||
. 'END ) AS name '
|
||||
. 'FROM '
|
||||
. 'pp_shop_products AS psp '
|
||||
. 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = psp.id '
|
||||
. 'WHERE '
|
||||
. 'status = 1 AND name LIKE :query AND lang_id = :lang_id '
|
||||
. ') AS q1', [
|
||||
':query' => '%' . $query . '%',
|
||||
':lang_id' => $lang_id
|
||||
] ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
return $results[0]['c'];
|
||||
}
|
||||
|
||||
// pobierz id produktów na podstawie wyszukiwanej nazwy
|
||||
static public function getProductsIdByName( string $query, string $lang_id, int $products_limit, int $from )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$results = $mdb -> query( 'SELECT '
|
||||
. 'psp.id, '
|
||||
. '( CASE '
|
||||
. 'WHEN copy_from IS NULL THEN name '
|
||||
. 'WHEN copy_from IS NOT NULL THEN ( '
|
||||
. 'SELECT '
|
||||
. 'name '
|
||||
. 'FROM '
|
||||
. 'pp_shop_products_langs '
|
||||
. 'WHERE '
|
||||
. 'lang_id = pspl.copy_from AND product_id = psp.id '
|
||||
. ') '
|
||||
. 'END ) AS name '
|
||||
. 'FROM '
|
||||
. 'pp_shop_products AS psp '
|
||||
. 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = psp.id '
|
||||
. 'WHERE '
|
||||
. 'status = 1 AND name LIKE :query AND lang_id = :lang_id '
|
||||
. 'ORDER BY '
|
||||
. 'name ASC '
|
||||
. 'LIMIT '
|
||||
. $from . ',' . $products_limit, [
|
||||
':query' => '%' . $query . '%',
|
||||
':lang_id' => $lang_id
|
||||
] ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
|
||||
$output[] = $row['id'];
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
static public function searchProductsByName( string $query, string $lang_id, int $bs = 0 )
|
||||
{
|
||||
$count = \shop\Product::searchProductsByNameCount( $query, $lang_id );
|
||||
$ls = ceil( $count / 12 );
|
||||
|
||||
if ( $bs < 1 )
|
||||
$bs = 1;
|
||||
else if ( $bs > $ls )
|
||||
$bs = $ls;
|
||||
|
||||
$from = 12 * ( $bs - 1 );
|
||||
|
||||
if ( $from < 0 )
|
||||
$from = 0;
|
||||
|
||||
$results['products'] = \shop\Product::getProductsIdByName( $query, $lang_id, 12, $from );
|
||||
$results['count'] = $count;
|
||||
$results['ls'] = $ls;
|
||||
|
||||
return $results;
|
||||
|
||||
}
|
||||
|
||||
static public function searchProductByNameAjax( string $query, string $lang_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> query(
|
||||
'SELECT
|
||||
product_id
|
||||
FROM
|
||||
pp_shop_products_langs AS pspl
|
||||
INNER JOIN pp_shop_products AS psp ON psp.id = pspl.product_id
|
||||
WHERE
|
||||
status = 1 AND lang_id = :lang_id AND LOWER(name) LIKE :query
|
||||
ORDER BY visits DESC
|
||||
LIMIT 12', [
|
||||
':query' => '%' . $query . '%',
|
||||
':lang_id' => $lang_id
|
||||
] ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
}
|
||||
|
||||
public function permutations()
|
||||
{
|
||||
if ( is_array( $this -> attributes ) ) foreach ( $this -> attributes as $attribute )
|
||||
{
|
||||
$attribute_obj = new \shop\ProductAttribute( $attribute['attribute_id'] );
|
||||
|
||||
if ( $attribute_obj['stock_influences'] )
|
||||
$attributes_tmp[ $attribute[ 'attribute_id' ] ][] = $attribute[ 'value_id' ];
|
||||
}
|
||||
|
||||
if ( is_array( $attributes_tmp ) )
|
||||
return $this -> permutations = self::array_cartesian( $attributes_tmp );
|
||||
}
|
||||
|
||||
/// sprawdź czy produkt można zamawiać przy stanie magazynowym zero
|
||||
static public function is_stock_0_buy( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
// jeżeli jest to kombinacja produktu
|
||||
if ( $parent_id = $mdb -> get( 'pp_shop_products', 'parent_id', [ 'id' => $product_id ] ) )
|
||||
return $mdb -> get( 'pp_shop_products', 'stock_0_buy', [ 'id' => $parent_id ] );
|
||||
else
|
||||
return $mdb -> get( 'pp_shop_products', 'stock_0_buy', [ 'id' => $product_id ] );
|
||||
}
|
||||
|
||||
// pobierz stan magazynowy i komunikaty dla kombinacji produktu
|
||||
static public function get_product_permutation_quantity_options( int $product_id, $permutation )
|
||||
{
|
||||
global $mdb, $settings;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\Product::get_product_permutation_quantity_options:$product_id:$permutation";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
if ( $mdb -> count( 'pp_shop_products', [ 'AND' => [ 'parent_id' => $product_id, 'permutation_hash' => $permutation ] ] ) )
|
||||
{
|
||||
$result['quantity'] = $mdb -> get( 'pp_shop_products', 'quantity', [ 'AND' => [ 'parent_id' => $product_id, 'permutation_hash' => $permutation ] ] );
|
||||
if ( $result['quantity'] == null )
|
||||
{
|
||||
$result['quantity'] = $mdb -> get( 'pp_shop_products', 'quantity', [ 'id' => $product_id ] );
|
||||
$result['stock_0_buy'] = $mdb -> get( 'pp_shop_products', 'stock_0_buy', [ 'id' => $product_id] );
|
||||
$result['messages'] = $mdb -> get( 'pp_shop_products_langs', [ 'warehouse_message_zero', 'warehouse_message_nonzero' ], [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => 'pl' ] ] );
|
||||
|
||||
if ( !$result['messages']['warehouse_message_zero'] )
|
||||
$result['messages']['warehouse_message_zero'] = $settings['warehouse_message_zero_pl'];
|
||||
|
||||
if ( !$result['messages']['warehouse_message_nonzero'] )
|
||||
$result['messages']['warehouse_message_nonzero'] = $settings['warehouse_message_nonzero_pl'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$result['stock_0_buy'] = $mdb -> get( 'pp_shop_products', 'stock_0_buy', [ 'AND' => [ 'parent_id' => $product_id, 'permutation_hash' => $permutation ] ] );
|
||||
$result['messages'] = $mdb -> get( 'pp_shop_products_langs', [ 'warehouse_message_zero', 'warehouse_message_nonzero' ], [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => 'pl' ] ] );
|
||||
|
||||
if ( !$result['messages']['warehouse_message_zero'] )
|
||||
$result['messages']['warehouse_message_zero'] = $settings['warehouse_message_zero_pl'];
|
||||
|
||||
if ( !$result['messages']['warehouse_message_nonzero'] )
|
||||
$result['messages']['warehouse_message_nonzero'] = $settings['warehouse_message_nonzero_pl'];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result['quantity'] = $mdb -> get( 'pp_shop_products', 'quantity', [ 'id' => $product_id ] );
|
||||
$result['stock_0_buy'] = $mdb -> get( 'pp_shop_products', 'stock_0_buy', [ 'id' => $product_id] );
|
||||
$result['messages'] = $mdb -> get( 'pp_shop_products_langs', [ 'warehouse_message_zero', 'warehouse_message_nonzero' ], [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => 'pl' ] ] );
|
||||
|
||||
if ( !$result['messages']['warehouse_message_zero'] )
|
||||
$result['messages']['warehouse_message_zero'] = $settings['warehouse_message_zero_pl'];
|
||||
|
||||
if ( !$result['messages']['warehouse_message_nonzero'] )
|
||||
$result['messages']['warehouse_message_nonzero'] = $settings['warehouse_message_nonzero_pl'];
|
||||
}
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $result );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// pobierz stan magazynowy produktu
|
||||
static public function get_product_quantity( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_products', 'quantity', [ 'id' => $product_id ] );
|
||||
}
|
||||
|
||||
public static function product_categories( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> select( 'pp_shop_products_categories', 'category_id', [ 'product_id' => $product_id ] );
|
||||
}
|
||||
|
||||
public static function calculate_basket_product_price( float $price_brutto_promo, float $price_brutto, $coupon, $basket_position )
|
||||
{
|
||||
// ? produkty przecenione
|
||||
if ( $price_brutto_promo )
|
||||
{
|
||||
$price['price'] = $price_brutto;
|
||||
$price['price_new'] = $price_brutto_promo;
|
||||
|
||||
// ? zastosuje kod rabatowy
|
||||
if ( $coupon -> type && $coupon -> include_discounted_product )
|
||||
{
|
||||
// ? ograniczony do wybranych kategorii
|
||||
if ( $coupon -> categories != null )
|
||||
{
|
||||
$coupon_categories = json_decode( $coupon -> categories );
|
||||
$product_categories = \shop\Product::product_categories( (int)$basket_position['parent_id'] ? (int)$basket_position['parent_id'] : (int)$basket_position['product-id'] );
|
||||
if ( is_array( $coupon_categories ) ) foreach ( $coupon_categories as $category_tmp )
|
||||
{
|
||||
if ( in_array( $category_tmp, $product_categories ) )
|
||||
{
|
||||
$price['price_new'] = \S::normalize_decimal( $price['price_new'] - $price['price_new'] * $coupon -> amount / 100 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// ? nieograniczony kategoriami
|
||||
else
|
||||
$price['price_new'] = \S::normalize_decimal( $price['price_new'] - $price['price_new'] * $coupon -> amount / 100 );
|
||||
|
||||
// ? uwzględnij promocję jeżeli może łączyć się z rabatami i produktami przecenionymi
|
||||
if ( $basket_position['discount_amount'] && $basket_position['discount_include_coupon'] && $basket_position['include_product_promo'] )
|
||||
{
|
||||
// ? najtańszy produkt w koszyku (z wybranych kategorii) za X zł
|
||||
if ( $basket_position['discount_type'] == 3 )
|
||||
$price['price_new'] = \S::normalize_decimal( $basket_position['discount_amount'] );
|
||||
|
||||
// ? rabat procentowy
|
||||
if ( $basket_position['discount_type'] == 1 )
|
||||
$price['price_new'] = \S::normalize_decimal($price['price_new'] - $price['price_new'] * $basket_position['discount_amount'] / 100 );
|
||||
}
|
||||
}
|
||||
// ? brak kodu rabatowego
|
||||
else
|
||||
{
|
||||
if ( $basket_position['discount_amount'] && $basket_position['include_product_promo'] )
|
||||
{
|
||||
// ? Najtańszy produkt w koszyku (z wybranych kategorii) za X zł
|
||||
if ( $basket_position['discount_type'] == 3 )
|
||||
$price['price_new'] = \S::normalize_decimal( $basket_position['discount_amount'] );
|
||||
|
||||
// ? rabat procentowy
|
||||
if ( $basket_position['discount_type'] == 1 )
|
||||
$price['price_new'] = \S::normalize_decimal($price['price_new'] - $price['price_new'] * $basket_position['discount_amount'] / 100 );
|
||||
}
|
||||
}
|
||||
}
|
||||
// ? produkt nieprzeceniony
|
||||
else
|
||||
{
|
||||
$price['price'] = $price_brutto;
|
||||
$price['price_new'] = $price_brutto;
|
||||
|
||||
// ? zastosuj kod rabatowy
|
||||
if ( $coupon -> type )
|
||||
{
|
||||
// ? ograniczony do wybranych kategorii
|
||||
if ( $coupon -> categories != null )
|
||||
{
|
||||
$coupon_categories = json_decode( $coupon -> categories );
|
||||
$product_categories = \shop\Product::product_categories( $basket_position['parent_id'] ? $basket_position['parent_id'] : $basket_position['product-id'] );
|
||||
if ( is_array( $coupon_categories ) ) foreach ( $coupon_categories as $category_tmp )
|
||||
{
|
||||
if ( in_array( $category_tmp, $product_categories ) )
|
||||
{
|
||||
$price['price_new'] = \S::normalize_decimal( $price['price_new'] - $price['price_new'] * $coupon -> amount / 100 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// ? nieograniczony
|
||||
else
|
||||
$price['price_new'] = \S::normalize_decimal($price['price'] - $price['price'] * $coupon -> amount / 100 );
|
||||
|
||||
// ? uwzględnij promocję jeżeli może łączyć się z rabatami i produktami przecenionymi
|
||||
if ( $basket_position['discount_amount'] && $basket_position['discount_include_coupon'] && $basket_position['include_product_promo'] )
|
||||
{
|
||||
// ? najtańszy produkt w koszyku (z wybranych kategorii) za X zł
|
||||
if ( $basket_position['discount_type'] == 3 )
|
||||
$price['price_new'] = \S::normalize_decimal( $basket_position['discount_amount'] );
|
||||
|
||||
// ? rabat procentowy
|
||||
if ( $basket_position['discount_type'] == 1 )
|
||||
$price['price_new'] = \S::normalize_decimal($price['price'] - $price['price'] * $basket_position['discount_amount'] / 100 );
|
||||
}
|
||||
}
|
||||
// ? bez kodu rabatowego
|
||||
else
|
||||
{
|
||||
if ( $basket_position['discount_amount'] )
|
||||
{
|
||||
// ? Najtańszy produkt w koszyku (z wybranych kategorii) za X zł
|
||||
if ( $basket_position['discount_type'] == 3 )
|
||||
$price['price_new'] = \S::normalize_decimal( $basket_position['discount_amount'] );
|
||||
|
||||
// ? rabat procentowy
|
||||
if ( $basket_position['discount_type'] == 1 )
|
||||
$price['price_new'] = \S::normalize_decimal($price['price'] - $price['price'] * $basket_position['discount_amount'] / 100 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $price;
|
||||
}
|
||||
|
||||
// pobierz wiadomość zależną od stanu magazynowego
|
||||
// id_product - id produktu
|
||||
// attributes - wybrane atrybuty przez użytkownika
|
||||
public static function getWarehouseMessage( int $id_product, $attributes, string $lang_id )
|
||||
{
|
||||
global $settings;
|
||||
|
||||
$permutation = self::getPermutation( $attributes );
|
||||
|
||||
$quantity = self::getPermutationQuantity( $id_product, $permutation );
|
||||
|
||||
if ( $quantity )
|
||||
{
|
||||
if ( $msg = \front\factory\ShopProduct::warehouse_message_nonzero( $id_product, $lang_id ) )
|
||||
$result = [ 'msg' => $msg, 'quantity' => $quantity ];
|
||||
else if ( $settings[ 'warehouse_message_nonzero_' . $lang_id ] )
|
||||
$result = [ 'msg' => $settings[ 'warehouse_message_nonzero_' . $lang_id ], 'quantity' => $quantity ];
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( $msg = \front\factory\ShopProduct::warehouse_message_zero( $id_product, $lang_id ) )
|
||||
$result = [ 'msg' => $msg, 'quantity' => $quantity ];
|
||||
else if ( $settings[ 'warehouse_message_zero_' . $lang_id ] )
|
||||
$result = [ 'msg' => $settings[ 'warehouse_message_zero_' . $lang_id ], 'quantity' => $quantity ];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
// pobranie id produktu wg wybranych parametrów
|
||||
static public function get_product_id_by_attributes( int $parent_id, array $attributes )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
return $mdb -> get( 'pp_shop_products', 'id', [ 'AND' => [ 'parent_id' => $parent_id, 'permutation_hash' => implode( '|', $attributes ) ] ] );
|
||||
}
|
||||
|
||||
// pobranie listy atrybutów z wybranymi wartościami
|
||||
static public function get_product_attributes( $products )
|
||||
{
|
||||
if ( !is_array( $products ) or !count( $products ) )
|
||||
return false;
|
||||
|
||||
$attributes = array();
|
||||
|
||||
foreach ( $products as $product )
|
||||
{
|
||||
$permutations = explode( '|', $product['permutation_hash'] );
|
||||
foreach ( $permutations as $permutation )
|
||||
{
|
||||
$attribute = explode( '-', $permutation );
|
||||
|
||||
$value['id'] = $attribute[1];
|
||||
$value['is_default'] = \shop\ProductAttribute::is_value_default( $attribute[1] );
|
||||
|
||||
if ( array_search( $attribute[1], array_column( $attributes, 'id' ) ) === false )
|
||||
$attributes[ $attribute[0] ][] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = S::removeDuplicates( $attributes, 'id' );
|
||||
|
||||
foreach ( $attributes as $key => $val )
|
||||
{
|
||||
$row['id'] = $key;
|
||||
$row['values'] = $val;
|
||||
|
||||
$attributes_sort[ \shop\ProductAttribute::get_attribute_order( $key ) ] = $row;
|
||||
}
|
||||
|
||||
return $attributes_sort;
|
||||
}
|
||||
|
||||
public static function array_cartesian( $input )
|
||||
{
|
||||
$result = array();
|
||||
|
||||
while ( list($key, $values) = each( $input ) )
|
||||
{
|
||||
if ( empty( $values ) )
|
||||
continue;
|
||||
|
||||
if ( empty( $result ) )
|
||||
{
|
||||
foreach ( $values as $value )
|
||||
{
|
||||
$result[] = array( $key => $value );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$append = array();
|
||||
|
||||
foreach ( $result as &$product )
|
||||
{
|
||||
$product[$key] = array_shift( $values );
|
||||
$copy = $product;
|
||||
|
||||
foreach ( $values as $item )
|
||||
{
|
||||
$copy[$key] = $item;
|
||||
$append[] = $copy;
|
||||
}
|
||||
|
||||
array_unshift( $values, $product[$key] );
|
||||
}
|
||||
|
||||
$result = array_merge( $result, $append );
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __get( $variable )
|
||||
{
|
||||
if ( array_key_exists( $variable, $this -> data ) )
|
||||
return $this -> $variable;
|
||||
}
|
||||
|
||||
public function __set( $variable, $value )
|
||||
{
|
||||
$this -> $variable = $value;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
|
||||
// generate_sku_code
|
||||
public static function generate_sku_code( int $product_id )
|
||||
{
|
||||
global $mdb;
|
||||
// find product with sku like 'PP-000000'
|
||||
$skus = $mdb -> select( 'pp_shop_products', 'sku', [ 'sku[~]' => 'PP-' ] );
|
||||
if ( is_array( $skus ) )
|
||||
{
|
||||
foreach ( $skus as $sku )
|
||||
$sku_codes[] = (int)substr( $sku, 3 );
|
||||
}
|
||||
|
||||
// find max sku
|
||||
if ( is_array( $sku_codes ) )
|
||||
$sku = 'PP-' . str_pad( max( $sku_codes ) + 1, 6, '0', STR_PAD_LEFT );
|
||||
else
|
||||
$sku = 'PP-000001';
|
||||
|
||||
return $sku;
|
||||
}
|
||||
|
||||
// product_xml_name_save
|
||||
public static function product_xml_name_save( int $product_id, string $xml_name, string $lang_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_shop_products_langs', [ 'xml_name' => $xml_name ], [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $lang_id ] ] );
|
||||
}
|
||||
|
||||
// product_custom_label_suggestions
|
||||
public static function product_custom_label_suggestions( string $custom_label, string $label_type )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$results = $mdb -> query( 'SELECT DISTINCT
|
||||
' . $label_type . ' AS label
|
||||
FROM
|
||||
pp_shop_products
|
||||
WHERE
|
||||
' . $label_type . ' LIKE :custom_label
|
||||
LIMIT 10', [ ':custom_label' => '%' . $custom_label . '%' ] ) -> fetchAll( \PDO::FETCH_ASSOC );
|
||||
if ( is_array( $results ) )
|
||||
{
|
||||
foreach ( $results as $row )
|
||||
$output[] = $row;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
// product_custom_label_save
|
||||
public static function product_custom_label_save( int $product_id, string $custom_label, string $label_type )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> update( 'pp_shop_products', [ $label_type => $custom_label ], [ 'id' => $product_id ] );
|
||||
}
|
||||
public static function product_meta (int $product_id)
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb->select(
|
||||
"pp_shop_products_categories (ppc)",
|
||||
[
|
||||
"[>]pp_shop_categories_langs (pcl)" => ["ppc.category_id" => "category_id"]
|
||||
],
|
||||
[
|
||||
"pcl.title",
|
||||
"pcl.seo_link"
|
||||
],
|
||||
[
|
||||
"ppc.product_id" => $product_id
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
119
autoload/shop/class.ProductAttribute.php
Normal file
119
autoload/shop/class.ProductAttribute.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class ProductAttribute implements \ArrayAccess
|
||||
{
|
||||
// sprawdź czy wartość atrybutu jest domyślna
|
||||
static public function is_value_default( int $value_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\ProductAttribute::is_value_default:$value_id";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$is_default = $mdb -> get( 'pp_shop_attributes_values', 'is_default', [ 'id' => $value_id ] );
|
||||
$cacheHandler -> set( $cacheKey, $is_default );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
}
|
||||
|
||||
return $is_default;
|
||||
}
|
||||
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->$offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
return $this->$offset;
|
||||
}
|
||||
|
||||
public function offsetSet($offset , $value) {
|
||||
$this->$offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->$offset);
|
||||
}
|
||||
|
||||
public function __construct( int $attribute_id, $lang_id = null )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$lang_id )
|
||||
$lang_id = \front\factory\Languages::default_language();
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_attributes', '*', [ 'id' => $attribute_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
|
||||
$results = $mdb -> select( 'pp_shop_attributes_langs', '*', [ 'AND' => [ 'attribute_id' => $attribute_id, 'lang_id' => $lang_id ] ] );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
$this -> language = $row;
|
||||
}
|
||||
|
||||
// pobierz kolejność atrybutu
|
||||
static public function get_attribute_order( int $attribute_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\ProductAttribute::get_attribute_order:$attribute_id";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$order = $mdb -> get( 'pp_shop_attributes', 'o', [ 'id' => $attribute_id ] );
|
||||
$cacheHandler -> set( $cacheKey, $order );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
// pobierz nazwę atrybutu za pomocą wartości
|
||||
static public function getAttributeNameByValue( int $value_id, string $lang_id )
|
||||
{
|
||||
global $mdb;
|
||||
return array_pop( $mdb -> query( 'SELECT name FROM pp_shop_attributes_langs AS psal INNER JOIN pp_shop_attributes_values AS psav ON psal.attribute_id = psav.attribute_id WHERE psav.id = ' . $value_id . ' AND lang_id = \'' . $lang_id . '\'' ) -> fetch( \PDO::FETCH_ASSOC ) );
|
||||
}
|
||||
|
||||
// pobierz nazwę wartości
|
||||
static public function get_value_name( int $value_id, string $lang_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\ProductAttribute::get_value_name:$value_id:$lang_id";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$value_name = $mdb -> get( 'pp_shop_attributes_values_langs', 'name', [ 'AND' => [ 'value_id' => (int)$value_id, 'lang_id' => $lang_id ] ] );
|
||||
$cacheHandler -> set( $cacheKey, $value_name );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize($objectData);
|
||||
}
|
||||
return $value_name;
|
||||
}
|
||||
|
||||
// pobierz nazwę atrybutu
|
||||
static public function getAttributeName( int $attribute_id, string $lang_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_attributes_langs', 'name', [ 'AND' => [ 'attribute_id' => (int)$attribute_id, 'lang_id' => $lang_id ] ] );
|
||||
}
|
||||
}
|
||||
65
autoload/shop/class.ProductCustomField.php
Normal file
65
autoload/shop/class.ProductCustomField.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class ProductCustomField implements \ArrayAccess
|
||||
{
|
||||
public function __construct( int $custom_field_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_products_custom_fields', '*', [ 'id_additional_field' => $custom_field_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
}
|
||||
|
||||
static public function getFromCache( int $custom_field_id )
|
||||
{
|
||||
// Check if Redis extension is loaded
|
||||
if ( class_exists( 'Redis' ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
$redis = \RedisConnection::getInstance() -> getConnection();
|
||||
$objectData = $redis -> get( "shop\ProductCustomField:$custom_field_id" );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$object = new self( $custom_field_id );
|
||||
$redis -> setex( "shop\ProductCustomField:$custom_field_id", 60 * 60 * 24, serialize( $object ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$object = unserialize( $objectData );
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
// Log the exception if needed
|
||||
$object = new self( $custom_field_id );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Redis extension not loaded, create a new instance
|
||||
$object = new self( $custom_field_id );
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->$offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
return $this->$offset;
|
||||
}
|
||||
|
||||
public function offsetSet($offset , $value) {
|
||||
$this->$offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->$offset);
|
||||
}
|
||||
}
|
||||
54
autoload/shop/class.ProductSet.php
Normal file
54
autoload/shop/class.ProductSet.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
class ProductSet implements \ArrayAccess
|
||||
{
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->$offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset) {
|
||||
return $this->$offset;
|
||||
}
|
||||
|
||||
public function offsetSet($offset , $value) {
|
||||
$this->$offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->$offset);
|
||||
}
|
||||
|
||||
public function __construct( int $set_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_product_sets', '*', [ 'id' => $set_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
|
||||
$this -> products = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'set_id' => $set_id ] );
|
||||
}
|
||||
|
||||
//lista dostępnych kompletów
|
||||
static public function sets_list()
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> select( 'pp_shop_product_sets', [ 'id', 'name' ], [ 'ORDER' => [ 'name' => 'ASC' ] ] );
|
||||
}
|
||||
|
||||
// usuwanie kompletu produktów
|
||||
static public function set_delete( int $set_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if (
|
||||
$mdb -> delete( 'pp_shop_product_sets_products', [ 'set_id' => $set_id ] )
|
||||
and
|
||||
$mdb -> delete( 'pp_shop_product_sets', [ 'id' => $set_id ] )
|
||||
)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
87
autoload/shop/class.Promotion.php
Normal file
87
autoload/shop/class.Promotion.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
|
||||
use DbModel;
|
||||
|
||||
class Promotion extends DbModel
|
||||
{
|
||||
public $table = 'pp_shop_promotion';
|
||||
|
||||
public static $condition_type = [
|
||||
1 => 'Rabat procentowy na produkty z kategorii 1 jeżeli w koszyku jest produkt z kategorii 2',
|
||||
2 => 'Rabat procentowy na produkty z kategorii 1 i 2',
|
||||
3 => 'Najtańszy produkt w koszyku (z wybranych kategorii) za X zł',
|
||||
4 => 'Rabat procentowy na cały koszyk',
|
||||
5 => 'Rabat procentowy na produkty z kategorii 1 lub 2',
|
||||
];
|
||||
|
||||
public static $discount_type = [ 1 => 'Rabat procentowy' ];
|
||||
|
||||
public static function get_active_promotions()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\shop\Promotion::get_active_promotions";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$results = $mdb -> select( 'pp_shop_promotion', 'id', [ 'AND' => [ 'status' => 1, 'OR' => [ 'date_to' => null, 'date_to[>=]' => date( 'Y-m-d' ) ] ], 'ORDER' => [ 'id' => 'DESC' ] ] );
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $results );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function find_promotion( $basket )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
foreach ( $basket as $key => $val )
|
||||
{
|
||||
unset( $basket[$key]['discount_type'] );
|
||||
unset( $basket[$key]['discount_amount'] );
|
||||
unset( $basket[$key]['discount_include_coupon'] );
|
||||
unset( $basket[$key]['include_product_promo'] );
|
||||
}
|
||||
|
||||
$basket_tmp = $basket;
|
||||
|
||||
$results = self::get_active_promotions();
|
||||
if ( is_array( $results ) and count( $results ) )
|
||||
{
|
||||
foreach ( $results as $row )
|
||||
{
|
||||
$promotion = new \shop\Promotion( $row );
|
||||
|
||||
// Promocja na cały koszyk
|
||||
if ( $promotion -> condition_type == 4 )
|
||||
return \front\factory\ShopPromotion::promotion_type_05( $basket_tmp, $promotion );
|
||||
|
||||
// Najtańszy produkt w koszyku (z wybranych kategorii) za X zł
|
||||
if ( $promotion -> condition_type == 3 )
|
||||
return \front\factory\ShopPromotion::promotion_type_04( $basket_tmp, $promotion );
|
||||
|
||||
// Rabat procentowy na produkty z kategorii 1 lub kategorii 2
|
||||
if ( $promotion -> condition_type == 5 )
|
||||
return \front\factory\ShopPromotion::promotion_type_03( $basket_tmp, $promotion );
|
||||
|
||||
// Rabat procentowy na produkty z kategorii I i kategorii II
|
||||
if ( $promotion -> condition_type == 2 )
|
||||
return \front\factory\ShopPromotion::promotion_type_02( $basket_tmp, $promotion );
|
||||
|
||||
// Rabat procentowy na produkty z kategorii I jeżeli w koszyku jest produkt z kategorii II
|
||||
if ( $promotion -> condition_type == 1 )
|
||||
return \front\factory\ShopPromotion::promotion_type_01( $basket_tmp, $promotion );
|
||||
}
|
||||
}
|
||||
return $basket;
|
||||
}
|
||||
}
|
||||
70
autoload/shop/class.Search.php
Normal file
70
autoload/shop/class.Search.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace shop;
|
||||
use shop\Produt;
|
||||
|
||||
class Search implements \ArrayAccess
|
||||
{
|
||||
static public function simple_form()
|
||||
{
|
||||
return \Tpl::view( 'shop-search/simple-form' );
|
||||
}
|
||||
|
||||
static public function search_results()
|
||||
{
|
||||
global $lang_id;
|
||||
|
||||
$bs = \S::get( 'bs' );
|
||||
|
||||
$results = \shop\Product::searchProductsByName( \S::get( 'query' ), $lang_id, (int)$bs );
|
||||
|
||||
$out = \Tpl::view( 'shop-search/products', [
|
||||
'query' => \S::get( 'query' ),
|
||||
'products' => $results['products']
|
||||
]);
|
||||
|
||||
if ( $results['ls'] > 1 )
|
||||
{
|
||||
$tpl = new \Tpl;
|
||||
$tpl -> ls = $results['ls'];
|
||||
$tpl -> bs = $bs ? $bs : 1;
|
||||
$tpl -> link = 'wyszukiwarka/' . \S::get( 'query' );
|
||||
$out .= $tpl -> render( 'site/pager' );
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
static public function search_products()
|
||||
{
|
||||
global $lang_id;
|
||||
|
||||
$results = \shop\Product::searchProductByNameAjax( \S::get( 'query' ), $lang_id );
|
||||
if ( \S::is_array_fix( $results ) ) foreach ( $results as $row )
|
||||
$products[] = \Tpl::view( 'shop-search/product-search', [
|
||||
'product' => Product::getFromCache( $row['product_id'], $lang_id )
|
||||
] );
|
||||
|
||||
echo json_encode( $products );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
39
autoload/shop/class.Shop.php
Normal file
39
autoload/shop/class.Shop.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?
|
||||
namespace shop;
|
||||
class Shop implements \ArrayAccess
|
||||
{
|
||||
static public function shortPrice( $price )
|
||||
{
|
||||
if ( self::isWholeNumber( $price ) )
|
||||
$price = round( $price, 0 );
|
||||
else
|
||||
$price = \S::decimal( $price );
|
||||
|
||||
return $price;
|
||||
}
|
||||
|
||||
static public function isWholeNumber( $value )
|
||||
{
|
||||
return (is_numeric($value) && (round($value, 3) == round($value)));
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
15
autoload/shop/class.ShopStatus.php
Normal file
15
autoload/shop/class.ShopStatus.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?
|
||||
namespace shop;
|
||||
class ShopStatus {
|
||||
// get_status_by_baselinker_status
|
||||
static public function get_shop_status_by_baselinker_status( int $baselinker_status_id ) {
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_statuses', 'id', [ 'baselinker_status_id' => $baselinker_status_id ] );
|
||||
}
|
||||
|
||||
// get_baselinker_status_by_shop_status
|
||||
static public function get_baselinker_status_by_shop_status( int $shop_status_id ) {
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_statuses', 'baselinker_status_id', [ 'id' => $shop_status_id ] );
|
||||
}
|
||||
}
|
||||
31
autoload/shop/class.Transport.php
Normal file
31
autoload/shop/class.Transport.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?
|
||||
namespace shop;
|
||||
class Transport implements \ArrayAccess
|
||||
{
|
||||
// lista dostępnych form wysyłki
|
||||
static public function transport_list()
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> select( 'pp_shop_transports', '*', [ 'status' => 1 ] );
|
||||
}
|
||||
|
||||
public function offsetExists( $offset )
|
||||
{
|
||||
return isset( $this -> $offset );
|
||||
}
|
||||
|
||||
public function offsetGet( $offset )
|
||||
{
|
||||
return $this -> $offset;
|
||||
}
|
||||
|
||||
public function offsetSet( $offset, $value )
|
||||
{
|
||||
$this -> $offset = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset( $offset )
|
||||
{
|
||||
unset( $this -> $offset );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user