get invoice from url

This commit is contained in:
Peter Papp
2021-04-27 08:57:00 +02:00
parent 5be490c8d5
commit f7135c9b27
8 changed files with 175 additions and 43 deletions
@@ -5,7 +5,6 @@ namespace App\Console\Commands;
use App\Models\File;
use App\Models\Folder;
use App\Models\Share;
use App\Services\Oasis\OasisDevService;
use App\Services\SetupService;
use App\Models\Setting;
use App\Models\User;
@@ -74,9 +73,6 @@ class SetupDevEnvironment extends Command
$this->info('Clearing application cache...');
$this->clear_cache();
// Oasis demo content generator
resolve(OasisDevService::class)->create_demo_content();
$this->info('Dispatching jobs...');
$this->call('queue:work', [
'--stop-when-empty' => true,
@@ -0,0 +1,112 @@
<?php
namespace App\Console\Commands;
use App\Models\Oasis\Client;
use App\Models\Oasis\Invoice;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Factories\Sequence;
class SetupOasisEnvironment extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'setup:oasis';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup Oasis demo content';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->info('Setting up Oasis environment');
$this->create_demo_content();
$this->info('Dispatching jobs...');
$this->call('queue:work', [
'--stop-when-empty' => true,
]);
$this->info('Everything is done, congratulations! 🥳🥳🥳');
}
public function create_demo_content()
{
$user = User::whereEmail('howdy@hi5ve.digital')
->first();
$clients = Client::factory(Client::class)
->count(6)
->create(['user_id' => $user->id]);
$regular_invoices = Invoice::factory(Invoice::class)
->state(new Sequence(
['client_id' => $clients[0]->id],
['client_id' => $clients[1]->id],
['client_id' => $clients[2]->id],
['client_id' => $clients[3]->id],
['client_id' => $clients[4]->id],
['client_id' => $clients[5]->id],
))->count(2)
->create([
'user_id' => $user->id,
'invoice_type' => 'regular-invoice'
]);
$advance_invoices = Invoice::factory(Invoice::class)
->count(2)
->state(new Sequence(
['client_id' => $clients[0]->id],
['client_id' => $clients[1]->id],
['client_id' => $clients[2]->id],
['client_id' => $clients[3]->id],
['client_id' => $clients[4]->id],
['client_id' => $clients[5]->id],
))->create([
'user_id' => $user->id,
'invoice_type' => 'advance-invoice',
'discount_type' => null,
]);
// Generate PDF
collect([$regular_invoices, $advance_invoices])
->collapse()
->each(function ($invoice) use ($user) {
$this->info("Generating invoice id: $invoice->id");
\PDF::loadView('oasis.invoices.invoice', [
'invoice' => Invoice::find($invoice->id),
'user' => $user,
])
->setPaper('a4')
->setOrientation('portrait')
->save(
storage_path("app/files/{$invoice->user_id}/invoice-{$invoice->id}.pdf")
);
});
}
}
+2
View File
@@ -3,6 +3,7 @@
namespace App\Console;
use App\Console\Commands\SetupDevEnvironment;
use App\Console\Commands\SetupOasisEnvironment;
use App\Services\Oasis\OasisService;
use App\Console\Commands\SetupProdEnvironment;
use App\Services\SchedulerService;
@@ -19,6 +20,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
SetupDevEnvironment::class,
SetupProdEnvironment::class,
SetupOasisEnvironment::class,
];
/**
@@ -16,6 +16,7 @@ use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Storage;
class InvoiceController extends Controller
{
@@ -39,6 +40,19 @@ class InvoiceController extends Controller
);
}
/**
* @param Invoice $invoice
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function get_invoice(Invoice $invoice)
{
if (! Storage::exists(invoice_path($invoice))) {
abort(404, 'Not Found');
}
return Storage::download(invoice_path($invoice), "invoice-{$invoice->id}.pdf");
}
/**
* @return mixed
*/
@@ -3,6 +3,8 @@
namespace App\Http\Resources\Oasis;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class OasisInvoiceResource extends JsonResource
{
@@ -19,7 +21,7 @@ class OasisInvoiceResource extends JsonResource
'name' => $this->client['name'] . ' ' . format_to_currency($this->total_net, $this->currency),
'invoiceNumber' => $this->invoice_number,
'total' => format_to_currency($this->total_net, $this->currency),
'file_url' => '',
'file_url' => "/oasis/invoice/$this->id",
'clientName' => $this->client['name'],
'mimetype' => 'pdf',
'type' => 'invoice',
-38
View File
@@ -14,44 +14,6 @@ use Illuminate\Database\Eloquent\Factories\Sequence;
class OasisDevService
{
public function create_demo_content()
{
$user = User::whereEmail('howdy@hi5ve.digital')
->first();
$clients = Client::factory(Client::class)
->count(6)
->create(['user_id' => $user->id]);
Invoice::factory(Invoice::class)
->state(new Sequence(
['client_id' => $clients[0]->id],
['client_id' => $clients[1]->id],
['client_id' => $clients[2]->id],
['client_id' => $clients[3]->id],
['client_id' => $clients[4]->id],
['client_id' => $clients[5]->id],
))->count(14)
->create([
'user_id' => $user->id,
'invoice_type' => 'regular-invoice'
]);
Invoice::factory(Invoice::class)
->count(14)
->state(new Sequence(
['client_id' => $clients[0]->id],
['client_id' => $clients[1]->id],
['client_id' => $clients[2]->id],
['client_id' => $clients[3]->id],
['client_id' => $clients[4]->id],
['client_id' => $clients[5]->id],
))->create([
'user_id' => $user->id,
'invoice_type' => 'advance-invoice'
]);
}
/**
* @return Application|Factory|View
*/
+5
View File
@@ -43,6 +43,11 @@ Route::group(['middleware' => 'api', 'prefix' => '/api/oasis'], function () {
// Web routes
Route::group(['middleware' => 'web', 'prefix' => 'oasis'], function () {
Route::post('/subscribe/{order}/set-password', [SubscriptionController::class, 'set_password']);
// Admin
Route::group(['middleware' => 'auth:sanctum'], function () {
Route::get('/invoice/{invoice}', [InvoiceController::class, 'get_invoice']);
});
});
// Debug routes
+39
View File
@@ -278,6 +278,7 @@ class OasisInvoiceTest extends TestCase
*/
public function user_create_new_invoice_with_storing_new_client_without_avatar_and_mail()
{
Storage::fake('local');
Notification::fake();
PDF::fake();
@@ -332,6 +333,7 @@ class OasisInvoiceTest extends TestCase
*/
public function user_create_new_invoice_without_storing_client_without_avatar_and_mail()
{
Storage::fake('local');
Notification::fake();
PDF::fake();
@@ -387,6 +389,7 @@ class OasisInvoiceTest extends TestCase
*/
public function user_create_new_invoice_without_storing_client()
{
Storage::fake('local');
Notification::fake();
PDF::fake();
@@ -441,6 +444,7 @@ class OasisInvoiceTest extends TestCase
*/
public function user_create_new_invoice_with_existing_client()
{
Storage::fake('local');
Notification::fake();
PDF::fake();
@@ -480,6 +484,41 @@ class OasisInvoiceTest extends TestCase
Notification::assertTimesSent(1, InvoiceDeliveryNotification::class);
}
/**
* @test
*/
public function it_get_invoice_from_url()
{
$user = User::factory(User::class)
->create(['role' => 'user']);
Sanctum::actingAs($user);
$client = Client::factory(Client::class)
->create([
'user_id' => $user->id,
'name' => 'VueFileManager Inc.',
'email' => 'howdy@hi5ve.digital',
]);
$this->postJson('/api/oasis/invoices', [
'invoice_type' => 'regular-invoice',
'invoice_number' => '2120001',
'variable_number' => '2120001',
'client' => $client->id,
'items' => $this->items,
'discount_type' => 'percent',
'discount_rate' => 10,
'delivery_at' => Carbon::now()->addWeek(),
'send_invoice' => true,
])->assertStatus(201);
$invoice = Invoice::first();
$this->get("/oasis/invoice/{$invoice->id}")
->assertStatus(200);
}
/**
* @test
*/