Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -12,46 +12,68 @@
|
||||
namespace IPS\nexus;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use IPS\Application;
|
||||
use IPS\DateTime;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\Helpers\Form\CheckboxSet;
|
||||
use IPS\Helpers\Form\Date;
|
||||
use IPS\Helpers\Form\FormAbstract;
|
||||
use IPS\Helpers\Form\Radio;
|
||||
use IPS\Helpers\Form\Text;
|
||||
use IPS\Helpers\Form\YesNo;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Math\Number;
|
||||
use IPS\Member\Group;
|
||||
use IPS\nexus\extensions\nexus\Item\CouponDiscount;
|
||||
use IPS\nexus\Invoice\Item\Renewal;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Request;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coupon Node
|
||||
*/
|
||||
class _Coupon extends \IPS\Node\Model
|
||||
class Coupon extends Model
|
||||
{
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Table
|
||||
*/
|
||||
public static $databaseTable = 'nexus_coupons';
|
||||
public static ?string $databaseTable = 'nexus_coupons';
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Prefix
|
||||
*/
|
||||
public static $databasePrefix = 'c_';
|
||||
public static string $databasePrefix = 'c_';
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'menu__nexus_store_coupons';
|
||||
public static string $nodeTitle = 'menu__nexus_store_coupons';
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database ID Fields
|
||||
*/
|
||||
protected static $databaseIdFields = array( 'c_code' );
|
||||
protected static array $databaseIdFields = array( 'c_code' );
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Map
|
||||
*/
|
||||
protected static $multitonMap = array();
|
||||
protected static array $multitonMap = array();
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
@@ -69,102 +91,161 @@ class _Coupon extends \IPS\Node\Model
|
||||
'prefix' => 'foo_', // [Optional] Rather than specifying each key in the map, you can specify a prefix, and it will automatically look for restrictions with the key "[prefix]_add/edit/permissions/delete"
|
||||
* @endcode
|
||||
*/
|
||||
protected static $restrictions = array(
|
||||
protected static ?array $restrictions = array(
|
||||
'app' => 'nexus',
|
||||
'module' => 'store',
|
||||
'prefix' => 'coupons_',
|
||||
);
|
||||
|
||||
/**
|
||||
* Determines if this class can be extended via UI Extension
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static bool $canBeExtended = true;
|
||||
|
||||
/**
|
||||
* [Node] Get title
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get__title()
|
||||
protected function get__title(): string
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_products() : array|string
|
||||
{
|
||||
if( isset( $this->_data['products'] ) and $this->_data['products'] !== '*' )
|
||||
{
|
||||
if( $test = json_decode( $this->_data['products'], true ) )
|
||||
{
|
||||
return $test;
|
||||
}
|
||||
|
||||
return [
|
||||
'product' => [
|
||||
'products' => explode( ",", $this->_data['products'] )
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
return '*';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string|null $val
|
||||
* @return void
|
||||
*/
|
||||
public function set_products( array|string|null $val ) : void
|
||||
{
|
||||
$this->_data['products'] = ( is_array( $val ) and count( $val ) ) ? json_encode( $val ) : '*';
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Add/Edit Form
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @param Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function form( &$form )
|
||||
public function form( Form &$form ) : void
|
||||
{
|
||||
$groups = array();
|
||||
foreach ( \IPS\Member\Group::groups() as $group )
|
||||
foreach ( Group::groups() as $group )
|
||||
{
|
||||
$groups[ $group->g_id ] = $group->name;
|
||||
}
|
||||
|
||||
if ( $this->id and $this->products !== '*' )
|
||||
{
|
||||
$products = array();
|
||||
foreach ( explode( ',', $this->products ) as $packageId )
|
||||
{
|
||||
try
|
||||
{
|
||||
$products[] = \IPS\nexus\Package::load( $packageId );
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$products = 0;
|
||||
}
|
||||
|
||||
|
||||
$form->addHeader( 'coupon_basic_settings' );
|
||||
$id = $this->id ?: NULL;
|
||||
$form->add( new \IPS\Helpers\Form\Text( 'c_code', $this->id ? $this->code : mb_strtoupper( mb_substr( md5( mt_rand() ), 0, 5 ) ), TRUE, array( 'maxLength' => 25 ), function( $val ) use ( $id )
|
||||
$form->add( new Text( 'c_code', $this->id ? $this->code : mb_strtoupper( mb_substr( md5( mt_rand() ), 0, 5 ) ), TRUE, array( 'maxLength' => 25 ), function( $val ) use ( $id )
|
||||
{
|
||||
try
|
||||
{
|
||||
$coupon = \IPS\nexus\Coupon::load( $val, 'c_code' );
|
||||
$coupon = Coupon::load( $val, 'c_code' );
|
||||
if ( $coupon->id AND $coupon->id != $id )
|
||||
{
|
||||
throw new \DomainException('c_code_err');
|
||||
throw new DomainException('c_code_err');
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) {}
|
||||
catch ( OutOfRangeException ) {}
|
||||
} ) );
|
||||
$form->add( new \IPS\Helpers\Form\Radio( 'c_unit', $this->id ? $this->unit : 'v', TRUE, array( 'options' => array( 'v' => 'c_unit_v', 'p' => 'c_unit_p' ), 'toggles' => array( 'v' => array( 'c_discount_v' ), 'p' => array( 'c_discount_p', 'c_limit_discount' ) ) ) ) );
|
||||
$form->add( new Radio( 'c_unit', $this->id ? $this->unit : 'v', TRUE, array( 'options' => array( 'v' => 'c_unit_v', 'p' => 'c_unit_p' ), 'toggles' => array( 'v' => array( 'c_discount_v' ), 'p' => array( 'c_discount_p' ) ) ) ) );
|
||||
$form->add( new \IPS\nexus\Form\Money( 'c_discount_v', $this->unit === 'v' ? $this->discount : NULL, NULL, array(), function( $val )
|
||||
{
|
||||
if ( !$val and \IPS\Request::i()->c_unit === 'v' )
|
||||
if ( !$val and Request::i()->c_unit === 'v' )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
throw new DomainException('form_required');
|
||||
}
|
||||
}, NULL, NULL, 'c_discount_v' ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'c_discount_p', $this->unit === 'p' ? $this->discount : NULL, NULL, array( 'max' => 100 ), function( $val )
|
||||
$form->add( new Form\Number( 'c_discount_p', $this->unit === 'p' ? $this->discount : NULL, NULL, array( 'max' => 100 ), function($val )
|
||||
{
|
||||
if ( !$val and \IPS\Request::i()->c_unit === 'p' )
|
||||
if ( !$val and Request::i()->c_unit === 'p' )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
throw new DomainException('form_required');
|
||||
}
|
||||
}, NULL, '%', 'c_discount_p' ) );
|
||||
|
||||
$form->addHeader( 'coupon_products' );
|
||||
$form->add( new \IPS\Helpers\Form\Node( 'c_products', $products, FALSE, array( 'class' => 'IPS\nexus\Package\Group', 'multiple' => TRUE, 'zeroVal' => 'no_restriction', 'zeroValTogglesOff' => array( 'c_renewals' ), 'permissionCheck' => function( $node )
|
||||
$form->add( new Radio( 'c_limit_discount', $this->limit_discount, FALSE, array(
|
||||
'options' => array(
|
||||
0 => 'c_limit_discount_no',
|
||||
1 => 'c_limit_discount_yes'
|
||||
),
|
||||
'toggles' => array(
|
||||
1 => array( 'c_products', 'c_renewals' )
|
||||
)
|
||||
), NULL, NULL, NULL, 'c_limit_discount' ) );
|
||||
|
||||
$productTypes = [];
|
||||
$productFields = [];
|
||||
$toggles = [];
|
||||
foreach( Application::allExtensions( 'nexus', 'Item', null, null, null, false ) as $extension )
|
||||
{
|
||||
return !( $node instanceof \IPS\nexus\Package\Group );
|
||||
} ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\YesNo( 'c_renewals', $this->renewals ?: FALSE, FALSE, array(), NULL, NULL, NULL, 'c_renewals' ) );
|
||||
$form->add( new \IPS\Helpers\Form\Radio( 'c_limit_discount', $this->limit_discount, FALSE, array( 'options' => array( 0 => 'c_limit_discount_no', 1 => 'c_limit_discount_yes' ) ), NULL, NULL, NULL, 'c_limit_discount' ) );
|
||||
|
||||
$form->addHeader( 'coupon_dates' );
|
||||
$form->add( new \IPS\Helpers\Form\Date( 'c_start', $this->id ? \IPS\DateTime::ts( $this->start ) : \IPS\DateTime::ts( time() ), TRUE, array( 'time' => TRUE ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\Date( 'c_end', ( $this->id and $this->end ) ? \IPS\DateTime::ts( $this->end ) : 0, TRUE, array( 'time' => TRUE, 'unlimited' => 0, 'unlimitedLang' => 'no_end_date' ) ) );
|
||||
if( method_exists( $extension, 'customFormElements' ) )
|
||||
{
|
||||
$fields = $extension::customFormElements( $this->products );
|
||||
if( count( $fields ) )
|
||||
{
|
||||
$productTypes[$extension::$title] = $extension::$title;
|
||||
$toggles[$extension::$title] = [];
|
||||
foreach( $fields as $field )
|
||||
{
|
||||
/* @var FormAbstract $field */
|
||||
$productFields[] = $field;
|
||||
$toggles[$extension::$title][] = $field->htmlId ?? $field->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$form->add( new CheckboxSet( 'c_products', ( $this->products === '*' ? null : array_keys( $this->products ) ), null, array(
|
||||
'options' => $productTypes,
|
||||
'toggles' => $toggles,
|
||||
'noDefault' => true
|
||||
), null, null, null, 'c_products' ) );
|
||||
|
||||
foreach( $productFields as $field )
|
||||
{
|
||||
$form->add( $field );
|
||||
}
|
||||
|
||||
$form->add( new YesNo( 'c_renewals', (int) $this->renewals, false, array(), null, null, null, 'c_renewals' ) );
|
||||
|
||||
$form->addHeader( 'coupon_dates' );
|
||||
$form->add( new Date( 'c_start', $this->id ? DateTime::ts( $this->start ) : DateTime::ts( time() ), TRUE, array( 'time' => TRUE ) ) );
|
||||
$form->add( new Date( 'c_end', ( $this->id and $this->end ) ? DateTime::ts( $this->end ) : 0, TRUE, array( 'time' => TRUE, 'unlimited' => 0, 'unlimitedLang' => 'no_end_date' ) ) );
|
||||
|
||||
$form->addHeader( 'coupon_restrictions' );
|
||||
$form->add( new \IPS\Helpers\Form\YesNo( 'c_combine', $this->combine ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'c_uses', $this->id ? $this->uses : -1, FALSE, array( 'unlimited' => -1 ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'c_member_uses', $this->id ? $this->member_uses : 1, FALSE, array( 'unlimited' => -1 ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\CheckboxSet( 'c_groups', ( $this->id and $this->groups !== '*' ) ? explode( ',', $this->groups ) : '*', FALSE, array( 'options' => $groups, 'multiple' => TRUE, 'unlimited' => '*', 'impliedUnlimited' => TRUE ) ) );
|
||||
|
||||
$form->add( new YesNo( 'c_combine', $this->combine ) );
|
||||
$form->add( new Form\Number( 'c_uses', $this->id ? $this->uses : -1, FALSE, array( 'unlimited' => -1 ) ) );
|
||||
$form->add( new Form\Number( 'c_member_uses', $this->id ? $this->member_uses : 1, FALSE, array( 'unlimited' => -1 ) ) );
|
||||
$form->add( new CheckboxSet( 'c_groups', ( $this->id and $this->groups !== '*' ) ? explode( ',', $this->groups ) : '*', FALSE, array( 'options' => $groups, 'multiple' => TRUE, 'unlimited' => '*', 'impliedUnlimited' => TRUE ) ) );
|
||||
|
||||
parent::form( $form );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,7 +254,7 @@ class _Coupon extends \IPS\Node\Model
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
public function formatFormValues( array $values ): array
|
||||
{
|
||||
if( isset( $values['c_unit'] ) )
|
||||
{
|
||||
@@ -182,23 +263,27 @@ class _Coupon extends \IPS\Node\Model
|
||||
unset( $values['c_discount_v'] );
|
||||
unset( $values['c_discount_p'] );
|
||||
}
|
||||
|
||||
if( isset( $values['c_products'] ) )
|
||||
|
||||
/* We need to loop through the extensions even if we
|
||||
are not going to use the data, because the fields need to be unset */
|
||||
$productData = [];
|
||||
foreach( Application::allExtensions( 'nexus', 'Item', null, null, null, false ) as $extension )
|
||||
{
|
||||
if ( $values['c_products'] )
|
||||
if( method_exists( $extension, 'saveCustomForm' ) )
|
||||
{
|
||||
$products = array();
|
||||
foreach ( $values['c_products'] as $package )
|
||||
if( $extensionData = $extension::saveCustomForm( $values, $this ) )
|
||||
{
|
||||
$products[ $package->id ] = $package->id;
|
||||
$productData[ $extension::$title ] = $extensionData;
|
||||
}
|
||||
$values['c_products'] = implode( ',', $products );
|
||||
}
|
||||
else
|
||||
{
|
||||
$values['c_products'] = '*';
|
||||
}
|
||||
}
|
||||
|
||||
if( $values['c_limit_discount'] != 1 )
|
||||
{
|
||||
$productData = '*';
|
||||
}
|
||||
|
||||
$values['c_products'] = $productData;
|
||||
|
||||
if( isset( $values['c_start'] ) )
|
||||
{
|
||||
@@ -212,7 +297,7 @@ class _Coupon extends \IPS\Node\Model
|
||||
|
||||
if( isset( $values['c_groups'] ) )
|
||||
{
|
||||
$values['c_groups'] = \is_array( $values['c_groups'] ) ? implode( ',', $values['c_groups'] ) : '*';
|
||||
$values['c_groups'] = is_array( $values['c_groups'] ) ? implode( ',', $values['c_groups'] ) : '*';
|
||||
}
|
||||
|
||||
return $values;
|
||||
@@ -223,21 +308,21 @@ class _Coupon extends \IPS\Node\Model
|
||||
* Example code explains return value
|
||||
*
|
||||
* @code
|
||||
array(
|
||||
array(
|
||||
'icon' => 'plus-circle', // Name of FontAwesome icon to use
|
||||
'title' => 'foo', // Language key to use for button's title parameter
|
||||
'link' => \IPS\Http\Url::internal( 'app=foo...' ) // URI to link to
|
||||
'class' => 'modalLink' // CSS Class to use on link (Optional)
|
||||
),
|
||||
... // Additional buttons
|
||||
);
|
||||
* array(
|
||||
* array(
|
||||
* 'icon' => 'plus-circle', // Name of FontAwesome icon to use
|
||||
* 'title' => 'foo', // Language key to use for button's title parameter
|
||||
* 'link' => \IPS\Http\Url::internal( 'app=foo...' ) // URI to link to
|
||||
* 'class' => 'modalLink' // CSS Class to use on link (Optional)
|
||||
* ),
|
||||
* ... // Additional buttons
|
||||
* );
|
||||
* @endcode
|
||||
* @param string $url Base URL
|
||||
* @param Url $url Base URL
|
||||
* @param bool $subnode Is this a subnode?
|
||||
* @return array
|
||||
*/
|
||||
public function getButtons( $url, $subnode=FALSE )
|
||||
public function getButtons( Url $url, bool $subnode=FALSE ):array
|
||||
{
|
||||
return array_merge( array(
|
||||
'view' => array(
|
||||
@@ -251,12 +336,12 @@ class _Coupon extends \IPS\Node\Model
|
||||
/**
|
||||
* Use coupon
|
||||
*
|
||||
* @param \IPS\nexus\Invoice $invoice Invoice to use against
|
||||
* @param \IPS\nexus\Customer $customer The customer using
|
||||
* @return \IPS\nexus\extensions\nexus\Item\CouponDiscount
|
||||
* @throws \DomainException
|
||||
* @param Invoice $invoice Invoice to use against
|
||||
* @param Customer $customer The customer using
|
||||
* @return CouponDiscount
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function useCoupon( \IPS\nexus\Invoice $invoice, \IPS\nexus\Customer $customer )
|
||||
public function useCoupon( Invoice $invoice, Customer $customer ) : CouponDiscount
|
||||
{
|
||||
/* Restricted to groups? */
|
||||
if ( $this->groups !== '*' )
|
||||
@@ -272,24 +357,24 @@ class _Coupon extends \IPS\Node\Model
|
||||
}
|
||||
if ( !$inGroup )
|
||||
{
|
||||
throw new \DomainException( 'coupon_not_in_group' );
|
||||
throw new DomainException( 'coupon_not_in_group' );
|
||||
}
|
||||
}
|
||||
|
||||
/* Valid dates */
|
||||
if ( $this->start and $this->start > time() )
|
||||
{
|
||||
throw new \DomainException( 'coupon_not_started' );
|
||||
throw new DomainException( 'coupon_not_started' );
|
||||
}
|
||||
if ( $this->end and $this->end < time() )
|
||||
{
|
||||
throw new \DomainException( 'coupon_expired' );
|
||||
throw new DomainException( 'coupon_expired' );
|
||||
}
|
||||
|
||||
/* Maximum uses? */
|
||||
if ( $this->uses == 0 )
|
||||
{
|
||||
throw new \DomainException( 'coupon_expired' );
|
||||
throw new DomainException( 'coupon_expired' );
|
||||
}
|
||||
|
||||
/* Maximum uses per member? */
|
||||
@@ -299,24 +384,24 @@ class _Coupon extends \IPS\Node\Model
|
||||
{
|
||||
if ( isset( $uses[ $customerIdentifier ] ) and $uses[ $customerIdentifier ] >= $this->member_uses )
|
||||
{
|
||||
throw new \DomainException( 'coupon_exceeded_member_uses' );
|
||||
throw new DomainException( 'coupon_exceeded_member_uses' );
|
||||
}
|
||||
}
|
||||
|
||||
/* Use in conjunction with other coupons? */
|
||||
foreach ( $invoice->items as $item )
|
||||
{
|
||||
if ( $item instanceof \IPS\nexus\extensions\nexus\Item\CouponDiscount )
|
||||
if ( $item instanceof CouponDiscount )
|
||||
{
|
||||
if ( $item->id === $this->id )
|
||||
{
|
||||
throw new \DomainException( 'coupon_already_used' );
|
||||
throw new DomainException( 'coupon_already_used' );
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !$this->combine )
|
||||
{
|
||||
throw new \DomainException( $customer->language()->addToStack( 'coupon_not_in_conjunction', FALSE, array( 'sprintf' => array( $this->code ) ) ) );
|
||||
throw new DomainException( $customer->language()->addToStack( 'coupon_not_in_conjunction', FALSE, array( 'sprintf' => array( $this->code ) ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -325,10 +410,10 @@ class _Coupon extends \IPS\Node\Model
|
||||
$otherCoupon = static::load( $item->id );
|
||||
if ( !$otherCoupon->combine )
|
||||
{
|
||||
throw new \DomainException( $customer->language()->addToStack( 'coupon_not_in_conjunction', FALSE, array( 'sprintf' => array( $otherCoupon->code ) ) ) );
|
||||
throw new DomainException( $customer->language()->addToStack( 'coupon_not_in_conjunction', FALSE, array( 'sprintf' => array( $otherCoupon->code ) ) ) );
|
||||
}
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
catch ( OutOfRangeException ) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -338,52 +423,47 @@ class _Coupon extends \IPS\Node\Model
|
||||
$items = NULL;
|
||||
if ( $this->products !== '*' )
|
||||
{
|
||||
$allowedProductIds = explode( ',', $this->products );
|
||||
$hasAllowedProduct = FALSE;
|
||||
$productTotal = new \IPS\Math\Number('0');
|
||||
$productTotal = new Number('0');
|
||||
|
||||
$items = array();
|
||||
|
||||
foreach ( $invoice->items as $k => $item )
|
||||
{
|
||||
if ( $item instanceof \IPS\nexus\extensions\nexus\Item\Package and \in_array( $item->id, $allowedProductIds ) )
|
||||
if( $item instanceof \IPS\nexus\Invoice\Item\Purchase )
|
||||
{
|
||||
$hasAllowedProduct = TRUE;
|
||||
$productTotal = $productTotal->add( $item->price->amount->multiply( new \IPS\Math\Number("{$item->quantity}") ) );
|
||||
|
||||
$items[] = $k;
|
||||
}
|
||||
elseif ( $this->renewals and $item instanceof \IPS\nexus\Invoice\Item\Renewal )
|
||||
{
|
||||
try
|
||||
if( $item->isValid( $this->products, $invoice, $customer ) )
|
||||
{
|
||||
$purchase = \IPS\nexus\Purchase::load( $item->id );
|
||||
if ( $purchase->app == 'nexus' and $purchase->type == 'package' and \in_array( $purchase->item_id, $allowedProductIds ) )
|
||||
{
|
||||
$hasAllowedProduct = TRUE;
|
||||
$productTotal = $productTotal->add( $item->price->amount );
|
||||
|
||||
$items[] = $k;
|
||||
}
|
||||
$hasAllowedProduct = TRUE;
|
||||
$productTotal = $productTotal->add( $item->price->amount->multiply( new Number("{$item->quantity}") ) );
|
||||
$items[] = $k;
|
||||
}
|
||||
}
|
||||
elseif ( $this->renewals and $item instanceof Renewal )
|
||||
{
|
||||
if( $item->isValid( $this->products, $invoice, $customer ) )
|
||||
{
|
||||
$hasAllowedProduct = TRUE;
|
||||
$productTotal = $productTotal->add( $item->price->amount );
|
||||
$items[] = $k;
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
}
|
||||
}
|
||||
|
||||
if ( !$hasAllowedProduct )
|
||||
{
|
||||
throw new \DomainException( 'coupon_invalid_products' );
|
||||
throw new DomainException( 'coupon_invalid_products' );
|
||||
}
|
||||
}
|
||||
|
||||
/* How much are we taking off? */
|
||||
$discount = new \IPS\nexus\Money( 0, $invoice->currency );
|
||||
$discount = new Money( 0, $invoice->currency );
|
||||
if ( $this->unit === 'v' )
|
||||
{
|
||||
$prices = json_decode( $this->discount, TRUE );
|
||||
if ( isset( $prices[ $invoice->currency ] ) )
|
||||
{
|
||||
$discount = new \IPS\nexus\Money( -$prices[ $invoice->currency ]['amount'], $invoice->currency );
|
||||
$discount = new Money( -$prices[ $invoice->currency ]['amount'], $invoice->currency );
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -397,14 +477,14 @@ class _Coupon extends \IPS\Node\Model
|
||||
$summary = $invoice->summary();
|
||||
$base = $summary['subtotal']->amount;
|
||||
}
|
||||
|
||||
$discount = new \IPS\nexus\Money( $base->percentage( new \IPS\Math\Number( $this->discount ) )->multiply( new \IPS\Math\Number( '-1' ) ), $invoice->currency );
|
||||
|
||||
$discount = new Money( $base->percentage( $this->discount )->multiply( new Number( '-1' ) ), $invoice->currency );
|
||||
}
|
||||
|
||||
/* Never allow a discount greater than the invoice total */
|
||||
if ( $discount->amount->multiply( new \IPS\Math\Number( '-1' ) )->compare( $invoice->total->amount ) === 1 )
|
||||
if ( $discount->amount->multiply( new Number( '-1' ) )->compare( $invoice->total->amount ) === 1 )
|
||||
{
|
||||
$discount = new \IPS\nexus\Money( $invoice->total->amount->multiply( new \IPS\Math\Number( '-1' ) ), $invoice->currency );
|
||||
$discount = new Money( $invoice->total->amount->multiply( new Number( '-1' ) ), $invoice->currency );
|
||||
}
|
||||
|
||||
/* Save that we've used it */
|
||||
@@ -420,7 +500,7 @@ class _Coupon extends \IPS\Node\Model
|
||||
$this->save();
|
||||
|
||||
/* Generate item */
|
||||
$item = new \IPS\nexus\extensions\nexus\Item\CouponDiscount( $this->code, $discount );
|
||||
$item = new CouponDiscount( $this->code, $discount );
|
||||
$item->id = $this->id;
|
||||
$item->extra['usedBy'] = $customer->member_id;
|
||||
$item->extra['type'] = $this->unit;
|
||||
@@ -457,7 +537,7 @@ class _Coupon extends \IPS\Node\Model
|
||||
* @param mixed $where Where clause
|
||||
* @return array
|
||||
*/
|
||||
public static function search( $column, $query, $order, $where=array() )
|
||||
public static function search( string $column, string $query, ?string $order=NULL, mixed $where=array() ): array
|
||||
{
|
||||
if( $column === '_title' )
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user