2020-02-03 11:33:07 +01:00
< ? php
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2020-02-03 11:33:07 +01: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 ;
use App\Models\Invoice ;
use App\Models\Payment ;
2020-02-17 10:37:44 +01:00
use App\Services\AbstractService ;
2020-02-03 11:33:07 +01:00
2020-02-17 10:37:44 +01:00
class ApplyPayment extends AbstractService
2020-02-03 11:33:07 +01:00
{
private $invoice ;
2020-02-17 10:37:44 +01:00
private $payment ;
private $payment_amount ;
public function __construct ( $invoice , $payment , $payment_amount )
2020-02-03 11:33:07 +01:00
{
$this -> invoice = $invoice ;
2020-02-17 10:37:44 +01:00
$this -> payment = $payment ;
$this -> payment_amount = $payment_amount ;
2020-02-03 11:33:07 +01:00
}
2020-03-21 06:37:30 +01:00
public function run ()
{
2020-02-20 22:05:01 +01:00
$this -> payment
-> ledger ()
2020-09-06 11:38:10 +02:00
-> updatePaymentBalance ( $this -> payment_amount * - 1 );
2020-02-03 11:33:07 +01:00
2020-12-13 10:46:29 +01:00
// info("apply payment method - current client balance = {$this->payment->client->balance}");
2020-06-06 03:07:31 +02:00
2020-12-13 10:46:29 +01:00
// info("reducing client balance by payment amount {$this->payment_amount}");
2020-06-06 03:07:31 +02:00
2020-09-06 11:38:10 +02:00
$this -> invoice -> client -> service () -> updateBalance ( $this -> payment_amount * - 1 ) -> save ();
2020-06-06 03:07:31 +02:00
2020-12-13 10:46:29 +01:00
// info("post client balance = {$this->invoice->client->balance}");
2020-02-03 11:33:07 +01:00
/* Update Pivot Record amount */
2020-03-21 06:37:30 +01:00
$this -> payment -> invoices -> each ( function ( $inv ) {
2020-02-03 11:33:07 +01:00
if ( $inv -> id == $this -> invoice -> id ) {
2020-02-17 10:37:44 +01:00
$inv -> pivot -> amount = $this -> payment_amount ;
2020-02-03 11:33:07 +01:00
$inv -> pivot -> save ();
}
});
2020-06-06 03:07:31 +02:00
$this -> invoice -> fresh ( 'client' );
2020-12-13 10:46:29 +01:00
// info("1 end of apply payment method the client balance = {$this->invoice->client->balance}");
2020-02-03 11:33:07 +01:00
if ( $this -> invoice -> hasPartial ()) {
2020-03-21 06:37:30 +01:00
//is partial and amount is exactly the partial amount
2020-02-17 10:37:44 +01:00
if ( $this -> invoice -> partial == $this -> payment_amount ) {
2020-09-06 11:38:10 +02:00
$this -> invoice -> service () -> clearPartial () -> setDueDate () -> setStatus ( Invoice :: STATUS_PARTIAL ) -> updateBalance ( $this -> payment_amount * - 1 );
2020-02-17 10:37:44 +01:00
} elseif ( $this -> invoice -> partial > 0 && $this -> invoice -> partial > $this -> payment_amount ) { //partial amount exists, but the amount is less than the partial amount
2020-09-06 11:38:10 +02:00
$this -> invoice -> service () -> updatePartial ( $this -> payment_amount * - 1 ) -> updateBalance ( $this -> payment_amount * - 1 );
2020-02-17 10:37:44 +01:00
} elseif ( $this -> invoice -> partial > 0 && $this -> invoice -> partial < $this -> payment_amount ) { //partial exists and the amount paid is GREATER than the partial amount
2020-09-06 11:38:10 +02:00
$this -> invoice -> service () -> clearPartial () -> setDueDate () -> setStatus ( Invoice :: STATUS_PARTIAL ) -> updateBalance ( $this -> payment_amount * - 1 );
2020-02-03 11:33:07 +01:00
}
2020-02-17 10:37:44 +01:00
} elseif ( $this -> payment_amount == $this -> invoice -> balance ) { //total invoice paid.
2020-09-06 11:38:10 +02:00
$this -> invoice -> service () -> clearPartial () -> setStatus ( Invoice :: STATUS_PAID ) -> updateBalance ( $this -> payment_amount * - 1 );
2020-03-21 06:37:30 +01:00
} elseif ( $this -> payment_amount < $this -> invoice -> balance ) { //partial invoice payment made
2020-09-06 11:38:10 +02:00
$this -> invoice -> service () -> clearPartial () -> setStatus ( Invoice :: STATUS_PARTIAL ) -> updateBalance ( $this -> payment_amount * - 1 );
2020-02-03 11:33:07 +01:00
}
2020-12-13 10:46:29 +01:00
// info("2 end of apply payment method the client balnace = {$this->invoice->client->balance}");
2020-02-14 04:32:22 +01:00
2020-05-28 06:18:34 +02:00
$this -> invoice -> service () -> applyNumber () -> save ();
2020-12-13 10:46:29 +01:00
// info("3 end of apply payment method the client balnace = {$this->invoice->client->balance}");
2020-09-06 11:38:10 +02:00
2020-02-03 11:33:07 +01:00
return $this -> invoice ;
2020-03-21 06:37:30 +01:00
}
2020-02-14 04:32:22 +01:00
}