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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. 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;
|
|
|
|
|
2021-03-01 00:40:18 +01:00
|
|
|
use App\Jobs\Util\UnlinkFile;
|
2020-02-26 22:21:12 +01:00
|
|
|
use App\Models\Credit;
|
2020-11-04 08:57:16 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
|
|
|
class CreditService
|
|
|
|
{
|
2020-11-04 08:57:16 +01:00
|
|
|
use MakesHash;
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
protected $credit;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-24 07:44:14 +01:00
|
|
|
public function updatePaidToDate($adjustment)
|
|
|
|
{
|
|
|
|
$this->credit->paid_to_date += $adjustment;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-11-04 07:02:15 +01:00
|
|
|
|
2021-02-11 22:16:43 +01:00
|
|
|
public function updateBalance($adjustment)
|
|
|
|
{
|
|
|
|
$this->credit->balance -= $adjustment;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
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-04 07:02:15 +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()
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function save() : ?Credit
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|