Version 4.1.19

This commit is contained in:
Neo committed 2025-12-19 05:38:56 -08:00
1 parent 28bd025b35
commit 2bd5025018
2674 files changed
+233518 -77766

No files matched your search

@@ -2,10 +2,10 @@
/**
* @brief Send Invoice Warnings Task
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @subpackage nexus
* @package IPS Community Suite
* @subpackage Nexus
* @since 01 Apr 2014
* @version SVN_VERSION_NUMBER
*/
@@ -39,47 +39,95 @@ class _sendInvoiceWarnings extends \IPS\Task
{
if ( \IPS\Settings::i()->cm_invoice_warning )
{
$cutoff = \IPS\DateTime::create()->add( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_generate . 'D' ) )->add( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_warning . 'D' ) )->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_expire<?', $cutoff ), 'ps_member', 50 );
$normalCutoff = \IPS\DateTime::create()->add( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_generate . 'D' ) )->add( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_warning . 'D' ) )->getTimestamp();
$billingAgreementCutoff = \IPS\DateTime::create()->add( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_warning . 'D' ) )->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 );
$groupedPurchases = array();
foreach ( new \IPS\Patterns\ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
{
$groupedPurchases[ $purchase->member->member_id ][ $purchase->id ] = $purchase;
$groupedPurchases[ $purchase->member->member_id ][ $purchase->billing_agreement AND !$purchase->billing_agreement->canceled ? $purchase->billing_agreement->id : 0 ][ $purchase->renewal_currency ][ $purchase->id ] = $purchase;
}
/* Loop */
foreach ( $groupedPurchases as $memberId => $purchases )
foreach ( $groupedPurchases as $memberId => $_groupedPurchases )
{
$member = \IPS\nexus\Customer::load( $memberId );
$cards = \IPS\Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) );
try
foreach ( $_groupedPurchases as $billingAgreementId => $__groupedPurchases )
{
$card = \IPS\nexus\Customer\CreditCard::constructFromData( $cards->first() );
$invoice = new \IPS\nexus\Invoice; // We're not actually going to save this, it's just to know what the charges will be
foreach ( $purchases as $purchase )
foreach ( $__groupedPurchases as $currency => $purchases )
{
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $purchase ) );
}
\IPS\Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( $card, $invoice, $invoice->summary() ) )
->send(
$member,
array_map(
function( $contact )
{
return $contact->alt_id->email;
},
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() )
);
}
catch ( \UnderflowException $e ) {}
$email = NULL;
/* Create a temporary invoice (we're not going to save this) so that we knkow what the charges will be */
$invoice = new \IPS\nexus\Invoice;
foreach ( $purchases as $purchase )
{
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $purchase ) );
}
$invoice->setDefaultTitle();
/* If there is a billing agreement - send an email abiout that */
$billingAgreement = NULL;
if ( $billingAgreementId )
{
try
{
$billingAgreement = \IPS\nexus\Customer\BillingAgreement::load( $billingAgreementId );
\IPS\Db::i()->update( 'nexus_purchases', array( 'ps_invoice_warning_sent' => 1 ), \IPS\Db::i()->in( 'ps_id', array_keys( $purchases ) ) );
if ( $billingAgreement->status() == $billingAgreement::STATUS_CANCELED )
{
throw new \OutOfRangeException;
}
}
catch ( \OutOfRangeException $e ) { }
}
if ( $billingAgreement )
{
$email = \IPS\Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( array(), NULL, $billingAgreement, $invoice, $invoice->summary() ), \IPS\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 )
{
try
{
$cardDetails = $card->card; // We're just checking this doesn't throw an exception
$cards[] = $card;
}
catch ( \Exception $e ) { }
}
$credits = $member->cm_credits;
$credit = isset( $credits[ $currency ] ) ? $credits[ $currency ]->amount : ( new \IPS\Math\Number( '0' ) );
if ( count( $cards ) or $credit->isGreaterThanZero() )
{
$email = \IPS\Email::buildFromTemplate( 'nexus', 'invoiceWarning', array( $cards, isset( $credits[ $currency ] ) ? $credits[ $currency ] : ( new \IPS\nexus\Money( 0, $currency ) ), NULL, $invoice, $invoice->summary() ), \IPS\Email::TYPE_TRANSACTIONAL );
}
}
/* Send the email */
if ( $email )
{
$email->send(
$member,
array_map(
function( $contact )
{
return $contact->alt_id->email;
},
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() )
);
}
/* Update Purchases */
\IPS\Db::i()->update( 'nexus_purchases', array( 'ps_invoice_warning_sent' => 1 ), \IPS\Db::i()->in( 'ps_id', array_keys( $purchases ) ) );
}
}
}
}
}