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
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2020-06-30 06:10:40 +02:00
|
|
|
use App\Events\Credit\CreditWasUpdated;
|
2020-05-29 10:27:09 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasUpdated;
|
2020-06-30 06:10:40 +02:00
|
|
|
use App\Events\Quote\QuoteWasUpdated;
|
2020-02-26 22:21:12 +01:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
|
|
|
use App\Factory\QuoteInvitationFactory;
|
|
|
|
use App\Jobs\Product\UpdateOrCreateProduct;
|
2019-11-12 05:41:02 +01:00
|
|
|
use App\Models\Client;
|
2020-02-26 22:21:12 +01:00
|
|
|
use App\Models\ClientContact;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Models\Company;
|
2020-03-02 11:22:37 +01:00
|
|
|
use App\Models\Credit;
|
2020-02-26 22:21:12 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2020-03-02 11:22:37 +01:00
|
|
|
use App\Models\Quote;
|
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;
|
2020-04-10 07:07:36 +02:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
2020-02-26 22:21:12 +01:00
|
|
|
use ReflectionClass;
|
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;
|
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function getInstance()
|
|
|
|
{
|
|
|
|
$className = $this->getClassName();
|
|
|
|
|
|
|
|
return new $className();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
* @param $type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getEventClass($entity, $type)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return 'App\Events\\'.ucfirst(class_basename($entity)).'Was'.$type;
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*/
|
|
|
|
public function archive($entity)
|
|
|
|
{
|
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)) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new $className($entity, $entity->company, Ninja::eventVars()));
|
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
|
|
|
$entity->restore();
|
|
|
|
|
|
|
|
if ($entity->is_deleted) {
|
|
|
|
$fromDeleted = true;
|
|
|
|
$entity->is_deleted = false;
|
|
|
|
$entity->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = $this->getEventClass($entity, 'Restored');
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (class_exists($className)) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars()));
|
2019-12-30 22:59:12 +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)) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new $className($entity, $entity->company, Ninja::eventVars()));
|
2019-01-19 11:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
$entities = $this->findByPublicIdsWithTrashed($ids);
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
if (auth()->user()->can('edit', $entity)) {
|
|
|
|
$this->$action($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($entities);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $ids
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function findByPublicIds($ids)
|
|
|
|
{
|
|
|
|
return $this->getInstance()->scope($ids)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $ids
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function findByPublicIdsWithTrashed($ids)
|
|
|
|
{
|
|
|
|
return $this->getInstance()->scope($ids)->withTrashed()->get();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2020-03-02 11:22:37 +01:00
|
|
|
public function getInvitation($invitation, $resource)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (is_array($invitation) && ! array_key_exists('key', $invitation)) {
|
2020-03-02 11:22:37 +01:00
|
|
|
return false;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-02 11:22:37 +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
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invitation = $invitation_class::whereRaw('BINARY `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
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternative save used for Invoices, Quotes & Credits.
|
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)
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$class = new ReflectionClass($model);
|
2020-03-02 11:22:37 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (array_key_exists('client_id', $data)) {
|
2020-03-07 07:31:26 +01:00
|
|
|
$client = Client::find($data['client_id']);
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2020-03-07 07:31:26 +01:00
|
|
|
$client = Client::find($model->client_id);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-06 12:57:11 +01:00
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
$state = [];
|
|
|
|
$resource = explode('\\', $class->name)[2]; /** This will extract 'Invoice' from App\Models\Invoice */
|
2020-09-06 11:38:10 +02:00
|
|
|
$lcfirst_resource_id = lcfirst($resource).'_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));
|
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
|
|
|
|
2020-03-31 13:52:21 +02:00
|
|
|
$tmp_data = $data;
|
|
|
|
|
2020-04-01 10:54:22 +02:00
|
|
|
/* We need to unset some variable as we sometimes unguard the model */
|
2020-04-04 12:32:42 +02:00
|
|
|
if (isset($tmp_data['invitations'])) {
|
2020-03-31 13:52:21 +02:00
|
|
|
unset($tmp_data['invitations']);
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
if (isset($tmp_data['client_contacts'])) {
|
2020-03-31 13:52:21 +02:00
|
|
|
unset($tmp_data['client_contacts']);
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-31 13:52:21 +02:00
|
|
|
|
|
|
|
$model->fill($tmp_data);
|
2020-02-26 22:21:12 +01:00
|
|
|
$model->save();
|
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
if (array_key_exists('documents', $data)) {
|
2020-04-10 07:07:36 +02:00
|
|
|
$this->saveDocuments($data['documents'], $model);
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
2020-04-10 07:07:36 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invitation_factory_class = sprintf('App\\Factory\\%sInvitationFactory', $resource);
|
2020-02-26 22:21:12 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
$invitation = $invitation_class::whereRaw('BINARY `key`= ?', [$invitation])->first();
|
2020-07-21 01:00:59 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($invitation) {
|
|
|
|
$invitation->delete();
|
|
|
|
}
|
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)) {
|
2020-02-26 22:21:12 +01:00
|
|
|
if (isset($invitation['id'])) {
|
|
|
|
unset($invitation['id']);
|
|
|
|
}
|
|
|
|
|
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-10-17 01:32:53 +02:00
|
|
|
if ($contact && $model->client_id == $contact->client_id)
|
2020-03-04 05:06:27 +01:00
|
|
|
{
|
2020-07-23 05:55:11 +02:00
|
|
|
|
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 {
|
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();
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:06:27 +01:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->load('invitations');
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* If no invitations have been created, this is our fail safe to maintain state*/
|
|
|
|
if ($model->invitations->count() == 0) {
|
|
|
|
$model->service()->createInvitations();
|
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2020-06-22 00:34:02 +02:00
|
|
|
$model = $model->calc()->getInvoice();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$state['finished_amount'] = $model->amount;
|
2020-07-27 10:22:57 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$model = $model->service()->applyNumber()->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-03 10:44:26 +01:00
|
|
|
if ($model->company->update_products !== false) {
|
|
|
|
UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company);
|
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
|
2020-02-27 21:23:25 +01:00
|
|
|
if ($class->name == Invoice::class) {
|
2020-02-26 22:21:12 +01:00
|
|
|
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
|
|
|
|
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']));
|
2020-06-22 00:34:02 +02:00
|
|
|
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $model->design_id) {
|
2020-06-29 03:04:09 +02:00
|
|
|
$model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id'));
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
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();
|
|
|
|
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 21:23:25 +01:00
|
|
|
if ($class->name == Credit::class) {
|
2020-06-29 01:38:37 +02:00
|
|
|
|
2020-10-08 06:05:38 +02:00
|
|
|
$model = $model->calc()->getCredit();
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2020-10-08 05:31:02 +02:00
|
|
|
$model->ledger()->updateCreditBalance(($state['finished_amount'] - $state['starting_amount']));
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $model->design_id) {
|
2020-06-29 03:04:09 +02:00
|
|
|
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-02-26 22:21:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-27 21:23:25 +01:00
|
|
|
if ($class->name == Quote::class) {
|
2020-02-26 22:21:12 +01:00
|
|
|
$model = $model->calc()->getQuote();
|
2020-06-29 01:38:37 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $model->design_id) {
|
2020-06-29 03:04:09 +02:00
|
|
|
$model->design_id = $this->decodePrimaryKey($client->getSetting('quote_design_id'));
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-06-30 05:31:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 06:10:40 +02:00
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model->fresh();
|
2020-06-30 05:31:30 +02:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|