Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+28 -20
View File
@@ -12,16 +12,28 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use Exception;
use IPS\Db;
use IPS\Log;
use IPS\nexus\Customer\BillingAgreement;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Task;
use Throwable;
use UnderflowException;
use function defined;
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;
}
/**
* billingAgreements Task
*/
class _billingAgreements extends \IPS\Task
class billingAgreements extends Task
{
/**
* Execute
@@ -31,13 +43,14 @@ class _billingAgreements extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Task\Exception
*/
public function execute()
public function execute() : string|null
{
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_billing_agreements', array( 'ba_next_cycle<?', time() ), 'ba_next_cycle ASC', 50 ), 'IPS\nexus\Customer\BillingAgreement' ) as $billingAgreement )
foreach( new ActiveRecordIterator( Db::i()->select( '*', 'nexus_billing_agreements', array( 'ba_next_cycle<?', time() ), 'ba_next_cycle ASC', 50 ), 'IPS\nexus\Customer\BillingAgreement' ) as $billingAgreement )
{
/* @var BillingAgreement $billingAgreement */
try
{
/* Get the status? */
@@ -47,7 +60,7 @@ class _billingAgreements extends \IPS\Task
{
$billingAgreement->canceled = TRUE;
}
throw new \DomainException("{$billingAgreement->id} - {$billingAgreement->status()}");
throw new DomainException("{$billingAgreement->id} - {$billingAgreement->status()}");
}
/* Get the term */
@@ -56,27 +69,22 @@ class _billingAgreements extends \IPS\Task
/* Check for a recent transaction */
$billingAgreement->checkForLatestTransaction();
}
catch( \UnderflowException $e ){} // We intentionally ignore this because it's normal
catch( UnderflowException ){} // We intentionally ignore this because it's normal
catch ( \IPS\Http\Url\Exception $e )
{
/* Just log the issue, but don't do anything here,.. this was probably a temporary issue so let the next call run this again */
\IPS\Log::log( $e, 'ba_sync_fail' );
Log::log( $e, 'ba_sync_fail' );
}
catch ( \Exception $e )
catch ( Exception|Throwable $e )
{
\IPS\Log::log( $e, 'ba_sync_fail' );
$billingAgreement->next_cycle = NULL;
$billingAgreement->save();
}
catch ( \Throwable $e )
{
\IPS\Log::log( $e, 'ba_sync_fail' );
Log::log( $e, 'ba_sync_fail' );
$billingAgreement->next_cycle = NULL;
$billingAgreement->save();
}
}
return null;
}
/**
@@ -88,7 +96,7 @@ class _billingAgreements extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
+32 -17
View File
@@ -12,16 +12,27 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateInterval;
use IPS\DateTime;
use IPS\Db;
use IPS\nexus\Transaction;
use IPS\Task;
use IPS\Task\Exception;
use UnderflowException;
use function defined;
use function in_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;
}
/**
* Task to capture payments approaching their authorization deadlines
*/
class _capture extends \IPS\Task
class capture extends Task
{
/**
* Execute
@@ -31,19 +42,19 @@ class _capture extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Exception
*/
public function execute()
public function execute() : string|null
{
$taskFrequency = new \DateInterval( $this->frequency );
$time = \IPS\DateTime::create()->add( $taskFrequency )->add( $taskFrequency );
$taskFrequency = new DateInterval( $this->frequency );
$time = DateTime::create()->add( $taskFrequency )->add( $taskFrequency );
$this->runUntilTimeout( function() use ( $time )
{
try
{
$transaction = \IPS\nexus\Transaction::constructFromData( \IPS\Db::i()->select( '*', 'nexus_transactions', array( 't_auth<?', $time->getTimestamp() ), 't_auth ASC', 1 )->first() );
$transaction = Transaction::constructFromData( Db::i()->select( '*', 'nexus_transactions', array( 't_auth<?', $time->getTimestamp() ), 't_auth ASC', 1 )->first() );
if ( $transaction->method )
{
@@ -53,11 +64,11 @@ class _capture extends \IPS\Task
}
catch ( \Exception $e )
{
if ( \in_array( $transaction->status, array( $transaction::STATUS_PENDING, $transaction::STATUS_WAITING, $transaction::STATUS_GATEWAY_PENDING ) ) )
if ( in_array( $transaction->status, array( $transaction::STATUS_PENDING, $transaction::STATUS_WAITING, $transaction::STATUS_GATEWAY_PENDING ) ) )
{
$transaction->status = \IPS\nexus\Transaction::STATUS_REFUSED;
$transaction->status = Transaction::STATUS_REFUSED;
$extra = $transaction->extra;
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'noteRaw' => $e->getMessage() );
$extra['history'][] = array( 's' => Transaction::STATUS_REFUSED, 'noteRaw' => $e->getMessage() );
$transaction->extra = $extra;
}
@@ -70,19 +81,23 @@ class _capture extends \IPS\Task
/* the gateway doesn't exist anymore, so reset the auth time */
$transaction->auth = NULL;
$extra = $transaction->extra;
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'on' => time(), 'noteRaw' => 'invalid_gateway' );
$extra['history'][] = array( 's' => Transaction::STATUS_REFUSED, 'on' => time(), 'noteRaw' => 'invalid_gateway' );
$transaction->extra = $extra;
$transaction->status = \IPS\nexus\Transaction::STATUS_REFUSED;
$transaction->status = Transaction::STATUS_REFUSED;
$transaction->save();
throw new \IPS\Task\Exception( $this, array( 'invalid_gateway', $transaction->id ) );
throw new Exception( $this, array( 'invalid_gateway', $transaction->id ) );
}
}
catch ( \UnderflowException $e )
catch ( UnderflowException )
{
return FALSE;
}
return NULL;
});
return null;
}
/**
@@ -94,7 +109,7 @@ class _capture extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
+18 -10
View File
@@ -12,16 +12,24 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Db;
use IPS\File;
use IPS\Session\Store;
use IPS\Task;
use IPS\Task\Exception;
use function defined;
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;
}
/**
* cartCleanup Task
*/
class _cartCleanup extends \IPS\Task
class cartCleanup extends Task
{
/**
* Execute
@@ -31,21 +39,21 @@ class _cartCleanup extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Exception
*/
public function execute()
public function execute() : string|null
{
$ids = array();
/* Fetch active session IDs */
foreach ( \IPS\Db::i()->select( 'nexus_cart_uploads.id', 'nexus_cart_uploads', array( 'time<? AND ' . \IPS\Db::i()->in( 'session_id', \IPS\Session\Store::i()->getSessionids(), true ), ( time() - 86400 ) ) ) as $id )
foreach ( Db::i()->select( 'nexus_cart_uploads.id', 'nexus_cart_uploads', array( 'time<? AND ' . Db::i()->in( 'session_id', Store::i()->getSessionids(), true ), ( time() - 86400 ) ) ) as $id )
{
\IPS\File::unclaimAttachments( 'nexus_Purchases', $id, NULL, 'cart' );
File::unclaimAttachments( 'nexus_Purchases', $id, NULL, 'cart' );
$ids[] = $id;
}
\IPS\Db::i()->delete( 'nexus_cart_uploads', \IPS\Db::i()->in( 'id', $ids ) );
Db::i()->delete( 'nexus_cart_uploads', Db::i()->in( 'id', $ids ) );
return NULL;
}
@@ -59,7 +67,7 @@ class _cartCleanup extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
+31 -15
View File
@@ -12,16 +12,30 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateInterval;
use IPS\DateTime;
use IPS\Db;
use IPS\Email;
use IPS\Member;
use IPS\nexus\Invoice;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Settings;
use IPS\Task;
use IPS\Task\Exception;
use OutOfRangeException;
use function defined;
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;
}
/**
* Expire Invoices Task
*/
class _expireInvoices extends \IPS\Task
class expireInvoices extends Task
{
/**
* Execute
@@ -31,15 +45,15 @@ class _expireInvoices extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Exception
*/
public function execute()
public function execute() : string|null
{
$expireDate = \IPS\DateTime::create();
if( \IPS\Settings::i()->cm_invoice_expireafter )
$expireDate = DateTime::create();
if( Settings::i()->cm_invoice_expireafter )
{
$expireDate->sub( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_expireafter . 'D' ) );
$expireDate->sub( new DateInterval( 'P' . Settings::i()->cm_invoice_expireafter . 'D' ) );
}
else
{
@@ -47,16 +61,16 @@ class _expireInvoices extends \IPS\Task
return NULL;
}
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_invoices', array( 'i_status=? AND i_date<?', \IPS\nexus\Invoice::STATUS_PENDING, $expireDate->getTimestamp() ), 'i_date ASC', 100 ), 'IPS\nexus\Invoice' ) as $invoice )
foreach ( new ActiveRecordIterator( Db::i()->select( '*', 'nexus_invoices', array( 'i_status=? AND i_date<?', Invoice::STATUS_PENDING, $expireDate->getTimestamp() ), 'i_date ASC', 100 ), 'IPS\nexus\Invoice' ) as $invoice )
{
$invoice->status = $invoice::STATUS_EXPIRED;
$invoice->save();
/* Send tracked notifications */
foreach ( \IPS\Db::i()->select( 'member_id', 'nexus_invoice_tracker', array( 'invoice_id=?', $invoice->id ) ) as $trackingMember )
foreach ( Db::i()->select( 'member_id', 'nexus_invoice_tracker', array( 'invoice_id=?', $invoice->id ) ) as $trackingMember )
{
$trackingMember = \IPS\Member::load( $trackingMember );
\IPS\Email::buildFromTemplate( 'nexus', 'invoiceTrackExpired', array( $invoice, $invoice->summary( $trackingMember->language() ) ), \IPS\Email::TYPE_LIST )
$trackingMember = Member::load( $trackingMember );
Email::buildFromTemplate( 'nexus', 'invoiceTrackExpired', array( $invoice, $invoice->summary() ), Email::TYPE_LIST )
->send( $trackingMember );
}
@@ -68,8 +82,10 @@ class _expireInvoices extends \IPS\Task
'title' => $invoice->title,
), FALSE );
}
catch ( \OutOfRangeException $exception ) {}
catch ( OutOfRangeException ) {}
}
return null;
}
/**
@@ -81,7 +97,7 @@ class _expireInvoices extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
+17 -8
View File
@@ -12,16 +12,23 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Db;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Task;
use IPS\Task\Exception;
use function defined;
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;
}
/**
* Expire Purchases Task
*/
class _expirePurchases extends \IPS\Task
class expirePurchases extends Task
{
/**
* Execute
@@ -31,12 +38,12 @@ class _expirePurchases extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Exception
*/
public function execute()
public function execute() : string|null
{
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_purchases', array( 'ps_active=1 AND ps_expire>0 AND ( ps_expire + ps_grace_period < ? )', time() ), 'ps_expire ASC', 100 ), 'IPS\nexus\Purchase' ) as $purchase )
foreach ( new ActiveRecordIterator( Db::i()->select( '*', 'nexus_purchases', array( 'ps_active=1 AND ps_expire>0 AND ( ps_expire + ps_grace_period < ? )', time() ), 'ps_expire ASC', 100 ), 'IPS\nexus\Purchase' ) as $purchase )
{
$purchase->active = FALSE;
$purchase->save();
@@ -47,6 +54,8 @@ class _expirePurchases extends \IPS\Task
'name' => $purchase->name,
), FALSE );
}
return null;
}
/**
@@ -58,7 +67,7 @@ class _expirePurchases extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
+28 -28
View File
@@ -12,16 +12,25 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use Exception;
use IPS\Db;
use IPS\nexus\Gateway\Paypal;
use IPS\nexus\Transaction;
use IPS\Task;
use function count;
use function defined;
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;
}
/**
* gatewayPending Task
*/
class _gatewayPending extends \IPS\Task
class gatewayPending extends Task
{
/**
* Execute
@@ -31,25 +40,13 @@ class _gatewayPending extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Task\Exception
*/
public function execute()
public function execute() : string|null
{
$paypalGateways = [];
foreach( \IPS\nexus\Gateway::roots( null ) as $gateway )
{
if( $gateway instanceof \IPS\nexus\Gateway\PayPal )
{
$paypalGateways[] = $gateway->_id;
}
}
$transactions = \IPS\Db::i()->select( '*', 'nexus_transactions', array(
array( 't_status=?', \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING ),
array( \IPS\Db::i()->in( 't_method', $paypalGateways ) )
), 't_id ASC', 20 ); // We deliberately don't run until exhaustion here because we want to wait and try again later if it fails
if ( !\count( $transactions ) )
$transactions = Db::i()->select( '*', 'nexus_transactions', array( 't_status=?', Transaction::STATUS_GATEWAY_PENDING ), 't_id ASC', 20 ); // We deliberately don't run until exhaustion here because we want to wait and try again later if it fails
if ( !count( $transactions ) )
{
$this->enabled = FALSE;
$this->save();
@@ -59,20 +56,21 @@ class _gatewayPending extends \IPS\Task
foreach ( $transactions as $transaction )
{
/* Get it */
$transaction = \IPS\nexus\Transaction::constructFromData( $transaction );
$transaction = Transaction::constructFromData( $transaction );
if ( $transaction->method instanceof \IPS\nexus\Gateway\Paypal )
/* @var Transaction $transaction */
if ( $transaction->method instanceof Paypal )
{
/* Try to capture it */
try
{
$transaction->capture();
}
catch ( \Exception $e )
catch ( Exception $e )
{
if ( $e->getMessage() === 'FAIL' or $e->getMessage() === 'RFND' or $transaction->date->getTimestamp() < ( time() - ( 86400 * 5 ) ) )
{
$status = $e->getMessage() === 'RFND' ? \IPS\nexus\Transaction::STATUS_REFUNDED : \IPS\nexus\Transaction::STATUS_REFUSED;
$status = $e->getMessage() === 'RFND' ? Transaction::STATUS_REFUNDED : Transaction::STATUS_REFUSED;
$transaction->status = $status;
$transaction->save();
@@ -82,7 +80,7 @@ class _gatewayPending extends \IPS\Task
'id' => $transaction->id
), FALSE );
if ( $status === \IPS\nexus\Transaction::STATUS_REFUNDED )
if ( $status === Transaction::STATUS_REFUNDED )
{
$transaction->sendNotification();
}
@@ -94,9 +92,9 @@ class _gatewayPending extends \IPS\Task
$fraudResult = $transaction->fraud_blocked ? $transaction->fraud_blocked->action : NULL;
if ( $fraudResult )
{
$transaction->executeFraudAction( $fraudResult, TRUE );
$transaction->executeFraudAction( $fraudResult );
}
if ( !$fraudResult or $fraudResult === \IPS\nexus\Transaction::STATUS_PAID )
if ( !$fraudResult or $fraudResult === Transaction::STATUS_PAID )
{
$transaction->approve();
}
@@ -106,6 +104,8 @@ class _gatewayPending extends \IPS\Task
}
}
}
return null;
}
/**
@@ -117,7 +117,7 @@ class _gatewayPending extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
@@ -12,16 +12,39 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateInterval;
use Exception;
use IPS\DateTime;
use IPS\Db;
use IPS\Db\Select;
use IPS\Math\Number;
use IPS\nexus\Customer;
use IPS\nexus\Gateway;
use IPS\nexus\Invoice;
use IPS\nexus\Invoice\Item\Renewal;
use IPS\nexus\Money;
use IPS\nexus\Tax;
use IPS\nexus\Transaction;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Settings;
use IPS\Task;
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;
}
/**
* Generate Renewal Invoices Task
*/
class _generateRenewalInvoices extends \IPS\Task
class generateRenewalInvoices extends Task
{
/**
* Execute
@@ -31,25 +54,25 @@ class _generateRenewalInvoices extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Task\Exception
*/
public function execute()
public function execute() : string|null
{
/* Get purchases grouped by member and currency */
$select = $this->_getSelectQuery();
$log = \IPS\Db::_replaceBinds( $select->query, $select->binds ) . "\n" . \count( $select ) . " matches\n\n";
$availableTaxes = \IPS\nexus\Tax::roots();
$log = Db::_replaceBinds( $select->query, $select->binds ) . "\n" . count( $select ) . " matches\n\n";
$availableTaxes = Tax::roots();
$groupedPurchases = array();
foreach ( new \IPS\Patterns\ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
foreach ( new ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
{
/* If the member does not exist, we should not lock the task */
try
{
$groupedPurchases[ $purchase->member->member_id ][ $purchase->renewal_currency ][ $purchase->id ] = $purchase;
}
catch( \OutOfRangeException $e )
catch( OutOfRangeException )
{
/* Set the purchase inactive so we don't try again. */
$purchase->active = 0;
@@ -60,13 +83,13 @@ class _generateRenewalInvoices extends \IPS\Task
/* Loop */
foreach ( $groupedPurchases as $memberId => $currencies )
{
$member = \IPS\nexus\Customer::load( $memberId );
$member = Customer::load( $memberId );
foreach ( $currencies as $currency => $purchases )
{
$log .= "Member {$memberId}, {$currency}: " . \count( $purchases ) . " purchase(s) to be renewed: " . implode( ', ', array_keys( $purchases ) ) . ". ";
$log .= "Member {$memberId}, {$currency}: " . count( $purchases ) . " purchase(s) to be renewed: " . implode( ', ', array_keys( $purchases ) ) . ". ";
/* Create Invoice */
$invoice = new \IPS\nexus\Invoice;
$invoice = new Invoice;
$invoice->system = TRUE;
$invoice->currency = $currency;
$invoice->member = $member;
@@ -91,7 +114,7 @@ class _generateRenewalInvoices extends \IPS\Task
}
/* Continue to next invoice if no items left */
if( !\count( $items ) )
if( !count( $items ) )
{
continue;
}
@@ -99,14 +122,14 @@ class _generateRenewalInvoices extends \IPS\Task
/* Add items to invoice */
foreach( $items as $item )
{
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $item ) );
$invoice->addItem( Renewal::create( $item ) );
}
$invoice->save();
$log .= "Invoice {$invoice->id} generated... ";
/* Try to take payment automatically, but *only* if we have a billing address (i.e. the customer has a primary billing address set)
otherwise we don't know how we're taxing this and the customer will need to manually come and pay it - we can skip this if tax has not been configured */
if ( $invoice->billaddress OR \count( $availableTaxes ) === 0 )
if ( $invoice->billaddress OR count( $availableTaxes ) === 0 )
{
/* Nothing to pay? */
if ( $invoice->amountToPay()->amount->isZero() )
@@ -130,14 +153,14 @@ class _generateRenewalInvoices extends \IPS\Task
{
$take = NULL;
/* If credit is equal or larger than invoice value */
if ( \in_array( $credit->compare( $invoice->total->amount ), [ 0, 1 ] ) )
if ( in_array( $credit->compare( $invoice->total->amount ), [ 0, 1 ] ) )
{
$take = $invoice->total->amount;
}
else
{
/* Only use credit if amount remaining is greater than card gateway min amount */
if( $invoice->total->amount->subtract( $credit ) > new \IPS\Math\Number( '0.50' ) )
if( $invoice->total->amount->subtract( $credit ) > new Number( '0.50' ) )
{
$take = $credit;
}
@@ -147,10 +170,10 @@ class _generateRenewalInvoices extends \IPS\Task
{
$log .= "{$credit} account credit available... ";
$transaction = new \IPS\nexus\Transaction;
$transaction = new Transaction;
$transaction->member = $member;
$transaction->invoice = $invoice;
$transaction->amount = new \IPS\nexus\Money( $take, $currency );
$transaction->amount = new Money( $take, $currency );
$transaction->extra = array('automatic' => TRUE);
$transaction->save();
$transaction->approve();
@@ -159,7 +182,7 @@ class _generateRenewalInvoices extends \IPS\Task
$member->log( 'transaction', array(
'type' => 'paid',
'status' => \IPS\nexus\Transaction::STATUS_PAID,
'status' => Transaction::STATUS_PAID,
'id' => $transaction->id,
'invoice_id' => $invoice->id,
'invoice_title' => $invoice->title,
@@ -183,85 +206,45 @@ class _generateRenewalInvoices extends \IPS\Task
$allowedPaymentMethods = array();
foreach( $invoice->items as $item )
{
if( \is_array( $item->paymentMethodIds ) and !\in_array( '*', $item->paymentMethodIds ) )
if( is_array( $item->paymentMethodIds ) and !in_array( '*', $item->paymentMethodIds ) )
{
$allowedPaymentMethods = array_merge( $allowedPaymentMethods, $item->paymentMethodIds );
}
}
$cardWhere = array(
array( 'card_member=?', $member->member_id )
);
if( \count( $allowedPaymentMethods ) )
{
$cardWhere[] = array( \IPS\Db::i()->in( 'card_method', $allowedPaymentMethods ) );
}
/* Check all available gateways */
if( empty( $allowedPaymentMethods ) )
{
foreach( Gateway::roots() as $gateway )
{
$allowedPaymentMethods[] = $gateway->_id;
}
}
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', $cardWhere ), 'IPS\nexus\Customer\CreditCard' ) as $card )
/* Loop through each payment method and try to take payment */
foreach( $allowedPaymentMethods as $paymentMethodId )
{
$log .= "Attempting card {$card->id}... ";
try
{
$cardDetails = $card->card; // We're just checking this doesn't throw an exception
$amountToPay = $invoice->amountToPay();
$gateway = $card->method;
$transaction = new \IPS\nexus\Transaction;
$transaction->member = $member;
$transaction->invoice = $invoice;
$transaction->method = $gateway;
$transaction->amount = $amountToPay;
$transaction->currency = $currency;
$transaction->extra = array( 'automatic' => TRUE );
try
$gateway = Gateway::load( $paymentMethodId );
if( $gateway::SUPPORTS_AUTOPAY )
{
$transaction->auth = $gateway->auth( $transaction, array(
( $gateway->id . '_card' ) => $card
), NULL, array(), 'renewal' );
$transaction->capture();
$transaction->member->log( 'transaction', array(
'type' => 'paid',
'status' => \IPS\nexus\Transaction::STATUS_PAID,
'id' => $transaction->id,
'invoice_id' => $invoice->id,
'invoice_title' => $invoice->title,
'automatic' => TRUE,
), FALSE );
$transaction->approve();
$log .= "Transaction {$transaction->id} approved! ";
break;
foreach( $gateway->autopay( $invoice ) as $transaction )
{
if( $transaction->status == Transaction::STATUS_REFUSED )
{
$log .= "Transaction {$transaction->id} failed. ";
}
else
{
$log .= "Transaction {$transaction->id} approved! ";
}
$invoice->status = $transaction->invoice->status;
}
}
catch ( \Exception $e )
{
$transaction->status = \IPS\nexus\Transaction::STATUS_REFUSED;
$extra = $transaction->extra;
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'noteRaw' => $e->getMessage() );
$transaction->extra = $extra;
$transaction->save();
$log .= "Transaction {$transaction->id} failed. ";
$transaction->member->log( 'transaction', array(
'type' => 'paid',
'status' => \IPS\nexus\Transaction::STATUS_REFUSED,
'id' => $transaction->id,
'invoice_id' => $invoice->id,
'invoice_title' => $invoice->title,
'automatic' => TRUE,
), FALSE );
}
$invoice->status = $transaction->invoice->status;
}
// error with card, move on
catch ( \Exception $e ){}
catch( OutOfRangeException ){}
}
}
}
@@ -294,7 +277,7 @@ class _generateRenewalInvoices extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
@@ -302,19 +285,19 @@ class _generateRenewalInvoices extends \IPS\Task
/**
* Get Purchases Query
*
* @return \IPS\Db\Select
* @throws \Exception
* @return Select
* @throws Exception
*/
protected function _getSelectQuery(): \IPS\Db\Select
protected function _getSelectQuery(): Select
{
/* What's out cutoff? */
$renewalDate = \IPS\DateTime::create();
if( \IPS\Settings::i()->cm_invoice_generate )
$renewalDate = DateTime::create();
if( Settings::i()->cm_invoice_generate )
{
$renewalDate->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_generate . 'H' ) );
$renewalDate->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_generate . 'H' ) );
}
return \IPS\Db::i()->select( 'ps.*', [ 'nexus_purchases', 'ps' ],
return Db::i()->select( 'ps.*', [ 'nexus_purchases', 'ps' ],
[
"ps_cancelled=0 AND ps_renewals>0 AND ps_invoice_pending=0 AND ps_active=1 AND ps_expire>0 AND ps_expire<? AND (ps_billing_agreement IS NULL OR ba.ba_canceled=1) AND ( ps_grouped_renewals='' OR ps_grouped_renewals IS NULL )",
$renewalDate->getTimestamp()
+21 -11
View File
@@ -12,16 +12,26 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Db;
use IPS\nexus\Gateway;
use IPS\nexus\Payout;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Task;
use IPS\Task\Exception;
use function defined;
use function method_exists;
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;
}
/**
* payoutPending Task
*/
class _payoutPending extends \IPS\Task
class payoutPending extends Task
{
/**
* Execute
@@ -31,12 +41,12 @@ class _payoutPending extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Exception
*/
public function execute()
public function execute() : string|null
{
$iterator = new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_payouts', array( 'po_status=? AND po_gw_id IS NOT NULL', \IPS\nexus\Payout::STATUS_PROCESSING ) ), 'IPS\nexus\Payout' );
$iterator = new ActiveRecordIterator( Db::i()->select( '*', 'nexus_payouts', array( 'po_status=? AND po_gw_id IS NOT NULL', Payout::STATUS_PROCESSING ) ), 'IPS\nexus\Payout' );
if( !$iterator->count() )
{
@@ -47,14 +57,14 @@ class _payoutPending extends \IPS\Task
foreach( $iterator as $payout )
{
$payoutClass = \IPS\nexus\Gateway::payoutGateways()[ $payout->gateway ];
$payoutClass = Gateway::payoutGateways()[ $payout->gateway ];
if( !\method_exists( $payoutClass, 'checkStatus' ) )
if( !method_exists( $payoutClass, 'checkStatus' ) )
{
continue;
}
if( $payoutClass::checkStatus( $payout->gw_id ) == \IPS\nexus\Payout::STATUS_COMPLETE )
if( $payoutClass::checkStatus( $payout->gw_id ) == Payout::STATUS_COMPLETE )
{
$payout->markCompleted();
}
@@ -72,7 +82,7 @@ class _payoutPending extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
@@ -12,16 +12,38 @@
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateInterval;
use DomainException;
use Exception;
use IPS\DateTime;
use IPS\Db;
use IPS\Email;
use IPS\Math\Number;
use IPS\nexus\Customer;
use IPS\nexus\Customer\BillingAgreement;
use IPS\nexus\Invoice;
use IPS\nexus\Invoice\Item\Renewal;
use IPS\nexus\Money;
use IPS\nexus\Purchase;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Settings;
use IPS\Task;
use OutOfRangeException;
use function count;
use function defined;
use function in_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;
}
/**
* Send Invoice Warnings Task
*/
class _sendInvoiceWarnings extends \IPS\Task
class sendInvoiceWarnings extends Task
{
/**
* Execute
@@ -31,20 +53,21 @@ class _sendInvoiceWarnings extends \IPS\Task
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
* @return string|null Message to log or NULL
* @throws Task\Exception
*/
public function execute()
public function execute() : string|null
{
if ( \IPS\Settings::i()->cm_invoice_warning )
if ( Settings::i()->cm_invoice_warning )
{
$normalCutoff = \IPS\DateTime::create()->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_generate . 'H' ) )->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_warning . 'H' ) )->getTimestamp();
$billingAgreementCutoff = \IPS\DateTime::create()->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_warning . 'H' ) )->getTimestamp();
$select = \IPS\Db::i()->select( '*', 'nexus_purchases', array( 'ps_renewals>0 AND ps_invoice_pending=0 AND ps_invoice_warning_sent=0 AND ps_active=1 AND ps_expire>0 AND ( ( ps_billing_agreement IS NULL AND ps_expire<? ) OR ( ps_billing_agreement IS NOT NULL AND ps_expire<? ) )', $normalCutoff, $billingAgreementCutoff ), 'ps_member', 50 );
$normalCutoff = DateTime::create()->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_generate . 'H' ) )->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_warning . 'H' ) )->getTimestamp();
$billingAgreementCutoff = DateTime::create()->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_warning . 'H' ) )->getTimestamp();
$select = Db::i()->select( '*', 'nexus_purchases', array( 'ps_renewals>0 AND ps_invoice_pending=0 AND ps_invoice_warning_sent=0 AND ps_active=1 AND ps_expire>0 AND ( ( ps_billing_agreement IS NULL AND ps_expire<? ) OR ( ps_billing_agreement IS NOT NULL AND ps_expire<? ) )', $normalCutoff, $billingAgreementCutoff ), 'ps_member', 50 );
$groupedPurchases = array();
foreach ( new \IPS\Patterns\ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
foreach ( new ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
{
/* @var Purchase $purchase */
$agreementId = ( $purchase->billing_agreement AND !$purchase->billing_agreement->canceled ) ? $purchase->billing_agreement->id : 0;
if ( $purchase->onExpireWarning() )
@@ -61,7 +84,7 @@ class _sendInvoiceWarnings extends \IPS\Task
/* Loop */
foreach ( $groupedPurchases as $memberId => $_groupedPurchases )
{
$member = \IPS\nexus\Customer::load( $memberId );
$member = Customer::load( $memberId );
foreach ( $_groupedPurchases as $billingAgreementId => $__groupedPurchases )
{
foreach ( $__groupedPurchases as $currency => $purchases )
@@ -69,11 +92,11 @@ class _sendInvoiceWarnings extends \IPS\Task
$email = NULL;
/* Create a temporary invoice (we're not going to save this) so that we know what the charges will be */
$invoice = new \IPS\nexus\Invoice;
$invoice = new Invoice;
$invoice->currency = $currency;
foreach ( $purchases as $purchase )
{
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $purchase ) );
$invoice->addItem( Renewal::create( $purchase ) );
}
$invoice->setDefaultTitle();
@@ -83,7 +106,7 @@ class _sendInvoiceWarnings extends \IPS\Task
{
try
{
$billingAgreement = \IPS\nexus\Customer\BillingAgreement::load( $billingAgreementId );
$billingAgreement = BillingAgreement::load( $billingAgreementId );
if ( $billingAgreement->status() == $billingAgreement::STATUS_CANCELED )
{
@@ -91,35 +114,34 @@ class _sendInvoiceWarnings extends \IPS\Task
$billingAgreement = NULL;
}
}
catch ( \OutOfRangeException $e ) { }
catch ( OutOfRangeException|DomainException ) { }
/* Billing agreement may have been cancelled, but not yet marked cancelled */
catch ( \DomainException $e ) { }
}
if ( $billingAgreement )
{
$paymentDate = \IPS\DateTime::create()->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_warning . 'H' ) )->localeDate( $member );
$email = \IPS\Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( array(), NULL, $billingAgreement, $invoice, $invoice->summary( $member->language() ), $paymentDate ), \IPS\Email::TYPE_TRANSACTIONAL );
$paymentDate = DateTime::create()->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_warning . 'H' ) )->localeDate( $member );
$email = Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( array(), NULL, $billingAgreement, $invoice, $invoice->summary(), $paymentDate ), Email::TYPE_TRANSACTIONAL );
}
/* Otherwise check account credit and cards */
else
{
$cards = array();
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) ), 'IPS\nexus\Customer\CreditCard' ) as $card )
foreach ( new ActiveRecordIterator( Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) ), 'IPS\nexus\Customer\CreditCard' ) as $card )
{
try
{
$cardDetails = $card->card; // We're just checking this doesn't throw an exception
$cards[] = $card;
}
catch ( \Exception $e ) { }
catch ( Exception ) { }
}
$credits = $member->cm_credits;
$credit = isset( $credits[ $currency ] ) ? $credits[ $currency ]->amount : ( new \IPS\Math\Number( '0' ) );
$credit = isset( $credits[ $currency ] ) ? $credits[ $currency ]->amount : ( new Number( '0' ) );
if ( \count( $cards ) or $credit->isGreaterThanZero() )
if ( count( $cards ) or $credit->isGreaterThanZero() )
{
$paymentDate = \IPS\DateTime::create()->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_warning . 'H' ) )->localeDate( $member );
$email = \IPS\Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( $cards, isset( $credits[ $currency ] ) ? $credits[ $currency ] : ( new \IPS\nexus\Money( 0, $currency ) ), NULL, $invoice, $invoice->summary( $member->language() ), $paymentDate ), \IPS\Email::TYPE_TRANSACTIONAL );
$paymentDate = DateTime::create()->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_warning . 'H' ) )->localeDate( $member );
$email = Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( $cards, isset( $credits[ $currency ] ) ? $credits[ $currency ] : ( new Money( 0, $currency ) ), NULL, $invoice, $invoice->summary(), $paymentDate ), Email::TYPE_TRANSACTIONAL );
}
}
@@ -135,16 +157,18 @@ class _sendInvoiceWarnings extends \IPS\Task
},
iterator_to_array( $member->alternativeContacts( array( 'billing=1' ) ) )
),
( ( \in_array( 'invoice_warn', explode( ',', \IPS\Settings::i()->nexus_notify_copy_types ) ) AND \IPS\Settings::i()->nexus_notify_copy_email ) ? explode( ',', \IPS\Settings::i()->nexus_notify_copy_email ) : array() )
( ( in_array( 'invoice_warn', explode( ',', Settings::i()->nexus_notify_copy_types ) ) AND Settings::i()->nexus_notify_copy_email ) ? explode( ',', Settings::i()->nexus_notify_copy_email ) : array() )
);
}
/* Update Purchases */
\IPS\Db::i()->update( 'nexus_purchases', array( 'ps_invoice_warning_sent' => 1 ), \IPS\Db::i()->in( 'ps_id', array_keys( $purchases ) ) );
Db::i()->update( 'nexus_purchases', array( 'ps_invoice_warning_sent' => 1 ), Db::i()->in( 'ps_id', array_keys( $purchases ) ) );
}
}
}
}
}
return null;
}
/**
@@ -156,7 +180,7 @@ class _sendInvoiceWarnings extends \IPS\Task
*
* @return void
*/
public function cleanup()
public function cleanup() : void
{
}
@@ -1,83 +0,0 @@
<?php
/**
* @brief supportAssignedReminder Task
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @subpackage Nexus
* @since 25 Apr 2014
*/
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* supportAssignedReminder Task
*/
class _supportAssignedReminder extends \IPS\Task
{
/**
* Execute
*
* If ran successfully, should return anything worth logging. Only log something
* worth mentioning (don't log "task ran successfully"). Return NULL (actual NULL, not '' or 0) to not log (which will be most cases).
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
*/
public function execute()
{
/* Don't send reminders if the application is disabled */
if( !\IPS\Application::appIsEnabled('nexus') )
{
return NULL;
}
$openStatuses = iterator_to_array( \IPS\Db::i()->select( 'status_id', 'nexus_support_statuses', 'status_open=1' ) );
$staffIds = array_keys( \IPS\nexus\Support\Request::staff() );
$sendTo = array();
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_support_requests', array(
array( \IPS\Db::i()->in( 'r_status', $openStatuses ) ),
array( 'r_staff<>0' )
) ), 'IPS\nexus\Support\Request' ) as $request )
{
$sendTo[ $request->staff->member_id ][ $request->id ] = $request;
}
foreach ( $sendTo as $staffId => $requests )
{
if ( \in_array( $staffId, $staffIds ) )
{
$email = \IPS\Email::buildFromTemplate( 'nexus', 'staffAssignedReminder', array( $requests ), \IPS\Email::TYPE_TRANSACTIONAL )->send( \IPS\Member::load( $staffId ) );
}
else
{
\IPS\Db::i()->update( 'nexus_support_requests', array( 'r_staff' => 0 ), array( 'r_staff=?', $staffId ) );
}
}
}
/**
* Cleanup
*
* If your task takes longer than 15 minutes to run, this method
* will be called before execute(). Use it to clean up anything which
* may not have been done
*
* @return void
*/
public function cleanup()
{
}
}
@@ -1,101 +0,0 @@
<?php
/**
* @brief supportAutoresolve Task
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @subpackage Nexus
* @since 25 Apr 2014
*/
namespace IPS\nexus\tasks;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* supportAutoresolve Task
*/
class _supportAutoresolve extends \IPS\Task
{
/**
* Execute
*
* If ran successfully, should return anything worth logging. Only log something
* worth mentioning (don't log "task ran successfully"). Return NULL (actual NULL, not '' or 0) to not log (which will be most cases).
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
* Tasks should execute within the time of a normal HTTP request.
*
* @return mixed Message to log or NULL
* @throws \IPS\Task\Exception
*/
public function execute()
{
if ( \IPS\Settings::i()->nexus_autoresolve_days AND \IPS\Settings::i()->nexus_autoresolve_status != '' )
{
/* Build where */
$where = array();
$where[] = array( \IPS\Db::i()->in( 'r_status', array_filter( explode( ',', \IPS\Settings::i()->nexus_autoresolve_applicable ), function( $val ) {
return $val != \IPS\Settings::i()->nexus_autoresolve_status;
} ) ) );
if ( \IPS\Settings::i()->nexus_autoresolve_departments !== '*' )
{
$where[] = array( \IPS\Db::i()->in( 'r_department', explode( ',', \IPS\Settings::i()->nexus_autoresolve_departments ) ) );
}
/* Resolve */
$resolveWhere = array_merge( $where, array( array( 'r_last_reply<?', \IPS\DateTime::create()->sub( new \DateInterval( 'P' . \IPS\Settings::i()->nexus_autoresolve_days . 'D' ) )->getTimestamp() ) ) );
$resolvedStatus = \IPS\nexus\Support\Status::load( \IPS\Settings::i()->nexus_autoresolve_status );
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_support_requests', $resolveWhere ), 'IPS\nexus\Support\Request' ) as $request )
{
$oldStatus = $request->status;
$request->status = $resolvedStatus;
$request->save();
$request->log( 'autoresolve', $oldStatus, $resolvedStatus, FALSE );
}
/* Warnings */
if ( \IPS\Settings::i()->nexus_autoresolve_notify )
{
$warningWhere = array_merge( $where, array(
array( 'r_last_reply<?', \IPS\DateTime::create()->sub( new \DateInterval( 'P' . \IPS\Settings::i()->nexus_autoresolve_days . 'D' ) )->add( new \DateInterval( 'PT' . \IPS\Settings::i()->nexus_autoresolve_notify . 'H' ) )->getTimestamp() ),
array( 'r_ar_notify=0' )
) );
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_support_requests', $warningWhere ), 'IPS\nexus\Support\Request' ) as $request )
{
$fromEmail = $request->department->email ?: \IPS\Settings::i()->email_out;
$email = \IPS\Email::buildFromTemplate( 'nexus', 'autoresolveWarning', array( $request, \IPS\DateTime::ts( $request->last_reply )->add( new \DateInterval( 'P' . \IPS\Settings::i()->nexus_autoresolve_days . 'D' ) ) ), \IPS\Email::TYPE_TRANSACTIONAL );
$email->send( $request->email ?: $request->author(), [], [], $fromEmail, NULL, [ 'Message-ID' => "<IPS-000-SR{$request->id}.{$request->email_key}-{$fromEmail}>" ] );
$request->ar_notify = TRUE;
$request->save();
$request->log( 'autoresolve_warning', NULL, NULL, FALSE );
}
}
}
else
{
$this->enabled = FALSE;
$this->save();
}
}
/**
* Cleanup
*
* If your task takes longer than 15 minutes to run, this method
* will be called before execute(). Use it to clean up anything which
* may not have been done
*
* @return void
*/
public function cleanup()
{
}
}