2019-04-04 01:17:15 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2019-04-04 01:17:15 +02:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
2019-05-29 06:33:53 +02:00
|
|
|
use App\Models\InvoiceInvitation;
|
2019-09-23 05:22:24 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-04-04 01:17:15 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* InvoiceRepository.
|
2019-04-04 01:17:15 +02:00
|
|
|
*/
|
2020-03-21 06:37:30 +01:00
|
|
|
class InvoiceRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Saves the invoices.
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-11-03 14:27:41 +01:00
|
|
|
* @param array $data The invoice data
|
|
|
|
* @param Invoice $invoice The invoice
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-11-03 14:27:41 +01:00
|
|
|
* @return Invoice|null Returns the invoice object
|
2020-03-21 06:37:30 +01:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function save($data, Invoice $invoice): ?Invoice
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
|
|
|
return $this->alternativeSave($data, $invoice);
|
|
|
|
}
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/**
|
|
|
|
* Mark the invoice as sent.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Invoice $invoice The invoice
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Invoice|null Return the invoice object
|
2020-03-21 06:37:30 +01:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function markSent(Invoice $invoice): ?Invoice
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
|
|
|
return $invoice->service()->markSent()->save();
|
|
|
|
}
|
2020-03-02 11:22:37 +01:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function getInvitationByKey($key): ?InvoiceInvitation
|
2020-03-02 11:22:37 +01:00
|
|
|
{
|
2023-08-06 09:35:19 +02:00
|
|
|
return InvoiceInvitation::query()->where('key', $key)->first();
|
2020-03-02 11:22:37 +01:00
|
|
|
}
|
2020-04-04 12:32:42 +02:00
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
/**
|
|
|
|
* Method is not protected, assumes that
|
|
|
|
* other protections have been implemented prior
|
|
|
|
* to hitting this method.
|
|
|
|
*
|
|
|
|
* ie. invoice can be deleted from a business logic perspective.
|
2020-04-15 02:30:52 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Invoice $invoice
|
2020-11-03 14:34:24 +01:00
|
|
|
* @return Invoice $invoice
|
2020-04-08 12:48:31 +02:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function delete($invoice): Invoice
|
2020-04-04 12:32:42 +02:00
|
|
|
{
|
2024-06-24 09:37:08 +02:00
|
|
|
$invoice = $invoice->fresh();
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
if ($invoice->is_deleted) {
|
2020-11-03 14:34:24 +01:00
|
|
|
return $invoice;
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:20:39 +01:00
|
|
|
$invoice = $invoice->service()->markDeleted()->save();
|
2020-09-23 12:52:54 +02:00
|
|
|
|
2020-11-03 21:19:59 +01:00
|
|
|
parent::delete($invoice);
|
2020-04-08 12:48:31 +02:00
|
|
|
|
|
|
|
return $invoice;
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-04-06 14:32:27 +02:00
|
|
|
|
2020-12-03 05:30:32 +01:00
|
|
|
/**
|
|
|
|
* Handles the restoration on a deleted invoice.
|
2020-12-05 14:24:21 +01:00
|
|
|
*
|
2023-04-28 03:39:41 +02:00
|
|
|
* @param Invoice $invoice
|
|
|
|
* @return Invoice
|
2020-12-03 05:30:32 +01:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function restore($invoice): Invoice
|
2020-12-03 05:20:39 +01:00
|
|
|
{
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($invoice->is_proforma) {
|
2023-03-16 06:45:40 +01:00
|
|
|
return $invoice;
|
2023-03-18 08:24:56 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2020-12-03 05:30:32 +01:00
|
|
|
//if we have just archived, only perform a soft restore
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invoice->is_deleted) {
|
2020-12-03 05:30:32 +01:00
|
|
|
parent::restore($invoice);
|
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
}
|
2020-12-03 05:20:39 +01:00
|
|
|
|
2020-12-03 05:30:32 +01:00
|
|
|
// reversed delete invoice actions
|
2020-12-03 11:46:36 +01:00
|
|
|
$invoice = $invoice->service()->handleRestore()->save();
|
2020-12-03 05:20:39 +01:00
|
|
|
|
2023-07-16 09:42:34 +02:00
|
|
|
/* If the reverse did not succeed due to rules, then do not restore / unarchive */
|
2023-10-26 04:57:44 +02:00
|
|
|
if($invoice->is_deleted) {
|
2023-07-16 09:42:34 +02:00
|
|
|
return $invoice;
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-07-16 09:42:34 +02:00
|
|
|
|
2020-12-03 05:20:39 +01:00
|
|
|
parent::restore($invoice);
|
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
}
|
|
|
|
|
2020-04-06 14:32:27 +02:00
|
|
|
public function reverse()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cancel()
|
|
|
|
{
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|