1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Services/Credit/CreditService.php

163 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Credit;
2021-03-01 00:40:18 +01:00
use App\Jobs\Util\UnlinkFile;
use App\Models\Credit;
use App\Utils\Traits\MakesHash;
class CreditService
{
use MakesHash;
protected $credit;
public function __construct($credit)
{
$this->credit = $credit;
}
2020-11-11 01:13:39 +01:00
public function getCreditPdf($invitation)
{
2020-11-11 01:13:39 +01:00
return (new GetCreditPdf($invitation))->run();
}
/**
* Applies the invoice number.
* @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();
return $this;
}
public function createInvitations()
{
2020-02-18 21:56:21 +01:00
$this->credit = (new CreateInvitations($this->credit))->run();
return $this;
}
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()
{
2020-11-25 15:19:52 +01:00
if ((int)$this->credit->balance == 0) {
2020-10-23 06:18:16 +02:00
$this->credit->status_id = Credit::STATUS_APPLIED;
2020-11-25 15:19:52 +01: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;
}
public function markSent()
{
$this->credit = (new MarkSent($this->credit->client, $this->credit))->run();
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
2021-03-10 11:00:18 +01:00
$this->deletePdf();
2020-10-13 14:28:30 +02:00
return $this;
}
2020-10-23 06:18:16 +02:00
public function adjustBalance($adjustment)
{
$this->credit->balance += $adjustment;
return $this;
}
public function updatePaidToDate($adjustment)
{
$this->credit->paid_to_date += $adjustment;
return $this;
}
public function updateBalance($adjustment)
{
$this->credit->balance -= $adjustment;
return $this;
}
public function fillDefaults()
{
2020-11-04 09:43:20 +01:00
$settings = $this->credit->client->getMergedSettings();
2021-01-18 21:02:32 +01:00
if (! $this->credit->design_id)
2020-11-04 09:43:20 +01:00
$this->credit->design_id = $this->decodePrimaryKey($settings->credit_design_id);
2021-01-18 21:02:32 +01:00
if (!isset($this->credit->footer))
2020-11-04 09:43:20 +01:00
$this->credit->footer = $settings->credit_footer;
2021-01-18 21:02:32 +01:00
if (!isset($this->credit->terms))
2020-11-04 09:43:20 +01:00
$this->credit->terms = $settings->credit_terms;
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.*/
if(!isset($this->credit->exchange_rate) && $this->credit->client->currency()->id != (int) $this->credit->company->settings->currency_id)
$this->credit->exchange_rate = $this->credit->client->currency()->exchange_rate;
2021-01-18 21:04:13 +01:00
if (!isset($this->credit->public_notes))
2021-01-18 12:08:18 +01:00
$this->credit->public_notes = $this->credit->client->public_notes;
2021-01-18 21:04:13 +01:00
2020-11-25 15:19:52 +01:00
return $this;
}
2021-03-01 00:40:18 +01:00
public function deletePdf()
{
2021-06-12 13:50:01 +02:00
$this->credit->invitations->each(function ($invitation){
UnlinkFile::dispatchNow(config('filesystems.default'), $this->credit->client->credit_filepath($invitation) . $this->credit->numberFormatter().'.pdf');
});
2021-03-01 00:40:18 +01:00
return $this;
}
/**
* Saves the credit.
* @return Credit object
*/
public function save() : ?Credit
{
$this->credit->save();
return $this->credit;
}
}