Skip to content

Commit

Permalink
Add query sales orders endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vollborn committed Aug 20, 2024
1 parent 026ed7b commit 1a04512
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Enums/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum Permission: string
case QueryStockChanges = 'stock.querystockchanges';

// Sales Order
case QuerySalesOrders = 'salesorder.querysalesorders';
case CreateSalesOrder = 'salesorder.createsalesorder';
case CreateSalesOrderLineItem = 'salesorder.createsalesorderlineitem';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DREID\LaravelJtlApi\Modules\SalesOrder\DataTransferObjects;

readonly class SalesOrderAddressDto
{
public function __construct(
public ?string $company,
public ?string $firstName,
public ?string $lastName,
public string $street,
public ?string $postalCode,
public ?string $city,
public string $countryIso,
) {}

public static function fromResponse(array $data): static
{
return new self(
$data['Company'] ?? null,
$data['FirstName'] ?? null,
$data['LastName'] ?? null,
$data['Street'],
$data['PostalCode'] ?? null,
$data['City'] ?? null,
$data['CountryIso'],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DREID\LaravelJtlApi\Modules\SalesOrder\DataTransferObjects;

readonly class SalesOrderDepartureCountryDto
{
public function __construct(
public string $countryIso,
public string $currencyIso,
public float $currencyFactor,
) {}

public static function fromResponse(array $data): static
{
return new self(
$data['CountryISO'],
$data['CurrencyIso'],
$data['CurrencyFactor'],
);
}
}
16 changes: 12 additions & 4 deletions src/Modules/SalesOrder/DataTransferObjects/SalesOrderDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ public function __construct(
public ?string $externalNumber,
public ?string $billingNumber,
public int $companyId,
public int $customerId,
public SalesOrderDepartureCountryDto $departureCountry,
public SalesOrderAddressDto $billingAddress,
public SalesOrderAddressDto $shipmentAddress,
public ?int $customerId,
public ?string $customerVatId,
public ?string $merchantVatId,
public ?string $salesOrderDate,
public SalesOrderShippingDetailDto $salesOrderShippingDetail,
public ?int $colorCodeId,
public ?bool $isExternalInvoice,
public ?string $comment,
public ?string $customerComment,
public ?bool $isCancelled,
public ?string $languageIso,
public ?int $salesChannelId,
public ?string $salesChannelId,
public ?int $userCreatedId,
public ?int $userId,
) {}
Expand All @@ -33,11 +37,15 @@ public static function fromResponse(array $data): static
$data['ExternalNumber'],
$data['BillingNumber'] ?? null,
$data['CompanyId'],
$data['CustomerId'],
SalesOrderDepartureCountryDto::fromResponse($data['DepartureCountry']),
SalesOrderAddressDto::fromResponse($data['BillingAddress']),
SalesOrderAddressDto::fromResponse($data['Shipmentaddress']),
$data['CustomerId'] ?? null,
$data['CustomerVatID'] ?? null,
$data['MerchantVatID'] ?? null,
$data['SalesOrderDate'],
$data['ColorcodeId'] ?? null,
SalesOrderShippingDetailDto::fromResponse($data['SalesOrderShippingDetail']),
$data['ColorcodeId'] ?? null,
$data['IsExternalInvoice'] ?? null,
$data['Comment'] ?? null,
$data['CustomerComment'] ?? null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace DREID\LaravelJtlApi\Modules\SalesOrder\DataTransferObjects;

readonly class SalesOrderShippingDetailDto
{
public function __construct(
public ?int $shippingMethodId,
public ?int $deliveryCompleteStatus,
public ?int $shippingPriority,
public ?string $shippingDate,
public ?string $estimatedDeliveryDate,
public ?string $deliveredDate,
public ?int $onHoldReasonId,
public ?float $extraWeight,
) {}

public static function fromResponse(array $data): static
{
return new self(
$data['ShippingMethodId'] ?? null,
$data['DeliveryCompleteStatus'] ?? null,
$data['ShippingPriority'] ?? null,
$data['ShippingDate'] ?? null,
$data['EstimatedDeliveryDate'] ?? null,
$data['DeliveredDate'] ?? null,
$data['OnHoldReasonId'] ?? null,
$data['ExtraWeight'] ?? null,
);
}
}
31 changes: 31 additions & 0 deletions src/Modules/SalesOrder/Requests/QuerySalesOrdersRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace DREID\LaravelJtlApi\Modules\SalesOrder\Requests;

readonly class QuerySalesOrdersRequest
{
public function __construct(
public ?string $salesOrderNumber = null,
public ?string $externalOrderNumber = null,
public ?string $billingNumber = null,
public ?int $itemId = null,
public ?int $customerId = null,
public ?int $paymentStatus = null,
public ?int $paymentMethodId = null,
public ?int $deliveryCompleteStatus = null,
public ?int $createdUserId = null,
public ?int $companyId = null,
public ?string $salesChannelId = null,
public ?string $createdSince = null,
public ?string $createdUntil = null,
public ?int $colorId = null,
public ?string $ebayUsername = null,
public ?int $shippingMethodId = null,
public ?string $deliveredDate = null,
public ?bool $isCancelled = null,
public ?int $onHoldReasonId = null,
public ?bool $isExternalInvoice = null,
public ?int $pageNumber = null,
public ?int $pageSize = null,
) {}
}
17 changes: 17 additions & 0 deletions src/Modules/SalesOrder/Responses/QuerySalesOrdersResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace DREID\LaravelJtlApi\Modules\SalesOrder\Responses;

use DREID\LaravelJtlApi\ApiResponse;
use DREID\LaravelJtlApi\Modules\SalesOrder\DataTransferObjects\SalesOrderDto;
use DREID\LaravelJtlApi\PaginatedResponse;

readonly class QuerySalesOrdersResponse extends PaginatedResponse
{
public function __construct(ApiResponse $response)
{
parent::__construct($response, array_map(static function ($item) {
return SalesOrderDto::fromResponse($item);
}, $response->json['Items']));
}
}
53 changes: 53 additions & 0 deletions src/Modules/SalesOrder/SalesOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,67 @@
use DREID\LaravelJtlApi\Exceptions\UnauthorizedException;
use DREID\LaravelJtlApi\Exceptions\UnhandledResponseException;
use DREID\LaravelJtlApi\Modules\SalesOrder\Requests\CreateSalesOrderRequest;
use DREID\LaravelJtlApi\Modules\SalesOrder\Requests\QuerySalesOrdersRequest;
use DREID\LaravelJtlApi\Modules\SalesOrder\Responses\CreateSalesOrderResponse;
use DREID\LaravelJtlApi\Modules\SalesOrder\Responses\QuerySalesOrdersResponse;
use DREID\LaravelJtlApi\Repository;
use DREID\LaravelJtlApi\Traits\MapAddress;

class SalesOrderRepository extends Repository
{
use MapAddress;

/**
* @throws UnauthorizedException
* @throws UnhandledResponseException
* @throws ConnectionException
* @throws MissingPermissionException
* @throws MissingLicenseException
* @throws MissingApiKeyException
*/
public function querySalesOrders(QuerySalesOrdersRequest $request): QuerySalesOrdersResponse
{
$permissions = [Permission::AllRead, Permission::QuerySalesOrders];

if (!Permission::allowsOneOf($permissions)) {
throw MissingPermissionException::oneOf($permissions);
}

$params = $this->deleteNullValues([
'salesOrderNumber' => $request->salesOrderNumber,
'externalOrderNumber' => $request->externalOrderNumber,
'billingNumber' => $request->billingNumber,
'itemId' => $request->itemId,
'customerId' => $request->customerId,
'paymentStatus' => $request->paymentStatus,
'paymentMethodId' => $request->paymentMethodId,
'deliveryCompleteStatus' => $request->deliveryCompleteStatus,
'createdUserId' => $request->createdUserId,
'companyId' => $request->companyId,
'salesChannelId' => $request->salesChannelId,
'createdSince' => $request->createdSince,
'createdUntil' => $request->createdUntil,
'colorId' => $request->colorId,
'ebayUsername' => $request->ebayUsername,
'shippingMethodId' => $request->shippingMethodId,
'deliveredDate' => $request->deliveredDate,
'isCancelled' => $request->isCancelled,
'onHoldReasonId' => $request->onHoldReasonId,
'isExternalInvoice' => $request->isExternalInvoice,
'pageNumber' => $request->pageNumber,
'pageSize' => $request->pageSize,
]);

$response = $this->get('/v1/salesOrders', $params);

if ($response->wasSuccessful) {
return new QuerySalesOrdersResponse($response);
}

$this->throwExceptionsIfPossible($response);
throw new UnhandledResponseException($response);
}

/**
* @throws MissingApiKeyException
* @throws MissingLicenseException
Expand Down

0 comments on commit 1a04512

Please sign in to comment.