Add shopping list manipulation
This commit is contained in:
parent
5a65036620
commit
1795efbaf7
@ -39,7 +39,7 @@ class Client
|
||||
);
|
||||
}
|
||||
|
||||
public function post(string $url, array $data): array
|
||||
public function post(string $url, array $data): ?array
|
||||
{
|
||||
$data = $this->client->post($url, [
|
||||
RequestOptions::JSON => $data,
|
||||
|
@ -8,6 +8,7 @@ use Ardent\GrocyApi\Objects\AbstractApiObject;
|
||||
use Ardent\GrocyApi\Objects\ObjectTransformer;
|
||||
use Ardent\GrocyApi\Objects\ProductsObject;
|
||||
use Ardent\GrocyApi\Objects\QuantityUnitsObject;
|
||||
use Ardent\GrocyApi\Objects\ShoppingListObject;
|
||||
|
||||
class GrocyClient
|
||||
{
|
||||
@ -46,6 +47,14 @@ class GrocyClient
|
||||
return $this->getObjects('quantity_units');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ShoppingListObject[]
|
||||
*/
|
||||
public function getShoppingListProducts(): array
|
||||
{
|
||||
return $this->getObjects('shopping_list');
|
||||
}
|
||||
|
||||
public function getProduct(int $id): ProductsObject
|
||||
{
|
||||
return $this->getObject('products', $id);
|
||||
@ -78,6 +87,11 @@ class GrocyClient
|
||||
]);
|
||||
}
|
||||
|
||||
public function getStockClient(): StockClient
|
||||
{
|
||||
return new StockClient($this->client);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractApiObject[]
|
||||
*/
|
||||
|
@ -12,19 +12,21 @@ use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
class ObjectTransformer
|
||||
{
|
||||
private static function objectList(): array
|
||||
private static function objectClass(string $name): string
|
||||
{
|
||||
return [
|
||||
return match ($name) {
|
||||
ProductsObject::getEntity() => ProductsObject::class,
|
||||
QuantityUnitsObject::getEntity() => QuantityUnitsObject::class,
|
||||
];
|
||||
ShoppingListObject::getEntity() => ShoppingListObject::class,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public static function denormalize($object, string $name): AbstractApiObject
|
||||
{
|
||||
// $name = (new CamelCaseToSnakeCaseNameConverter())->denormalize($name);
|
||||
// sprintf('%s\\%sObject', __NAMESPACE__, ucfirst($name))
|
||||
return (new ObjectNormalizer())->denormalize($object, self::objectList()[$name]);
|
||||
return (new ObjectNormalizer())->denormalize($object, self::objectClass($name));
|
||||
}
|
||||
|
||||
public static function normalize(AbstractApiObject $object): array
|
||||
|
@ -93,6 +93,11 @@ class ProductsObject extends AbstractApiObject
|
||||
return $this->userfields;
|
||||
}
|
||||
|
||||
public function getPicnicId(): ?int
|
||||
{
|
||||
return $this->getUserfields()['picnic'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $userfields
|
||||
*
|
||||
|
158
src/Objects/ShoppingListObject.php
Normal file
158
src/Objects/ShoppingListObject.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace Ardent\GrocyApi\Objects;
|
||||
|
||||
class ShoppingListObject extends AbstractApiObject
|
||||
{
|
||||
private int $product_id;
|
||||
|
||||
private ?string $note;
|
||||
|
||||
private int $amount;
|
||||
|
||||
private string $row_created_timestamp;
|
||||
|
||||
private int $shopping_list_id;
|
||||
|
||||
private int $done;
|
||||
|
||||
private int $qu_id;
|
||||
|
||||
public static function getEntity(): string
|
||||
{
|
||||
return 'shopping_list';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getProductId(): int
|
||||
{
|
||||
return $this->product_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $product_id
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setProductId(int $product_id): ShoppingListObject
|
||||
{
|
||||
$this->product_id = $product_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getNote(): ?string
|
||||
{
|
||||
return $this->note;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $note
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setNote(?string $note): ShoppingListObject
|
||||
{
|
||||
$this->note = $note;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAmount(): int
|
||||
{
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $amount
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setAmount(int $amount): ShoppingListObject
|
||||
{
|
||||
$this->amount = $amount;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRowCreatedTimestamp(): string
|
||||
{
|
||||
return $this->row_created_timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $row_created_timestamp
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setRowCreatedTimestamp(string $row_created_timestamp): ShoppingListObject
|
||||
{
|
||||
$this->row_created_timestamp = $row_created_timestamp;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getShoppingListId(): int
|
||||
{
|
||||
return $this->shopping_list_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $shopping_list_id
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setShoppingListId(int $shopping_list_id): ShoppingListObject
|
||||
{
|
||||
$this->shopping_list_id = $shopping_list_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getDone(): int
|
||||
{
|
||||
return $this->done;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $done
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setDone(int $done): ShoppingListObject
|
||||
{
|
||||
$this->done = $done;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getQuId(): int
|
||||
{
|
||||
return $this->qu_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $qu_id
|
||||
*
|
||||
* @return ShoppingListObject
|
||||
*/
|
||||
public function setQuId(int $qu_id): ShoppingListObject
|
||||
{
|
||||
$this->qu_id = $qu_id;
|
||||
return $this;
|
||||
}
|
||||
}
|
48
src/StockClient.php
Normal file
48
src/StockClient.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Ardent\GrocyApi;
|
||||
|
||||
class StockClient
|
||||
{
|
||||
public function __construct(
|
||||
private Client $client,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function shoppingListAddMissingProducts(int $listId = 1): void
|
||||
{
|
||||
$this->client->post('stock/shoppinglist/add-missing-products', ['list_id' => $listId]);
|
||||
}
|
||||
|
||||
public function shoppingListClear(int $listId = 1): void
|
||||
{
|
||||
$this->client->post('stock/shoppinglist/clear', ['list_id' => $listId]);
|
||||
}
|
||||
|
||||
public function shoppingListAdd(int $productId, $amount, int $listId = 1, $note = ''): void
|
||||
{
|
||||
$this->client->post('stock/shoppinglist/clear', [
|
||||
'product_id' => $productId,
|
||||
'list_id' => $listId,
|
||||
'product_amount' => $amount,
|
||||
'note' => $note,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array array of StockLockEntry
|
||||
*/
|
||||
public function stockAdd(int $productId, int $amount, string $price = "0.00", string $type = 'purchase', ?string $bestBeforeDate = null): array
|
||||
{
|
||||
$data = [
|
||||
'amount' => $amount,
|
||||
'transaction_type' => $type,
|
||||
'price' => $price,
|
||||
];
|
||||
if ($bestBeforeDate) {
|
||||
$data['best_before_date'] = $bestBeforeDate; // "2019-01-19"
|
||||
}
|
||||
return $this->client->post(sprintf('stock/products/%s/add', $productId), $data);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user