1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Repositories/Migration/InvoiceMigrationRepository.php

182 lines
5.7 KiB
PHP
Raw Normal View History

2020-07-01 06:37:05 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-01 06:37:05 +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)
2020-07-01 06:37:05 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-01 06:37:05 +02:00
*/
namespace App\Repositories\Migration;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Credit;
use App\Models\Invoice;
2020-12-29 00:44:24 +01:00
use App\Models\InvoiceInvitation;
2020-07-01 06:37:05 +02:00
use App\Models\Quote;
2020-10-05 23:46:47 +02:00
use App\Models\RecurringInvoice;
2020-12-29 00:44:24 +01:00
use App\Models\RecurringInvoiceInvitation;
2020-07-01 06:37:05 +02:00
use App\Repositories\BaseRepository;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use Illuminate\Support\Carbon;
2020-07-01 07:51:19 +02:00
use ReflectionClass;
2020-07-01 06:37:05 +02:00
/**
* InvoiceMigrationRepository.
2020-07-01 06:37:05 +02:00
*/
class InvoiceMigrationRepository extends BaseRepository
{
use MakesHash;
use SavesDocuments;
public function save($data, $model)
{
$class = new ReflectionClass($model);
if (array_key_exists('client_id', $data)) {
$client = Client::where('id', $data['client_id'])->withTrashed()->first();
2020-07-01 06:37:05 +02:00
} else {
$client = Client::where('id', $model->client_id)->withTrashed()->first();
2020-07-01 06:37:05 +02:00
}
$state = [];
$resource = explode('\\', $class->name)[2]; /** This will extract 'Invoice' from App\Models\Invoice */
$lcfirst_resource_id = lcfirst($resource).'_id';
2020-07-01 06:37:05 +02:00
2020-10-05 23:46:47 +02:00
if ($class->name == Invoice::class || $class->name == Quote::class || $class->name == RecurringInvoice::class) {
2020-07-01 06:37:05 +02:00
$state['starting_amount'] = $model->amount;
}
if (! $model->id) {
2020-07-01 06:37:05 +02:00
$company_defaults = $client->setCompanyDefaults($data, lcfirst($resource));
$model->uses_inclusive_taxes = $client->getSetting('inclusive_taxes');
$data = array_merge($company_defaults, $data);
}
2020-07-01 06:37:05 +02:00
$tmp_data = $data;
if (array_key_exists('tax_rate1', $tmp_data) && is_null($tmp_data['tax_rate1'])) {
$tmp_data['tax_rate1'] = 0;
}
if (array_key_exists('tax_rate2', $tmp_data) && is_null($tmp_data['tax_rate2'])) {
$tmp_data['tax_rate2'] = 0;
}
2020-07-01 06:37:05 +02:00
/* We need to unset some variable as we sometimes unguard the model */
if (isset($tmp_data['invitations'])) {
unset($tmp_data['invitations']);
}
if (isset($tmp_data['client_contacts'])) {
unset($tmp_data['client_contacts']);
}
$model->fill($tmp_data);
2020-12-30 00:23:14 +01:00
$model->status_id = $tmp_data['status_id'];
if ($tmp_data['created_at']) {
$model->created_at = Carbon::parse($tmp_data['created_at']);
}
if ($tmp_data['updated_at']) {
$model->updated_at = Carbon::parse($tmp_data['updated_at']);
}
$model->save(['timestamps' => false]);
2020-07-01 06:37:05 +02:00
if (array_key_exists('documents', $data)) {
$this->saveDocuments($data['documents'], $model);
}
$invitation_factory_class = sprintf('App\\Factory\\%sInvitationFactory', $resource);
2020-07-01 06:37:05 +02: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();
}
}
}
2020-12-29 00:44:24 +01:00
InvoiceInvitation::unguard();
RecurringInvoiceInvitation::unguard();
2021-01-04 13:38:00 +01:00
if ($model instanceof RecurringInvoice) {
2020-12-29 22:43:27 +01:00
$lcfirst_resource_id = 'recurring_invoice_id';
2021-01-04 13:38:00 +01:00
}
2021-01-04 13:38:00 +01:00
foreach ($data['invitations'] as $invitation) {
2021-01-07 23:03:29 +01:00
// nlog($invitation);
2020-12-29 00:44:24 +01:00
$new_invitation = $invitation_factory_class::create($model->company_id, $model->user_id);
$new_invitation->{$lcfirst_resource_id} = $model->id;
$new_invitation->fill($invitation);
$new_invitation->save();
}
InvoiceInvitation::reguard();
RecurringInvoiceInvitation::reguard();
2020-07-01 06:37:05 +02:00
$model->load('invitations');
/* If no invitations have been created, this is our fail safe to maintain state*/
if ($model->invitations->count() == 0) {
$model->service()->createInvitations();
}
$model = $model->calc()->getInvoice();
2020-07-01 06:37:05 +02:00
$state['finished_amount'] = $model->amount;
2020-07-01 06:37:05 +02:00
$model = $model->service()->applyNumber()->save();
2020-10-05 23:46:47 +02:00
if ($class->name == Invoice::class || $class->name == RecurringInvoice::class) {
2020-07-01 06:37:05 +02:00
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
}
if (! $model->design_id) {
2020-07-01 06:37:05 +02:00
$model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id'));
}
2020-07-01 06:37:05 +02:00
}
if ($class->name == Credit::class) {
$model = $model->calc()->getCredit();
if (! $model->design_id) {
2020-07-01 06:37:05 +02:00
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
}
2020-07-01 06:37:05 +02:00
}
2020-07-01 06:37:05 +02:00
if ($class->name == Quote::class) {
$model = $model->calc()->getQuote();
if (! $model->design_id) {
2020-07-01 06:37:05 +02:00
$model->design_id = $this->decodePrimaryKey($client->getSetting('quote_design_id'));
}
2020-07-01 06:37:05 +02:00
}
if ($data['is_deleted']) {
2021-02-03 13:29:44 +01:00
$model->is_deleted = true;
$model->save();
}
if ($data['deleted_at'] == '0000-00-00 00:00:00.000000') {
$model->deleted_at = null;
} elseif ($data['deleted_at']) {
2021-02-03 13:29:44 +01:00
$model->delete();
}
2021-02-03 13:29:44 +01:00
2020-07-01 06:37:05 +02:00
$model->save();
2020-10-06 02:52:16 +02:00
2020-07-01 06:37:05 +02:00
return $model->fresh();
}
}