2022-05-29 04:29:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
2022-06-09 04:24:42 +02:00
|
|
|
use App\Events\PurchaseOrder\PurchaseOrderWasCreated;
|
|
|
|
use App\Events\PurchaseOrder\PurchaseOrderWasUpdated;
|
2022-05-29 04:29:06 +02:00
|
|
|
use App\Factory\PurchaseOrderFactory;
|
|
|
|
use App\Filters\PurchaseOrderFilters;
|
2022-06-07 00:12:06 +02:00
|
|
|
use App\Http\Requests\PurchaseOrder\ActionPurchaseOrderRequest;
|
2022-05-29 04:29:06 +02:00
|
|
|
use App\Http\Requests\PurchaseOrder\CreatePurchaseOrderRequest;
|
|
|
|
use App\Http\Requests\PurchaseOrder\DestroyPurchaseOrderRequest;
|
|
|
|
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;
|
2022-06-07 00:12:06 +02:00
|
|
|
use App\Jobs\Invoice\ZipInvoices;
|
2022-06-10 10:00:07 +02:00
|
|
|
use App\Jobs\PurchaseOrder\PurchaseOrderEmail;
|
|
|
|
use App\Jobs\PurchaseOrder\ZipPurchaseOrders;
|
2022-07-06 07:18:41 +02:00
|
|
|
use App\Models\Account;
|
2022-05-29 04:29:06 +02:00
|
|
|
use App\Models\Client;
|
2022-07-06 11:25:22 +02:00
|
|
|
use App\Models\Expense;
|
2022-05-29 04:29:06 +02:00
|
|
|
use App\Models\PurchaseOrder;
|
|
|
|
use App\Repositories\PurchaseOrderRepository;
|
2022-07-06 11:25:22 +02:00
|
|
|
use App\Transformers\ExpenseTransformer;
|
2022-05-29 04:29:06 +02:00
|
|
|
use App\Transformers\PurchaseOrderTransformer;
|
2022-06-09 04:24:42 +02:00
|
|
|
use App\Utils\Ninja;
|
2022-05-29 04:29:06 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2022-07-06 07:18:41 +02:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
2022-05-29 04:29:06 +02:00
|
|
|
use Illuminate\Http\Response;
|
2022-06-10 10:29:15 +02:00
|
|
|
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",
|
|
|
|
* @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\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",
|
|
|
|
* @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\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)
|
|
|
|
{
|
|
|
|
$purchase_order = PurchaseOrderFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
|
|
|
|
|
|
|
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",
|
|
|
|
* @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\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)
|
|
|
|
{
|
|
|
|
|
|
|
|
$purchase_order = $this->purchase_order_repository->save($request->all(), PurchaseOrderFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
2022-06-09 04:24:42 +02:00
|
|
|
event(new PurchaseOrderWasCreated($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);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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",
|
|
|
|
* @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="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",
|
|
|
|
* @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="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",
|
|
|
|
* @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="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",
|
|
|
|
* @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="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.
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/purchase_orders/bulk",
|
|
|
|
* operationId="bulkPurchaseOrderss",
|
|
|
|
* tags={"purchase_orders"},
|
|
|
|
* summary="Performs bulk actions on an array of purchase_orders",
|
|
|
|
* description="",
|
|
|
|
* @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/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"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
|
|
|
|
$action = request()->input('action');
|
|
|
|
|
|
|
|
$ids = request()->input('ids');
|
|
|
|
|
2022-07-27 06:57:17 +02:00
|
|
|
if(Ninja::isHosted() && (stripos($action, 'email') !== false) && !auth()->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);
|
|
|
|
|
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) {
|
2022-06-07 00:12:06 +02:00
|
|
|
$purchase_orders->each(function ($purchase_order) {
|
|
|
|
if (auth()->user()->cannot('view', $purchase_order)) {
|
|
|
|
nlog("access denied");
|
|
|
|
return response()->json(['message' => ctrans('text.access_denied')]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-10 10:00:07 +02:00
|
|
|
ZipPurchaseOrders::dispatch($purchase_orders, $purchase_orders->first()->company, auth()->user());
|
2022-06-07 00:12:06 +02:00
|
|
|
|
|
|
|
return response()->json(['message' => ctrans('texts.sent_message')], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the other actions to the switch
|
|
|
|
*/
|
|
|
|
$purchase_orders->each(function ($purchase_order, $key) use ($action) {
|
|
|
|
if (auth()->user()->can('edit', $purchase_order)) {
|
|
|
|
$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",
|
|
|
|
* @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="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)
|
|
|
|
{
|
|
|
|
/*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();
|
|
|
|
|
|
|
|
return response()->streamDownload(function () use($file) {
|
|
|
|
echo Storage::get($file);
|
|
|
|
}, basename($file), ['Content-Type' => 'application/pdf']);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'restore':
|
|
|
|
$this->purchase_order_repository->restore($purchase_order);
|
|
|
|
|
|
|
|
if (! $bulk) {
|
|
|
|
return $this->listResponse($purchase_order);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'archive':
|
|
|
|
$this->purchase_order_repository->archive($purchase_order);
|
|
|
|
|
|
|
|
if (! $bulk) {
|
|
|
|
return $this->listResponse($purchase_order);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
|
|
|
|
$this->purchase_order_repository->delete($purchase_order);
|
|
|
|
|
|
|
|
if (! $bulk) {
|
|
|
|
return $this->listResponse($purchase_order);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-07-06 12:04:59 +02:00
|
|
|
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':
|
|
|
|
|
|
|
|
if($purchase_order->expense()->exists())
|
|
|
|
return response()->json(['message' => ctrans('texts.purchase_order_already_expensed')], 400);
|
|
|
|
|
|
|
|
$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':
|
|
|
|
|
|
|
|
if($purchase_order->status_id <= PurchaseOrder::STATUS_SENT)
|
|
|
|
{
|
|
|
|
$purchase_order->status_id = PurchaseOrder::STATUS_CANCELLED;
|
|
|
|
$purchase_order->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $bulk) {
|
|
|
|
return $this->listResponse($purchase_order);
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
* @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="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)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(!$this->checkFeature(Account::FEATURE_DOCUMENTS))
|
|
|
|
return $this->featureFailure();
|
|
|
|
|
|
|
|
if ($request->has('documents'))
|
|
|
|
$this->saveDocuments($request->file('documents'), $purchase_order);
|
|
|
|
|
|
|
|
return $this->itemResponse($purchase_order->fresh());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-29 04:29:06 +02:00
|
|
|
}
|