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,70 @@
|
||||
namespace IPS\nexus;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DateInterval;
|
||||
use IPS\Data\Cache;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\GeoLocation;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\Helpers\Form\Matrix;
|
||||
use IPS\Helpers\Form\Number;
|
||||
use IPS\Helpers\Form\Radio;
|
||||
use IPS\Helpers\Form\Translatable;
|
||||
use IPS\Http\Request\Exception;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Lang;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Form\StateSelect;
|
||||
use IPS\Node\Model;
|
||||
use IPS\Xml\DOMDocument;
|
||||
use OutOfRangeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tax Rate Node
|
||||
*/
|
||||
class _Tax extends \IPS\Node\Model
|
||||
class Tax extends Model
|
||||
{
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Table
|
||||
*/
|
||||
public static $databaseTable = 'nexus_tax';
|
||||
public static ?string $databaseTable = 'nexus_tax';
|
||||
|
||||
/**
|
||||
* @brief [ActiveRecord] Database Prefix
|
||||
*/
|
||||
public static $databasePrefix = 't_';
|
||||
public static string $databasePrefix = 't_';
|
||||
|
||||
/**
|
||||
* @brief [Node] Order Database Column
|
||||
*/
|
||||
public static $databaseColumnOrder = 'order';
|
||||
public static ?string $databaseColumnOrder = 'order';
|
||||
|
||||
/**
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'tax_rates';
|
||||
public static string $nodeTitle = 'tax_rates';
|
||||
|
||||
/**
|
||||
* @brief [Node] Title prefix. If specified, will look for a language key with "{$key}_title" as the key
|
||||
*/
|
||||
public static $titleLangPrefix = 'nexus_tax_';
|
||||
public static ?string $titleLangPrefix = 'nexus_tax_';
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
@@ -69,7 +93,7 @@ class _Tax 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' => 'payments',
|
||||
'all' => 'tax_manage',
|
||||
@@ -78,10 +102,10 @@ class _Tax extends \IPS\Node\Model
|
||||
/**
|
||||
* [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
|
||||
{
|
||||
$defaultVal = array( 'consumer' => 0, 'business' => 0, 'eu' => 0 );
|
||||
if ( $this->rate )
|
||||
@@ -108,8 +132,8 @@ class _Tax extends \IPS\Node\Model
|
||||
|
||||
$form->addTab( 'tax_settings' );
|
||||
$form->addHeader('tax_basic_settings');
|
||||
$form->add( new \IPS\Helpers\Form\Translatable( 'tax_name', NULL, TRUE, array( 'app' => 'nexus', 'key' => $this->id ? "nexus_tax_{$this->id}" : NULL, 'placeholder' => \IPS\Member::loggedIn()->language()->addToStack('tax_name_placeholder') ) ) );
|
||||
$form->add( new \IPS\Helpers\Form\Radio( 'tax_rate_type', $this->type, TRUE, array(
|
||||
$form->add( new Translatable( 'tax_name', NULL, TRUE, array( 'app' => 'nexus', 'key' => $this->id ? "nexus_tax_{$this->id}" : NULL, 'placeholder' => Member::loggedIn()->language()->addToStack('tax_name_placeholder') ) ) );
|
||||
$form->add( new Radio( 'tax_rate_type', $this->type, TRUE, array(
|
||||
'options' => array(
|
||||
'single' => 'tax_rate_type_single',
|
||||
'business' => 'tax_rate_type_business',
|
||||
@@ -123,62 +147,62 @@ class _Tax extends \IPS\Node\Model
|
||||
) ) );
|
||||
$form->addHeader('tax_default');
|
||||
$form->addMessage('tax_default_desc');
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'tax_default_single', $defaultVal['consumer'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_single' ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'tax_default_consumer', $defaultVal['consumer'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_consumer' ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'tax_default_business', $defaultVal['business'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_business' ) );
|
||||
$form->add( new \IPS\Helpers\Form\Number( 'tax_default_eu', $defaultVal['eu'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_eu' ) );
|
||||
$form->add( new Number( 'tax_default_single', $defaultVal['consumer'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_single' ) );
|
||||
$form->add( new Number( 'tax_default_consumer', $defaultVal['consumer'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_consumer' ) );
|
||||
$form->add( new Number( 'tax_default_business', $defaultVal['business'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_business' ) );
|
||||
$form->add( new Number( 'tax_default_eu', $defaultVal['eu'], TRUE, array( 'decimals' => 2 ), NULL, NULL, '%', 'tax_default_eu' ) );
|
||||
|
||||
$form->addTab( 'tax_rates_tab' );
|
||||
$singleMatrix = new \IPS\Helpers\Form\Matrix;
|
||||
$singleMatrix = new Matrix;
|
||||
$singleMatrix->squashFields = FALSE;
|
||||
$singleMatrix->columns = array(
|
||||
'tax_locations' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\nexus\Form\StateSelect( $key, $value, FALSE, array( 'options' => array( 'foo' ), 'multiple' => TRUE ) );
|
||||
return new StateSelect( $key, $value, FALSE, array( 'options' => array( 'foo' ), 'multiple' => TRUE ) );
|
||||
},
|
||||
'tax_rate' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\Helpers\Form\Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
return new Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
}
|
||||
);
|
||||
$doubleMatrix = new \IPS\Helpers\Form\Matrix;
|
||||
$doubleMatrix = new Matrix;
|
||||
$doubleMatrix->squashFields = FALSE;
|
||||
$doubleMatrix->columns = array(
|
||||
'tax_locations' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\nexus\Form\StateSelect( $key, $value, FALSE, array( 'options' => array( 'foo' ), 'multiple' => TRUE ) );
|
||||
return new StateSelect( $key, $value, FALSE, array( 'options' => array( 'foo' ), 'multiple' => TRUE ) );
|
||||
},
|
||||
'tax_rate_consumer' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\Helpers\Form\Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
return new Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
},
|
||||
'tax_rate_business' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\Helpers\Form\Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
return new Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
},
|
||||
);
|
||||
$tripleMatrix = new \IPS\Helpers\Form\Matrix;
|
||||
$tripleMatrix = new Matrix;
|
||||
$tripleMatrix->squashFields = FALSE;
|
||||
$tripleMatrix->columns = array(
|
||||
'tax_locations' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\nexus\Form\StateSelect( $key, $value, FALSE, array( 'options' => array( 'foo' ), 'multiple' => TRUE ) );
|
||||
return new StateSelect( $key, $value, FALSE, array( 'options' => array( 'foo' ), 'multiple' => TRUE ) );
|
||||
},
|
||||
'tax_rate_consumer' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\Helpers\Form\Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
return new Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
},
|
||||
'tax_rate_business' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\Helpers\Form\Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
return new Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
},
|
||||
'tax_rate_eu' => function( $key, $value, $data )
|
||||
{
|
||||
return new \IPS\Helpers\Form\Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
return new Number( $key, $value, FALSE, array( 'decimals' => 2 ), NULL, NULL, '%' );
|
||||
},
|
||||
);
|
||||
$rates = json_decode( $this->rate, TRUE );
|
||||
if ( \is_array( $rates ) )
|
||||
$rates = $this->rate ? json_decode( $this->rate, TRUE ) : [];
|
||||
if ( is_array( $rates ) )
|
||||
{
|
||||
foreach ( $rates as $rate )
|
||||
{
|
||||
@@ -186,18 +210,18 @@ class _Tax extends \IPS\Node\Model
|
||||
{
|
||||
$singleMatrix->rows[] = array(
|
||||
'tax_locations' => $rate['locations'],
|
||||
'tax_rate' => isset( $rate['consumer'] ) ? $rate['consumer'] : $rate['rate'],
|
||||
'tax_rate' => $rate['consumer'] ?? $rate['rate'],
|
||||
);
|
||||
$doubleMatrix->rows[] = array(
|
||||
'tax_locations' => $rate['locations'],
|
||||
'tax_rate_consumer' => isset( $rate['consumer'] ) ? $rate['consumer'] : $rate['rate'],
|
||||
'tax_rate_business' => isset( $rate['business'] ) ? $rate['business'] : $rate['rate'],
|
||||
'tax_rate_consumer' => $rate['consumer'] ?? $rate['rate'],
|
||||
'tax_rate_business' => $rate['business'] ?? $rate['rate'],
|
||||
);
|
||||
$tripleMatrix->rows[] = array(
|
||||
'tax_locations' => $rate['locations'],
|
||||
'tax_rate_consumer' => isset( $rate['consumer'] ) ? $rate['consumer'] : $rate['rate'],
|
||||
'tax_rate_business' => isset( $rate['business'] ) ? $rate['business'] : $rate['rate'],
|
||||
'tax_rate_eu' => isset( $rate['eu'] ) ? $rate['eu'] : $rate['rate'],
|
||||
'tax_rate_consumer' => $rate['consumer'] ?? $rate['rate'],
|
||||
'tax_rate_business' => $rate['business'] ?? $rate['rate'],
|
||||
'tax_rate_eu' => $rate['eu'] ?? $rate['rate'],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -214,7 +238,7 @@ class _Tax extends \IPS\Node\Model
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
public function formatFormValues( array $values ): array
|
||||
{
|
||||
$rates = array();
|
||||
|
||||
@@ -238,8 +262,8 @@ class _Tax extends \IPS\Node\Model
|
||||
$rates[] = array(
|
||||
'locations' => $rate['tax_locations'],
|
||||
'consumer' => $rate['tax_rate_consumer'],
|
||||
'business' => isset( $rate['tax_rate_business'] ) ? $rate['tax_rate_business'] : $rate['tax_rate_consumer'],
|
||||
'eu' => isset( $rate['tax_rate_eu'] ) ? $rate['tax_rate_eu'] : ( isset( $rate['tax_rate_business'] ) ? $rate['tax_rate_business'] : $rate['tax_rate_consumer'] ),
|
||||
'business' => $rate['tax_rate_business'] ?? $rate['tax_rate_consumer'],
|
||||
'eu' => $rate['tax_rate_eu'] ?? ($rate['tax_rate_business'] ?? $rate['tax_rate_consumer']),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -256,7 +280,7 @@ class _Tax extends \IPS\Node\Model
|
||||
$rates[] = array(
|
||||
'locations' => '*',
|
||||
'consumer' => $values['tax_default_consumer'],
|
||||
'business' => ( \in_array( $values['type'], array( 'business', 'eu' ) ) ) ? $values['tax_default_business'] : $values['tax_default_consumer'],
|
||||
'business' => ( in_array( $values['type'], array( 'business', 'eu' ) ) ) ? $values['tax_default_business'] : $values['tax_default_consumer'],
|
||||
'eu' => ( $values['type'] === 'eu' ) ? $values['tax_default_eu'] : ( ( $values['type'] === 'business' ) ? $values['tax_default_business'] : $values['tax_default_consumer'] )
|
||||
);
|
||||
}
|
||||
@@ -275,7 +299,7 @@ class _Tax extends \IPS\Node\Model
|
||||
$name = $values['tax_name'];
|
||||
unset( $values['tax_name'] );
|
||||
$this->save();
|
||||
\IPS\Lang::saveCustom( 'nexus', "nexus_tax_{$this->id}", $name );
|
||||
Lang::saveCustom( 'nexus', "nexus_tax_{$this->id}", $name );
|
||||
}
|
||||
|
||||
return $values;
|
||||
@@ -284,11 +308,11 @@ class _Tax extends \IPS\Node\Model
|
||||
/**
|
||||
* [Node] Delete
|
||||
*
|
||||
* @return void
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
public function delete(): void
|
||||
{
|
||||
\IPS\Db::i()->update( 'nexus_packages', array( 'p_tax' => 0 ), array( "p_tax=?", $this->_id ) );
|
||||
Db::i()->update( 'nexus_packages', array( 'p_tax' => 0 ), array( "p_tax=?", $this->_id ) );
|
||||
|
||||
parent::delete();
|
||||
}
|
||||
@@ -298,10 +322,10 @@ class _Tax extends \IPS\Node\Model
|
||||
*
|
||||
* For example, if the rate for the location is 10%, will return 0.1
|
||||
*
|
||||
* @param \IPS\GeoLocation|NULL $billingAddress The billing address
|
||||
* @return string Result after running through number_format, which is a float represented as a string
|
||||
* @param GeoLocation|NULL $billingAddress The billing address
|
||||
* @return mixed Result after running through number_format, which is a float represented as a string
|
||||
*/
|
||||
public function rate( \IPS\GeoLocation $billingAddress = NULL )
|
||||
public function rate( ?GeoLocation $billingAddress = NULL ) : mixed
|
||||
{
|
||||
$type = 'consumer';
|
||||
if ( isset( $billingAddress->business ) and $billingAddress->business )
|
||||
@@ -314,13 +338,13 @@ class _Tax extends \IPS\Node\Model
|
||||
{
|
||||
if ( $rate['locations'] === '*' )
|
||||
{
|
||||
$defaultVal = isset( $rate[ $type ] ) ? $rate[ $type ] : $rate['rate'];
|
||||
$defaultVal = $rate[$type] ?? $rate['rate'];
|
||||
}
|
||||
elseif ( $billingAddress and isset( $rate['locations'][ $billingAddress->country ] ) )
|
||||
{
|
||||
if ( $rate['locations'][ $billingAddress->country ] === '*' or \in_array( $billingAddress->region, $rate['locations'][ $billingAddress->country ] ) )
|
||||
if ( $rate['locations'][ $billingAddress->country ] === '*' or in_array( $billingAddress->region, $rate['locations'][ $billingAddress->country ] ) )
|
||||
{
|
||||
return number_format( ( ( isset( $rate[ $type ] ) ? $rate[ $type ] : $rate['rate'] ) / 100 ), 5, '.', '' );
|
||||
return number_format( ( ($rate[$type] ?? $rate['rate']) / 100 ), 5, '.', '' );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -331,12 +355,12 @@ class _Tax extends \IPS\Node\Model
|
||||
/**
|
||||
* Get output for API
|
||||
*
|
||||
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @param Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @return array
|
||||
* @apiresponse int id ID Number
|
||||
* @apiresponse string name Name
|
||||
*/
|
||||
public function apiOutput( \IPS\Member $authorizedMember = NULL )
|
||||
public function apiOutput( ?Member $authorizedMember = NULL ): array
|
||||
{
|
||||
return array(
|
||||
'id' => $this->id,
|
||||
@@ -350,18 +374,18 @@ class _Tax extends \IPS\Node\Model
|
||||
*
|
||||
* @param string $vatNumber The VAT number
|
||||
* @return array|null
|
||||
* @throws \IPS\Http\Request\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function validateVAT( string $vatNumber )
|
||||
public static function validateVAT( string $vatNumber ) : array|null
|
||||
{
|
||||
/* Have we cached the response? */
|
||||
$vatNumber = mb_strtoupper( preg_replace( '/[^A-Z0-9]/', '', $vatNumber ) );
|
||||
$cacheKey = 'vatValidate-' . $vatNumber;
|
||||
try
|
||||
{
|
||||
return \IPS\Data\Cache::i()->getWithExpire( $cacheKey, TRUE );
|
||||
return Cache::i()->getWithExpire( $cacheKey, TRUE );
|
||||
}
|
||||
catch( \OutOfRangeException $e ){}
|
||||
catch( OutOfRangeException ){}
|
||||
|
||||
/* Split up the number */
|
||||
$countryCode = mb_substr( $vatNumber, 0, 2 );
|
||||
@@ -372,22 +396,22 @@ class _Tax extends \IPS\Node\Model
|
||||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns1="urn:ec.europa.eu:taxud:vies:services:checkVat:types" xmlns:impl="urn:ec.europa.eu:taxud:vies:services:checkVat"><soap:Header></soap:Header><soap:Body><tns1:checkVat xmlns:tns1="urn:ec.europa.eu:taxud:vies:services:checkVat:types" xmlns="urn:ec.europa.eu:taxud:vies:services:checkVat:types"><tns1:countryCode>{$countryCode}</tns1:countryCode><tns1:vatNumber>{$vatNumberWithoutCountryCode}</tns1:vatNumber></tns1:checkVat></soap:Body></soap:Envelope>
|
||||
XML;
|
||||
/* Send the request */
|
||||
$request = \IPS\Http\Url::external('https://ec.europa.eu/taxation_customs/vies/services/checkVatService')->request()->setHeaders( [ 'Content-Type' => 'text/xml' ] );
|
||||
$request = Url::external('https://ec.europa.eu/taxation_customs/vies/services/checkVatService')->request()->setHeaders( [ 'Content-Type' => 'text/xml' ] );
|
||||
$response = $request->post( $xml );
|
||||
|
||||
if( $response->httpResponseCode != 200 )
|
||||
{
|
||||
throw new \IPS\Http\Request\Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
|
||||
/* Interpret */
|
||||
$responseArray = array();
|
||||
$doc = new \IPS\Xml\DOMDocument;
|
||||
$doc->loadXML( \IPS\Xml\DOMDocument::wrapHtml( (string) $response ) );
|
||||
$doc = new DOMDocument;
|
||||
$doc->loadXML( DOMDocument::wrapHtml( (string) $response ) );
|
||||
|
||||
$data = iterator_to_array( $doc->getElementsByTagName( 'checkVatResponse' ) );
|
||||
|
||||
if ( isset( $data[0] ) AND \count( $data[0]->childNodes ) )
|
||||
if ( isset( $data[0] ) AND count( $data[0]->childNodes ) )
|
||||
{
|
||||
foreach( $data[0]->childNodes AS $child )
|
||||
{
|
||||
@@ -398,12 +422,12 @@ XML;
|
||||
/* Cache and return */
|
||||
if ( isset( $responseArray['valid'] ) and $responseArray['valid'] === 'true' )
|
||||
{
|
||||
\IPS\Data\Cache::i()->storeWithExpire( $cacheKey, $responseArray, \IPS\DateTime::create()->add( new \DateInterval( 'P7D' ) ), TRUE );
|
||||
Cache::i()->storeWithExpire( $cacheKey, $responseArray, DateTime::create()->add( new DateInterval( 'P7D' ) ), TRUE );
|
||||
return $responseArray;
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Data\Cache::i()->storeWithExpire( $cacheKey, NULL, \IPS\DateTime::create()->add( new \DateInterval( 'PT10S' ) ), TRUE );
|
||||
Cache::i()->storeWithExpire( $cacheKey, NULL, DateTime::create()->add( new DateInterval( 'PT10S' ) ), TRUE );
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user