1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/Credit/CreditService.php

304 lines
8.5 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Credit;
2023-06-30 07:43:28 +02:00
use App\Utils\Ninja;
use App\Models\Credit;
use App\Models\Payment;
use App\Models\PaymentType;
2023-06-30 07:43:28 +02:00
use App\Jobs\Util\UnlinkFile;
use App\Factory\PaymentFactory;
use App\Utils\Traits\MakesHash;
use App\Jobs\Entity\CreateEntityPdf;
2021-11-22 11:09:28 +01:00
use App\Repositories\CreditRepository;
use App\Repositories\PaymentRepository;
2023-06-30 07:43:28 +02:00
use Illuminate\Support\Facades\Storage;
class CreditService
{
use MakesHash;
2021-11-09 11:15:08 +01:00
public $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()
{
if ((int) $this->credit->balance == 0) {
2020-10-23 06:18:16 +02:00
$this->credit->status_id = Credit::STATUS_APPLIED;
} 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;
}
/*
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()
{
if ($this->credit->balance > 0) {
return $this;
}
2022-01-19 05:32:04 +01:00
$this->markSent();
2021-11-22 11:09:28 +01:00
$payment_repo = new PaymentRepository(new CreditRepository());
//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)
->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-06-15 08:23:08 +02:00
$payment->date = now()->addSeconds($this->credit->company->timezone()->utc_offset)->format('Y-m-d');
$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]);
$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();
event('eloquent.created: App\Models\Payment', $payment);
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
2023-09-05 03:54:05 +02: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)
2023-01-15 05:10:41 +01:00
{
2022-03-27 08:04:13 +02:00
$this->credit->balance += $adjustment;
2020-10-23 06:18:16 +02:00
return $this;
}
public function updatePaidToDate($adjustment)
{
$this->credit->paid_to_date += $adjustment;
return $this;
}
public function updateBalance($adjustment)
{
2022-03-27 08:04:13 +02:00
$this->credit->balance -= $adjustment;
return $this;
}
2022-01-19 05:32:04 +01:00
/**
* Sometimes we need to refresh the
* PDF when it is updated etc.
2023-07-26 04:59:36 +02:00
* @return self
2022-01-19 05:32:04 +01:00
*/
public function touchPdf($force = false)
{
try {
if ($force) {
2022-01-19 05:32:04 +01:00
$this->credit->invitations->each(function ($invitation) {
(new CreateEntityPdf($invitation))->handle();
2022-01-19 05:32:04 +01:00
});
return $this;
}
$this->credit->invitations->each(function ($invitation) {
CreateEntityPdf::dispatch($invitation);
});
} catch (\Exception $e) {
nlog('failed creating invoices in Touch PDF');
2022-01-19 05:32:04 +01:00
}
return $this;
}
public function fillDefaults()
{
2020-11-04 09:43:20 +01:00
$settings = $this->credit->client->getMergedSettings();
if (! $this->credit->design_id) {
2020-11-04 09:43:20 +01:00
$this->credit->design_id = $this->decodePrimaryKey($settings->credit_design_id);
}
if (! isset($this->credit->footer)) {
2020-11-04 09:43:20 +01:00
$this->credit->footer = $settings->credit_footer;
}
2020-11-04 09:43:20 +01:00
if (! isset($this->credit->terms)) {
2020-11-04 09:43:20 +01:00
$this->credit->terms = $settings->credit_terms;
}
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.*/
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;
}
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()
{
$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();
//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());
}
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-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;
}
/**
* Saves the credit.
* @return Credit object
*/
public function save() : ?Credit
{
$this->credit->saveQuietly();
return $this->credit;
}
}