diff --git a/app/Events/User/UserWasRestored.php b/app/Events/User/UserWasRestored.php index c16bea7cb8..6c1de4341f 100644 --- a/app/Events/User/UserWasRestored.php +++ b/app/Events/User/UserWasRestored.php @@ -35,6 +35,8 @@ class UserWasRestored public $event_vars; + public $fromDeleted; + /** * Create a new event instance. * @@ -42,11 +44,12 @@ class UserWasRestored * @param Company $company * @param array $event_vars */ - public function __construct(User $user, Company $company, array $event_vars) + public function __construct(User $user, bool $fromDeleted, Company $company, array $event_vars) { $this->user = $user; $this->company = $company; $this->event_vars = $event_vars; + $this->fromDeleted = $fromDeleted; } /** diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index f46a6de8c7..d8d966432b 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -459,6 +459,24 @@ class Import implements ShouldQueue $saveable_contacts['contacts'] = $modified_contacts; $contact_repository->save($saveable_contacts, $client); + + //link contact ids + $client->fresh(); + $new_contacts = $client->contacts; + + foreach($resource['contacts'] as $key => $old_contact) + { + $contact_match = $new_contacts->where('contact_key', $old_contact['contact_key'])->first(); + + if($contact_match) + { + $this->ids['client_contacts']['client_contacts_'.$old_contact['id']] = [ + 'old' => $old_contact['id'], + 'new' => $contact_match->id, + ]; + } + } + } $key = "clients_{$resource['id']}"; @@ -611,6 +629,16 @@ class Import implements ShouldQueue unset($modified['id']); + foreach($resource['invitations'] as $key => $invite) + { + + $resource['invitations'][$key]['client_contact_id'] = $this->transformId('client_contacts', $invite['client_contact_id']); + $resource['invitations'][$key]['user_id'] = $modified['user_id']; + $resource['invitations'][$key]['company_id'] = $this->company->id; + unset($resource['invitations'][$key]['recurring_invoice_id']); + + } + $invoice = $invoice_repository->save( $modified, RecurringInvoiceFactory::create($this->company->id, $modified['user_id']) @@ -660,6 +688,18 @@ class Import implements ShouldQueue $modified['line_items'] = $this->cleanItems($modified['line_items']); unset($modified['id']); + + foreach($resource['invitations'] as $key => $invite) + { + $resource['invitations'][$key]['client_contact_id'] = $this->transformId('client_contacts', $invite['client_contact_id']); + $resource['invitations'][$key]['user_id'] = $modified['user_id']; + $resource['invitations'][$key]['company_id'] = $this->company->id; + unset($resource['invitations'][$key]['invoice_id']); + + nlog("find a match for " . $invite['client_contact_id'] . " " .$resource['invitations'][$key]['client_contact_id']); + } + + $modified['invitations'] = $resource['invitations']; $invoice = $invoice_repository->save( $modified, @@ -811,11 +851,6 @@ class Import implements ShouldQueue //$modified['invoice_id'] = $this->transformId('invoices', $resource['invoice_id']); $modified['company_id'] = $this->company->id; -// nlog($resource); -// nlog($resource['company_gateway_id']); -// nlog(strlen($resource['company_gateway_id'])); - - //unset($modified['invoices']); unset($modified['invoice_id']); diff --git a/app/Repositories/Migration/InvoiceMigrationRepository.php b/app/Repositories/Migration/InvoiceMigrationRepository.php index fce68791f3..8d06ab14b0 100644 --- a/app/Repositories/Migration/InvoiceMigrationRepository.php +++ b/app/Repositories/Migration/InvoiceMigrationRepository.php @@ -16,8 +16,10 @@ use App\Models\Client; use App\Models\ClientContact; use App\Models\Credit; use App\Models\Invoice; +use App\Models\InvoiceInvitation; use App\Models\Quote; use App\Models\RecurringInvoice; +use App\Models\RecurringInvoiceInvitation; use App\Repositories\BaseRepository; use App\Utils\Traits\MakesHash; use App\Utils\Traits\SavesDocuments; @@ -86,10 +88,25 @@ class InvoiceMigrationRepository extends BaseRepository } } + InvoiceInvitation::unguard(); + RecurringInvoiceInvitation::unguard(); + + foreach($data['invitations'] as $invitation) + { + nlog($invitation); + + $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(); +/* 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) { $this->getInvitation($invitation, $resource)->delete(); }); @@ -114,7 +131,7 @@ class InvoiceMigrationRepository extends BaseRepository } } } - +*/ $model->load('invitations'); /* If no invitations have been created, this is our fail safe to maintain state*/