1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/PurchaseOrderController.php

921 lines
36 KiB
PHP
Raw Normal View History

2022-05-29 04:29:06 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-05-29 04:29:06 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2023-11-18 02:46:03 +01:00
use App\Events\PurchaseOrder\PurchaseOrderWasCreated;
use App\Events\PurchaseOrder\PurchaseOrderWasUpdated;
2023-11-26 08:41:42 +01:00
use App\Factory\PurchaseOrderFactory;
use App\Filters\PurchaseOrderFilters;
use App\Http\Requests\PurchaseOrder\ActionPurchaseOrderRequest;
2023-02-09 03:30:39 +01:00
use App\Http\Requests\PurchaseOrder\BulkPurchaseOrderRequest;
2023-11-26 08:41:42 +01:00
use App\Http\Requests\PurchaseOrder\CreatePurchaseOrderRequest;
use App\Http\Requests\PurchaseOrder\DestroyPurchaseOrderRequest;
2022-05-29 04:29:06 +02:00
use App\Http\Requests\PurchaseOrder\EditPurchaseOrderRequest;
use App\Http\Requests\PurchaseOrder\ShowPurchaseOrderRequest;
use App\Http\Requests\PurchaseOrder\StorePurchaseOrderRequest;
use App\Http\Requests\PurchaseOrder\UpdatePurchaseOrderRequest;
2022-07-06 07:18:41 +02:00
use App\Http\Requests\PurchaseOrder\UploadPurchaseOrderRequest;
2023-11-26 08:41:42 +01:00
use App\Jobs\Entity\CreateRawPdf;
use App\Jobs\PurchaseOrder\PurchaseOrderEmail;
use App\Jobs\PurchaseOrder\ZipPurchaseOrders;
use App\Models\Account;
use App\Models\Client;
use App\Models\PurchaseOrder;
use App\Repositories\PurchaseOrderRepository;
use App\Services\PdfMaker\PdfMerge;
use App\Services\Template\TemplateAction;
use App\Transformers\PurchaseOrderTransformer;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
2022-05-29 04:29:06 +02:00
class PurchaseOrderController extends BaseController
{
use MakesHash;
2022-07-06 07:18:41 +02:00
use SavesDocuments;
2022-05-29 04:29:06 +02:00
protected $entity_type = PurchaseOrder::class;
protected $entity_transformer = PurchaseOrderTransformer::class;
protected $purchase_order_repository;
public function __construct(PurchaseOrderRepository $purchase_order_repository)
{
parent::__construct();
$this->purchase_order_repository = $purchase_order_repository;
}
/**
* Show the list of Purchase Orders.
*
* @param \App\Filters\PurchaseOrderFilters $filters The filters
*
* @return Response
*
* @OA\Get(
* path="/api/v1/purchase_orders",
* operationId="getPurchaseOrders",
* tags={"purchase_orders"},
* summary="Gets a list of purchase orders",
* description="Lists purchase orders, search and filters allow fine grained lists to be generated.
*
* Query parameters can be added to performed more fine grained filtering of the purchase orders, these are handled by the PurchaseOrderFilters class which defines the methods available",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A list of purchase orders",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Credit"),
* ),
* @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 index(PurchaseOrderFilters $filters)
{
$purchase_orders = PurchaseOrder::filter($filters);
return $this->listResponse($purchase_orders);
}
/**
* Show the form for creating a new resource.
*
* @param CreatePurchaseOrderRequest $request The request
*
* @return Response
*
*
* @OA\Get(
* path="/api/v1/purchase_orders/create",
* operationId="getPurchaseOrderCreate",
* tags={"purchase_orders"},
* summary="Gets a new blank purchase order object",
* description="Returns a blank object with default values",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank purchase order object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Credit"),
* ),
* @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 create(CreatePurchaseOrderRequest $request)
{
2023-09-05 03:54:05 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$purchase_order = PurchaseOrderFactory::create($user->company()->id, $user->id);
2023-12-14 23:08:44 +01:00
$purchase_order->date = now()->addSeconds($user->company()->utc_offset())->format('Y-m-d');
2022-05-29 04:29:06 +02:00
return $this->itemResponse($purchase_order);
}
/**
* Store a newly created resource in storage.
*
* @param StorePurchaseOrderRequest $request The request
*
* @return Response
*
*
* @OA\Post(
* path="/api/v1/purchase_orders",
* operationId="storePurchaseOrder",
* tags={"purhcase_orders"},
* summary="Adds a purchase order",
* description="Adds an purchase order to the system",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved purchase order object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Credit"),
* ),
* @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 store(StorePurchaseOrderRequest $request)
{
2023-09-05 03:54:05 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$purchase_order = $this->purchase_order_repository->save($request->all(), PurchaseOrderFactory::create($user->company()->id, $user->id));
2022-05-29 04:29:06 +02:00
$purchase_order = $purchase_order->service()
->fillDefaults()
2022-06-10 09:04:16 +02:00
->triggeredActions($request)
2022-05-29 04:29:06 +02:00
->save();
2024-01-14 05:05:00 +01:00
2022-06-09 04:24:42 +02:00
event(new PurchaseOrderWasCreated($purchase_order, $purchase_order->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
return $this->itemResponse($purchase_order->fresh());
2022-05-29 04:29:06 +02:00
}
/**
* Display the specified resource.
*
* @param ShowPurchaseOrderRequest $request The request
* @param PurchaseOrder $purchase_order The purchase order
*
* @return Response
*
*
* @OA\Get(
* path="/api/v1/purchase_orders/{id}",
* operationId="showPurchaseOrder",
* tags={"purchase_orders"},
* summary="Shows an purcase orders",
* description="Displays an purchase order by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Purchase order Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the purchase order object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Credit"),
* ),
* @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 show(ShowPurchaseOrderRequest $request, PurchaseOrder $purchase_order)
{
return $this->itemResponse($purchase_order);
}
/**
* Show the form for editing the specified resource.
*
* @param EditPurchaseOrderRequest $request The request
* @param PurchaseOrder $purchase_order The purchase order
*
* @return Response
*
* @OA\Get(
* path="/api/v1/purchase_orders/{id}/edit",
* operationId="editPurchaseOrder",
* tags={"purchase_orders"},
* summary="Shows an purchase order for editting",
* description="Displays an purchase order by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The purchase order Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the purchase order object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Invoice"),
* ),
* @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 edit(EditPurchaseOrderRequest $request, PurchaseOrder $purchase_order)
{
return $this->itemResponse($purchase_order);
}
/**
* Update the specified resource in storage.
*
* @param UpdatePurchaseOrderRequest $request The request
* @param PurchaseOrder $purchase_order
* @return Response
*
*
* @throws \ReflectionException
* @OA\Put(
* path="/api/v1/purchase_order/{id}",
* operationId="updatePurchaseOrder",
* tags={"purchase_orders"},
* summary="Updates an purchase order",
* description="Handles the updating of an purchase order by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The purchase order Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the purchase order object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Credit"),
* ),
* @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 update(UpdatePurchaseOrderRequest $request, PurchaseOrder $purchase_order)
{
if ($request->entityIsDeleted($purchase_order)) {
return $request->disallowUpdate();
}
$purchase_order = $this->purchase_order_repository->save($request->all(), $purchase_order);
2022-07-07 14:09:39 +02:00
$purchase_order = $purchase_order->service()
->triggeredActions($request)
->save();
2022-06-09 04:24:42 +02:00
event(new PurchaseOrderWasUpdated($purchase_order, $purchase_order->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2022-05-29 04:29:06 +02:00
return $this->itemResponse($purchase_order);
}
/**
* Remove the specified resource from storage.
*
* @param DestroyPurchaseOrderRequest $request
* @param PurchaseOrder $purchase_order
*
* @return Response
*
* @throws \Exception
* @OA\Delete(
* path="/api/v1/purchase_orders/{id}",
* operationId="deletePurchaseOrder",
* tags={"purchase_orders"},
* summary="Deletes a purchase order",
* description="Handles the deletion of an purchase orders by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-05-29 04:29:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The purhcase order Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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 destroy(DestroyPurchaseOrderRequest $request, PurchaseOrder $purchase_order)
{
$this->purchase_order_repository->delete($purchase_order);
return $this->itemResponse($purchase_order->fresh());
}
2022-06-07 00:12:06 +02:00
/**
* Perform bulk actions on the list view.
*
2023-07-26 04:59:36 +02:00
* @return \Illuminate\Support\Collection
2022-06-07 00:12:06 +02:00
*
* @OA\Post(
* path="/api/v1/purchase_orders/bulk",
* operationId="bulkPurchaseOrderss",
* tags={"purchase_orders"},
* summary="Performs bulk actions on an array of purchase_orders",
* description="",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-06-07 00:12:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="Purchase Order IDS",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="The Bulk Action response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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"),
* ),
* )
*/
2023-02-09 03:30:39 +01:00
public function bulk(BulkPurchaseOrderRequest $request)
2022-06-07 00:12:06 +02:00
{
2023-09-05 02:49:16 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-02-09 03:30:39 +01:00
$action = $request->input('action');
2022-06-07 00:12:06 +02:00
2023-02-09 03:30:39 +01:00
$ids = $request->input('ids');
2022-06-07 00:12:06 +02:00
2023-09-05 02:49:16 +02:00
if (Ninja::isHosted() && (stripos($action, 'email') !== false) && !$user->company()->account->account_sms_verified) {
2022-07-27 03:21:12 +02:00
return response(['message' => 'Please verify your account to send emails.'], 400);
2023-02-16 02:36:09 +01:00
}
2022-07-27 03:21:12 +02:00
2022-06-07 00:12:06 +02:00
$purchase_orders = PurchaseOrder::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
2022-06-09 05:33:13 +02:00
if (! $purchase_orders) {
2022-06-07 00:12:06 +02:00
return response()->json(['message' => 'No Purchase Orders Found']);
}
/*
* Download Purchase Order/s
*/
2022-06-10 10:29:15 +02:00
if ($action == 'bulk_download' && $purchase_orders->count() >= 1) {
2023-10-26 04:57:44 +02:00
$purchase_orders->each(function ($purchase_order) use ($user) {
2023-09-05 02:49:16 +02:00
if ($user->cannot('view', $purchase_order)) {
2022-06-07 00:12:06 +02:00
return response()->json(['message' => ctrans('text.access_denied')]);
}
});
2023-09-05 03:16:47 +02:00
ZipPurchaseOrders::dispatch($purchase_orders->pluck("id")->toArray(), $purchase_orders->first()->company, auth()->user());
2022-06-07 00:12:06 +02:00
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}
2023-09-05 02:49:16 +02:00
if ($action == 'bulk_print' && $user->can('view', $purchase_orders->first())) {
2023-02-16 02:36:09 +01:00
$paths = $purchase_orders->map(function ($purchase_order) {
return (new CreateRawPdf($purchase_order->invitations->first()))->handle();
2022-10-30 21:50:29 +01:00
});
$merge = (new PdfMerge($paths->toArray()))->run();
2023-02-16 02:36:09 +01:00
return response()->streamDownload(function () use ($merge) {
echo($merge);
}, 'print.pdf', ['Content-Type' => 'application/pdf']);
2022-10-30 21:50:29 +01:00
}
2023-11-18 02:46:03 +01:00
if($action == 'template' && $user->can('view', $purchase_orders->first())) {
$hash_or_response = $request->boolean('send_email') ? 'email sent' : \Illuminate\Support\Str::uuid();
TemplateAction::dispatch(
2023-11-18 08:45:26 +01:00
$purchase_orders->pluck('hashed_id')->toArray(),
2023-11-18 02:46:03 +01:00
$request->template_id,
PurchaseOrder::class,
$user->id,
$user->company(),
$user->company()->db,
$hash_or_response,
$request->boolean('send_email')
);
return response()->json(['message' => $hash_or_response], 200);
}
2022-06-07 00:12:06 +02:00
/*
* Send the other actions to the switch
*/
2023-09-05 02:49:16 +02:00
$purchase_orders->each(function ($purchase_order, $key) use ($action, $user) {
if ($user->can('edit', $purchase_order)) {
2022-06-07 00:12:06 +02:00
$this->performAction($purchase_order, $action, true);
}
});
/* Need to understand which permission are required for the given bulk action ie. view / edit */
return $this->listResponse(PurchaseOrder::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
}
/**
* @OA\Get(
* path="/api/v1/purchase_orders/{id}/{action}",
* operationId="actionPurchaseOrder",
* tags={"purchase_orders"},
* summary="Performs a custom action on an purchase order",
* description="Performs a custom action on an purchase order.
*
* The current range of actions are as follows
* - mark_paid
* - download
* - archive
* - delete
* - email",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-06-07 00:12:06 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Purchase Order Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Parameter(
* name="action",
* in="path",
* description="The action string to be performed",
* example="clone_to_quote",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the invoice object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Invoice"),
* ),
* @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"),
* ),
* )
* @param ActionPurchaseOrderRequest $request
* @param PurchaseOrder $purchase_order
* @param $action
* @return \App\Http\Controllers\Response|\Illuminate\Http\JsonResponse|Response|mixed|\Symfony\Component\HttpFoundation\StreamedResponse
*/
public function action(ActionPurchaseOrderRequest $request, PurchaseOrder $purchase_order, $action)
{
2022-06-10 10:29:15 +02:00
return $this->performAction($purchase_order, $action);
2022-06-07 00:12:06 +02:00
}
private function performAction(PurchaseOrder $purchase_order, $action, $bulk = false)
2023-02-16 02:36:09 +01:00
{
2022-06-07 00:12:06 +02:00
/*If we are using bulk actions, we don't want to return anything */
switch ($action) {
case 'mark_sent':
$purchase_order->service()->markSent()->save();
if (! $bulk) {
return $this->itemResponse($purchase_order);
}
break;
case 'download':
$file = $purchase_order->service()->getPurchaseOrderPdf();
2023-02-16 02:36:09 +01:00
return response()->streamDownload(function () use ($file) {
echo $file;
}, $purchase_order->numberFormatter().".pdf", ['Content-Type' => 'application/pdf']);
2022-06-07 00:12:06 +02:00
case 'restore':
$this->purchase_order_repository->restore($purchase_order);
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($purchase_order);
2022-06-07 00:12:06 +02:00
}
break;
case 'archive':
$this->purchase_order_repository->archive($purchase_order);
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($purchase_order);
2022-06-07 00:12:06 +02:00
}
break;
case 'delete':
$this->purchase_order_repository->delete($purchase_order);
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($purchase_order);
2022-06-07 00:12:06 +02:00
}
break;
2024-01-14 05:05:00 +01:00
2022-06-07 00:12:06 +02:00
case 'email':
//check query parameter for email_type and set the template else use calculateTemplate
2022-06-10 09:04:16 +02:00
PurchaseOrderEmail::dispatch($purchase_order, $purchase_order->company);
2022-06-07 00:12:06 +02:00
2022-06-10 10:00:07 +02:00
if (! $bulk) {
return response()->json(['message' => 'email sent'], 200);
}
2022-07-06 11:25:22 +02:00
break;
2022-07-08 01:00:36 +02:00
case 'send_email':
//check query parameter for email_type and set the template else use calculateTemplate
PurchaseOrderEmail::dispatch($purchase_order, $purchase_order->company);
if (! $bulk) {
return response()->json(['message' => 'email sent'], 200);
}
break;
case 'add_to_inventory':
$purchase_order->service()->add_to_inventory();
return $this->itemResponse($purchase_order);
2022-07-06 11:25:22 +02:00
case 'expense':
2023-02-16 02:36:09 +01:00
if ($purchase_order->expense()->exists()) {
2022-07-06 11:25:22 +02:00
return response()->json(['message' => ctrans('texts.purchase_order_already_expensed')], 400);
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2022-07-06 11:25:22 +02:00
$expense = $purchase_order->service()->expense();
return $this->itemResponse($purchase_order);
2022-06-10 10:00:07 +02:00
2022-06-25 01:44:14 +02:00
case 'cancel':
2023-02-16 02:36:09 +01:00
if ($purchase_order->status_id <= PurchaseOrder::STATUS_SENT) {
2022-06-25 01:44:14 +02:00
$purchase_order->status_id = PurchaseOrder::STATUS_CANCELLED;
$purchase_order->save();
}
2024-01-14 05:05:00 +01:00
2022-06-25 01:44:14 +02:00
if (! $bulk) {
2022-08-18 06:08:50 +02:00
return $this->itemResponse($purchase_order);
2022-06-25 01:44:14 +02:00
}
break;
2022-06-07 00:12:06 +02:00
default:
return response()->json(['message' => ctrans('texts.action_unavailable', ['action' => $action])], 400);
break;
}
}
2022-07-06 07:18:41 +02:00
/**
* Update the specified resource in storage.
*
* @param UploadPurchaseOrderRequest $request
* @param PurchaseOrder $purchase_order
* @return Response
*
*
*
* @OA\Put(
* path="/api/v1/purchase_orders/{id}/upload",
* operationId="uploadPurchaseOrder",
* tags={"purchase_orders"},
* summary="Uploads a document to a purchase_orders",
* description="Handles the uploading of a document to a purchase_order",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2022-07-06 07:18:41 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Purchase Order Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Purchase Order object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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\JsonContent(ref="#/components/schemas/Vendor"),
* ),
* @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 upload(UploadPurchaseOrderRequest $request, PurchaseOrder $purchase_order)
{
2023-02-16 02:36:09 +01:00
if (!$this->checkFeature(Account::FEATURE_DOCUMENTS)) {
2022-07-06 07:18:41 +02:00
return $this->featureFailure();
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $purchase_order, $request->input('is_public', true));
2023-02-16 02:36:09 +01:00
}
2022-07-06 07:18:41 +02:00
return $this->itemResponse($purchase_order->fresh());
2023-02-16 02:36:09 +01:00
}
2022-07-06 07:18:41 +02:00
2023-02-01 10:00:55 +01:00
/**
* @OA\Get(
* path="/api/v1/purchase_order/{invitation_key}/download",
* operationId="downloadPurchaseOrder",
* tags={"purchase_orders"},
* summary="Download a specific purchase order by invitation key",
* description="Downloads a specific purchase order",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2023-02-01 10:00:55 +01:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="invitation_key",
* in="path",
* description="The Purchase Order Invitation Key",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Purchase Order pdf",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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"),
* ),
* )
* @param $invitation_key
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function downloadPdf($invitation_key)
{
$invitation = $this->purchase_order_repository->getInvitationByKey($invitation_key);
if (! $invitation) {
return response()->json(['message' => 'no record found'], 400);
}
$purchase_order = $invitation->purchase_order;
$file = $purchase_order->service()->getPurchaseOrderPdf();
$headers = ['Content-Type' => 'application/pdf'];
if (request()->input('inline') == 'true') {
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
return response()->streamDownload(function () use ($file) {
echo $file;
}, $purchase_order->numberFormatter().".pdf", $headers);
2023-02-01 10:00:55 +01:00
}
2024-03-10 16:55:49 +01:00
/**
* @OA\Get(
* path="/api/v1/credit/{invitation_key}/download_e_purchase_order",
* operationId="downloadEPurchaseOrder",
* tags={"purchase_orders"},
* summary="Download a specific E-Purchase-Order by invitation key",
* description="Downloads a specific E-Purchase-Order",
* @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 E-Purchase-Order Invitation Key",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the E-Purchase-Order pdf",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-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"),
* ),
* )
* @param $invitation_key
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function downloadEPurchaseOrder($invitation_key)
{
$invitation = $this->purchase_order_repository->getInvitationByKey($invitation_key);
if (! $invitation) {
return response()->json(['message' => 'no record found'], 400);
}
$contact = $invitation->contact;
$purchase_order = $invitation->purchase_order;
$file = $purchase_order->service()->getEPurchaseOrder($contact);
$file_name = $purchase_order->getFileName("xml");
$headers = ['Content-Type' => 'application/xml'];
if (request()->input('inline') == 'true') {
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
return response()->streamDownload(function () use ($file) {
echo $file;
}, $file_name, $headers);
}
2022-05-29 04:29:06 +02:00
}