2018-11-22 12:12:41 +01: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
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. 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
|
|
|
*/
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Jobs\Client\UpdateTaxData;
|
|
|
|
use App\Jobs\Product\UpdateOrCreateProduct;
|
2019-11-12 05:41:02 +01:00
|
|
|
use App\Models\Client;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\ClientContact;
|
2023-06-02 07:53:33 +02:00
|
|
|
use App\Models\Company;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Credit;
|
2023-06-02 07:53:33 +02:00
|
|
|
use App\Models\Invoice;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Quote;
|
2023-06-02 07:53:33 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Helpers;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-04-10 07:07:36 +02:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
2019-06-12 06:22:05 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
class BaseRepository
|
2018-11-22 12:12:41 +01:00
|
|
|
{
|
2019-06-12 06:22:05 +02:00
|
|
|
use MakesHash;
|
2020-04-10 07:07:36 +02:00
|
|
|
use SavesDocuments;
|
2021-09-07 05:57:55 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
public bool $import_mode = false;
|
2020-04-10 07:07:36 +02:00
|
|
|
|
2021-10-10 11:56:05 +02:00
|
|
|
private bool $new_model = false;
|
2019-01-19 11:35:21 +01:00
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
* @param $type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getEventClass($entity, $type)
|
|
|
|
{
|
2020-11-03 11:04:15 +01:00
|
|
|
return 'App\Events\\'.ucfirst(class_basename($entity)).'\\'.ucfirst(class_basename($entity)).'Was'.$type;
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*/
|
2023-01-31 11:05:01 +01:00
|
|
|
public function archive($entity)
|
2019-01-19 11:35:21 +01:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($entity->trashed()) {
|
2019-01-19 11:35:21 +01:00
|
|
|
return;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
$entity->delete();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
$className = $this->getEventClass($entity, 'Archived');
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (class_exists($className)) {
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*/
|
|
|
|
public function restore($entity)
|
|
|
|
{
|
|
|
|
if (! $entity->trashed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fromDeleted = false;
|
2019-11-12 05:41:02 +01:00
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
if ($entity->is_deleted) {
|
|
|
|
$fromDeleted = true;
|
|
|
|
$entity->is_deleted = false;
|
2023-01-31 11:05:01 +01:00
|
|
|
$entity->saveQuietly();
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 12:21:23 +01:00
|
|
|
$entity->restore();
|
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
$className = $this->getEventClass($entity, 'Restored');
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (class_exists($className)) {
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2023-01-31 08:59:02 +01:00
|
|
|
}
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*/
|
|
|
|
public function delete($entity)
|
|
|
|
{
|
|
|
|
if ($entity->is_deleted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity->is_deleted = true;
|
|
|
|
$entity->save();
|
|
|
|
|
|
|
|
$entity->delete();
|
|
|
|
|
|
|
|
$className = $this->getEventClass($entity, 'Deleted');
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (class_exists($className) && ! ($entity instanceof Company)) {
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* Returns an invoice if defined as a key in the $resource array*/
|
2020-03-02 11:22:37 +01:00
|
|
|
public function getInvitation($invitation, $resource)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (is_array($invitation) && ! array_key_exists('key', $invitation)) {
|
2020-03-02 11:22:37 +01:00
|
|
|
return false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-30 08:02:02 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
|
2020-03-02 11:22:37 +01:00
|
|
|
|
2022-07-28 06:09:13 +02:00
|
|
|
$invitation = $invitation_class::with('company')->where('key', $invitation['key'])->first();
|
2020-03-02 11:22:37 +01:00
|
|
|
|
|
|
|
return $invitation;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* Clean return of a key rather than butchering the model*/
|
|
|
|
private function resolveEntityKey($model)
|
|
|
|
{
|
|
|
|
switch ($model) {
|
|
|
|
case ($model instanceof RecurringInvoice):
|
|
|
|
return 'recurring_invoice_id';
|
|
|
|
case ($model instanceof Invoice):
|
|
|
|
return 'invoice_id';
|
|
|
|
case ($model instanceof Quote):
|
2023-01-30 08:02:02 +01:00
|
|
|
return 'quote_id';
|
2021-01-15 01:59:23 +01:00
|
|
|
case ($model instanceof Credit):
|
2023-01-30 08:02:02 +01:00
|
|
|
return 'credit_id';
|
2021-01-15 01:59:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
/**
|
2021-01-15 01:59:23 +01:00
|
|
|
* Alternative save used for Invoices, Recurring Invoices, Quotes & Credits.
|
2023-01-30 08:02:02 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $data
|
|
|
|
* @param $model
|
|
|
|
* @return mixed
|
|
|
|
* @throws \ReflectionException
|
2020-02-26 22:21:12 +01:00
|
|
|
*/
|
|
|
|
protected function alternativeSave($data, $model)
|
2022-10-21 00:05:37 +02:00
|
|
|
{ //$start = microtime(true);
|
2021-05-26 04:37:16 +02:00
|
|
|
//forces the client_id if it doesn't exist
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('client_id', $data)) {
|
2021-01-15 01:59:23 +01:00
|
|
|
$model->client_id = $data['client_id'];
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-01-15 01:59:23 +01:00
|
|
|
|
2023-08-29 00:53:25 +02:00
|
|
|
$client = Client::query()->with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail();
|
2020-03-06 12:57:11 +01:00
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
$state = [];
|
2021-01-14 23:58:21 +01:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
$resource = class_basename($model); //ie Invoice
|
|
|
|
|
|
|
|
$lcfirst_resource_id = $this->resolveEntityKey($model); //ie invoice_id
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2020-10-08 11:12:44 +02:00
|
|
|
$state['starting_amount'] = $model->amount;
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $model->id) {
|
2020-03-21 06:37:30 +01:00
|
|
|
$company_defaults = $client->setCompanyDefaults($data, lcfirst($resource));
|
2023-07-24 07:22:12 +02:00
|
|
|
$data['exchange_rate'] = $company_defaults['exchange_rate'];
|
2020-02-27 21:23:25 +01:00
|
|
|
$model->uses_inclusive_taxes = $client->getSetting('inclusive_taxes');
|
2020-03-10 07:45:24 +01:00
|
|
|
$data = array_merge($company_defaults, $data);
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-05-26 04:37:16 +02:00
|
|
|
$tmp_data = $data; //preserves the $data array
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-04-01 10:54:22 +02:00
|
|
|
/* We need to unset some variable as we sometimes unguard the model */
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($tmp_data['invitations'])) {
|
2020-03-31 13:52:21 +02:00
|
|
|
unset($tmp_data['invitations']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-30 08:02:02 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($tmp_data['client_contacts'])) {
|
2020-03-31 13:52:21 +02:00
|
|
|
unset($tmp_data['client_contacts']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-30 08:02:02 +01:00
|
|
|
|
2020-03-31 13:52:21 +02:00
|
|
|
$model->fill($tmp_data);
|
2021-02-21 10:36:34 +01:00
|
|
|
|
|
|
|
$model->custom_surcharge_tax1 = $client->company->custom_surcharge_taxes1;
|
|
|
|
$model->custom_surcharge_tax2 = $client->company->custom_surcharge_taxes2;
|
|
|
|
$model->custom_surcharge_tax3 = $client->company->custom_surcharge_taxes3;
|
|
|
|
$model->custom_surcharge_tax4 = $client->company->custom_surcharge_taxes4;
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$model->id) {
|
2021-10-10 11:56:05 +02:00
|
|
|
$this->new_model = true;
|
2022-10-21 00:05:37 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (is_array($model->line_items) && !($model instanceof RecurringInvoice)) {
|
2023-04-22 09:07:22 +02:00
|
|
|
$model->line_items = (collect($model->line_items))->map(function ($item) use ($client) {
|
2022-10-21 06:00:33 +02:00
|
|
|
$item->notes = Helpers::processReservedKeywords($item->notes, $client);
|
2022-10-21 00:05:37 +02:00
|
|
|
|
2022-10-21 06:00:33 +02:00
|
|
|
return $item;
|
|
|
|
});
|
|
|
|
}
|
2022-10-21 00:05:37 +02:00
|
|
|
}
|
2023-11-03 23:53:20 +01:00
|
|
|
nlog($model->toArray());
|
2021-10-10 11:56:05 +02:00
|
|
|
$model->saveQuietly();
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* Model now persisted, now lets do some child tasks */
|
2020-04-10 07:07:36 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($model instanceof Invoice) {
|
2021-08-01 00:44:04 +02:00
|
|
|
$model->service()->setReminder()->save();
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-08-01 00:44:04 +02:00
|
|
|
|
2021-01-18 00:01:37 +01:00
|
|
|
/* Save any documents */
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('documents', $data)) {
|
2021-01-15 01:59:23 +01:00
|
|
|
$this->saveDocuments($data['documents'], $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('file', $data)) {
|
2022-03-09 10:39:32 +01:00
|
|
|
$this->saveDocuments($data['file'], $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-03-09 10:39:32 +01:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* If invitations are present we need to filter existing invitations with the new ones */
|
2020-02-26 22:21:12 +01:00
|
|
|
if (isset($data['invitations'])) {
|
|
|
|
$invitations = collect($data['invitations']);
|
|
|
|
|
|
|
|
/* Get array of Keys which have been removed from the invitations array and soft delete each invitation */
|
2020-09-06 11:38:10 +02:00
|
|
|
$model->invitations->pluck('key')->diff($invitations->pluck('key'))->each(function ($invitation) use ($resource) {
|
|
|
|
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
|
2023-08-06 09:35:19 +02:00
|
|
|
$invitation = $invitation_class::query()->where('key', $invitation)->first();
|
2020-07-21 01:00:59 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($invitation) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$invitation->delete();
|
2023-01-31 15:06:21 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($data['invitations'] as $invitation) {
|
2020-03-02 11:22:37 +01:00
|
|
|
//if no invitations are present - create one.
|
2020-03-21 06:37:30 +01:00
|
|
|
if (! $this->getInvitation($invitation, $resource)) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($invitation['id'])) {
|
2020-02-26 22:21:12 +01:00
|
|
|
unset($invitation['id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2020-03-04 05:06:27 +01:00
|
|
|
//make sure we are creating an invite for a contact who belongs to the client only!
|
|
|
|
$contact = ClientContact::find($invitation['client_contact_id']);
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($contact && $model->client_id == $contact->client_id) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
|
2020-07-23 05:55:11 +02:00
|
|
|
|
|
|
|
$new_invitation = $invitation_class::withTrashed()
|
|
|
|
->where('client_contact_id', $contact->id)
|
|
|
|
->where($lcfirst_resource_id, $model->id)
|
|
|
|
->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($new_invitation && $new_invitation->trashed()) {
|
2020-07-23 05:55:11 +02:00
|
|
|
$new_invitation->restore();
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2021-01-15 01:59:23 +01:00
|
|
|
$invitation_factory_class = sprintf('App\\Factory\\%sInvitationFactory', $resource);
|
2020-07-23 05:55:11 +02:00
|
|
|
$new_invitation = $invitation_factory_class::create($model->company_id, $model->user_id);
|
|
|
|
$new_invitation->{$lcfirst_resource_id} = $model->id;
|
|
|
|
$new_invitation->client_contact_id = $contact->id;
|
2021-11-06 01:46:12 +01:00
|
|
|
$new_invitation->key = $this->createDbHash($model->company->db);
|
2023-01-31 15:06:21 +01:00
|
|
|
$new_invitation->saveQuietly();
|
2020-07-23 05:55:11 +02:00
|
|
|
}
|
2020-03-04 05:06:27 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* If no invitations have been created, this is our fail safe to maintain state*/
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($model->invitations()->count() == 0) {
|
2020-03-21 06:37:30 +01:00
|
|
|
$model->service()->createInvitations();
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* Recalculate invoice amounts */
|
2020-06-22 00:34:02 +02:00
|
|
|
$model = $model->calc()->getInvoice();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* We use this to compare to our starting amount */
|
2020-03-21 06:37:30 +01:00
|
|
|
$state['finished_amount'] = $model->amount;
|
2020-07-27 10:22:57 +02:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* Apply entity number */
|
2020-03-21 06:37:30 +01:00
|
|
|
$model = $model->service()->applyNumber()->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-04-15 00:09:36 +02:00
|
|
|
/* Handle attempts where the deposit is greater than the amount/balance of the invoice */
|
2023-02-16 02:36:09 +01:00
|
|
|
if ((int)$model->balance != 0 && $model->partial > $model->amount && $model->amount > 0) {
|
2021-04-15 00:09:36 +02:00
|
|
|
$model->partial = min($model->amount, $model->balance);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-04-15 00:09:36 +02:00
|
|
|
|
2021-07-29 04:19:56 +02:00
|
|
|
/* Update product details if necessary - if we are inside a transaction - do nothing */
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($model->company->update_products && $model->id && \DB::transactionLevel() == 0) {
|
2020-11-03 22:26:28 +01:00
|
|
|
UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
/* Perform model specific tasks */
|
|
|
|
if ($model instanceof Invoice) {
|
2022-07-12 12:47:17 +02:00
|
|
|
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
|
2021-04-28 03:18:27 +02:00
|
|
|
$model->service()->updateStatus()->save();
|
2022-07-10 07:53:23 +02:00
|
|
|
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
2021-01-21 05:42:30 +01:00
|
|
|
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! $model->design_id) {
|
2023-04-28 03:39:41 +02:00
|
|
|
$model->design_id = intval($this->decodePrimaryKey($client->getSetting('invoice_design_id')));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-11-01 09:53:23 +01:00
|
|
|
|
2022-10-30 10:48:43 +01:00
|
|
|
//links tasks and expenses back to the invoice, but only if we are not in the middle of a transaction.
|
2023-02-16 02:36:09 +01:00
|
|
|
if (\DB::transactionLevel() == 0) {
|
2022-10-30 10:48:43 +01:00
|
|
|
$model->service()->linkEntities()->save();
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-01-15 01:59:23 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->new_model) {
|
2021-10-10 11:56:05 +02:00
|
|
|
event('eloquent.created: App\Models\Invoice', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2021-10-14 08:54:38 +02:00
|
|
|
event('eloquent.updated: App\Models\Invoice', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-06-02 07:53:33 +02:00
|
|
|
|
|
|
|
/** If the client does not have tax_data - then populate this now */
|
2023-10-26 04:57:44 +02:00
|
|
|
if($client->country_id == 840 && !$client->tax_data && $model->company->calculate_taxes && !$model->company->account->isFreeHostedClient()) {
|
2023-06-02 07:53:33 +02:00
|
|
|
UpdateTaxData::dispatch($client, $client->company);
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-06-02 07:53:33 +02:00
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
if ($model instanceof Credit) {
|
2020-10-08 06:05:38 +02:00
|
|
|
$model = $model->calc()->getCredit();
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! $model->design_id) {
|
2020-06-29 03:04:09 +02:00
|
|
|
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-10-10 11:56:05 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('invoice_id', $data) && $data['invoice_id']) {
|
2022-01-28 06:30:40 +01:00
|
|
|
$model->invoice_id = $data['invoice_id'];
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-10-10 11:56:05 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->new_model) {
|
2023-01-30 08:02:02 +01:00
|
|
|
event('eloquent.created: App\Models\Credit', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2021-10-14 08:54:38 +02:00
|
|
|
event('eloquent.updated: App\Models\Credit', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-10-14 08:54:38 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Credit::STATUS_DRAFT)) {
|
2022-08-22 00:48:52 +02:00
|
|
|
$model->client->service()->adjustCreditBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
if ($model instanceof Quote) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! $model->design_id) {
|
2023-04-28 03:39:41 +02:00
|
|
|
$model->design_id = intval($this->decodePrimaryKey($client->getSetting('quote_design_id')));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-06-19 06:05:45 +02:00
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
$model = $model->calc()->getQuote();
|
2021-01-15 01:59:23 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->new_model) {
|
2021-10-10 11:56:05 +02:00
|
|
|
event('eloquent.created: App\Models\Quote', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2021-10-14 08:54:38 +02:00
|
|
|
event('eloquent.updated: App\Models\Quote', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-06-30 05:31:30 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 01:59:23 +01:00
|
|
|
if ($model instanceof RecurringInvoice) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! $model->design_id) {
|
2023-04-28 03:39:41 +02:00
|
|
|
$model->design_id = intval($this->decodePrimaryKey($client->getSetting('invoice_design_id')));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-30 08:02:02 +01:00
|
|
|
|
2021-01-14 23:58:21 +01:00
|
|
|
$model = $model->calc()->getRecurringInvoice();
|
2021-01-15 01:59:23 +01:00
|
|
|
|
2021-10-10 11:56:05 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->new_model) {
|
2021-10-10 11:56:05 +02:00
|
|
|
event('eloquent.created: App\Models\RecurringInvoice', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2021-10-14 08:54:38 +02:00
|
|
|
event('eloquent.updated: App\Models\RecurringInvoice', $model);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-01-14 23:58:21 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 15:06:21 +01:00
|
|
|
$model->saveQuietly();
|
2022-10-21 00:05:37 +02:00
|
|
|
|
2020-06-30 06:10:40 +02:00
|
|
|
return $model->fresh();
|
2020-06-30 05:31:30 +02:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|