2016-01-04 12:49:26 +01:00
< ? php
/*
* 2007 - 2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License ( AFL 3.0 )
* that is bundled with this package in the file LICENSE . txt .
* It is also available through the world - wide - web at this URL :
* http :// opensource . org / licenses / afl - 3.0 . php
* If you did not receive a copy of the license and are unable to
* obtain it through the world - wide - web , please send an email
* to license @ prestashop . com so we can send you a copy immediately .
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future . If you wish to customize PrestaShop for your
* needs please refer to http :// www . prestashop . com for more information .
*
* @ author PrestaShop SA < contact @ prestashop . com >
* @ copyright 2007 - 2011 PrestaShop SA
* @ version Release : $Revision : 8005 $
* @ license http :// opensource . org / licenses / afl - 3.0 . php Academic Free License ( AFL 3.0 )
* International Registered Trademark & Property of PrestaShop SA
*/
if ( ! defined ( '_PS_VERSION_' ))
exit ;
class LoyaltyModule extends ObjectModel
{
public $id_loyalty_state ;
public $id_customer ;
public $id_order ;
public $id_discount ;
2016-04-21 11:14:33 +02:00
public $discount_value ;
2016-01-04 12:49:26 +01:00
public $date_add ;
public $date_upd ;
2016-04-21 11:14:33 +02:00
protected $fieldsRequired = array ( 'id_customer' , 'discount_value' );
2016-04-21 12:39:07 +02:00
protected $fieldsValidate = array ( 'id_loyalty_state' => 'isInt' , 'id_customer' => 'isInt' , 'id_discount' => 'isInt' , 'id_order' => 'isInt' , 'discount_value' => 'isFloat' );
2016-01-04 12:49:26 +01:00
protected $table = 'loyalty' ;
protected $identifier = 'id_loyalty' ;
public function getFields ()
{
parent :: validateFields ();
$fields [ 'id_loyalty_state' ] = ( int ) $this -> id_loyalty_state ;
$fields [ 'id_customer' ] = ( int ) $this -> id_customer ;
$fields [ 'id_order' ] = ( int ) $this -> id_order ;
$fields [ 'id_discount' ] = ( int ) $this -> id_discount ;
2016-04-21 17:52:52 +02:00
$fields [ 'discount_value' ] = ( float ) $this -> discount_value ;
2016-01-04 12:49:26 +01:00
$fields [ 'date_add' ] = pSQL ( $this -> date_add );
$fields [ 'date_upd' ] = pSQL ( $this -> date_upd );
return $fields ;
}
public function save ( $nullValues = false , $autodate = true )
{
parent :: save ( $nullValues , $autodate );
$this -> historize ();
}
public static function getByOrderId ( $id_order )
{
if ( ! Validate :: isUnsignedId ( $id_order ))
return false ;
$result = Db :: getInstance () -> getRow ( '
SELECT f . id_loyalty
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . id_order = ' . ( int )( $id_order ));
return isset ( $result [ 'id_loyalty' ]) ? $result [ 'id_loyalty' ] : false ;
}
2016-04-21 11:14:33 +02:00
public static function getOrderDiscountValue ( $order )
2016-01-04 12:49:26 +01:00
{
if ( ! Validate :: isLoadedObject ( $order ))
return false ;
2016-04-21 11:14:33 +02:00
return self :: getCartDiscountValue ( new Cart (( int ) $order -> id_cart ));
2016-01-04 12:49:26 +01:00
}
2016-04-21 11:14:33 +02:00
public static function getCartDiscountValue ( $cart , $newProduct = NULL )
2016-01-04 12:49:26 +01:00
{
$total = 0 ;
if ( Validate :: isLoadedObject ( $cart ))
{
$cartProducts = $cart -> getProducts ();
$taxesEnabled = Product :: getTaxCalculationMethod ();
if ( isset ( $newProduct ) AND ! empty ( $newProduct ))
{
$cartProductsNew [ 'id_product' ] = ( int ) $newProduct -> id ;
if ( $taxesEnabled == PS_TAX_EXC )
$cartProductsNew [ 'price' ] = number_format ( $newProduct -> getPrice ( false , ( int )( $newProduct -> getIdProductAttributeMostExpensive ())), 2 , '.' , '' );
else
$cartProductsNew [ 'price_wt' ] = number_format ( $newProduct -> getPrice ( true , ( int )( $newProduct -> getIdProductAttributeMostExpensive ())), 2 , '.' , '' );
$cartProductsNew [ 'cart_quantity' ] = 1 ;
$cartProducts [] = $cartProductsNew ;
}
foreach ( $cartProducts AS $product )
{
if ( ! ( int )( Configuration :: get ( 'PS_LOYALTY_NONE_AWARD' )) AND Product :: isDiscounted (( int ) $product [ 'id_product' ]))
{
global $smarty ;
if ( isset ( $smarty ) AND is_object ( $newProduct ) AND $product [ 'id_product' ] == $newProduct -> id )
$smarty -> assign ( 'no_pts_discounted' , 1 );
continue ;
}
$total += ( $taxesEnabled == PS_TAX_EXC ? $product [ 'price' ] : $product [ 'price_wt' ]) * ( int )( $product [ 'cart_quantity' ]);
}
2016-06-03 16:34:30 +02:00
/* foreach ( $cart -> getDiscounts ( false ) AS $discount )
$total -= $discount [ 'value_real' ]; */
2016-04-21 12:01:10 +02:00
2016-01-04 12:49:26 +01:00
}
2016-04-21 11:14:33 +02:00
return self :: getDiscountValueByPrice ( $total );
2016-01-04 12:49:26 +01:00
}
2016-04-21 11:14:33 +02:00
/* public static function getVoucherValue ( $nbPoints , $id_currency = NULL )
2016-01-04 12:49:26 +01:00
{
global $cookie ;
if ( empty ( $id_currency ))
$id_currency = ( int ) $cookie -> id_currency ;
return ( int ) $nbPoints * ( float ) Tools :: convertPrice ( Configuration :: get ( 'PS_LOYALTY_POINT_VALUE' ), new Currency (( int ) $id_currency ));
2016-04-21 11:14:33 +02:00
} */
2016-01-04 12:49:26 +01:00
2016-04-21 11:14:33 +02:00
public static function getVoucherValueByPercentOfOrder ( $orders , $id_currency = NULL )
2016-04-20 15:33:56 +02:00
{
global $cookie ;
if ( empty ( $id_currency ))
$id_currency = ( int ) $cookie -> id_currency ;
$value = 0 ;
2016-04-21 17:41:04 +02:00
if ( $orders && ! empty ( $orders )) {
foreach ( $orders as $key => $order ) {
$value += $order [ 'discount_value' ];
}
2016-04-20 15:33:56 +02:00
}
return ( float ) Tools :: convertPrice ( $value , new Currency (( int ) $id_currency ));
}
2016-04-21 11:14:33 +02:00
public static function getDiscountValueByPrice ( $price )
2016-01-04 12:49:26 +01:00
{
global $cookie ;
if ( Configuration :: get ( 'PS_CURRENCY_DEFAULT' ) != $cookie -> id_currency )
{
$currency = new Currency (( int )( $cookie -> id_currency ));
if ( $currency -> conversion_rate )
$price = $price / $currency -> conversion_rate ;
}
2016-04-21 18:06:22 +02:00
$discount_value = ( float )((( float ) Configuration :: get ( 'PS_LOYALTY_PERCENT_VALUE' ) * $price ) / 100 );
2016-01-04 12:49:26 +01:00
2016-04-21 12:01:10 +02:00
return $discount_value ;
2016-01-04 12:49:26 +01:00
}
2016-04-21 11:14:33 +02:00
/* public static function getPointsByCustomer ( $id_customer )
2016-01-04 12:49:26 +01:00
{
return
Db :: getInstance () -> getValue ( '
SELECT SUM ( f . points ) points
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . id_customer = '.(int)($id_customer).'
AND f . id_loyalty_state IN ( '.(int)(LoyaltyStateModule::getValidationId()).' , '.(int)(LoyaltyStateModule::getNoneAwardId()).' ) ' )
+
Db :: getInstance () -> getValue ( '
SELECT SUM ( f . points ) points
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . id_customer = '.(int)($id_customer).'
AND f . id_loyalty_state = '.(int)LoyaltyStateModule::getCancelId().' AND points < 0 ' );
2016-04-21 11:14:33 +02:00
} */
2016-01-04 12:49:26 +01:00
public static function getAllByIdCustomer ( $id_customer , $id_lang , $onlyValidate = false , $pagination = false , $nb = 10 , $page = 1 )
{
$query = '
2016-06-08 11:44:13 +02:00
SELECT f . id_order AS id , f . date_add AS date , f . discount_value , f . id_loyalty , f . id_loyalty_state , fsl . name state
2016-01-04 12:49:26 +01:00
FROM `'._DB_PREFIX_.'loyalty` f
LEFT JOIN `'._DB_PREFIX_.'loyalty_state_lang` fsl ON ( f . id_loyalty_state = fsl . id_loyalty_state AND fsl . id_lang = '.(int)($id_lang).' )
WHERE f . id_customer = ' . ( int )( $id_customer );
if ( $onlyValidate === true )
$query .= ' AND f.id_loyalty_state = ' . ( int ) LoyaltyStateModule :: getValidationId ();
$query .= ' GROUP BY f.id_loyalty ' .
( $pagination ? 'LIMIT ' . ((( int )( $page ) - 1 ) * ( int )( $nb )) . ', ' . ( int )( $nb ) : '' );
return Db :: getInstance () -> ExecuteS ( $query );
}
2016-05-30 17:04:26 +02:00
public static function getNbOrdersByIdCustomer ( $id_customer , $notCancel = true )
{
$query = '
SELECT COUNT ( f . `id_loyalty` )
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . `id_customer` = ' . ( int )( $id_customer );
if ( $notCancel === true )
$query .= ' AND f.id_loyalty_state != ' . ( int ) LoyaltyStateModule :: getCancelId ();
return Db :: getInstance () -> getValue ( $query );
}
2016-06-08 12:02:16 +02:00
public static function getOrdersWithDiscountByIdCustomer ( $id_customer , $notCancel = true )
2016-04-20 15:33:56 +02:00
{
2016-06-08 12:02:16 +02:00
$query = '
SELECT f . `id_loyalty` , f . `id_loyalty_state` , f . `discount_value`
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . `id_customer` = ' . ( int )( $id_customer );
if ( $notCancel === true )
$query .= ' AND f.id_loyalty_state != ' . ( int ) LoyaltyStateModule :: getCancelId ();
return Db :: getInstance () -> ExecuteS ( $query );
}
2016-04-21 17:41:04 +02:00
2016-06-08 12:02:16 +02:00
public static function getAllByIdCustomerCustom ( $id_customer , $id_lang , $onlyValidate = false , $pagination = false , $nb = 10 , $page = 1 , $onlyDefault = false )
{
/* $percent = ( float )( Configuration :: get ( 'PS_LOYALTY_PERCENT_VALUE' ) / 100 );
2016-04-20 15:33:56 +02:00
$query = '
SELECT f . id_order AS id , f . date_add AS date ,
( o . total_paid - o . total_shipping ) total_without_shipping ,
2016-04-21 17:41:04 +02:00
(( '.(float)$percent.' * ( o . total_paid - o . total_shipping ))) as reduc_value ,
2016-04-21 11:14:33 +02:00
f . discount_value , f . id_loyalty , f . id_loyalty_state , fsl . name state
2016-04-20 15:33:56 +02:00
FROM `'._DB_PREFIX_.'loyalty` f
LEFT JOIN `'._DB_PREFIX_.'orders` o ON ( f . id_order = o . id_order )
LEFT JOIN `'._DB_PREFIX_.'loyalty_state_lang` fsl ON ( f . id_loyalty_state = fsl . id_loyalty_state AND fsl . id_lang = '.(int)($id_lang).' )
2016-06-08 12:02:16 +02:00
WHERE f . id_customer = '.(int)($id_customer).' */
$query = '
SELECT f . id_order AS id , f . date_add AS date , f . discount_value , f . id_loyalty , f . id_loyalty_state , fsl . name state
FROM `'._DB_PREFIX_.'loyalty` f
LEFT JOIN `'._DB_PREFIX_.'loyalty_state_lang` fsl ON ( f . id_loyalty_state = fsl . id_loyalty_state AND fsl . id_lang = '.(int)($id_lang).' )
2016-04-20 15:33:56 +02:00
WHERE f . id_customer = '.(int)($id_customer).'
2016-04-22 16:20:53 +02:00
'.(($onlyValidate)?' ':' AND f . id_loyalty_state != ' . ( int ) LoyaltyStateModule :: getCancelId ());
2016-04-21 11:14:33 +02:00
if ( $onlyValidate ) {
2016-04-22 16:20:53 +02:00
$query .= ' AND (f.id_loyalty_state = ' . ( int ) LoyaltyStateModule :: getCancelId () . ' OR f.id_loyalty_state = ' . ( int ) LoyaltyStateModule :: getValidationId () . ')' ;
2016-04-21 17:41:04 +02:00
} elseif ( $onlyDefault ){
$query .= ' AND f.id_loyalty_state = ' . ( int ) LoyaltyStateModule :: getDefaultId ();
2016-04-20 15:33:56 +02:00
}
$query .= ' GROUP BY f.id_loyalty ' .
( $pagination ? 'LIMIT ' . ((( int )( $page ) - 1 ) * ( int )( $nb )) . ', ' . ( int )( $nb ) : '' );
return Db :: getInstance () -> ExecuteS ( $query );
}
2016-01-04 12:49:26 +01:00
public static function getDiscountByIdCustomer ( $id_customer , $last = false )
{
$query = '
SELECT f . id_discount AS id_discount , f . date_upd AS date_add
FROM `'._DB_PREFIX_.'loyalty` f
LEFT JOIN `'._DB_PREFIX_.'orders` o ON ( f . `id_order` = o . `id_order` )
WHERE f . `id_customer` = '.(int)($id_customer).'
AND f . `id_discount` > 0
AND o . `valid` = 1 ' ;
if ( $last === true )
$query .= ' ORDER BY f.id_loyalty DESC LIMIT 0,1' ;
$query .= ' GROUP BY f.id_discount' ;
return Db :: getInstance () -> ExecuteS ( $query );
}
2016-04-20 15:33:56 +02:00
public static function registerDiscountCustom ( $discount , $orders )
{
if ( ! Validate :: isLoadedObject ( $discount ))
die ( Tools :: displayError ( 'Incorrect object Discount.' ));
foreach ( $orders AS $item )
{
$f = new LoyaltyModule (( int ) $item [ 'id_loyalty' ]);
2016-04-21 11:14:33 +02:00
2016-04-21 17:41:04 +02:00
if ( $f -> discount_value == 0 ) {
2016-04-21 11:14:33 +02:00
continue ;
}
2016-04-20 15:33:56 +02:00
$f -> id_discount = ( int ) $discount -> id ;
2016-04-21 17:41:04 +02:00
$f -> discount_value = $f -> discount_value ;
2016-04-20 15:33:56 +02:00
$f -> id_loyalty_state = ( int ) LoyaltyStateModule :: getConvertId ();
$f -> save ();
}
}
2016-04-21 11:14:33 +02:00
/* public static function getOrdersByIdDiscount ( $id_discount )
2016-01-04 12:49:26 +01:00
{
$items = Db :: getInstance () -> ExecuteS ( '
SELECT f . id_order AS id_order , f . points AS points , f . date_upd AS date
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . id_discount = '.(int)($id_discount).' AND f . id_loyalty_state = ' . ( int )( LoyaltyStateModule :: getConvertId ()));
if ( ! empty ( $items ) AND is_array ( $items ))
{
foreach ( $items AS $key => $item )
{
$order = new Order (( int ) $item [ 'id_order' ]);
$items [ $key ][ 'id_currency' ] = ( int ) $order -> id_currency ;
$items [ $key ][ 'id_lang' ] = ( int ) $order -> id_lang ;
$items [ $key ][ 'total_paid' ] = $order -> total_paid ;
$items [ $key ][ 'total_shipping' ] = $order -> total_shipping ;
}
return $items ;
}
return false ;
2016-04-21 11:14:33 +02:00
} */
2016-01-04 12:49:26 +01:00
2016-04-20 15:33:56 +02:00
public static function getOrdersByIdDiscountCustom ( $id_discount )
{
$items = Db :: getInstance () -> ExecuteS ( '
2016-04-21 17:41:04 +02:00
SELECT f . id_order AS id_order , f . discount_value , f . date_upd AS date
FROM `'._DB_PREFIX_.'loyalty` f
WHERE f . id_discount = '.(int)($id_discount).' AND f . id_loyalty_state = ' . ( int )( LoyaltyStateModule :: getConvertId ())
);
2016-04-20 15:33:56 +02:00
if ( ! empty ( $items ) AND is_array ( $items ))
{
foreach ( $items AS $key => $item )
{
$order = new Order (( int ) $item [ 'id_order' ]);
$items [ $key ][ 'id_currency' ] = ( int ) $order -> id_currency ;
$items [ $key ][ 'id_lang' ] = ( int ) $order -> id_lang ;
$items [ $key ][ 'total_paid' ] = $order -> total_paid ;
$items [ $key ][ 'total_shipping' ] = $order -> total_shipping ;
2016-04-21 17:41:04 +02:00
$items [ $key ][ 'discount_value' ] = $item [ 'discount_value' ];
2016-04-20 15:33:56 +02:00
}
return $items ;
}
return false ;
}
2016-01-04 12:49:26 +01:00
/* Register all transaction in a specific history table */
private function historize ()
{
Db :: getInstance () -> Execute ( '
2016-04-21 11:14:33 +02:00
INSERT INTO `'._DB_PREFIX_.'loyalty_history` ( `id_loyalty` , `id_loyalty_state` , `discount_value` , `date_add` )
2016-04-21 17:56:54 +02:00
VALUES ( '.(int)($this->id).' , '.(int)($this->id_loyalty_state).' , '.(float)($this->discount_value).' , NOW ()) ' );
2016-01-04 12:49:26 +01:00
}
}