From 94da1d9ded0fd1e2d7a2d60e24c637ffe82fdf8d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 30 Aug 2024 15:00:25 +1000 Subject: [PATCH] Logic to update e_invoice --- app/Models/BaseModel.php | 2 +- app/Repositories/BaseRepository.php | 11 +++++++++++ app/Services/EDocument/Jobs/SendEDocument.php | 7 ++++--- app/Services/EDocument/Standards/Peppol.php | 7 +++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 5ea237f04a7b..40c7ab9208cc 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -298,7 +298,7 @@ class BaseModel extends Model } // special catch here for einvoicing eventing - if($event_id == Webhook::EVENT_SENT_INVOICE && ($this instanceof Invoice) && is_null($this->backup)){ + if($event_id == Webhook::EVENT_SENT_INVOICE && ($this instanceof Invoice) && is_null($this->backup) && $this->client->getSetting('e_invoice_type') == 'PEPPOL'){ \App\Services\EDocument\Jobs\SendEDocument::dispatch(get_class($this), $this->id, $this->company->db); } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 5d6d9a15d56c..54b43d130ee5 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -320,6 +320,17 @@ class BaseRepository UpdateTaxData::dispatch($client, $client->company); } + /** If Peppol is enabled - we will save the e_invoice document here at this point, document will not update after being sent */ + if(strlen($model->backup ?? '') == 0 && $client->getSetting('e_invoice_type') == 'PEPPOL' && $model->company->legal_entity_id) + { + try{ + $model->service()->getEInvoice(); + } + catch(\Exception $e){ + nlog("EXCEPTION:: BASEREPOSITORY:: Error generating e_invoice for model {$model->id}"); + nlog($e->getMessage()); + } + } } if ($model instanceof Credit) { diff --git a/app/Services/EDocument/Jobs/SendEDocument.php b/app/Services/EDocument/Jobs/SendEDocument.php index a1d6e1d429b2..16c021510edf 100644 --- a/app/Services/EDocument/Jobs/SendEDocument.php +++ b/app/Services/EDocument/Jobs/SendEDocument.php @@ -50,10 +50,11 @@ class SendEDocument implements ShouldQueue MultiDB::setDB($this->db); $model = $this->entity::find($this->id); - $e_invoice_standard = $model->client ? $model->client->getSetting('e_invoice_type') : $model->company->getSetting('e_invoice_type'); - if($e_invoice_standard != 'PEPPOL') - return; + // $e_invoice_standard = $model->client ? $model->client->getSetting('e_invoice_type') : $model->company->getSetting('e_invoice_type'); + + // if($e_invoice_standard != 'PEPPOL') + // return; if(Ninja::isSelfHost() && ($model instanceof Invoice) && $model->company->legal_entity_id) { diff --git a/app/Services/EDocument/Standards/Peppol.php b/app/Services/EDocument/Standards/Peppol.php index fe8e7b058650..d97bc3be0d5f 100644 --- a/app/Services/EDocument/Standards/Peppol.php +++ b/app/Services/EDocument/Standards/Peppol.php @@ -287,8 +287,11 @@ class Peppol extends AbstractService $this->senderSpecificLevelMutators() ->receiverSpecificLevelMutators(); - $this->invoice->e_invoice = $this->toObject(); - $this->invoice->save(); + if(strlen($this->invoice->backup ?? '') == 0) + { + $this->invoice->e_invoice = $this->toObject(); + $this->invoice->save(); + } return $this;