Skip to content

Commit

Permalink
✨ Handle updating customer resource (#3)
Browse files Browse the repository at this point in the history
* ✨ Handle updating customer resource

* 📝 Update readme

* 📝 Update readme
  • Loading branch information
SimonJnsson authored Jan 9, 2024
1 parent de36430 commit 2665637
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 20 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,28 @@ composer require morningtrain/economic

## Usage

### 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,
]);

$customer->name = 'New name';

$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
$skeleton = new Morningtrain\Economic();
echo $skeleton->echoPhrase('Hello, Morningtrain!');
$customer = new \Morningtrain\Economic\Resources\Customer([
'customerNumber' => 1,
'name' => 'New Name',
]);

$customer->save();
```

## Testing
Expand Down
91 changes: 74 additions & 17 deletions src/Resources/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,46 @@
use Morningtrain\Economic\Attributes\Resources\GetSingle;
use Morningtrain\Economic\Attributes\Resources\Properties\PrimaryKey;
use Morningtrain\Economic\Attributes\Resources\Properties\ResourceType;
use Morningtrain\Economic\Attributes\Resources\Update;
use Morningtrain\Economic\Classes\EconomicCollection;
use Morningtrain\Economic\Resources\Customer\Contact;
use Morningtrain\Economic\Traits\Resources\Creatable;
use Morningtrain\Economic\Traits\Resources\GetCollectionable;
use Morningtrain\Economic\Traits\Resources\GetSingleable;
use Morningtrain\Economic\Traits\Resources\Updatable;

#[GetCollection('customers')]
#[GetSingle('customers/:customerNumber')]
#[Create('customers')]
#[Update('customers/:customerNumber')]
class Customer extends Resource
{
/**
* @use GetCollectionable<Customer>
*/
use Creatable, GetCollectionable, GetSingleable;
use Creatable, GetCollectionable, GetSingleable, Updatable;

public string $address;
public ?string $address;

public Contact $attention;

public float $balance;

public bool $barred;

public string $city;
public ?string $city;

/**
* @var EconomicCollection<Contact>
*/
#[ResourceType(Contact::class)]
public EconomicCollection $contacts;

public string $corporateIdentificationNumber;
public ?string $corporateIdentificationNumber;

public string $country;
public ?string $country;

public float $creditLimit;
public ?float $creditLimit;

public string $currency;

Expand All @@ -62,45 +65,45 @@ class Customer extends Resource

public float $dueAmount;

public string $ean;
public ?string $ean;

public bool $eInvoicingDisabledByDefault;

public string $email;
public ?string $email;

// TODO: implement $invoices (Invoice)

public DateTime $lastUpdated;

public Layout $layout;
public ?Layout $layout;

public string $mobilePhone;
public ?string $mobilePhone;

public string $name;

public PaymentTerm $paymentTerms;

public string $pNumber;
public ?string $pNumber;

public string $publicEntryNumber;
public ?string $publicEntryNumber;

// TODO: implement $salesPerson (Employee)

public string $telephoneAndFaxNumber;
public ?string $telephoneAndFaxNumber;

// TODO: impelement $tempaltes

public array $totals;

public string $vatNumber;
public ?string $vatNumber;

public VatZone $vatZone;

public string $website;
public ?string $website;

public string $zip;
public ?string $zip;

public Employee $salesPerson;
public ?Employee $salesPerson;

public static function create(
string $name,
Expand Down Expand Up @@ -132,4 +135,58 @@ public static function create(

return static::createRequest($creationParameters);
}

public static function new(
string $name,
CustomerGroup|int $customerGroup,
string $currency,
VatZone|int $vatZone,
PaymentTerm|int $paymentTerms,
?string $email = null,
?string $address = null,
?string $zip = null,
?string $city = null,
?string $country = null,
?string $corporateIdentificationNumber = null,
?string $pNumber = null,
?string $vatNumber = null,
?string $ean = null,
?string $publicEntryNumber = null,
?string $website = null,
?string $mobilePhone = null,
?string $telephoneAndFaxNumber = null,
?bool $barred = null,
?bool $eInvoicingDisabledByDefault = null,
?float $creditLimit = null,
?int $customerNumber = null,
Layout|int|null $layout = null,
Employee|int|null $salesPerson = null,
): static {
return new static([
'name' => $name,
'customerGroup' => $customerGroup,
'currency' => $currency,
'vatZone' => $vatZone,
'paymentTerms' => $paymentTerms,
'email' => $email,
'address' => $address,
'zip' => $zip,
'city' => $city,
'country' => $country,
'corporateIdentificationNumber' => $corporateIdentificationNumber,
'pNumber' => $pNumber,
'vatNumber' => $vatNumber,
'ean' => $ean,
'publicEntryNumber' => $publicEntryNumber,
'website' => $website,
'mobilePhone' => $mobilePhone,
'telephoneAndFaxNumber' => $telephoneAndFaxNumber,
'barred' => $barred,
'eInvoicingDisabledByDefault' => $eInvoicingDisabledByDefault,
'creditLimit' => $creditLimit,
'customerNumber' => $customerNumber,
'layout' => $layout,
'salesPerson' => $salesPerson,
]);
}
}
2 changes: 1 addition & 1 deletion src/Traits/Resources/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function save()
throw new Exception('Economic API Service returned a non 200 status code when updating a resource');
}

return true;
return new static($response->getBody());
}
}
56 changes: 56 additions & 0 deletions tests/Fixtures/Customers/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"customerNumber": 1,
"currency": "DKK",
"paymentTerms": {
"paymentTermsNumber": 1,
"self": "https:\/\/restapi.e-conomic.com\/payment-terms\/1"
},
"customerGroup": {
"customerGroupNumber": 1,
"self": "https:\/\/restapi.e-conomic.com\/customer-groups\/1"
},
"balance": 1300,
"dueAmount": 1300,
"creditLimit": 0,
"name": "John Doe Renamed",
"vatZone": {
"vatZoneNumber": 1,
"self": "https:\/\/restapi.e-conomic.com\/vat-zones\/2"
},
"layout": {
"layoutNumber": 14,
"self": "https:\/\/restapi.e-conomic.com\/layouts\/14"
},
"lastUpdated": "2021-12-29T08:24:42Z",
"contacts": "https:\/\/restapi.e-conomic.com\/customers\/1\/contacts",
"templates": {
"invoice": "https:\/\/restapi.e-conomic.com\/customers\/1\/templates\/invoice",
"invoiceLine": "https:\/\/restapi.e-conomic.com\/customers\/1\/templates\/invoiceline",
"self": "https:\/\/restapi.e-conomic.com\/customers\/1\/templates"
},
"totals": {
"drafts": "https:\/\/restapi.e-conomic.com\/invoices\/totals\/drafts\/customers\/1",
"booked": "https:\/\/restapi.e-conomic.com\/invoices\/totals\/booked\/customers\/1",
"self": "https:\/\/restapi.e-conomic.com\/customers\/1\/totals"
},
"deliveryLocations": "https:\/\/restapi.e-conomic.com\/customers\/1\/delivery-locations",
"invoices": {
"drafts": "https:\/\/restapi.e-conomic.com\/customers\/1\/invoices\/drafts",
"booked": "https:\/\/restapi.e-conomic.com\/customers\/1\/invoices\/booked",
"self": "https:\/\/restapi.e-conomic.com\/customers\/1\/invoices"
},
"eInvoicingDisabledByDefault": false,
"metaData": {
"delete": {
"description": "Delete this customer.",
"href": "https:\/\/restapi.e-conomic.com\/customers\/1",
"httpMethod": "delete"
},
"replace": {
"description": "Replace this customer.",
"href": "https:\/\/restapi.e-conomic.com\/customers\/1",
"httpMethod": "put"
}
},
"self": "https:\/\/restapi.e-conomic.com\/customers\/1"
}
26 changes: 26 additions & 0 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,29 @@
->paymentTerms->toBeInstanceOf(PaymentTerm::class)
->paymentTerms->paymentTermsNumber->toBe(1);
});

it('can update a customer', function () {
$this->driver->expects()->put()
->withArgs(function (string $url, array $body) {
return $url === 'https://restapi.e-conomic.com/customers/1'
&& $body === [
'customerNumber' => 1,
'name' => 'John Doe Renamed',
'self' => 'customers/1',
];
})
->once()
->andReturn(new EconomicResponse(200, fixture('Customers/update')));

$customer = new Customer([
'customerNumber' => 1,
'name' => 'John Doe Renamed',
]);

$updatedCustomer = $customer->save();

expect($updatedCustomer)
->toBeInstanceOf(Customer::class)
->customerNumber->toBe(1)
->name->toBe('John Doe Renamed');
});

0 comments on commit 2665637

Please sign in to comment.