2020-07-07 14:33:11 +02:00
< ? php
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2020-07-07 14:33:11 +02:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
* @ copyright Copyright ( c ) 2020. Invoice Ninja LLC ( https :// invoiceninja . com )
*
* @ license https :// opensource . org / licenses / AAL
*/
namespace App\Services\Invoice ;
2020-07-08 04:20:44 +02:00
use App\DataMapper\InvoiceItem ;
2020-07-07 14:33:11 +02:00
use App\Events\Payment\PaymentWasCreated ;
use App\Factory\PaymentFactory ;
use App\Models\Client ;
2020-10-12 06:10:34 +02:00
use App\Models\Credit ;
2020-07-07 14:33:11 +02:00
use App\Models\Invoice ;
use App\Models\Payment ;
2020-09-04 00:01:17 +02:00
use App\Models\PaymentHash ;
2020-07-07 14:33:11 +02:00
use App\Services\AbstractService ;
use App\Services\Client\ClientService ;
use App\Services\Payment\PaymentService ;
2020-10-08 05:31:02 +02:00
use App\Utils\Ninja ;
2020-07-07 14:33:11 +02:00
use App\Utils\Traits\GeneratesCounter ;
2020-09-04 00:01:17 +02:00
use Illuminate\Support\Str ;
2020-07-07 14:33:11 +02:00
class AutoBillInvoice extends AbstractService
{
private $invoice ;
2020-09-06 11:38:10 +02:00
private $client ;
2020-07-07 14:33:11 +02:00
2020-10-08 02:28:23 +02:00
private $used_credit = [];
2020-10-07 13:03:53 +02:00
2020-07-07 14:33:11 +02:00
public function __construct ( Invoice $invoice )
{
$this -> invoice = $invoice ;
2020-09-06 11:38:10 +02:00
2020-07-07 14:33:11 +02:00
$this -> client = $invoice -> client ;
}
public function run ()
{
2020-10-07 13:03:53 +02:00
/* Is the invoice payable? */
if ( ! $this -> invoice -> isPayable ())
2020-07-08 04:20:44 +02:00
return $this -> invoice ;
2020-10-07 13:03:53 +02:00
/* Mark the invoice as sent */
2020-07-15 08:08:57 +02:00
$this -> invoice = $this -> invoice -> service () -> markSent () -> save ();
2020-10-07 13:03:53 +02:00
/* Mark the invoice as paid if there is no balance */
if (( int ) $this -> invoice -> balance == 0 )
2020-07-13 01:29:44 +02:00
return $this -> invoice -> service () -> markPaid () -> save ();
2020-07-13 00:28:19 +02:00
2020-10-09 03:59:59 +02:00
//if the credits cover the payments, we stop here, build the payment with credits and exit early
$this -> applyCreditPayment ();
2020-07-07 14:33:11 +02:00
2020-10-12 06:10:34 +02:00
info ( " partial = { $this -> invoice -> partial } " );
info ( " balance = { $this -> invoice -> balance } " );
2020-10-07 13:03:53 +02:00
/* Determine $amount */
if ( $this -> invoice -> partial > 0 )
2020-09-04 00:01:17 +02:00
$amount = $this -> invoice -> partial ;
2020-10-08 02:28:23 +02:00
elseif ( $this -> invoice -> balance > 0 )
2020-09-04 00:01:17 +02:00
$amount = $this -> invoice -> balance ;
2020-10-07 13:03:53 +02:00
else
2020-10-08 02:28:23 +02:00
return $this -> finalizePaymentUsingCredits ();
2020-10-07 13:03:53 +02:00
2020-10-12 06:10:34 +02:00
info ( " balance remains to be paid!! " );
2020-10-12 06:30:53 +02:00
2020-10-07 13:03:53 +02:00
$gateway_token = $this -> getGateway ( $amount );
/* Bail out if no payment methods available */
if ( ! $gateway_token || ! $gateway_token -> gateway -> driver ( $this -> client ) -> token_billing )
return $this -> invoice ;
/* $gateway fee */
2020-10-12 13:25:27 +02:00
$fee = $gateway_token -> gateway -> calcGatewayFee ( $amount , $this -> invoice -> uses_inclusive_taxes );
2020-07-08 04:20:44 +02:00
2020-10-12 06:58:07 +02:00
//todo determine exact fee as per PaymentController
2020-10-12 06:30:53 +02:00
2020-10-07 13:03:53 +02:00
/* Build payment hash */
2020-09-04 00:01:17 +02:00
$payment_hash = PaymentHash :: create ([
'hash' => Str :: random ( 128 ),
2020-10-14 12:45:26 +02:00
'data' => [[ 'invoice_id' => $this -> invoice -> hashed_id , 'amount' => $amount ]],
2020-09-04 00:01:17 +02:00
'fee_total' => $fee ,
'fee_invoice_id' => $this -> invoice -> id ,
]);
2020-07-08 04:20:44 +02:00
2020-10-07 13:03:53 +02:00
$payment = $gateway_token -> gateway
-> driver ( $this -> client )
-> tokenBilling ( $gateway_token , $payment_hash );
2020-08-30 12:47:32 +02:00
2020-07-15 07:05:02 +02:00
return $this -> invoice ;
2020-07-07 14:33:11 +02:00
}
2020-10-08 02:28:23 +02:00
/**
* If the credits on file cover the invoice amount
* the we create a matching payment using credits only
*
* @ return Invoice $invoice
*/
private function finalizePaymentUsingCredits ()
{
2020-10-13 12:32:15 +02:00
2020-10-08 02:28:23 +02:00
$amount = array_sum ( array_column ( $this -> used_credit , 'amount' ));
$payment = PaymentFactory :: create ( $this -> invoice -> company_id , $this -> invoice -> user_id );
$payment -> amount = $amount ;
2020-10-12 06:10:34 +02:00
$payment -> applied = $amount ;
2020-10-08 05:31:02 +02:00
$payment -> client_id = $this -> invoice -> client_id ;
$payment -> currency_id = $this -> invoice -> client -> getSetting ( 'currency_id' );
2020-10-08 02:28:23 +02:00
$payment -> date = now ();
2020-10-08 05:31:02 +02:00
$payment -> status_id = Payment :: STATUS_COMPLETED ;
$payment -> service () -> applyNumber () -> save ();
2020-10-08 02:28:23 +02:00
$payment -> invoices () -> attach ( $this -> invoice -> id , [ 'amount' => $amount ]);
$this -> invoice -> service () -> setStatus ( Invoice :: STATUS_PAID ) -> save ();
2020-10-12 06:10:34 +02:00
foreach ( $this -> used_credit as $credit )
{
$current_credit = Credit :: find ( $credit [ 'credit_id' ]);
$payment -> credits () -> attach ( $current_credit -> id , [ 'amount' => $credit [ 'amount' ]]);
$this -> applyPaymentToCredit ( $current_credit , $credit [ 'amount' ]);
}
2020-10-08 02:28:23 +02:00
$payment -> ledger ()
-> updatePaymentBalance ( $amount * - 1 )
-> save ();
$this -> invoice -> client -> service ()
-> updateBalance ( $amount * - 1 )
-> updatePaidToDate ( $amount )
-> adjustCreditBalance ( $amount * - 1 )
-> save ();
$this -> invoice -> ledger ()
-> updateInvoiceBalance ( $amount * - 1 , 'Invoice payment using Credit' )
2020-10-08 05:31:02 +02:00
-> updateCreditBalance ( $amount * - 1 , 'Credits used to pay down Invoice ' . $this -> invoice -> number )
2020-10-08 02:28:23 +02:00
-> save ();
2020-10-08 05:31:02 +02:00
event ( new PaymentWasCreated ( $payment , $payment -> company , Ninja :: eventVars ()));
2020-10-08 02:28:23 +02:00
return $this -> invoice -> service () -> setStatus ( Invoice :: STATUS_PAID ) -> save ();
}
2020-10-07 13:03:53 +02:00
/**
* Applies credits to a payment prior to push
* to the payment gateway
*
* @ return $this
*/
private function applyCreditPayment ()
{
$available_credits = $this -> client
-> credits
-> where ( 'is_deleted' , false )
-> where ( 'balance' , '>' , 0 )
-> sortBy ( 'created_at' );
$available_credit_balance = $available_credits -> sum ( 'balance' );
if (( int ) $available_credit_balance == 0 )
return ;
$is_partial_amount = false ;
if ( $this -> invoice -> partial > 0 ) {
$is_partial_amount = true ;
}
2020-10-08 05:31:02 +02:00
$this -> used_credit = [];
2020-10-07 13:03:53 +02:00
2020-10-08 01:36:06 +02:00
foreach ( $available_credits as $key => $credit ) {
2020-10-07 13:03:53 +02:00
2020-10-08 01:36:06 +02:00
if ( $is_partial_amount ) {
//more credit than needed
if ( $credit -> balance >= $this -> invoice -> partial ) {
2020-10-08 02:28:23 +02:00
$this -> used_credit [ $key ][ 'credit_id' ] = $credit -> id ;
$this -> used_credit [ $key ][ 'amount' ] = $this -> invoice -> partial ;
$this -> invoice -> balance -= $this -> invoice -> partial ;
2020-10-08 01:36:06 +02:00
$this -> invoice -> partial = 0 ;
break ;
}
else {
2020-10-08 02:28:23 +02:00
$this -> used_credit [ $key ][ 'credit_id' ] = $credit -> id ;
$this -> used_credit [ $key ][ 'amount' ] = $credit -> balance ;
2020-10-08 01:36:06 +02:00
$this -> invoice -> partial -= $credit -> balance ;
2020-10-08 02:28:23 +02:00
$this -> invoice -> balance -= $credit -> balance ;
2020-10-08 01:36:06 +02:00
}
}
else {
//more credit than needed
if ( $credit -> balance >= $this -> invoice -> balance ) {
2020-10-08 05:31:02 +02:00
$this -> used_credit [ $key ][ 'credit_id' ] = $credit -> id ;
$this -> used_credit [ $key ][ 'amount' ] = $this -> invoice -> balance ;
2020-10-08 01:36:06 +02:00
$this -> invoice -> balance = 0 ;
break ;
}
else {
2020-10-08 05:31:02 +02:00
$this -> used_credit [ $key ][ 'credit_id' ] = $credit -> id ;
$this -> used_credit [ $key ][ 'amount' ] = $credit -> balance ;
2020-10-08 01:36:06 +02:00
$this -> invoice -> balance -= $credit -> balance ;
}
}
}
2020-10-07 13:03:53 +02:00
return $this ;
}
2020-10-12 06:10:34 +02:00
private function applyPaymentToCredit ( $credit , $amount ) : Credit
2020-10-07 13:03:53 +02:00
{
2020-10-07 13:06:00 +02:00
2020-10-07 13:03:53 +02:00
$credit_item = new InvoiceItem ;
$credit_item -> type_id = '1' ;
$credit_item -> product_key = ctrans ( 'texts.credit' );
$credit_item -> notes = ctrans ( 'texts.credit_payment' , [ 'invoice_number' => $this -> invoice -> number ]);
$credit_item -> quantity = 1 ;
$credit_item -> cost = $amount * - 1 ;
$credit_items = $credit -> line_items ;
$credit_items [] = $credit_item ;
$credit -> line_items = $credit_items ;
$credit = $credit -> calc () -> getCredit ();
2020-10-12 06:10:34 +02:00
$credit -> save ();
2020-10-07 13:03:53 +02:00
2020-10-12 06:10:34 +02:00
return $credit ;
2020-10-07 13:03:53 +02:00
}
2020-07-15 07:05:02 +02:00
/**
* Harvests a client gateway token which passes the
2020-09-06 11:38:10 +02:00
* necessary filters for an $amount .
*
2020-07-15 07:05:02 +02:00
* @ param float $amount The amount to charge
* @ return ClientGatewayToken The client gateway token
*/
2020-07-07 14:33:11 +02:00
private function getGateway ( $amount )
{
2020-07-13 00:28:19 +02:00
$gateway_tokens = $this -> client -> gateway_tokens () -> orderBy ( 'is_default' , 'DESC' ) -> get ();
2020-09-06 11:38:10 +02:00
foreach ( $gateway_tokens as $gateway_token ) {
if ( $this -> validGatewayLimits ( $gateway_token , $amount )) {
2020-07-13 00:28:19 +02:00
return $gateway_token ;
2020-07-15 07:05:02 +02:00
}
2020-07-13 00:28:19 +02:00
}
2020-07-08 02:18:13 +02:00
}
2020-07-07 16:50:51 +02:00
2020-07-08 05:07:07 +02:00
/**
2020-09-06 11:38:10 +02:00
* Adds a gateway fee to the invoice .
*
2020-07-08 05:07:07 +02:00
* @ param float $fee The fee amount .
*/
2020-07-08 04:20:44 +02:00
private function addFeeToInvoice ( float $fee )
{
2020-07-08 05:07:07 +02:00
//todo if we increase the invoice balance here, we will also need to adjust UP the client balance and ledger?
$starting_amount = $this -> invoice -> amount ;
2020-07-08 04:20:44 +02:00
$item = new InvoiceItem ;
$item -> quantity = 1 ;
$item -> cost = $fee ;
$item -> notes = ctrans ( 'texts.online_payment_surcharge' );
$item -> type_id = 3 ;
2020-09-06 11:38:10 +02:00
$items = ( array ) $this -> invoice -> line_items ;
2020-07-08 04:20:44 +02:00
$items [] = $item ;
$this -> invoice -> line_items = $items ;
$this -> invoice -> save ();
$this -> invoice = $this -> invoice -> calc () -> getInvoice () -> save ();
2020-09-06 11:38:10 +02:00
if ( $starting_amount != $this -> invoice -> amount && $this -> invoice -> status_id != Invoice :: STATUS_DRAFT ) {
2020-07-08 05:07:07 +02:00
$this -> invoice -> client -> service () -> updateBalance ( $this -> invoice -> amount - $starting_amount ) -> save ();
$this -> invoice -> ledger () -> updateInvoiceBalance ( $this -> invoice -> amount - $starting_amount , 'Invoice balance updated after stale gateway fee removed' ) -> save ();
}
2020-07-08 08:54:16 +02:00
2020-07-08 05:07:07 +02:00
return $this ;
}
/**
2020-09-06 11:38:10 +02:00
* Removes any existing unpaid gateway fees
2020-07-08 05:07:07 +02:00
* due to previous payment failure .
2020-09-06 11:38:10 +02:00
*
2020-07-08 05:07:07 +02:00
* @ return $this
*/
2020-08-30 12:47:32 +02:00
// private function purgeStaleGatewayFees()
// {
// $starting_amount = $this->invoice->amount;
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// $line_items = $this->invoice->line_items;
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// $new_items = [];
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// foreach($line_items as $item)
// {
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// if($item->type_id != 3)
// $new_items[] = $item;
2020-09-06 11:38:10 +02:00
2020-08-30 12:47:32 +02:00
// }
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// $this->invoice->line_items = $new_items;
// $this->invoice->save();
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// $this->invoice = $this->invoice->calc()->getInvoice();
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// if($starting_amount != $this->invoice->amount && $this->invoice->status_id != Invoice::STATUS_DRAFT){
// $this->invoice->client->service()->updateBalance($this->invoice->amount - $starting_amount)->save();
// $this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount - $starting_amount, 'Invoice balance updated after stale gateway fee removed')->save();
// }
2020-07-08 05:07:07 +02:00
2020-08-30 12:47:32 +02:00
// return $this;
// }
2020-07-08 04:20:44 +02:00
2020-07-08 02:18:13 +02:00
/**
* Checks whether a given gateway token is able
* to process the payment after passing through the
2020-09-06 11:38:10 +02:00
* fees and limits check .
*
2020-07-08 02:18:13 +02:00
* @ param CompanyGateway $cg The CompanyGateway instance
* @ param float $amount The amount to be paid
* @ return bool
*/
public function validGatewayLimits ( $cg , $amount ) : bool
{
2020-09-06 11:38:10 +02:00
if ( isset ( $cg -> fees_and_limits )) {
$fees_and_limits = $cg -> fees_and_limits -> { '1' };
} else {
2020-07-15 07:05:02 +02:00
return true ;
2020-07-08 02:18:13 +02:00
}
2020-09-06 11:38:10 +02:00
if (( property_exists ( $fees_and_limits , 'min_limit' )) && $fees_and_limits -> min_limit !== null && $amount < $fees_and_limits -> min_limit ) {
info ( " amount { $amount } less than " . $fees_and_limits -> min_limit );
2020-07-08 02:18:13 +02:00
$passes = false ;
2020-09-06 11:38:10 +02:00
} elseif (( property_exists ( $fees_and_limits , 'max_limit' )) && $fees_and_limits -> max_limit !== null && $amount > $fees_and_limits -> max_limit ) {
info ( " amount { $amount } greater than " . $fees_and_limits -> max_limit );
$passes = false ;
} else {
2020-07-08 02:18:13 +02:00
$passes = true ;
2020-09-06 11:38:10 +02:00
}
2020-07-08 02:18:13 +02:00
return $passes ;
2020-07-07 14:33:11 +02:00
}
}