diff --git a/app/Console/Commands/ArtisanUpgrade.php b/app/Console/Commands/ArtisanUpgrade.php index 25494d64032c..ec83adcb0a3e 100644 --- a/app/Console/Commands/ArtisanUpgrade.php +++ b/app/Console/Commands/ArtisanUpgrade.php @@ -59,7 +59,5 @@ class ArtisanUpgrade extends Command \Log::error("I wasn't able to optimize."); } - - \Log::error("finished upgrade post"); } } diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 9b38e73b88a7..3165d798fd3b 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -652,16 +652,59 @@ class InvoiceController extends BaseController { } } + /** + * + * @OA\Get( + * path="/api/v1/invoice/{invitation_key}/download", + * operationId="downloadInvoice", + * tags={"invoices"}, + * summary="Download a specific invoice by invitation key", + * description="Downloads a specific invoice", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter(ref="#/components/parameters/include"), + * @OA\Parameter( + * name="invitation_key", + * in="path", + * description="The Invoice Invitation Key", + * example="D2J234DFA", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\Response( + * response=200, + * description="Returns the invoice pdf", + * @OA\Header(header="X-API-Version", ref="#/components/headers/X-API-Version"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + * + */ public function downloadPdf($invitation_key) { $invitation = $this->invoice_repo->getInvitationByKey($invitation_key); $contact = $invitation->contact; $invoice = $invitation->invoice; - $file_path = $invoice->service()->getInvoicePdf(); //CreateInvoicePdf::dispatchNow($invoice, $invoice->company, $contact); + $file_path = $invoice->service()->getInvoicePdf($contact); return response()->download($file_path); - //return response()->json($invitation_key); } } diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index 04e165c5f46d..e3f8c9d5789a 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -152,7 +152,7 @@ class MigrationController extends BaseController * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), - * @OA\Parameter(ref="#/components/parameters/X-Api-Password"), // Needs verification. + * @OA\Parameter(ref="#/components/parameters/X-Api-Password"), * @OA\Parameter( * name="migration", * in="path", @@ -160,7 +160,7 @@ class MigrationController extends BaseController * example="migration.zip", * required=true, * @OA\Schema( - * type="file", + * type="object", * format="file", * ), * ), diff --git a/app/Http/Controllers/OpenAPI/Parameters.php b/app/Http/Controllers/OpenAPI/Parameters.php index 78712e4aa26b..783cd1f26742 100644 --- a/app/Http/Controllers/OpenAPI/Parameters.php +++ b/app/Http/Controllers/OpenAPI/Parameters.php @@ -33,6 +33,16 @@ * example="HcRvs0oCvYbY5g3RzgBZrSBOChCiq8u4AL0ieuFN5gn4wUV14t0clVhfPc5OX99q" * ) * ), + * @OA\Parameter( + * name="X-Api-Password", + * in="header", + * description="The login password when challenged", + * required=true, + * @OA\Schema( + * type="string", + * example="supersecretpassword" + * ) + * ), * * @OA\Parameter( * name="include", diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 8a7c6d611454..57c4281f70a9 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -43,14 +43,14 @@ class StoreClientRequest extends Request $rules['settings'] = new ValidClientGroupSettingsRule(); $rules['contacts.*.email'] = 'nullable|distinct'; - $contacts = request('contacts'); +// $contacts = request('contacts'); - if (is_array($contacts)) { - for ($i = 0; $i < count($contacts); $i++) { + // if (is_array($contacts)) { + // for ($i = 0; $i < count($contacts); $i++) { - //$rules['contacts.' . $i . '.email'] = 'nullable|email|distinct'; - } - } + // //$rules['contacts.' . $i . '.email'] = 'nullable|email|distinct'; + // } + // } return $rules; } @@ -68,6 +68,17 @@ class StoreClientRequest extends Request $input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']); } + if(isset($input['contacts'])) + { + foreach($input['contacts'] as $contact) + { + if(is_numeric($contact['id'])) + unset($contact['id']); + elseif(is_string($contact['id'])) + $contact['id'] = $this->decodePrimaryKey($contact['id']); + } + } + $this->replace($input); } diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index 5bc9afe6d156..b5035d1ced2d 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -11,9 +11,11 @@ namespace App\Http\Requests\Client; +use App\DataMapper\ClientSettings; use App\Http\Requests\Request; use App\Http\ValidationRules\IsDeletedRule; use App\Http\ValidationRules\ValidClientGroupSettingsRule; +use App\Http\ValidationRules\ValidSettingsRule; use App\Utils\Traits\ChecksEntityStatus; use App\Utils\Traits\MakesHash; use Illuminate\Support\Facades\Log; @@ -48,14 +50,14 @@ class UpdateClientRequest extends Request $rules['settings'] = new ValidClientGroupSettingsRule(); $rules['contacts.*.email'] = 'nullable|distinct'; - $contacts = request('contacts'); + // $contacts = request('contacts'); - if (is_array($contacts)) { - // for ($i = 0; $i < count($contacts); $i++) { - // // $rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,' . isset($contacts[$i]['id'].',company_id,'.$this->company_id); - // //$rules['contacts.' . $i . '.email'] = 'nullable|email'; - // } - } + // if (is_array($contacts)) { + // // for ($i = 0; $i < count($contacts); $i++) { + // // // $rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,' . isset($contacts[$i]['id'].',company_id,'.$this->company_id); + // // //$rules['contacts.' . $i . '.email'] = 'nullable|email'; + // // } + // } return $rules; } @@ -73,11 +75,21 @@ class UpdateClientRequest extends Request { $input = $this->all(); - if (isset($input['group_settings_id'])) { $input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']); } + if(isset($input['contacts'])) + { + foreach($input['contacts'] as $contact) + { + if(is_numeric($contact['id'])) + unset($contact['id']); + elseif(is_string($contact['id'])) + $contact['id'] = $this->decodePrimaryKey($contact['id']); + } + } + $this->replace($input); } } diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index a153988424e9..3bba60fcaf8a 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -32,6 +32,7 @@ use Illuminate\Support\Facades\Storage; use Spatie\Browsershot\Browsershot; class CreateInvoicePdf implements ShouldQueue { + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml; public $invoice; @@ -41,12 +42,13 @@ class CreateInvoicePdf implements ShouldQueue { public $contact; private $disk; + /** * Create a new job instance. * * @return void */ - public function __construct(Invoice $invoice, Company $company, ClientContact $contact, $disk = 'public') + public function __construct(Invoice $invoice, Company $company, ClientContact $contact = null, $disk = 'public') { $this->invoice = $invoice; @@ -63,11 +65,16 @@ class CreateInvoicePdf implements ShouldQueue { MultiDB::setDB($this->company->db); + $this->invoice->load('client'); + + if(!$this->contact) + $this->contact = $this->invoice->client->primary_contact()->first(); + App::setLocale($this->contact->preferredLocale()); - $this->invoice->load('client'); $path = $this->invoice->client->client_hash . '/invoices/'; - $file_path = $path . $this->invoice->number . '.pdf'; + + $file_path = $path . $this->invoice->number . '-' . $this->contact->contact_key .'.pdf'; $modern = new Modern(); $designer = new Designer($modern, $this->invoice->client->getSetting('invoice_variables')); @@ -83,13 +90,10 @@ class CreateInvoicePdf implements ShouldQueue { $pdf = $this->makePdf(null, null, $html); $instance = Storage::disk($this->disk)->put($file_path, $pdf); - - \Log::error($instance); - //$instance = Storage::disk($this->disk)->putFileAs($path, $pdf, $this->invoice->number . '.pdf'); - //$instance = Storage::putFileAs($path, $pdf, $this->invoice->number . '.pdf'); + //$instance= Storage::disk($this->disk)->path($file_path); - return $instance; + return $file_path; } /** diff --git a/app/Repositories/ClientContactRepository.php b/app/Repositories/ClientContactRepository.php index 6fdfde58bb2e..997f66dad7bb 100644 --- a/app/Repositories/ClientContactRepository.php +++ b/app/Repositories/ClientContactRepository.php @@ -28,6 +28,8 @@ class ClientContactRepository extends BaseRepository /* Get array of IDs which have been removed from the contacts array and soft delete each contact */ collect($client->contacts->pluck('id'))->diff($contacts->pluck('id'))->each(function ($contact) { + //ClientContact::find($contact)->delete(); + ClientContact::destroy($contact); }); @@ -44,7 +46,7 @@ class ClientContactRepository extends BaseRepository $update_contact = null; if (isset($contact['id'])) { - $update_contact = ClientContact::find($this->decodePrimaryKey($contact['id'])); + $update_contact = ClientContact::find($contact['id']); } if (!$update_contact) { diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index 3bd999940f5f..ca5401159c2e 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -57,7 +57,7 @@ class ClientRepository extends BaseRepository */ public function save(array $data, Client $client) : ?Client { - +\Log::error($data); $client->fill($data); $client->save(); @@ -68,6 +68,8 @@ class ClientRepository extends BaseRepository $client->save(); +// \Log::error($client); + if (isset($data['contacts'])) { $contacts = $this->contact_repo->save($data['contacts'], $client); } diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index abc17839e941..da038dc3819e 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -17,24 +17,20 @@ use Illuminate\Support\Facades\Storage; class GetInvoicePdf { - public function __construct() - { - } - public function __invoke($invoice, $contact = null) { + if(!$contact) + $contact = $invoice->client->primary_contact()->first(); + $path = $invoice->client->client_hash . '/invoices/'; - $file_path = $path . $invoice->number . '.pdf'; + $file_path = $path . $invoice->number . '-' . $contact->contact_key . '.pdf'; $disk = config('filesystems.default'); $file = Storage::disk($disk)->exists($file_path); - if(!$contact) - $contact = $invoice->client->primary_contact()->first(); - if(!$file) { diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 3c7cc35e79dd..13cca6b999cf 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -113,11 +113,11 @@ class InvoiceService return $this; } - public function getInvoicePdf() + public function getInvoicePdf($contact) { $get_invoice_pdf = new GetInvoicePdf(); - return $get_invoice_pdf($this->invoice); + return $get_invoice_pdf($this->invoice, $contact); } diff --git a/tests/Integration/InvoiceUploadTest.php b/tests/Integration/InvoiceUploadTest.php index 58fe24fd8bac..ec292d5b3300 100644 --- a/tests/Integration/InvoiceUploadTest.php +++ b/tests/Integration/InvoiceUploadTest.php @@ -31,7 +31,7 @@ class InvoiceUploadTest extends TestCase CreateInvoicePdf::dispatchNow($this->invoice, $this->invoice->company, $this->invoice->client->primary_contact()->first()); - $this->assertNotNull($this->invoice->service()->getInvoicePdf()); + $this->assertNotNull($this->invoice->service()->getInvoicePdf($this->invoice->client->primary_contact()->first())); }