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