Move invoice archive to Base Repository

This commit is contained in:
Lars Kusch 2023-01-30 08:02:02 +01:00
parent 949722ff57
commit 0f05065279
2 changed files with 34 additions and 39 deletions

View File

@ -93,18 +93,4 @@ class InvoiceObserver
{
//
}
/**
* @param Invoice $invoice
* @return void
*/
public function archived(Invoice $invoice){
$subscriptions = Webhook::where('company_id', $invoice->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2));
}
}
}

View File

@ -12,6 +12,7 @@
namespace App\Repositories;
use App\Jobs\Product\UpdateOrCreateProduct;
use App\Jobs\Util\WebhookHandler;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
@ -19,6 +20,7 @@ use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\Webhook;
use App\Utils\Helpers;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
@ -60,6 +62,13 @@ class BaseRepository
if (class_exists($className)) {
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
}
@ -115,7 +124,7 @@ class BaseRepository
{
if (is_array($invitation) && ! array_key_exists('key', $invitation))
return false;
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::with('company')->where('key', $invitation['key'])->first();
@ -132,15 +141,15 @@ class BaseRepository
case ($model instanceof Invoice):
return 'invoice_id';
case ($model instanceof Quote):
return 'quote_id';
return 'quote_id';
case ($model instanceof Credit):
return 'credit_id';
return 'credit_id';
}
}
/**
* Alternative save used for Invoices, Recurring Invoices, Quotes & Credits.
*
*
* @param $data
* @param $model
* @return mixed
@ -149,10 +158,10 @@ class BaseRepository
protected function alternativeSave($data, $model)
{ //$start = microtime(true);
//forces the client_id if it doesn't exist
if(array_key_exists('client_id', $data))
if(array_key_exists('client_id', $data))
$model->client_id = $data['client_id'];
$client = Client::with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail();
$client = Client::with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail();
$state = [];
@ -171,12 +180,12 @@ class BaseRepository
$tmp_data = $data; //preserves the $data array
/* We need to unset some variable as we sometimes unguard the model */
if (isset($tmp_data['invitations']))
if (isset($tmp_data['invitations']))
unset($tmp_data['invitations']);
if (isset($tmp_data['client_contacts']))
if (isset($tmp_data['client_contacts']))
unset($tmp_data['client_contacts']);
$model->fill($tmp_data);
$model->custom_surcharge_tax1 = $client->company->custom_surcharge_taxes1;
@ -188,7 +197,7 @@ class BaseRepository
$this->new_model = true;
if(is_array($model->line_items) && !($model instanceof RecurringInvoice))
{
{
$model->line_items = (collect($model->line_items))->map(function ($item) use($model,$client) {
$item->notes = Helpers::processReservedKeywords($item->notes, $client);
@ -207,10 +216,10 @@ class BaseRepository
$model->service()->setReminder()->save();
/* Save any documents */
if (array_key_exists('documents', $data))
if (array_key_exists('documents', $data))
$this->saveDocuments($data['documents'], $model);
if (array_key_exists('file', $data))
if (array_key_exists('file', $data))
$this->saveDocuments($data['file'], $model);
/* If invitations are present we need to filter existing invitations with the new ones */
@ -222,9 +231,9 @@ class BaseRepository
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::where('key', $invitation)->first();
if ($invitation)
if ($invitation)
$invitation->delete();
});
foreach ($data['invitations'] as $invitation) {
@ -232,7 +241,7 @@ class BaseRepository
//if no invitations are present - create one.
if (! $this->getInvitation($invitation, $resource)) {
if (isset($invitation['id']))
if (isset($invitation['id']))
unset($invitation['id']);
//make sure we are creating an invite for a contact who belongs to the client only!
@ -267,7 +276,7 @@ class BaseRepository
}
/* If no invitations have been created, this is our fail safe to maintain state*/
if ($model->invitations()->count() == 0)
if ($model->invitations()->count() == 0)
$model->service()->createInvitations();
/* Recalculate invoice amounts */
@ -284,7 +293,7 @@ class BaseRepository
$model->partial = min($model->amount, $model->balance);
/* Update product details if necessary - if we are inside a transaction - do nothing */
if ($model->company->update_products && $model->id && \DB::transactionLevel() == 0)
if ($model->company->update_products && $model->id && \DB::transactionLevel() == 0)
UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company);
/* Perform model specific tasks */
@ -298,11 +307,11 @@ class BaseRepository
}
if (! $model->design_id)
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id'));
//links tasks and expenses back to the invoice, but only if we are not in the middle of a transaction.
if (\DB::transactionLevel() == 0)
if (\DB::transactionLevel() == 0)
$model->service()->linkEntities()->save();
if($this->new_model)
@ -316,14 +325,14 @@ class BaseRepository
$model = $model->calc()->getCredit();
if (! $model->design_id)
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
if(array_key_exists('invoice_id', $data) && $data['invoice_id'])
$model->invoice_id = $data['invoice_id'];
if($this->new_model)
event('eloquent.created: App\Models\Credit', $model);
event('eloquent.created: App\Models\Credit', $model);
else
event('eloquent.updated: App\Models\Credit', $model);
@ -336,7 +345,7 @@ class BaseRepository
if ($model instanceof Quote) {
if (! $model->design_id)
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('quote_design_id'));
$model = $model->calc()->getQuote();
@ -350,9 +359,9 @@ class BaseRepository
if ($model instanceof RecurringInvoice) {
if (! $model->design_id)
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id'));
$model = $model->calc()->getRecurringInvoice();