1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 00:53:10 +01:00
invoiceninja/app/Repositories/BaseRepository.php

348 lines
11 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Repositories;
use App\Jobs\Product\UpdateOrCreateProduct;
use App\Models\Client;
use App\Models\ClientContact;
2020-07-08 14:02:16 +02:00
use App\Models\Company;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Quote;
use App\Models\RecurringInvoice;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2019-06-12 06:22:05 +02:00
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use ReflectionClass;
2019-06-12 06:22:05 +02:00
class BaseRepository
{
2019-06-12 06:22:05 +02:00
use MakesHash;
use SavesDocuments;
public $import_mode = false;
/**
* @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;
}
/**
* @param $entity
*/
public function archive($entity)
{
if ($entity->trashed()) {
return;
}
$entity->delete();
$className = $this->getEventClass($entity, 'Archived');
if (class_exists($className)) {
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user()->id)));
}
}
/**
* @param $entity
*/
public function restore($entity)
{
if (! $entity->trashed()) {
return;
}
$fromDeleted = false;
$entity->restore();
if ($entity->is_deleted) {
$fromDeleted = true;
$entity->is_deleted = false;
$entity->save();
}
$className = $this->getEventClass($entity, 'Restored');
if (class_exists($className)) {
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user()->id)));
}
}
/**
* @param $entity
*/
public function delete($entity)
{
if ($entity->is_deleted) {
return;
}
$entity->is_deleted = true;
$entity->save();
$entity->delete();
$className = $this->getEventClass($entity, 'Deleted');
if (class_exists($className) && ! ($entity instanceof Company)) {
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user()->id)));
}
}
/**
* @param $ids
* @param $action
*
* @return int
*/
public function bulk($ids, $action)
{
if (! $ids) {
return 0;
}
2019-06-12 06:22:05 +02:00
$ids = $this->transformKeys($ids);
$entities = $this->findByPublicIdsWithTrashed($ids);
foreach ($entities as $entity) {
if (auth()->user()->can('edit', $entity)) {
$this->$action($entity);
}
}
return count($entities);
}
2021-01-15 01:59:23 +01:00
/* Returns an invoice if defined as a key in the $resource array*/
public function getInvitation($invitation, $resource)
{
2021-01-15 01:59:23 +01:00
if (is_array($invitation) && ! array_key_exists('key', $invitation))
return false;
2021-01-15 01:59:23 +01:00
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::whereRaw('BINARY `key`= ?', [$invitation['key']])->first();
return $invitation;
}
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):
return 'quote_id';
case ($model instanceof Credit):
return 'credit_id';
}
}
/**
2021-01-15 01:59:23 +01:00
* Alternative save used for Invoices, Recurring Invoices, Quotes & Credits.
*
2020-10-28 11:10:49 +01:00
* @param $data
* @param $model
* @return mixed
* @throws \ReflectionException
*/
protected function alternativeSave($data, $model)
{
2021-01-15 01:59:23 +01:00
if (array_key_exists('client_id', $data)) //forces the client_id if it doesn't exist
$model->client_id = $data['client_id'];
$client = Client::where('id', $model->client_id)->withTrashed()->first();
$state = [];
2021-01-15 01:59:23 +01:00
$resource = class_basename($model); //ie Invoice
$lcfirst_resource_id = $this->resolveEntityKey($model); //ie invoice_id
2020-10-08 11:12:44 +02:00
$state['starting_amount'] = $model->amount;
2020-10-28 11:10:49 +01:00
if (! $model->id) {
$company_defaults = $client->setCompanyDefaults($data, lcfirst($resource));
$model->uses_inclusive_taxes = $client->getSetting('inclusive_taxes');
$data = array_merge($company_defaults, $data);
}
2021-01-15 01:59:23 +01:00
$tmp_data = $data; //preserves the $data arrayss
/* We need to unset some variable as we sometimes unguard the model */
2021-01-15 01:59:23 +01:00
if (isset($tmp_data['invitations']))
unset($tmp_data['invitations']);
2021-01-15 01:59:23 +01:00
if (isset($tmp_data['client_contacts']))
unset($tmp_data['client_contacts']);
2021-01-15 01:59:23 +01:00
$model->fill($tmp_data);
$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;
$model->save();
2021-01-15 01:59:23 +01:00
/* Model now persisted, now lets do some child tasks */
/* Save any documents */
2021-01-15 01:59:23 +01:00
if (array_key_exists('documents', $data))
$this->saveDocuments($data['documents'], $model);
2021-01-15 01:59:23 +01:00
/* Marks whether the client contact should receive emails based on the send_email property */
if (isset($data['client_contacts'])) {
foreach ($data['client_contacts'] as $contact) {
if ($contact['send_email'] == 1 && is_string($contact['id'])) {
$client_contact = ClientContact::find($this->decodePrimaryKey($contact['id']));
$client_contact->send_email = true;
$client_contact->save();
}
}
}
2021-01-15 01:59:23 +01:00
/* If invitations are present we need to filter existing invitations with the new ones */
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 */
$model->invitations->pluck('key')->diff($invitations->pluck('key'))->each(function ($invitation) use ($resource) {
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::whereRaw('BINARY `key`= ?', [$invitation])->first();
2021-01-15 01:59:23 +01:00
if ($invitation)
$invitation->delete();
2021-01-15 01:59:23 +01:00
});
foreach ($data['invitations'] as $invitation) {
//if no invitations are present - create one.
if (! $this->getInvitation($invitation, $resource)) {
2021-01-15 01:59:23 +01:00
if (isset($invitation['id']))
unset($invitation['id']);
//make sure we are creating an invite for a contact who belongs to the client only!
$contact = ClientContact::find($invitation['client_contact_id']);
2020-11-25 15:19:52 +01:00
if ($contact && $model->client_id == $contact->client_id) {
2021-01-15 01:59:23 +01: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();
if ($new_invitation && $new_invitation->trashed()) {
2021-01-15 01:59:23 +01:00
2020-07-23 05:55:11 +02:00
$new_invitation->restore();
2021-01-15 01:59:23 +01: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;
$new_invitation->save();
2021-01-15 01:59:23 +01:00
2020-07-23 05:55:11 +02:00
}
}
}
}
}
$model->load('invitations');
/* If no invitations have been created, this is our fail safe to maintain state*/
2021-01-15 01:59:23 +01:00
if ($model->invitations->count() == 0)
$model->service()->createInvitations();
2021-01-15 01:59:23 +01:00
/* Recalculate invoice amounts */
2020-06-22 00:34:02 +02:00
$model = $model->calc()->getInvoice();
2021-01-15 01:59:23 +01:00
/* We use this to compare to our starting amount */
$state['finished_amount'] = $model->amount;
2020-07-27 10:22:57 +02:00
2021-01-15 01:59:23 +01:00
/* Apply entity number */
$model = $model->service()->applyNumber()->save();
/* Handle attempts where the deposit is greater than the amount/balance of the invoice */
if((int)$model->balance != 0 && $model->partial > $model->amount)
$model->partial = min($model->amount, $model->balance);
2021-01-15 01:59:23 +01:00
/* Update product details if necessary */
2021-02-22 23:08:43 +01:00
if ($model->company->update_products)
UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company);
2021-01-15 01:59:23 +01:00
/* Perform model specific tasks */
if ($model instanceof Invoice) {
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
2021-01-15 01:59:23 +01:00
2021-01-21 05:42:30 +01:00
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
2020-06-22 00:34:02 +02:00
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
2021-01-15 01:59:23 +01:00
}
2021-01-15 01:59:23 +01:00
if (! $model->design_id)
2020-06-29 03:04:09 +02:00
$model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id'));
2020-11-01 09:53:23 +01:00
2020-11-01 10:41:49 +01:00
//links tasks and expenses back to the invoice.
2020-11-01 09:53:23 +01:00
$model->service()->linkEntities()->save();
2021-01-15 01:59:23 +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
2021-02-03 13:29:44 +01:00
// $model->ledger()->updateCreditBalance(-1*($state['finished_amount'] - $state['starting_amount']));
2020-10-08 05:31:02 +02:00
2021-01-15 01:59:23 +01:00
if (! $model->design_id)
2020-06-29 03:04:09 +02:00
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
2021-01-15 01:59:23 +01:00
}
2021-01-15 01:59:23 +01:00
if ($model instanceof Quote) {
$model = $model->calc()->getQuote();
2021-01-15 01:59:23 +01:00
2020-06-30 05:31:30 +02:00
}
2021-01-15 01:59:23 +01:00
if ($model instanceof RecurringInvoice) {
$model = $model->calc()->getRecurringInvoice();
2021-01-15 01:59:23 +01:00
}
2020-06-30 06:10:40 +02:00
$model->save();
return $model->fresh();
2020-06-30 05:31:30 +02:00
}
}