2020-02-15 10:01:15 +01:00
< ? php
2020-05-09 00:35:49 +02:00
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2020-05-09 00:35:49 +02:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
2020-05-09 00:35:49 +02:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2020-05-09 00:35:49 +02:00
*/
2020-02-15 10:01:15 +01:00
namespace App\Services\Credit ;
2023-10-26 04:57:44 +02:00
use App\Factory\PaymentFactory ;
2020-02-26 22:21:12 +01:00
use App\Models\Credit ;
2021-11-22 05:25:43 +01:00
use App\Models\Payment ;
use App\Models\PaymentType ;
2021-11-22 11:09:28 +01:00
use App\Repositories\CreditRepository ;
2021-11-22 05:25:43 +01:00
use App\Repositories\PaymentRepository ;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja ;
use App\Utils\Traits\MakesHash ;
2023-06-30 07:43:28 +02:00
use Illuminate\Support\Facades\Storage ;
2020-02-15 10:01:15 +01:00
class CreditService
{
2020-11-04 08:57:16 +01:00
use MakesHash ;
2021-11-09 11:15:08 +01:00
public $credit ;
2020-02-15 10:01:15 +01:00
public function __construct ( $credit )
{
$this -> credit = $credit ;
}
2020-11-11 01:13:39 +01:00
public function getCreditPdf ( $invitation )
2020-02-15 10:01:15 +01:00
{
2020-11-11 01:13:39 +01:00
return ( new GetCreditPdf ( $invitation )) -> run ();
2020-02-15 10:01:15 +01:00
}
/**
2020-09-06 11:38:10 +02:00
* Applies the invoice number .
2020-02-15 10:01:15 +01:00
* @ return $this InvoiceService object
*/
public function applyNumber ()
{
2020-02-18 21:56:21 +01:00
$this -> credit = ( new ApplyNumber ( $this -> credit -> client , $this -> credit )) -> run ();
2020-02-15 10:01:15 +01:00
return $this ;
}
public function createInvitations ()
{
2020-02-18 21:56:21 +01:00
$this -> credit = ( new CreateInvitations ( $this -> credit )) -> run ();
2020-02-15 10:01:15 +01:00
return $this ;
}
2020-03-03 10:44:26 +01:00
public function setStatus ( $status )
{
$this -> credit -> status_id = $status ;
return $this ;
}
2020-10-28 00:02:32 +01:00
public function sendEmail ( $contact = null )
{
$send_email = new SendEmail ( $this -> credit , null , $contact );
return $send_email -> run ();
}
2020-10-23 06:18:16 +02:00
public function setCalculatedStatus ()
{
2022-06-21 11:57:17 +02:00
if (( int ) $this -> credit -> balance == 0 ) {
2020-10-23 06:18:16 +02:00
$this -> credit -> status_id = Credit :: STATUS_APPLIED ;
2022-06-21 11:57:17 +02:00
} elseif (( string ) $this -> credit -> amount == ( string ) $this -> credit -> balance ) {
2020-10-23 06:18:16 +02:00
$this -> credit -> status_id = Credit :: STATUS_SENT ;
2020-11-25 15:19:52 +01:00
} elseif ( $this -> credit -> balance > 0 ) {
2020-10-23 06:18:16 +02:00
$this -> credit -> status_id = Credit :: STATUS_PARTIAL ;
2020-11-25 15:19:52 +01:00
}
2020-10-23 06:18:16 +02:00
return $this ;
}
2022-06-21 11:57:17 +02:00
/*
2021-11-22 05:25:43 +01:00
For euro users - we mark a credit as paid when
we need to document a refund of sorts .
Criteria : Credit must be a negative value
A negative payment for the balance will be generated
This amount will be reduced from the clients paid to date .
*/
public function markPaid ()
{
2022-06-21 11:57:17 +02:00
if ( $this -> credit -> balance > 0 ) {
2021-11-22 05:25:43 +01:00
return $this ;
2022-06-21 11:57:17 +02:00
}
2021-11-22 05:25:43 +01:00
2022-01-19 05:32:04 +01:00
$this -> markSent ();
2021-11-22 11:09:28 +01:00
$payment_repo = new PaymentRepository ( new CreditRepository ());
2021-11-22 05:25:43 +01:00
//set credit balance to zero
$adjustment = $this -> credit -> balance ;
$this -> updateBalance ( $adjustment )
-> updatePaidToDate ( $adjustment )
2022-01-25 03:43:44 +01:00
-> setStatus ( Credit :: STATUS_APPLIED )
2021-11-22 05:25:43 +01:00
-> save ();
//create a negative payment of total $this->credit->balance
$payment = PaymentFactory :: create ( $this -> credit -> company_id , $this -> credit -> user_id );
$payment -> client_id = $this -> credit -> client_id ;
$payment -> amount = $adjustment ;
$payment -> applied = $adjustment ;
$payment -> refunded = 0 ;
$payment -> status_id = Payment :: STATUS_COMPLETED ;
$payment -> type_id = PaymentType :: CREDIT ;
$payment -> is_manual = true ;
2022-01-19 23:26:52 +01:00
$payment -> currency_id = $this -> credit -> client -> getSetting ( 'currency_id' );
2023-11-15 05:36:24 +01:00
$payment -> date = now () -> addSeconds ( $this -> credit -> company -> utc_offset ()) -> format ( 'Y-m-d' );
2021-11-22 05:25:43 +01:00
$payment -> saveQuietly ();
$payment -> number = $payment -> client -> getNextPaymentNumber ( $payment -> client , $payment );
$payment = $payment_repo -> processExchangeRates ([ 'client_id' => $this -> credit -> client_id ], $payment );
$payment -> saveQuietly ();
$payment
-> credits ()
-> attach ( $this -> credit -> id , [ 'amount' => $adjustment ]);
2022-06-21 11:57:17 +02:00
$client = $this -> credit -> client -> fresh ();
$client -> service ()
2022-03-08 11:49:33 +01:00
-> updatePaidToDate ( $adjustment )
2022-08-22 00:24:36 +02:00
-> adjustCreditBalance ( $adjustment * - 1 )
2022-03-08 11:49:33 +01:00
-> save ();
2021-11-22 05:25:43 +01:00
event ( 'eloquent.created: App\Models\Payment' , $payment );
return $this ;
}
2020-03-03 10:44:26 +01:00
public function markSent ()
{
2020-03-04 00:51:50 +01:00
$this -> credit = ( new MarkSent ( $this -> credit -> client , $this -> credit )) -> run ();
2020-03-03 10:44:26 +01:00
return $this ;
}
2020-10-13 14:28:30 +02:00
public function applyPayment ( $invoice , $amount , $payment )
{
2020-10-14 01:53:20 +02:00
$this -> credit = ( new ApplyPayment ( $this -> credit , $invoice , $amount , $payment )) -> run ();
2020-10-13 14:28:30 +02:00
2023-09-05 03:54:05 +02:00
// $this->deletePdf();
2022-06-21 11:57:17 +02:00
2020-10-13 14:28:30 +02:00
return $this ;
}
2020-10-23 06:18:16 +02:00
public function adjustBalance ( $adjustment )
2023-01-15 05:10:41 +01:00
{
2022-03-27 08:04:13 +02:00
$this -> credit -> balance += $adjustment ;
2022-06-21 11:57:17 +02:00
2020-10-23 06:18:16 +02:00
return $this ;
}
2021-01-24 07:44:14 +01:00
public function updatePaidToDate ( $adjustment )
{
2022-06-21 11:57:17 +02:00
$this -> credit -> paid_to_date += $adjustment ;
2021-01-24 07:44:14 +01:00
return $this ;
}
2020-11-04 07:02:15 +01:00
2021-02-11 22:16:43 +01:00
public function updateBalance ( $adjustment )
{
2022-03-27 08:04:13 +02:00
$this -> credit -> balance -= $adjustment ;
2022-06-21 11:57:17 +02:00
2022-01-19 05:32:04 +01:00
return $this ;
}
2020-11-04 07:02:15 +01:00
public function fillDefaults ()
{
2020-11-04 09:43:20 +01:00
$settings = $this -> credit -> client -> getMergedSettings ();
2022-06-21 11:57:17 +02:00
if ( ! $this -> credit -> design_id ) {
2020-11-04 09:43:20 +01:00
$this -> credit -> design_id = $this -> decodePrimaryKey ( $settings -> credit_design_id );
2022-06-21 11:57:17 +02:00
}
if ( ! isset ( $this -> credit -> footer )) {
2020-11-04 09:43:20 +01:00
$this -> credit -> footer = $settings -> credit_footer ;
2022-06-21 11:57:17 +02:00
}
2020-11-04 09:43:20 +01:00
2022-06-21 11:57:17 +02:00
if ( ! isset ( $this -> credit -> terms )) {
2020-11-04 09:43:20 +01:00
$this -> credit -> terms = $settings -> credit_terms ;
2022-06-21 11:57:17 +02:00
}
2020-11-04 09:43:20 +01:00
2021-01-18 21:02:32 +01:00
/* If client currency differs from the company default currency, then insert the client exchange rate on the model.*/
2022-06-21 11:57:17 +02:00
if ( ! isset ( $this -> credit -> exchange_rate ) && $this -> credit -> client -> currency () -> id != ( int ) $this -> credit -> company -> settings -> currency_id ) {
2021-01-18 21:02:32 +01:00
$this -> credit -> exchange_rate = $this -> credit -> client -> currency () -> exchange_rate ;
2022-06-21 11:57:17 +02:00
}
2021-01-18 21:04:13 +01:00
2022-06-21 11:57:17 +02:00
if ( ! isset ( $this -> credit -> public_notes )) {
2021-01-18 12:08:18 +01:00
$this -> credit -> public_notes = $this -> credit -> client -> public_notes ;
2022-06-21 11:57:17 +02:00
}
2021-01-18 21:04:13 +01:00
2020-11-25 15:19:52 +01:00
return $this ;
2020-11-04 07:02:15 +01:00
}
2021-03-01 00:40:18 +01:00
public function deletePdf ()
{
2022-06-21 11:57:17 +02:00
$this -> credit -> invitations -> each ( function ( $invitation ) {
2023-06-30 07:43:28 +02:00
// (new UnlinkFile(config('filesystems.default'), $this->credit->client->credit_filepath($invitation).$this->credit->numberFormatter().'.pdf'))->handle();
2024-01-14 05:05:00 +01:00
2023-06-30 07:43:28 +02:00
//30-06-2023
try {
// if (Storage::disk(config('filesystems.default'))->exists($this->invoice->client->invoice_filepath($invitation).$this->invoice->numberFormatter().'.pdf')) {
Storage :: disk ( config ( 'filesystems.default' )) -> delete ( $this -> credit -> client -> credit_filepath ( $invitation ) . $this -> credit -> numberFormatter () . '.pdf' );
// }
// if (Ninja::isHosted() && Storage::disk('public')->exists($this->invoice->client->invoice_filepath($invitation).$this->invoice->numberFormatter().'.pdf')) {
if ( Ninja :: isHosted ()) {
Storage :: disk ( 'public' ) -> delete ( $this -> credit -> client -> credit_filepath ( $invitation ) . $this -> credit -> numberFormatter () . '.pdf' );
}
} catch ( \Exception $e ) {
nlog ( $e -> getMessage ());
}
2024-01-14 05:05:00 +01:00
2021-06-12 13:50:01 +02:00
});
2021-03-01 00:40:18 +01:00
return $this ;
}
2021-11-15 04:14:58 +01:00
public function triggeredActions ( $request )
{
2023-06-19 11:48:53 +02:00
$this -> credit = ( new TriggeredActions ( $this -> credit , $request )) -> run ();
2021-11-15 04:14:58 +01:00
return $this ;
}
2022-06-21 11:57:17 +02:00
2022-08-22 02:27:11 +02:00
public function deleteCredit ()
{
$this -> credit
-> client
-> service ()
-> adjustCreditBalance ( $this -> credit -> balance * - 1 )
-> save ();
return $this ;
}
public function restoreCredit ()
{
$this -> credit
-> client
-> service ()
-> adjustCreditBalance ( $this -> credit -> balance )
-> save ();
return $this ;
}
2020-02-15 10:01:15 +01:00
/**
2020-09-06 11:38:10 +02:00
* Saves the credit .
2020-02-15 10:01:15 +01:00
* @ return Credit object
*/
2024-01-14 05:05:00 +01:00
public function save () : ? Credit
2020-02-15 10:01:15 +01:00
{
2021-10-10 11:56:05 +02:00
$this -> credit -> saveQuietly ();
2020-02-22 03:25:49 +01:00
2020-02-15 10:01:15 +01:00
return $this -> credit ;
}
}