Version 4.5.0

This commit is contained in:
Neo committed 2025-12-19 05:51:58 -08:00
1 parent 1a0c7fd3c2
commit f79dcf067a
3791 files changed
+248032 -76372

No files matched your search

@@ -12,7 +12,7 @@
namespace IPS\nexus\Customer;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -139,7 +139,7 @@ class _Address extends \IPS\Patterns\ActiveRecord
/**
* Set member
*
* @param \IPS\Member
* @param \IPS\Member $member Member
* @return void
*/
public function set_member( \IPS\Member $member )
@@ -160,7 +160,7 @@ class _Address extends \IPS\Patterns\ActiveRecord
/**
* Set member
*
* @param \IPS\Member
* @param \IPS\GeoLocation $address Address
* @return void
*/
public function set_address( \IPS\GeoLocation $address )
@@ -12,7 +12,7 @@
namespace IPS\nexus\Customer;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -51,7 +51,7 @@ class _AlternativeContact extends \IPS\Patterns\ActiveRecord
/**
* Set main account
*
* @param \IPS\Member
* @param \IPS\Member $member Member
* @return void
*/
public function set_main_id( \IPS\Member $member )
@@ -72,7 +72,7 @@ class _AlternativeContact extends \IPS\Patterns\ActiveRecord
/**
* Set alternate account
*
* @param \IPS\Member
* @param \IPS\Member $member Member
* @return void
*/
public function set_alt_id( \IPS\Member $member )
@@ -133,7 +133,7 @@ class _AlternativeContact extends \IPS\Patterns\ActiveRecord
{
foreach( $this->$k->values as $field => $value )
{
$data[ $field ] = intval( $value );
$data[ $field ] = \intval( $value );
}
}
}
@@ -12,7 +12,7 @@
namespace IPS\nexus\Customer;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -22,10 +22,21 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
* Billing Agreement Model
*/
class _BillingAgreement extends \IPS\Patterns\ActiveRecord
{
const STATUS_ACTIVE = 'active'; // Billing Agreement is active and will charge automatically
const STATUS_SUSPENDED = 'suspended'; // Billing Agreement has been suspended but can be reactivated
const STATUS_CANCELED = 'canceled'; // Billing Agreement has been canceled
{
/**
* @brief Billing Agreement is active and will charge automatically
*/
const STATUS_ACTIVE = 'active';
/**
* @brief Billing Agreement has been suspended but can be reactivated
*/
const STATUS_SUSPENDED = 'suspended';
/**
* @brief Billing Agreement has been canceled
*/
const STATUS_CANCELED = 'canceled';
/**
* @brief Multiton Store
@@ -83,6 +94,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
/**
* Load and check permissions
*
* @param mixed $id ID to load
* @return \IPS\Content\Item
* @throws \OutOfRangeException
*/
@@ -124,7 +136,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
/**
* Set member
*
* @param \IPS\Member
* @param \IPS\Member $member Member
* @return void
*/
public function set_member( \IPS\Member $member )
@@ -145,7 +157,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
/**
* Set payment gateway
*
* @param \IPS\nexus\Gateway
* @param \IPS\nexus\Gateway $gateway Payment gateway
* @return void
*/
public function set_method( \IPS\nexus\Gateway $gateway )
@@ -12,7 +12,7 @@
namespace IPS\nexus\Customer;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -48,7 +48,7 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
public static function constructFromData( $data, $updateMultitonStoreIfExists = TRUE )
{
$gateway = \IPS\nexus\Gateway::load( $data['card_method'] );
$classname = 'IPS\nexus\Gateway\\' . $gateway->gateway . '\\CreditCard';
$classname = \IPS\nexus\Gateway::gateways()[ $gateway->gateway ] . '\\CreditCard';
/* Initiate an object */
$obj = new $classname;
@@ -84,19 +84,58 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
* Add Form
*
* @param \IPS\nexus\Customer $customer The customer
* @param bool $admin Set to TRUE if the *admin* (opposed to the customer themselves) wants to create a new payment method
* @return \IPS\Helpers\Form|\IPS\nexus\CreditCard
*/
public static function create( \IPS\nexus\Customer $customer )
public static function create( \IPS\nexus\Customer $customer, $admin )
{
$form = new \IPS\Helpers\Form;
$gateways = \IPS\nexus\Gateway::cardStorageGateways();
$showSubmitButton = FALSE;
$hiddenValues = array();
foreach ( static::createFormElements( $customer, $admin, $showSubmitButton, $hiddenValues ) as $element )
{
$form->add( $element );
}
foreach ( $hiddenValues as $k => $v )
{
$form->hiddenValues[ $k ] = $v;
}
if ( $values = $form->values() )
{
try
{
return static::createFormSubmit( $values, $customer, $admin );
}
catch ( \DomainException $e )
{
$form->error = $e->getMessage();
}
}
return $form->customTemplate( array( \IPS\Theme::i()->getTemplate( 'forms', 'nexus', 'global' ), 'addPaymentMethodForm' ), $showSubmitButton );
}
/**
* Get form elements for creating a new card
*
* @param \IPS\nexus\Customer $customer The customer
* @param bool $admin Set to TRUE if the *admin* (opposed to the customer themselves) wants to create a new payment method
* @param bool $showSubmitButton Will be set to a bool indicating if the submit button should be shown
* @param array $hiddenValues Hidden values to add to the form
* @return array
*/
public static function createFormElements( \IPS\nexus\Customer $customer, $admin, &$showSubmitButton, &$hiddenValues )
{
$gateways = \IPS\nexus\Gateway::cardStorageGateways( $admin );
$elements = array();
$paymentMethodsToggles = array();
foreach ( $gateways as $gateway )
{
$invoice = new \IPS\nexus\Invoice;
foreach ( $gateway->paymentScreen( $invoice, $invoice->total, $customer ) as $element )
$invoice->currency = $customer->defaultCurrency();
foreach ( $gateway->paymentScreen( $invoice, $invoice->total, $customer, array(), 'card' ) as $element )
{
if ( !$element->htmlId )
{
@@ -111,61 +150,90 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
}
}
if ( count( $gateways ) > 1 )
if ( \count( $gateways ) > 1 )
{
$showSubmitButton = FALSE;
$options = array();
$toggles = array();
foreach ( $gateways as $gateway )
{
$options[ $gateway->id ] = $gateway->_title;
}
if ( $gateway->showSubmitButton() )
{
$showSubmitButton = TRUE;
$paymentMethodsToggles[ $gateway->id ][] = 'paymentMethodSubmit';
}
}
$element = new \IPS\Helpers\Form\Radio( 'payment_method', NULL, TRUE, array( 'options' => $options, 'toggles' => $paymentMethodsToggles ) );
$element->label = \IPS\Member::loggedIn()->language()->addToStack('card_gateway');
$form->add( $element );
$element->label = \IPS\Member::loggedIn()->language()->addToStack('card_gateway');
array_unshift( $elements, $element );
}
else
{
foreach ( $gateways as $gateway )
{
$form->hiddenValues['payment_method'] = $gateway->id;
$hiddenValues['payment_method'] = $gateway->id;
$showSubmitButton = $gateway->showSubmitButton();
}
}
foreach ( $elements as $element )
return $elements;
}
/**
* Handle submission of the form for creating a new card
*
* @param array $values Values from the form
* @param \IPS\nexus\Customer $customer The customer
* @param bool $admin Set to TRUE if the *admin* (opposed to the customer themselves) wants to create a new payment method
* @param ?\IPS\nexus\Invoice $invoice If customer is a guest, will sa`ve the guest data onto the provided invoice
* @return \IPS\nexus\CreditCard
* @throws \DomainException
*/
public static function createFormSubmit( $values, $customer, $admin, \IPS\nexus\Invoice $invoice = NULL )
{
if ( isset( $values['payment_method'] ) )
{
$form->add( $element );
if ( $values['payment_method'] != 0 )
{
$gateway = \IPS\nexus\Gateway::load( $values['payment_method'] );
}
}
else
{
$gateways = \IPS\nexus\Gateway::cardStorageGateways( $admin );
$gateway = array_pop( $gateways );
}
if ( $values = $form->values() )
{
if ( !$values[ \IPS\Request::i()->payment_method . '_card' ] )
if ( !$values[ $gateway->id . '_card' ] )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack('card_number_invalid') );
}
else
{
$classname = \IPS\nexus\Gateway::gateways()[ $gateway->gateway ] . '\\CreditCard';
$card = new $classname;
$card->member = $customer;
$card->method = $gateway;
if ( \is_array( $values[ $gateway->id . '_card' ] ) )
{
$form->error = \IPS\Member::loggedIn()->language()->addToStack('card_number_invalid');
$_card = new \IPS\nexus\CreditCard;
$_card->token = $values[ $gateway->id . '_card' ]['token'];
$card->set_card( $_card, $invoice );
}
else
{
try
{
$gateway = \IPS\nexus\Gateway::load( \IPS\Request::i()->payment_method );
$classname = 'IPS\nexus\Gateway\\' . $gateway->gateway . '\CreditCard';
$card = new $classname;
$card->member = $customer;
$card->method = $gateway;
$card->card = $values[ $gateway->id . '_card' ];
$card->save();
$customer->log( 'card', array( 'type' => 'add', 'number' => $card->card->lastFour ) );
return $card;
}
catch ( \DomainException $e )
{
$form->error = $e->getMessage();
}
$card->set_card( $values[ $gateway->id . '_card' ], $invoice );
}
$card->save();
$customer->log( 'card', array( 'type' => 'add', 'number' => $card->card->lastFour ) );
return $card;
}
return $form;
}
/**
@@ -181,12 +249,12 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
/**
* Set member
*
* @param \IPS\Member
* @param \IPS\Member $member Member
* @return void
*/
public function set_member( \IPS\Member $member )
{
$this->_data['member'] = $member->member_id;
$this->_data['member'] = $member->member_id ?: 0;
}
/**
@@ -202,7 +270,7 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
/**
* Set payment gateway
*
* @param \IPS\nexus\Gateway
* @param \IPS\nexus\Gateway $gateway Payment gateway
* @return void
*/
public function set_method( \IPS\nexus\Gateway $gateway )
@@ -12,7 +12,7 @@
namespace IPS\nexus\Customer;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -171,12 +171,7 @@ class _CustomField extends \IPS\CustomField
{
parent::delete();
static::recountCustomerFields();
if ( isset( static::$multitons[ $this->id ] ) )
{
unset( static::$multitons[ $this->id ] );
}
/* Do we need to do stuff with profile steps? */
\IPS\Member\ProfileStep::resync();
}
@@ -188,7 +183,7 @@ class _CustomField extends \IPS\CustomField
*/
protected static function recountCustomerFields()
{
$count = count( static::roots() );
$count = \count( static::roots() );
\IPS\Settings::i()->changeValues( array( 'customer_fields' => $count ) );
}
@@ -205,4 +200,17 @@ class _CustomField extends \IPS\CustomField
return parent::formatFormValues( $values );
}
/**
* [ActiveRecord] Duplicate
*
* @return void
*/
public function __clone()
{
parent::__clone();
$this->column = "field_{$this->id}";
$this->save();
}
}
+117 -40
View File
@@ -12,7 +12,7 @@
namespace IPS\nexus;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -67,7 +67,7 @@ class _Customer extends \IPS\Member
$where = array( array( 'core_members.' . $idField . '=?', $id ) );
if( $extraWhereClause !== NULL )
{
if ( !is_array( $extraWhereClause ) or !is_array( $extraWhereClause[0] ) )
if ( !\is_array( $extraWhereClause ) or !\is_array( $extraWhereClause[0] ) )
{
$extraWhereClause = array( $extraWhereClause );
}
@@ -93,7 +93,7 @@ class _Customer extends \IPS\Member
/* Guests */
if( $id === NULL OR $id === 0 OR $id === '' )
{
$classname = get_called_class();
$classname = \get_called_class();
return new $classname;
}
@@ -104,7 +104,7 @@ class _Customer extends \IPS\Member
}
/* If we did, check it's valid */
elseif( !in_array( $idField, static::$databaseIdFields ) )
elseif( !\in_array( $idField, static::$databaseIdFields ) )
{
throw new \InvalidArgumentException;
}
@@ -256,7 +256,7 @@ class _Customer extends \IPS\Member
{
foreach ( $currencies as $k => $v )
{
if ( in_array( $this->language()->id, $v ) )
if ( \in_array( $this->language()->id, $v ) )
{
return $k;
}
@@ -270,6 +270,33 @@ class _Customer extends \IPS\Member
return \IPS\Settings::i()->nexus_currency;
}
}
/**
* @brief Cache primary billing address in case the method is called multiple times
*/
protected $primaryBillingAddress = FALSE;
/**
* Get primary billing address, if one exists
*
* @return \IPS\GeoLocation|NULL
*/
public function primaryBillingAddress()
{
if( $this->primaryBillingAddress === FALSE )
{
try
{
$this->primaryBillingAddress = \IPS\GeoLocation::buildFromJson( \IPS\Db::i()->select( 'address', 'nexus_customer_addresses', array( '`member`=? AND primary_billing=1', $this->member_id ) )->first() );
}
catch ( \UnderflowException $e )
{
$this->primaryBillingAddress = NULL;
}
}
return $this->primaryBillingAddress;
}
/**
* Estimated location
@@ -280,47 +307,48 @@ class _Customer extends \IPS\Member
{
if ( $this->member_id === \IPS\nexus\Customer::loggedIn()->member_id )
{
if ( isset( $_SESSION['location'] ) )
if ( isset( \IPS\Request::i()->cookie['location'] ) AND ( !\IPS\Request::i()->cookie['location'] OR ( \IPS\Request::i()->cookie['location'] AND $data = json_decode( \IPS\Request::i()->cookie['location'], true ) AND isset( $data['member_id'] ) AND $data['member_id'] == $this->member_id ) ) )
{
$location = $_SESSION['location'] ? \IPS\GeoLocation::buildFromJson( $_SESSION['location'] ) : NULL;
$location = NULL;
if( \IPS\Request::i()->cookie['location'] AND $data = json_decode( \IPS\Request::i()->cookie['location'], true ) AND $data['member_id'] == $this->member_id )
{
$location = \IPS\GeoLocation::buildFromJson( \IPS\Request::i()->cookie['location'] );
}
}
else
{
try
{
$defaultAddress = \IPS\Db::i()->select( 'address', 'nexus_customer_addresses', array( 'member=? AND primary_billing=1', $this->member_id ) )->first();
$_SESSION['location'] = $defaultAddress;
$location = \IPS\GeoLocation::buildFromJson( $defaultAddress );
}
catch ( \UnderflowException $e )
$location = $this->primaryBillingAddress();
if ( !$location )
{
try
{
$location = \IPS\GeoLocation::getByIp( \IPS\Request::i()->ipAddress() );
$_SESSION['location'] = json_encode( $location );
\IPS\Request::i()->setCookie( 'location', $this->_addMemberToAddress( $location ) );
}
catch ( \Exception $e )
{
if( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) )
{
$exploded = explode( '-', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
if ( in_array( mb_strtoupper( $exploded[1] ), \IPS\GeoLocation::$countries ) )
$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
$exploded = explode( '-', array_shift( $languages ) );
if ( \in_array( mb_strtoupper( $exploded[1] ), \IPS\GeoLocation::$countries ) )
{
$location = new \IPS\GeoLocation;
$location->country = mb_strtoupper( $exploded[1] );
$_SESSION['location'] = json_encode( $location );
\IPS\Request::i()->setCookie( 'location', $this->_addMemberToAddress( $location ) );
}
else
{
$location = NULL;
$_SESSION['location'] = '';
\IPS\Request::i()->setCookie( 'location', '' );
}
}
else
{
$location = NULL;
$_SESSION['location'] = '';
\IPS\Request::i()->setCookie( 'location', '' );
}
}
}
@@ -328,18 +356,22 @@ class _Customer extends \IPS\Member
}
else
{
try
{
$location = \IPS\GeoLocation::buildFromJson( \IPS\Db::i()->select( 'address', 'nexus_customer_addresses', array( 'member=? AND primary_billing=1', $this->member_id ) )->first() );
}
catch ( \UnderflowException $e )
{
$location = NULL;
}
$location = $this->primaryBillingAddress();
}
return $location;
}
/**
* Add the member ID to the address object so we can validate it on subsequent requests
*
* @param \IPS\GeoLocation $location Location object
* @return string
*/
protected function _addMemberToAddress( $location )
{
return json_encode( array_merge( json_decode( json_encode( $location ), true ), array( 'member_id' => $this->member_id ) ) );
}
/**
* Save Changed Columns
@@ -353,7 +385,7 @@ class _Customer extends \IPS\Member
$customerTable = array();
foreach ( ( $this->_new ? $this->_data : $this->changed ) as $k => $v )
{
if ( ( mb_substr( $k, 0, 3 ) === 'cm_' and !in_array( $k, array( 'cm_credits', 'cm_no_sev', 'cm_return_group', 'cm_reg' ) ) ) or mb_substr( $k, 0, 6 ) === 'field_' )
if ( ( mb_substr( $k, 0, 3 ) === 'cm_' and !\in_array( $k, array( 'cm_credits', 'cm_no_sev', 'cm_return_group', 'cm_reg' ) ) ) or mb_substr( $k, 0, 6 ) === 'field_' )
{
$customerTable[ $k ] = $v;
unset( $this->_data[ $k ] );
@@ -365,7 +397,7 @@ class _Customer extends \IPS\Member
$data['member_id'] = $this->_data['member_id'];
$this->_data = $data;
if ( count( $customerTable ) )
if ( \count( $customerTable ) )
{
$customerTable['member_id'] = $this->member_id;
\IPS\Db::i()->insert( 'nexus_customers', $customerTable, TRUE );
@@ -397,7 +429,7 @@ class _Customer extends \IPS\Member
{
$return[] = (string) new \IPS\nexus\Money( $amount['amount'], $amount['t_currency'] );
}
return count( $return ) ? implode( ' + ', $return ) : new \IPS\nexus\Money( 0, $this->defaultCurrency() );
return \count( $return ) ? implode( ' + ', $return ) : new \IPS\nexus\Money( 0, $this->defaultCurrency() );
}
/**
@@ -437,28 +469,28 @@ class _Customer extends \IPS\Member
}
$return[] = 'addresses';
if ( count( \IPS\nexus\Gateway::cardStorageGateways() ) )
if ( \count( \IPS\nexus\Gateway::cardStorageGateways() ) or \count( \IPS\nexus\Gateway::otherStoredPaymentMethodGateways() ) )
{
$return[] = 'cards';
}
if ( count( \IPS\nexus\Customer\CustomField::roots() ) )
if ( \count( \IPS\nexus\Customer\CustomField::roots() ) )
{
$return[] = 'info';
}
if ( \IPS\Settings::i()->nexus_min_topup or count( json_decode( \IPS\Settings::i()->nexus_payout, TRUE ) ) )
if ( \IPS\Settings::i()->nexus_min_topup or \count( json_decode( \IPS\Settings::i()->nexus_payout, TRUE ) ) )
{
$return[] = 'credit';
}
$return[] = 'alternatives';
if ( count( \IPS\nexus\Donation\Goal::roots() ) )
if ( \count( \IPS\nexus\Donation\Goal::roots() ) )
{
$return[] = 'donations';
}
if ( \IPS\Settings::i()->cm_ref_on )
if ( \IPS\Settings::i()->ref_on )
{
$return[] = 'referrals';
}
@@ -474,7 +506,7 @@ class _Customer extends \IPS\Member
*/
public function alternativeContacts( $where = array() )
{
$where = count( $where ) ? array( $where ) : $where;
$where = \count( $where ) ? array( $where ) : $where;
$where[] = array( 'main_id=?', $this->member_id );
return new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_alternate_contacts', $where )->setKeyField( 'alt_id' ), 'IPS\nexus\Customer\AlternativeContact' );
}
@@ -487,7 +519,7 @@ class _Customer extends \IPS\Member
*/
public function parentContacts( $where = array() )
{
$where = count( $where ) ? array( $where ) : $where;
$where = \count( $where ) ? array( $where ) : $where;
$where[] = array( 'alt_id=?', $this->member_id );
return new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_alternate_contacts', $where )->setKeyField( 'main_id' ), 'IPS\nexus\Customer\AlternativeContact' );
}
@@ -500,5 +532,50 @@ class _Customer extends \IPS\Member
public function acpUrl()
{
return parent::acpUrl()->setQueryString( 'tab', 'nexus_Main' );
}
}
/**
* ACP Customer Page URL
*
* @param \IPS\Math\Number $amount Adjustment amount
* @param string $currency Currency code
* @param bool $refund Should the spend be reduced?
*
* @return void
*/
public function updateSpend( \IPS\Math\Number $amount, $currency, $refund=false )
{
try
{
$currentSpend = \IPS\Db::i()->select( 'spend_amount', 'nexus_customer_spend', array( "spend_member_id=? AND spend_currency=?", $this->member_id, $currency ) )->first();
$newSpend = new \IPS\Math\Number( number_format( $currentSpend, \IPS\nexus\Money::numberOfDecimalsForCurrency( $currency ), '.', '' ) );
$newSpend = ( $refund ) ? $newSpend->subtract( $amount ) : $newSpend->add( $amount );
\IPS\Db::i()->replace( 'nexus_customer_spend', array( 'spend_member_id' => $this->member_id, 'spend_amount' => $newSpend, 'spend_currency' => $currency ) );
}
catch ( \UnderflowException $e )
{
\IPS\Db::i()->insert( 'nexus_customer_spend', array( 'spend_member_id' => $this->member_id, 'spend_amount' => ( $refund ) ? $amount->multiply( new \IPS\Math\Number( "-1" ) ) : $amount, 'spend_currency' => $currency ) );
}
}
/**
* Recalculate total amount spent in all currencies
*
* @return void
*/
public function recountTotalSpend()
{
\IPS\Db::i()->delete( 'nexus_customer_spend', array( 'spend_member_id=?', $this->member_id ) );
$spend = array();
foreach ( \IPS\Db::i()->select( 't_currency, ( SUM(t_amount)-SUM(t_partial_refund) ) AS amount', 'nexus_transactions', array( 't_member=? AND ( t_status=? OR t_status=? )', $this->member_id, \IPS\nexus\Transaction::STATUS_PAID, \IPS\nexus\Transaction::STATUS_PART_REFUNDED ), NULL, NULL, 't_currency' ) as $amount )
{
$spend[] = array( 'spend_member_id' => $this->member_id, 'spend_amount' => $amount['amount'], 'spend_currency' => $amount['t_currency'] );
}
if( \count( $spend ) )
{
\IPS\Db::i()->insert( 'nexus_customer_spend', $spend );
}
}
}