From 2bf1afa2c4622abac48e9c155540ad7dc9bc7cfe Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 19 Nov 2024 15:56:15 +1100 Subject: [PATCH] Add tax entity to licenses table --- app/Casts/AsTaxEntityCollection.php | 43 ++++++++++++++ app/DataMapper/EInvoice/TaxEntity.php | 58 +++++++++++++++++++ app/DataMapper/Tax/TaxModel.php | 12 ++-- .../Controllers/EInvoiceTokenController.php | 2 +- app/Jobs/Company/CompanyTaxRate.php | 4 +- app/Jobs/Report/SendToAdmin.php | 8 +-- app/Models/Company.php | 2 +- app/Models/License.php | 5 ++ .../EDocument/Gateway/Storecove/Storecove.php | 14 +---- app/Services/EDocument/Jobs/SendEDocument.php | 10 +++- ...20259_add_entity_set_to_licenses_table.php | 25 ++++++++ 11 files changed, 155 insertions(+), 28 deletions(-) create mode 100644 app/Casts/AsTaxEntityCollection.php create mode 100644 app/DataMapper/EInvoice/TaxEntity.php create mode 100644 database/migrations/2024_11_19_020259_add_entity_set_to_licenses_table.php diff --git a/app/Casts/AsTaxEntityCollection.php b/app/Casts/AsTaxEntityCollection.php new file mode 100644 index 0000000000..8b08359cf9 --- /dev/null +++ b/app/Casts/AsTaxEntityCollection.php @@ -0,0 +1,43 @@ + new TaxEntity($item), $items); + } + + public function set($model, string $key, $value, array $attributes) + { + if (!$value) { + return '[]'; + } + + if ($value instanceof TaxEntity) { + $value = [$value]; + } + + return json_encode(array_map(fn ($entity) => get_object_vars($entity), $value)); + } +} diff --git a/app/DataMapper/EInvoice/TaxEntity.php b/app/DataMapper/EInvoice/TaxEntity.php new file mode 100644 index 0000000000..64c2846bd5 --- /dev/null +++ b/app/DataMapper/EInvoice/TaxEntity.php @@ -0,0 +1,58 @@ + */ + public array $received_documents = []; + + /** + * __construct + * + * @param mixed $entity + */ + public function __construct(mixed $entity = null) + { + if (!$entity) { + $this->init(); + return $this; + } + + $entityArray = is_object($entity) ? get_object_vars($entity) : $entity; + + // $entityArray = get_object_vars($entity); + foreach ($entityArray as $key => $value) { + $this->{$key} = $value; + } + + $this->migrate(); + } + + public function init(): self + { + return $this; + } + + private function migrate(): self + { + return $this; + } +} diff --git a/app/DataMapper/Tax/TaxModel.php b/app/DataMapper/Tax/TaxModel.php index 51716f82d9..f828cc1691 100644 --- a/app/DataMapper/Tax/TaxModel.php +++ b/app/DataMapper/Tax/TaxModel.php @@ -22,11 +22,11 @@ class TaxModel /** @var object $regions */ public object $regions; - /** @var bool $act_as_sender */ - public bool $act_as_sender = false; + /** @var bool $acts_as_sender */ + public bool $acts_as_sender = false; - /** @var bool $act_as_receiver */ - public bool $act_as_receiver = false; + /** @var bool $acts_as_receiver */ + public bool $acts_as_receiver = false; /** * __construct @@ -42,8 +42,8 @@ class TaxModel } else { $this->seller_subregion = $model->seller_subregion ?? ''; - $this->act_as_sender = $model->act_as_sender ?? false; - $this->act_as_receiver = $model->act_as_receiver ?? false; + $this->acts_as_sender = $model->acts_as_sender ?? false; + $this->acts_as_receiver = $model->acts_as_receiver ?? false; $modelArray = get_object_vars($model); foreach ($modelArray as $key => $value) { diff --git a/app/Http/Controllers/EInvoiceTokenController.php b/app/Http/Controllers/EInvoiceTokenController.php index 915be7d39f..eafb6175b7 100644 --- a/app/Http/Controllers/EInvoiceTokenController.php +++ b/app/Http/Controllers/EInvoiceTokenController.php @@ -17,7 +17,7 @@ use App\Http\Requests\EInvoice\UpdateTokenRequest; use Illuminate\Http\Response; use Illuminate\Support\Facades\Http; -class EInvoiceTokenController extends BaseController +class EInvoiceTokenController extends BaseController //@ben - can we remove this? { public function __invoke(UpdateTokenRequest $request): Response { diff --git a/app/Jobs/Company/CompanyTaxRate.php b/app/Jobs/Company/CompanyTaxRate.php index 684065952a..d0b7f0864b 100644 --- a/app/Jobs/Company/CompanyTaxRate.php +++ b/app/Jobs/Company/CompanyTaxRate.php @@ -65,8 +65,8 @@ class CompanyTaxRate implements ShouldQueue nlog("could not calculate state from postal code => {$this->company->settings->postal_code} or from state {$this->company->settings->state}"); } - if(!$calculated_state && $this->company->tax_data?->seller_subregion) { - $calculated_state = $this->company->tax_data?->seller_subregion; + if(!$calculated_state && $this->company->tax_data?->seller_subregion) { //@phpstan-ignore-line + $calculated_state = $this->company->tax_data->seller_subregion; } if(!$calculated_state) { diff --git a/app/Jobs/Report/SendToAdmin.php b/app/Jobs/Report/SendToAdmin.php index 0d78cae177..233de2ff0f 100644 --- a/app/Jobs/Report/SendToAdmin.php +++ b/app/Jobs/Report/SendToAdmin.php @@ -61,10 +61,10 @@ class SendToAdmin implements ShouldQueue $files = []; $files[] = ['file' => $csv, 'file_name' => "{$this->file_name}", 'mime' => 'text/csv']; - if(in_array(get_class($export), [ARDetailReport::class, ARSummaryReport::class])) { - $pdf = base64_encode($export->getPdf()); - $files[] = ['file' => $pdf, 'file_name' => str_replace(".csv", ".pdf", $this->file_name), 'mime' => 'application/pdf']; - } + // if(in_array(get_class($export), [ARDetailReport::class, ARSummaryReport::class])) { + // $pdf = base64_encode($export->getPdf()); + // $files[] = ['file' => $pdf, 'file_name' => str_replace(".csv", ".pdf", $this->file_name), 'mime' => 'application/pdf']; + // } $user = $this->company->owner(); diff --git a/app/Models/Company.php b/app/Models/Company.php index f66ef201c8..567c94f2c7 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -228,7 +228,7 @@ use Laracasts\Presenter\PresentableTrait; * @method static \Illuminate\Database\Eloquent\Builder|Company find($query) * @property-read int|null $webhooks_count * @property int $calculate_taxes - * @property mixed $tax_data + * @property \App\DataMapper\Tax\TaxModel $tax_data * @method \App\Models\User|null owner() * @mixin \Eloquent */ diff --git a/app/Models/License.php b/app/Models/License.php index 6c4820662c..8af927da50 100644 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -11,7 +11,9 @@ namespace App\Models; +use App\Casts\AsTaxEntityCollection; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Casts\AsArrayObject; /** * App\Models\License @@ -30,6 +32,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property int|null $recurring_invoice_id * @property int|null $e_invoice_quota * @property bool $is_flagged + * @property object|null $entities * @property-read \App\Models\RecurringInvoice $recurring_invoice * @method static \Illuminate\Database\Eloquent\Builder|StaticModel company() * @method static \Illuminate\Database\Eloquent\Builder|StaticModel exclude($columns) @@ -59,6 +62,7 @@ class License extends StaticModel protected $casts = [ 'created_at' => 'date', + 'entities' => AsTaxEntityCollection::class, ]; public function expiry(): string @@ -80,4 +84,5 @@ class License extends StaticModel { return $this->hasMany(EInvoicingToken::class, 'license_key', 'license_key'); } + } diff --git a/app/Services/EDocument/Gateway/Storecove/Storecove.php b/app/Services/EDocument/Gateway/Storecove/Storecove.php index c376e24f82..03703888b8 100644 --- a/app/Services/EDocument/Gateway/Storecove/Storecove.php +++ b/app/Services/EDocument/Gateway/Storecove/Storecove.php @@ -232,21 +232,9 @@ class Storecove /** * Get Sending Evidence - * * * "guid" => "661c079d-0c2b-4b45-8263-678ed81224af", - "sender" => "9930:DE923356489", - "receiver" => "9930:DE321281763", - "documents" => [ - [ - "mime_type" => "application/xml", - "document" => "html URL to fileg", - "expires_at" => "2024-11-17 21:46:47+00:00", - ], - ], - "evidence" => [ - "receiving_accesspoint" => "CN=PNL000151, OU=PEPPOL TEST AP, O=Storecove (Datajust B.V.), C=NL", - + * * @param string $guid * @return mixed */ diff --git a/app/Services/EDocument/Jobs/SendEDocument.php b/app/Services/EDocument/Jobs/SendEDocument.php index 28cb096cea..cb630e620b 100644 --- a/app/Services/EDocument/Jobs/SendEDocument.php +++ b/app/Services/EDocument/Jobs/SendEDocument.php @@ -162,7 +162,15 @@ class SendEDocument implements ShouldQueue // Successful send - update quota! if(is_string($r)){ - $model->company->account->decrement('e_invoice_quota', 1); + + $account = $model->company->account; + $account->decrement('e_invoice_quota', 1); + $account->refresh(); + + if($account->e_invoice_quota == 0 && class_exists(\Modules\Admin\Jobs\Account\SuspendESendReceive::class)){ + \Modules\Admin\Jobs\Account\SuspendESendReceive::dispatch($account->key); + } + return $this->writeActivity($model, $r); } diff --git a/database/migrations/2024_11_19_020259_add_entity_set_to_licenses_table.php b/database/migrations/2024_11_19_020259_add_entity_set_to_licenses_table.php new file mode 100644 index 0000000000..e624acef7a --- /dev/null +++ b/database/migrations/2024_11_19_020259_add_entity_set_to_licenses_table.php @@ -0,0 +1,25 @@ +text('entities')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + } +}; \ No newline at end of file