Files
shopPRO/autoload/shop/class.Promotion.php
Jacek Pyziak 89d9e61bec ver. 0.292: ShopProduct + ShopPaymentMethod + ShopPromotion + ShopStatuses + ShopTransport frontend migration to Domain
Full migration of front\factory\ — entire directory removed (all 20 classes migrated).
ProductRepository +20 frontend methods, PromotionRepository +5 applyType methods,
TransportRepository +4 cached methods, PaymentMethodRepository +cached frontend methods.
Fix: broken transports_list() in ajax.php replaced with forPaymentMethod().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 21:55:16 +01:00

107 lines
3.2 KiB
PHP

<?php
namespace shop;
class Promotion
{
private $data = [];
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 function __construct( $id )
{
global $mdb;
if ( $id )
{
$result = $mdb->get( 'pp_shop_promotion', '*', [ 'id' => $id ] );
if ( is_array( $result ) )
$this->data = $result;
}
}
public function __get( $variable )
{
if ( isset( $this->data[$variable] ) )
return $this->data[$variable];
return null;
}
public static function get_active_promotions()
{
global $mdb;
$cacheHandler = new \Shared\Cache\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_from' => [ 'date_from' => null, 'date_from[<=]' => date( 'Y-m-d' ) ], 'OR #date_to' => [ '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 ) )
{
$promoRepo = new \Domain\Promotion\PromotionRepository( $mdb );
foreach ( $results as $row )
{
$promotion = new \shop\Promotion( $row );
// Promocja na cały koszyk
if ( $promotion -> condition_type == 4 )
return $promoRepo->applyTypeWholeBasket( $basket_tmp, $promotion );
// Najtańszy produkt w koszyku (z wybranych kategorii) za X zł
if ( $promotion -> condition_type == 3 )
return $promoRepo->applyTypeCheapestProduct( $basket_tmp, $promotion );
// Rabat procentowy na produkty z kategorii 1 lub kategorii 2
if ( $promotion -> condition_type == 5 )
return $promoRepo->applyTypeCategoriesOr( $basket_tmp, $promotion );
// Rabat procentowy na produkty z kategorii I i kategorii II
if ( $promotion -> condition_type == 2 )
return $promoRepo->applyTypeCategoriesAnd( $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 $promoRepo->applyTypeCategoryCondition( $basket_tmp, $promotion );
}
}
return $basket;
}
}