Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagof committed Oct 4, 2024
1 parent 1712e37 commit 557a57a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
26 changes: 16 additions & 10 deletions src/API/Data/ClientData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@ class ClientData extends EntityData
{
public const USE_PROPERTIES = [
'name',
'description',
'unitPrice',
'unit',
'quantity',
// 'tax.name',
'code',
];

public const CREATE_PROPERTIES = [
'name',
'code',
'email',
'address',
'city',
'postalCode',
'country',
'fiscalId',
'website',
'phone',
'fax',
'observations',
];

public function __construct(
Expand All @@ -45,9 +56,4 @@ public function __construct(
public null|Optional|TaxExemptionCodeEnum $taxExemptionCode,
public null|Optional|string $openAccountLink,
) {}

public static function getUseProperties(): array
{
return self::USE_PROPERTIES;
}
}
21 changes: 19 additions & 2 deletions src/API/Data/EntityData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ abstract class EntityData extends Data
{
public Optional|int $id;

public const CREATE_PROPERTIES = null;

public const UPDATE_PROPERTIES = null;

public const USE_PROPERTIES = null;

public function getId(): ?int
{
return $this->id instanceof Optional ? null : $this->id;
Expand All @@ -23,12 +29,23 @@ protected static function prefixProperties(string $prefix, array $properties): a

public function toCreateData(): static
{
return $this;
return static::CREATE_PROPERTIES
? static::from($this)->only(...static::CREATE_PROPERTIES)
: static::from($this);
}

public function toUpdateData(): static
{
return $this;
return static::UPDATE_PROPERTIES
? static::from($this)->only(...static::UPDATE_PROPERTIES)
: static::from($this);
}

public function toUseData(): static
{
return static::USE_PROPERTIES
? static::from($this)->only(...static::USE_PROPERTIES)
: static::from($this);
}

public function toModelData(): static
Expand Down
8 changes: 4 additions & 4 deletions src/API/Data/InvoiceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public function __construct(
public static function getCreateProperties(): array
{
return array_merge(
self::CREATE_PROPERTIES,
self::prefixProperties('items', ItemData::getUseProperties()),
self::prefixProperties('client', ClientData::getUseProperties()),
static::CREATE_PROPERTIES,
static::prefixProperties('items', ItemData::getUseProperties()),
static::prefixProperties('client', ClientData::CREATE_PROPERTIES),
);
}

public function toCreateData(): static
{
return static::from($this)
->only(...self::getCreateProperties());
->only(...static::getCreateProperties());
}
}
16 changes: 16 additions & 0 deletions src/API/Data/TaxData.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ public function __construct(
#[WithTransformer(BoolToIntTransformer::class)]
public Optional|bool $defaultTax,
) {}

public static function IVA23(): self
{
return self::from([
'name' => 'IVA23',
'code' => 'NOR'
]);
}

public static function IVA0(): self
{
return self::from([
'name' => 'IVA0',
'code' => 'ISE',
]);
}
}
2 changes: 1 addition & 1 deletion src/API/Endpoints/Concerns/CreatesWithType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function create(EntityTypeEnum $entityType, EntityData $data): EntityData
$response = $this->call(
action: static::CREATE,
urlParams: ['type' => $entityType->toUrlVariable()],
bodyData: [$entityType->value => $data->toCreateData()]
bodyData: [$entityType->value => $data->toCreateData()->toArray()]
);

return $this->responseToDataObject($response[$entityType->value]);
Expand Down

0 comments on commit 557a57a

Please sign in to comment.