Updates for e_invoice_quota

This commit is contained in:
David Bomba 2024-11-19 11:18:21 +11:00
parent 5284b12022
commit 26b956768f
4 changed files with 59 additions and 27 deletions

View File

@ -328,7 +328,7 @@ class BaseModel extends Model
} }
// special catch here for einvoicing eventing // special catch here for einvoicing eventing
if($event_id == Webhook::EVENT_SENT_INVOICE && ($this instanceof Invoice) && is_null($this->backup) && $this->client->getSetting('e_invoice_type') == 'PEPPOL'){ if($event_id == Webhook::EVENT_SENT_INVOICE && ($this instanceof Invoice) && is_null($this->backup) && $this->client->peppolSendingEnabled()){
\App\Services\EDocument\Jobs\SendEDocument::dispatch(get_class($this), $this->id, $this->company->db); \App\Services\EDocument\Jobs\SendEDocument::dispatch(get_class($this), $this->id, $this->company->db);
} }

View File

@ -1022,4 +1022,17 @@ class Client extends BaseModel implements HasLocalePreference
{ {
return $use_react_url ? config('ninja.react_url'). "/#/clients/{$this->hashed_id}" : config('ninja.app_url'); return $use_react_url ? config('ninja.react_url'). "/#/clients/{$this->hashed_id}" : config('ninja.app_url');
} }
/**
* peppolSendingEnabled
*
* Determines the sending status of the company
*
* @return bool
*/
public function peppolSendingEnabled(): bool
{
return $this->getSetting('e_invoice_type') == 'PEPPOL' && $this->company->peppolSendingEnabled();
}
} }

View File

@ -985,4 +985,16 @@ class Company extends BaseModel
return Ninja::isHosted() && $this->account->isPaid() && $this->account->isEnterpriseClient() && $this->account->e_invoice_quota > 0 && $this->settings->e_invoice_type == 'PEPPOL' && $this->tax_data->acts_as_sender; return Ninja::isHosted() && $this->account->isPaid() && $this->account->isEnterpriseClient() && $this->account->e_invoice_quota > 0 && $this->settings->e_invoice_type == 'PEPPOL' && $this->tax_data->acts_as_sender;
} }
/**
* peppolSendingEnabled
*
* Determines the sending status of the company
*
* @return bool
*/
public function peppolSendingEnabled(): bool
{
return !$this->account->is_flagged && $this->account->e_invoice_quota > 0 && isset($this->legal_entity_id) && isset($this->tax_data->acts_as_sender) && $this->tax_data->acts_as_sender;
}
} }

View File

@ -86,14 +86,10 @@ class SendEDocument implements ShouldQueue
'routing' => $identifiers['routing'], 'routing' => $identifiers['routing'],
'account_key' => $model->company->account->key, 'account_key' => $model->company->account->key,
'e_invoicing_token' => $model->company->account->e_invoicing_token, 'e_invoicing_token' => $model->company->account->e_invoicing_token,
// 'identifiers' => $identifiers,
]; ];
nlog($payload);
nlog(json_encode($payload)); //Self Hosted Sending Code Path
if(Ninja::isSelfHost() && ($model instanceof Invoice) && $model->company->peppolSendingEnabled())
if(Ninja::isSelfHost() && ($model instanceof Invoice) && $model->company->legal_entity_id)
{ {
$r = Http::withHeaders($this->getHeaders()) $r = Http::withHeaders($this->getHeaders())
@ -114,27 +110,35 @@ class SendEDocument implements ShouldQueue
} }
} }
elseif(Ninja::isSelfHost())
return;
//Run this check outside of the next loop as it will never send otherwise.
if ($model->company->account->e_invoice_quota == 0 && $model->company->legal_entity_id) {
$key = "e_invoice_quota_exhausted_{$model->company->account->key}";
if(Ninja::isHosted() && ($model instanceof Invoice) && !$model->company->account->is_flagged && $model->company->legal_entity_id) if (! Cache::has($key)) {
$mo = new EmailObject();
$mo->subject = ctrans('texts.notification_no_credits');
$mo->body = ctrans('texts.notification_no_credits_text');
$mo->text_body = ctrans('texts.notification_no_credits_text');
$mo->company_key = $model->company->company_key;
$mo->html_template = 'email.template.generic';
$mo->to = [new Address($model->company->account->owner()->email, $model->company->account->owner()->name())];
$mo->email_template_body = 'notification_no_credits';
$mo->email_template_subject = 'notification_no_credits_text';
Email::dispatch($mo, $model->company);
Cache::put($key, true, now()->addHours(24));
}
return;
}
//Hosted Sending Code Path.
if(($model instanceof Invoice) && $model->company->peppolSendingEnabled())
{ {
if ($model->company->account->e_invoice_quota === 0) { if ($model->company->account->e_invoice_quota <= config('ninja.e_invoice_quota_warning')) {
$key = "e_invoice_quota_exhausted_{$model->company->account->key}";
if (! Cache::has($key)) {
$mo = new EmailObject();
$mo->subject = ctrans('texts.notification_no_credits');
$mo->body = ctrans('texts.notification_no_credits_text');
$mo->text_body = ctrans('texts.notification_no_credits_text');
$mo->company_key = $model->company->company_key;
$mo->html_template = 'email.template.generic';
$mo->to = [new Address($model->company->account->owner()->email, $model->company->account->owner()->name())];
$mo->email_template_body = 'notification_no_credits';
$mo->email_template_subject = 'notification_no_credits_text';
Email::dispatch($mo, $model->company);
Cache::put($key, true, now()->addHours(24));
}
} else if ($model->company->account->e_invoice_quota <= config('ninja.e_invoice_quota_warning')) {
$key = "e_invoice_quota_low_{$model->company->account->key}"; $key = "e_invoice_quota_low_{$model->company->account->key}";
if (! Cache::has($key)) { if (! Cache::has($key)) {
@ -156,8 +160,11 @@ class SendEDocument implements ShouldQueue
$sc = new \App\Services\EDocument\Gateway\Storecove\Storecove(); $sc = new \App\Services\EDocument\Gateway\Storecove\Storecove();
$r = $sc->sendJsonDocument($payload); $r = $sc->sendJsonDocument($payload);
if(is_string($r)) // Successful send - update quota!
if(is_string($r)){
$model->company->account->decrement('e_invoice_quota', 1);
return $this->writeActivity($model, $r); return $this->writeActivity($model, $r);
}
if($r->failed()) { if($r->failed()) {
nlog("Model {$model->number} failed to be accepted by invoice ninja, error follows:"); nlog("Model {$model->number} failed to be accepted by invoice ninja, error follows:");