Skip to content

Commit

Permalink
Merge pull request #8 from Morning-Train/feature/products
Browse files Browse the repository at this point in the history
Product resource
  • Loading branch information
mschadegg authored Feb 8, 2024
2 parents 6b3f107 + 7f5455c commit a95f8fe
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 92 deletions.
92 changes: 88 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,75 @@ $customer = Customer::where('email', '[email protected]')
Is not yet implemented.

## Usage
Every resource in the e-conomic REST API is represented by a class in this SDK. And every class is implementing function corresponding to the endpoints in the API.


### Get multiple resources
When fetching multiple resources from the e-conomic REST API, you will get a collection of the resources. The collection is an implementation of the Laravel lazy collection, so you can use all the methods from the Laravel collection class on the collection.

To get multiple resources from the e-conomic REST API, you can use the `all` method on the resource class.

```php
use Morningtrain\Economic\Resources\Customer;

$customers = Customer::all();
```

#### Filtering
You can filter the resources you want to get, if the endpoint allows it. See [https://restdocs.e-conomic.com/#endpoints](https://restdocs.e-conomic.com/#endpoints) for more information about which filters every resource support. This will return af collection of resources matching the filter.

```php
use Morningtrain\Economic\Resources\Customer;

$customers = Customer::where('email', '[email protected]');
```

### Get a single resource
When fetching a single resource from the e-conomic REST API, you will get an instance of the resource class you are asking for.

To get a single resource from the e-conomic REST API, you can use the `find` method on the resource class. The find method is using the primary key to find the resource. The primary key is different from resource to resource, so you need to look in the e-conomic REST API documentation to find the primary key for the resource you want to get.
The type of primary key is mixed, so you can use a string or an integer to find the resource.

```php
use Morningtrain\Economic\Resources\Customer;
use Morningtrain\Economic\Resources\Product;

$customer = Customer::find(1); // Where 1 is the customer number

$product = Product::find('proudct-1'); // Where 'product-1' is the product number
```

### Creating a resource
Some resources can be created in E-conomic. When created succesfully you will get the resource you just created.
We have implemented a `create` method on the resource class, so you can create a resource like this:

The parameters you need to provide when creating a resource is different from resource to resource, so you need to look in the implementation of the resource class to see which parameters you need to provide.
Some parameter is required and some is optional. You can see which parameters are required and optional in the implementation of the resource class.
To just use some of the optional parameters, you can use named parameters in PHP.

```php
use Morningtrain\Economic\Resources\Product;

$product = Product::create(
'Product 1', // product name
1, // product group number
'p-1', // product number
barCode: '1234567890',
costPrice: 100.0,
recommendedPrice: 150.0,
salesPrice: 199.95,
description: 'test',
unit: 1 // unit number
);
```

### Updating a resource
Some resources can be updated after creation. This can be done as follows:

```php
$customer = new \Morningtrain\Economic\Resources\Customer([
'customerNumber' => 1,
]);
use Morningtrain\Economic\Resources\Customer;

$customer = new Customer::find(1); // Where 1 is the customer number

$customer->name = 'New name';

Expand All @@ -90,14 +145,37 @@ $customer->save();
This will update the customer name in E-conomic. You can also simply provide all the new values when instantiating the customer.

```php
$customer = new \Morningtrain\Economic\Resources\Customer([
use Morningtrain\Economic\Resources\Customer;

$customer = new Customer([
'customerNumber' => 1,
'name' => 'New Name',
]);

$customer->save();
```

### Deleting a resource
Some resources can be deleted in E-conomic. This can be done using the `delete` method on the resource class.

```php
use Morningtrain\Economic\Resources\Customer;

$customer = new Customer::find(1); // Where 1 is the customer number

$customer->delete();
```

This will delete the customer in E-conomic.

You can also delete a resource by calling the static method `deleteByPrimaryKey`.

```php
use Morningtrain\Economic\Resources\Customer;

Customer::deleteByPrimaryKey(1); // Where 1 is the customer number
```

## Examples

### Get all customers
Expand Down Expand Up @@ -136,6 +214,12 @@ if(!empty($customer)) {
}
```

## Testing

```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
2 changes: 1 addition & 1 deletion src/Abstracts/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getPrimaryKey(): mixed
{
$primaryKeyPropertyName = $this->getPrimaryKeyPropertyName();

if (! empty($primaryKeyPropertyName) && ! empty($this->{$primaryKeyPropertyName})) {
if (! empty($primaryKeyPropertyName) && isset($this->{$primaryKeyPropertyName})) {
return $this->{$primaryKeyPropertyName};
}

Expand Down
7 changes: 0 additions & 7 deletions src/EconomicClass.php

This file was deleted.

129 changes: 53 additions & 76 deletions src/Resources/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
use DateTime;
use Morningtrain\Economic\Abstracts\Resource;
use Morningtrain\Economic\Attributes\Resources\Create;
use Morningtrain\Economic\Attributes\Resources\Delete;
use Morningtrain\Economic\Attributes\Resources\GetCollection;
use Morningtrain\Economic\Attributes\Resources\GetSingle;
use Morningtrain\Economic\Attributes\Resources\Properties\Filterable;
use Morningtrain\Economic\Attributes\Resources\Properties\PrimaryKey;
use Morningtrain\Economic\Attributes\Resources\Properties\ResourceType;
use Morningtrain\Economic\Attributes\Resources\Properties\Sortable;
use Morningtrain\Economic\Classes\EconomicCollection;
use Morningtrain\Economic\Resources\AccountingYear\Entry;
use Morningtrain\Economic\Resources\AccountingYear\Period;
use Morningtrain\Economic\Resources\AccountingYear\Total;
use Morningtrain\Economic\Resources\AccountingYear\Voucher;
use Morningtrain\Economic\Attributes\Resources\Update;
use Morningtrain\Economic\Traits\Resources\Creatable;
use Morningtrain\Economic\Traits\Resources\Deletable;
use Morningtrain\Economic\Traits\Resources\GetCollectionable;
use Morningtrain\Economic\Traits\Resources\GetSingleable;

#[GetCollection('products')]
#[GetSingle('products/:product', ':product')]
#[GetCollection('products')] // https://restdocs.e-conomic.com/#get-products
#[GetSingle('products/:productNumber', ':productNumber')]
#[Create('products')]
#[Update('products/:productNumber', ':productNumber')]
#[Delete('products/:productNumber', ':productNumber')]
class Product extends Resource
{
use Creatable, GetCollectionable, GetSingleable;

public ?string $name;
/**
* @use GetCollectionable<Product>
*/
use Creatable, Deletable, GetCollectionable, GetSingleable;

#[Filterable]
#[Sortable]
Expand All @@ -41,89 +40,67 @@ class Product extends Resource
#[Sortable]
public ?float $costPrice;

#[Filterable]
#[Sortable]
public ?float $recommendedPrice;
//public ?DepartmentalDistribution $departmentalDistribution; // TODO: implement

#[Filterable]
#[Sortable]
public ?float $salesPrice;
public ?string $description;

/**
* @var EconomicCollection<Entry>|null
*/
#[ResourceType(Entry::class)]
public ?EconomicCollection $entries;
//public ?object $inventory; // TODO: implement

#[Filterable]
#[Sortable]
public ?DateTime $fromDate;
public ?array $invoices;

/**
* @var EconomicCollection<Period>|null
*/
#[ResourceType(Period::class)]
public ?EconomicCollection $periods;
public ?DateTime $lastUpdated;

#[Filterable]
#[Sortable]
public ?DateTime $toDate;
public ?string $name;

/**
* @var EconomicCollection<Total>|null
*/
#[ResourceType(Total::class)]
public ?EconomicCollection $totals;
public ?ProductGroup $productGroup;

/**
* @var EconomicCollection<Voucher>|null
*/
#[ResourceType(Voucher::class)]
public ?EconomicCollection $vouchers;
public ?string $productNumber;

#[Filterable]
#[Sortable]
public ?string $year;
public ?float $recommendedPrice;

#[PrimaryKey]
public string $productNumber;
#[Filterable]
#[Sortable]
public ?float $salesPrice;

public static function create(DateTime $fromDate, DateTime $toDate): static
{
return static::createRequest(compact('fromDate', 'toDate'));
}
public ?Unit $unit;

public static function new(
public static function create(
string $name,
ProductGroup|int $productGroup,
string $productNumber,
?string $name = null,
?string $barCode = null,
?bool $barred = null,
?float $costPrice = null,
?DepartmentalDistribution $departmentalDistribution = null,
?string $description = null,
?object $inventory = null,
?float $recommendedPrice = null,
?float $salesPrice = null,
?EconomicCollection $entries = null,
DateTime|string|null $fromDate = null,
?EconomicCollection $periods = null,
DateTime|string|null $toDate = null,
?EconomicCollection $totals = null,
?EconomicCollection $vouchers = null,
?string $year = null,
Unit|int|null $unit = null,
): static {
return new static([
'productNumber' => $productNumber,
'name' => $name,
'barCode' => $barCode,
'barred' => $barred,
'costPrice' => $costPrice,
'recommendedPrice' => $recommendedPrice,
'salesPrice' => $salesPrice,
'entries' => $entries,
'fromDate' => $fromDate,
'periods' => $periods,
'toDate' => $toDate,
'totals' => $totals,
'vouchers' => $vouchers,
'year' => $year,
]);
if (is_int($productGroup)) {
$productGroup = new ProductGroup(['productGroupNumber' => $productGroup]);
}

if (is_int($unit)) {
$unit = new Unit(['unitNumber' => $unit]);
}

return static::createRequest(compact(
'name',
'productGroup',
'productNumber',
'barCode',
'barred',
'costPrice',
'departmentalDistribution',
'description',
'inventory',
'recommendedPrice',
'salesPrice',
'unit',
));
}
}
34 changes: 34 additions & 0 deletions src/Resources/ProductGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Morningtrain\Economic\Resources;

use Morningtrain\Economic\Abstracts\Resource;
use Morningtrain\Economic\Attributes\Resources\GetCollection;
use Morningtrain\Economic\Attributes\Resources\GetSingle;
use Morningtrain\Economic\Attributes\Resources\Properties\PrimaryKey;
use Morningtrain\Economic\Traits\Resources\GetCollectionable;
use Morningtrain\Economic\Traits\Resources\GetSingleable;

#[GetCollection('product-groups')] // https://restdocs.e-conomic.com/#get-product-groups
#[GetSingle('product-groups/:productGroupNumber', ':productGroupNumber')] // https://restdocs.e-conomic.com/#get-product-groups-productgroupnumber
class ProductGroup extends Resource
{
/**
* @use GetCollectionable<ProductGroup>
*/
use GetCollectionable, GetSingleable;

// public ?object $accrual; // TODO: implement

public ?bool $inventoryEnabled;

public ?string $name;

#[PrimaryKey]
public ?int $productGroupNumber;

// public ?string $products; // TODO: implement

// public ?string $salesAccounts; // TODO: implement

}
13 changes: 13 additions & 0 deletions src/Traits/Resources/Deletable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ public function delete(): bool

return true;
}

public static function deleteByPrimaryKey(int|string $primaryKey): bool
{
$response = EconomicApiService::delete(static::getEndpoint(Delete::class, $primaryKey));

if ($response->getStatusCode() !== 204) {
// TODO: Log error and throw exception

return false;
}

return true;
}
}
6 changes: 3 additions & 3 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
)
->addLine(ProductLine::new(
description: 'T-shirt - Size L',
product: Product::new(
productNumber: 1,
),
product: new Product([
'productNumber' => 1,
]),
quantity: 1,
unitNetPrice: 500
))
Expand Down
Loading

0 comments on commit a95f8fe

Please sign in to comment.