mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Download historical invoice
This commit is contained in:
parent
cb508e1daa
commit
928a9d46af
@ -11,12 +11,16 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\Activity\DownloadHistoricalInvoiceRequest;
|
||||
use App\Models\Activity;
|
||||
use App\Transformers\ActivityTransformer;
|
||||
use App\Utils\Traits\Pdf\PdfMaker;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ActivityController extends BaseController
|
||||
{
|
||||
use PdfMaker;
|
||||
|
||||
protected $entity_type = Activity::class;
|
||||
|
||||
protected $entity_transformer = ActivityTransformer::class;
|
||||
@ -78,7 +82,26 @@ class ActivityController extends BaseController
|
||||
$activities = Activity::orderBy('created_at', 'DESC')->company()
|
||||
->take($default_activities);
|
||||
|
||||
|
||||
return $this->listResponse($activities);
|
||||
}
|
||||
|
||||
public function downloadHistoricalInvoice(DownloadHistoricalInvoiceRequest $request, Activity $activity)
|
||||
{
|
||||
|
||||
$pdf = $this->makePdf(null, null, $activity->backup->html_backup);
|
||||
|
||||
if(isset($activity->invoice_id))
|
||||
$filename = $activity->invoice->number . ".pdf";
|
||||
elseif(isset($activity->quote_id))
|
||||
$filename = $activity->quote->number . ".pdf";
|
||||
elseif(isset($activity->credit_id))
|
||||
$filename = $activity->credit->number . ".pdf";
|
||||
else
|
||||
$filename = "backup.pdf";
|
||||
|
||||
return response()->streamDownload(function () use($pdf) {
|
||||
echo $pdf;
|
||||
}, $filename);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Http\Requests\Activity;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Activity;
|
||||
|
||||
class DownloadHistoricalInvoiceRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('view', $this->activity);
|
||||
}
|
||||
}
|
@ -11,10 +11,14 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Activity extends StaticModel
|
||||
{
|
||||
|
||||
use MakesHash;
|
||||
|
||||
const CREATE_CLIENT=1; //
|
||||
const ARCHIVE_CLIENT=2; //
|
||||
const DELETE_CLIENT=3; //
|
||||
@ -144,4 +148,16 @@ class Activity extends StaticModel
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
|
||||
public function resolveRouteBinding($value)
|
||||
{
|
||||
if (is_numeric($value)) {
|
||||
throw new ModelNotFoundException("Record with value {$value} not found");
|
||||
}
|
||||
|
||||
return $this
|
||||
//->withTrashed()
|
||||
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,9 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
|
||||
Route::get('ping', 'PingController@index')->name('ping');
|
||||
Route::get('health_check', 'PingController@health')->name('health_check');
|
||||
|
||||
Route::resource('activities', 'ActivityController');// name = (clients. index / create / show / update / destroy / edit
|
||||
Route::get('activities', 'ActivityController@index');
|
||||
|
||||
Route::get('activities/download_invoice/{activity}', 'ActivityController@downloadHistoricalInvoice');
|
||||
|
||||
Route::resource('clients', 'ClientController');// name = (clients. index / create / show / update / destroy / edit
|
||||
|
||||
|
71
tests/Integration/DownloadHistoricalInvoiceTest.php
Normal file
71
tests/Integration/DownloadHistoricalInvoiceTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Integration;
|
||||
|
||||
use App\Events\Invoice\InvoiceWasUpdated;
|
||||
use App\Models\Activity;
|
||||
use App\Repositories\ActivityRepository;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @covers App\Http\Controllers\ActivityController
|
||||
*/
|
||||
class DownloadHistoricalInvoiceTest extends TestCase
|
||||
{
|
||||
use MockAccountData;
|
||||
use DatabaseTransactions;
|
||||
use MakesHash;
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->makeTestData();
|
||||
}
|
||||
|
||||
private function mockActivity()
|
||||
{
|
||||
$activity_repo = new ActivityRepository();
|
||||
|
||||
$obj = new \stdClass;
|
||||
$obj->invoice_id = $this->invoice->id;
|
||||
$obj->user_id = $this->invoice->user_id;
|
||||
$obj->company_id = $this->company->id;
|
||||
|
||||
$activity_repo->save($obj, $this->invoice, Ninja::eventVars());
|
||||
|
||||
}
|
||||
public function testActivityAccessible()
|
||||
{
|
||||
$this->mockActivity();
|
||||
|
||||
$this->assertNotNull($this->invoice->activities);
|
||||
}
|
||||
|
||||
public function testBackupExists()
|
||||
{
|
||||
$this->mockActivity();
|
||||
|
||||
$this->assertNotNull($this->invoice->activities->first()->backup->html_backup);
|
||||
}
|
||||
|
||||
|
||||
public function testBackupDownload()
|
||||
{
|
||||
$this->mockActivity();
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token
|
||||
])->get('/api/v1/activities/download_invoice/'.$this->encodePrimaryKey($this->invoice->activities->first()->id));
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user