diff --git a/app/Models/Document.php b/app/Models/Document.php index e1f01b5842..905ea09235 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -204,6 +204,21 @@ class Document extends EntityModel $array['preview_url'] = $this->getPreviewUrl(); return $array; } + + public function cloneDocument(){ + $document = Document::createNew($this); + $document->path = $this->path; + $document->preview = $this->preview; + $document->name = $this->name; + $document->type = $this->type; + $document->disk = $this->disk; + $document->hash = $this->hash; + $document->size = $this->size; + $document->width = $this->width; + $document->height = $this->height; + + return $document; + } } Document::deleted(function ($document) { diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index bf9cc59c39..d8aa051b6c 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -403,6 +403,13 @@ class InvoiceRepository extends BaseRepository foreach ($document_ids as $document_id){ $document = Document::scope($document_id)->first(); if($document && !$checkSubPermissions || $document->canEdit()){ + + if($document->invoice_id && $document->invoice_id != $invoice->id){ + // From a clone + $document = $document->cloneDocument(); + $document_ids[] = $document->public_id;// Don't remove this document + } + $document->invoice_id = $invoice->id; $document->save(); } @@ -412,7 +419,10 @@ class InvoiceRepository extends BaseRepository if(!in_array($document->public_id, $document_ids)){ // Removed if(!$checkSubPermissions || $document->canEdit()){ - $document->delete(); + if($document->invoice_id == $invoice->id){ + // Make sure the document isn't on a clone + $document->delete(); + } } } } @@ -572,6 +582,11 @@ class InvoiceRepository extends BaseRepository $clone->invoice_items()->save($cloneItem); } + foreach ($invoice->documents as $document) { + $cloneDocument = $document->cloneDocument(); + $invoice->documents()->save($cloneDocument); + } + foreach ($invoice->invitations as $invitation) { $cloneInvitation = Invitation::createNew($invoice); $cloneInvitation->contact_id = $invitation->contact_id; @@ -676,6 +691,11 @@ class InvoiceRepository extends BaseRepository $invoice->invoice_items()->save($item); } + foreach ($recurInvoice->documents as $recurDocument) { + $document = $recurDocument->cloneDocument(); + $invoice->documents()->save($document); + } + foreach ($recurInvoice->invitations as $recurInvitation) { $invitation = Invitation::createNew($recurInvitation); $invitation->contact_id = $recurInvitation->contact_id; diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 134826e44a..8bcb89a9d6 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -271,7 +271,7 @@