1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Services/Invoice/InvoiceService.php

410 lines
11 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\Invoice;
2020-10-26 20:10:04 +01:00
use App\Jobs\Entity\CreateEntityPdf;
2021-02-05 06:03:34 +01:00
use App\Jobs\Invoice\InvoiceWorkflowSettings;
use App\Jobs\Util\UnlinkFile;
2020-08-25 15:06:38 +02:00
use App\Models\CompanyGateway;
use App\Models\Expense;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Task;
use App\Services\Client\ClientService;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
class InvoiceService
{
use MakesHash;
private $invoice;
protected $client_service;
public function __construct($invoice)
{
$this->invoice = $invoice;
$this->client_service = new ClientService($invoice->client);
}
/**
* Marks as invoice as paid
* and executes child sub functions.
* @return $this InvoiceService object
*/
public function markPaid()
{
$this->invoice = (new MarkPaid($this->client_service, $this->invoice))->run();
return $this;
}
/**
* Applies the invoice number.
* @return $this InvoiceService object
*/
public function applyNumber()
{
$this->invoice = (new ApplyNumber($this->invoice->client, $this->invoice))->run();
return $this;
}
2020-09-08 12:34:14 +02:00
/**
* Applies the recurring invoice number.
* @return $this InvoiceService object
*/
public function applyRecurringNumber()
{
$this->invoice = (new ApplyRecurringNumber($this->invoice->client, $this->invoice))->run();
return $this;
}
/**
* Apply a payment amount to an invoice.
* @param Payment $payment The Payment
* @param float $payment_amount The Payment amount
* @return InvoiceService Parent class object
*/
public function applyPayment(Payment $payment, float $payment_amount)
{
$this->invoice = (new ApplyPayment($this->invoice, $payment, $payment_amount))->run();
return $this;
}
2020-10-12 11:38:55 +02:00
public function addGatewayFee(CompanyGateway $company_gateway, $gateway_type_id, float $amount)
2020-08-25 15:06:38 +02:00
{
2020-10-12 11:38:55 +02:00
$this->invoice = (new AddGatewayFee($company_gateway, $gateway_type_id, $this->invoice, $amount))->run();
2020-08-25 15:06:38 +02:00
return $this;
}
/**
* Update an invoice balance.
*
* @param float $balance_adjustment The amount to adjust the invoice by
* a negative amount will REDUCE the invoice balance, a positive amount will INCREASE
* the invoice balance
*
* @return InvoiceService Parent class object
*/
public function updateBalance($balance_adjustment)
{
$this->invoice = (new UpdateBalance($this->invoice, $balance_adjustment))->run();
2020-11-25 15:19:52 +01:00
if ((int)$this->invoice->balance == 0) {
$this->invoice->next_send_date = null;
}
return $this;
}
public function updatePaidToDate($adjustment)
{
$this->invoice->paid_to_date += $adjustment;
return $this;
}
public function createInvitations()
{
$this->invoice = (new CreateInvitations($this->invoice))->run();
return $this;
}
public function markSent()
{
$this->invoice = (new MarkSent($this->invoice->client, $this->invoice))->run();
return $this;
}
public function getInvoicePdf($contact = null)
{
return (new GetInvoicePdf($this->invoice, $contact))->run();
}
2020-11-25 15:19:52 +01:00
public function getInvoiceDeliveryNote(\App\Models\Invoice $invoice, \App\Models\ClientContact $contact = null)
2020-11-04 02:27:07 +01:00
{
return (new GenerateDeliveryNote($invoice, $contact))->run();
2020-11-04 02:27:07 +01:00
}
public function sendEmail($contact = null)
{
$send_email = new SendEmail($this->invoice, null, $contact);
return $send_email->run();
}
public function handleReversal()
{
$this->invoice = (new HandleReversal($this->invoice))->run();
return $this;
}
public function handleCancellation()
{
$this->invoice = (new HandleCancellation($this->invoice))->run();
return $this;
}
public function markDeleted()
{
$this->invoice = (new MarkInvoiceDeleted($this->invoice))->run();
return $this;
}
public function handleRestore()
2020-12-03 05:20:39 +01:00
{
$this->invoice = (new HandleRestore($this->invoice))->run();
2020-12-03 05:20:39 +01:00
return $this;
}
public function reverseCancellation()
{
$this->invoice = (new HandleCancellation($this->invoice))->reverse();
return $this;
}
2020-07-06 01:34:25 +02:00
public function triggeredActions($request)
{
$this->invoice = (new TriggeredActions($this->invoice, $request))->run();
return $this;
}
2020-07-07 14:33:11 +02:00
public function autoBill()
{
$this->invoice = (new AutoBillInvoice($this->invoice))->run();
return $this;
}
public function markViewed()
{
$this->invoice->last_viewed = Carbon::now()->format('Y-m-d H:i');
return $this;
}
/* One liners */
public function setDueDate()
{
if ($this->invoice->due_date != '' || $this->invoice->client->getSetting('payment_terms') == '') {
return $this;
}
$this->invoice->due_date = Carbon::parse($this->invoice->date)->addDays($this->invoice->client->getSetting('payment_terms'));
return $this;
}
public function setStatus($status)
{
$this->invoice->status_id = $status;
return $this;
}
2020-10-23 06:18:16 +02:00
public function setCalculatedStatus()
{
2020-11-25 15:19:52 +01:00
if ((int)$this->invoice->balance == 0) {
2020-10-23 06:18:16 +02:00
$this->setStatus(Invoice::STATUS_PAID);
2020-11-25 15:19:52 +01:00
} elseif ($this->invoice->balance > 0 && $this->invoice->balance < $this->invoice->amount) {
2020-10-23 06:18:16 +02:00
$this->setStatus(Invoice::STATUS_PARTIAL);
2020-11-25 15:19:52 +01:00
}
2020-10-23 06:18:16 +02:00
return $this;
}
2020-09-11 02:10:53 +02:00
public function updateStatus()
{
2020-11-25 15:19:52 +01:00
if ((int)$this->invoice->balance == 0) {
2021-02-05 06:03:34 +01:00
InvoiceWorkflowSettings::dispatch($this->invoice);
2020-09-11 02:10:53 +02:00
$this->setStatus(Invoice::STATUS_PAID);
2020-11-25 15:19:52 +01:00
}
2020-09-11 02:10:53 +02:00
2020-11-25 15:19:52 +01:00
if ($this->invoice->balance > 0 && $this->invoice->balance < $this->invoice->amount) {
$this->setStatus(Invoice::STATUS_PARTIAL);
2020-11-25 15:19:52 +01:00
}
2020-10-28 11:10:49 +01:00
2020-09-11 02:10:53 +02:00
return $this;
}
2020-07-15 07:05:02 +02:00
public function toggleFeesPaid()
{
$this->invoice->line_items = collect($this->invoice->line_items)
2020-07-15 08:08:57 +02:00
->map(function ($item) {
if ($item->type_id == '3') {
$item->type_id = '4';
}
2020-07-15 07:05:02 +02:00
return $item;
})->toArray();
2020-07-15 07:05:02 +02:00
2020-10-10 12:57:28 +02:00
//$this->invoice = $this->invoice->calc()->getInvoice();
2021-02-16 14:31:00 +01:00
$this->deletePdf();
return $this;
}
public function deletePdf()
{
//UnlinkFile::dispatchNow(config('filesystems.default'), $this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
2020-07-15 07:05:02 +02:00
return $this;
}
public function removeUnpaidGatewayFees()
{
$this->invoice->line_items = collect($this->invoice->line_items)
->reject(function ($item) {
return $item->type_id == '3';
})->toArray();
2020-07-15 07:05:02 +02:00
$this->invoice = $this->invoice->calc()->getInvoice();
2020-07-15 07:05:02 +02:00
return $this;
}
2020-09-02 03:11:01 +02:00
/*Set partial value and due date to null*/
public function clearPartial()
{
$this->invoice->partial = null;
$this->invoice->partial_due_date = null;
return $this;
}
2020-09-02 03:11:01 +02:00
/*Update the partial amount of a invoice*/
public function updatePartial($amount)
{
$this->invoice->partial += $amount;
return $this;
}
/**
2020-10-28 11:10:49 +01:00
* Sometimes we need to refresh the
* PDF when it is updated etc.
2020-10-28 11:10:49 +01:00
* @return InvoiceService
*/
public function touchPdf()
{
2020-11-25 15:19:52 +01:00
$this->invoice->invitations->each(function ($invitation) {
2020-10-26 20:10:04 +01:00
CreateEntityPdf::dispatch($invitation);
});
return $this;
}
2020-09-02 03:11:01 +02:00
/*When a reminder is sent we want to touch the dates they were sent*/
public function touchReminder(string $reminder_template)
{
switch ($reminder_template) {
case 'reminder1':
$this->invoice->reminder1_sent = now()->format('Y-m-d');
$this->invoice->reminder_last_sent = now()->format('Y-m-d');
break;
case 'reminder2':
$this->invoice->reminder2_sent = now()->format('Y-m-d');
$this->invoice->reminder_last_sent = now()->format('Y-m-d');
break;
case 'reminder3':
$this->invoice->reminder3_sent = now()->format('Y-m-d');
$this->invoice->reminder_last_sent = now()->format('Y-m-d');
break;
default:
// code...
2020-09-02 03:11:01 +02:00
break;
}
2020-09-02 03:11:01 +02:00
return $this;
}
public function linkEntities()
{
//set all task.invoice_ids = 0
$this->invoice->tasks()->update(['invoice_id' => null]);
//set all tasks.invoice_ids = x with the current line_items
2020-11-25 15:19:52 +01:00
$tasks = collect($this->invoice->line_items)->map(function ($item) {
if (isset($item->task_id)) {
$item->task_id = $this->decodePrimaryKey($item->task_id);
2020-11-25 15:19:52 +01:00
}
2020-11-25 15:19:52 +01:00
if (isset($item->expense_id)) {
$item->expense_id = $this->decodePrimaryKey($item->expense_id);
2020-11-25 15:19:52 +01:00
}
return $item;
});
2020-11-25 15:19:52 +01:00
Task::whereIn('id', $tasks->pluck('task_id'))->update(['invoice_id' => $this->invoice->id]);
Expense::whereIn('id', $tasks->pluck('expense_id'))->update(['invoice_id' => $this->invoice->id]);
return $this;
}
public function fillDefaults()
{
2020-11-04 09:43:20 +01:00
$settings = $this->invoice->client->getMergedSettings();
2021-01-18 21:02:32 +01:00
if (! $this->invoice->design_id)
2020-11-04 09:43:20 +01:00
$this->invoice->design_id = $this->decodePrimaryKey($settings->invoice_design_id);
2021-01-18 21:02:32 +01:00
if (!isset($this->invoice->footer))
2020-11-04 09:43:20 +01:00
$this->invoice->footer = $settings->invoice_footer;
2021-01-18 21:02:32 +01:00
if (!isset($this->invoice->terms))
2020-11-04 09:43:20 +01:00
$this->invoice->terms = $settings->invoice_terms;
2021-01-18 12:08:18 +01:00
2021-01-18 21:04:13 +01:00
if (!isset($this->invoice->public_notes))
2021-01-18 12:08:18 +01:00
$this->invoice->public_notes = $this->invoice->client->public_notes;
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->invoice->exchange_rate) && $this->invoice->client->currency()->id != (int) $this->invoice->company->settings->currency_id)
$this->invoice->exchange_rate = $this->invoice->client->currency()->exchange_rate;
2020-11-25 15:19:52 +01:00
return $this;
}
/**
* Saves the invoice.
* @return Invoice object
*/
public function save() :?Invoice
{
$this->invoice->save();
return $this->invoice;
}
}