Add EInvoiceLog Table

This commit is contained in:
David Bomba 2024-11-21 14:21:54 +11:00
parent 4f52c56bf9
commit d4f3805285
4 changed files with 137 additions and 8 deletions

View File

@ -21,7 +21,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
*
* @package App\Models
* @property int $id
* @property string $tenant_id
* @property string $tenant_id (sent|received)
* @property string $direction
* @property int $legal_entity_id
* @property string|null $license_key The license key string
@ -36,7 +36,15 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
*/
class EInvoicingLog extends Model
{
use SoftDeletes;
protected $fillable = [
'tenant_id',
'direction',
'legal_entity_id',
'license_key',
'notes',
'counter',
];
protected $casts = [

View File

@ -17,6 +17,7 @@ use App\Utils\Ninja;
use App\Models\Invoice;
use App\Libraries\MultiDB;
use App\Models\Activity;
use App\Models\EInvoicingLog;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
@ -165,6 +166,14 @@ class SendEDocument implements ShouldQueue
$account->decrement('e_invoice_quota', 1);
$account->refresh();
EInvoicingLog::create([
'tenant_id' => $model->company->company_key,
'direction' => 'sent',
'legal_entity_id' => $model->company->legal_entity_id,
'notes' => $r,
'counter' => -1,
]);
if ($account->e_invoice_quota == 0 && class_exists(\Modules\Admin\Jobs\Account\SuspendESendReceive::class)) {
\Modules\Admin\Jobs\Account\SuspendESendReceive::dispatch($account->key);
}

View File

@ -13,7 +13,7 @@ return new class extends Migration
{
Schema::create('e_invoicing_logs', function (Blueprint $table){
$table->id();
$table->string('tenant_id');
$table->string('tenant_id')->nullable();
$table->unsignedInteger('legal_entity_id')->index();
$table->string('license_key')->nullable();
$table->string('direction')->default('sent');

View File

@ -11,13 +11,16 @@
namespace Tests\Unit;
use App\Jobs\EDocument\CreateEDocument;
use App\Jobs\Entity\CreateRawPdf;
use horstoeko\zugferd\ZugferdDocumentReader;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Tests\MockAccountData;
use Tests\TestCase;
use App\Models\Account;
use App\Models\Company;
use Tests\MockAccountData;
use App\Models\EInvoicingLog;
use App\Jobs\Entity\CreateRawPdf;
use App\Jobs\EDocument\CreateEDocument;
use horstoeko\zugferd\ZugferdDocumentReader;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
*
@ -38,6 +41,46 @@ class EInvoiceTest extends TestCase
$this->makeTestData();
}
public function testECreditExpiryLogic()
{
$a = Account::factory()->create([
'e_invoice_quota'=>100,
]);
$company = Company::factory()->create([
'account_id' => $a->id,
'legal_entity_id' => 1,
]);
$log = new EInvoicingLog();
$log->counter = 100;
$log->tenant_id = $company->company_key;
$log->legal_entity_id = 1;
$log->save();
for($x=0; $x<50; $x++){
$log = new EInvoicingLog();
$log->counter = -1;
$log->direction = (bool)rand(0, 1) ? 'sent' : 'received';
$log->tenant_id = $company->company_key;
$log->legal_entity_id = 1;
$log->save();
}
$this->travelTo(now()->addYears(2));
$data = $this->getExpiredPurchases([$company->company_key], true);
$this->assertEquals(100, $data['purchased']);
$this->assertEquals(-50, $data['sent'] + $data['received']);
$this->assertEquals(50, $data['purchased'] - abs($data['sent']) - abs($data['received']));
}
public function testEInvoiceGenerates()
{
$this->company->e_invoice_type = "EN16931";
@ -72,4 +115,73 @@ class EInvoiceTest extends TestCase
$document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest);
$this->assertEquals($this->invoice->number, $documentno);
}
private function getExpiredPurchases(array $identifier, bool $is_hosted = true): array
{
$stub = [
'purchased' => 0,
'sent' => 0,
'received' => 0,
'period' => now()->subYear()->format('Y-m-d')."|".now()->format('Y-m-d'),
];
$record_query = EInvoicingLog::where('created_at', '<', now()->subYear())
->where('counter', '>', 0)
->when($is_hosted, function ($q) use ($identifier){
$q->whereIn('tenant_id', $identifier);
})
->when(!$is_hosted, function ($q) use ($identifier) {
$q->where('license_key', $identifier[0]);
});
$log = $record_query->first();
$stub['purchased'] = $record_query->sum('counter');
if($stub['purchased'] == 0)
return $stub;
$stub['sent'] = EInvoicingLog::where('created_at', '<', now()->subYear())
->where('counter', '<', 0)
->where('direction', 'sent')
->when($is_hosted, function ($q) use ($identifier) {
$q->where('tenant_id', $identifier);
})
->when(!$is_hosted, function ($q) use ($identifier) {
$q->where('license_key', $identifier);
})
->sum('counter');
$stub['received'] = EInvoicingLog::where('created_at', '<', now()->subYear())
->where('counter', '<', 0)
->where('direction', 'received')
->when($is_hosted, function ($q) use ($identifier) {
$q->where('tenant_id', $identifier);
})
->when(!$is_hosted, function ($q) use ($identifier) {
$q->where('license_key', $identifier);
})
->sum('counter');
$log->notes = "{$stub['purchased']} purchased, {$stub['sent']} sent, {$stub['received']} received, {$stub['period']} period";
nlog($log->tenant_id ?? $log->license_key. " : " .$log->notes);
$log->save();
EInvoicingLog::where('created_at', '<', now()->subYear())
->when($is_hosted, function ($q) use ($identifier) {
$q->where('tenant_id', $identifier);
})
->when(!$is_hosted, function ($q) use ($identifier) {
$q->where('license_key', $identifier);
})
->delete();
return $stub;
}
}