2020-02-03 11:33:07 +01:00
< ? php
/**
* Invoice Ninja ( https :// invoiceninja . com )
*
* @ 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
use App\Services\Client\ClientService ;
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 ()
-> updatePaymentBalance ( $this -> payment_amount *- 1 );
2020-02-03 11:33:07 +01:00
2020-02-17 10:37:44 +01:00
$this -> payment -> client -> service () -> updateBalance ( $this -> payment_amount *- 1 ) -> save ();
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 ();
}
});
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 ) {
$this -> invoice -> service () -> clearPartial () -> setDueDate () -> setStatus ( Invoice :: STATUS_PARTIAL ) -> updateBalance ( $this -> payment_amount *- 1 );
} elseif ( $this -> invoice -> partial > 0 && $this -> invoice -> partial > $this -> payment_amount ) { //partial amount exists, but the amount is less than the partial amount
$this -> invoice -> service () -> updatePartial ( $this -> payment_amount *- 1 ) -> updateBalance ( $this -> payment_amount *- 1 );
} elseif ( $this -> invoice -> partial > 0 && $this -> invoice -> partial < $this -> payment_amount ) { //partial exists and the amount paid is GREATER than the partial amount
$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.
$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-03-10 23:20:09 +01:00
$this -> invoice -> service () -> clearPartial () -> setStatus ( Invoice :: STATUS_PARTIAL ) -> updateBalance ( $this -> payment_amount *- 1 );
2020-02-03 11:33:07 +01:00
}
2020-02-14 04:32:22 +01:00
2020-05-28 06:18:34 +02:00
$this -> invoice -> service () -> applyNumber () -> save ();
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
}