diff --git a/README.md b/README.md index f462048..29efd33 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Resources/Customer.php b/src/Resources/Customer.php index 977f27e..4101b25 100644 --- a/src/Resources/Customer.php +++ b/src/Resources/Customer.php @@ -9,23 +9,26 @@ 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 */ - use Creatable, GetCollectionable, GetSingleable; + use Creatable, GetCollectionable, GetSingleable, Updatable; - public string $address; + public ?string $address; public Contact $attention; @@ -33,7 +36,7 @@ class Customer extends Resource public bool $barred; - public string $city; + public ?string $city; /** * @var EconomicCollection @@ -41,11 +44,11 @@ class Customer extends Resource #[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; @@ -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, @@ -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, + ]); + } } diff --git a/src/Traits/Resources/Updatable.php b/src/Traits/Resources/Updatable.php index 10e4504..e63019b 100644 --- a/src/Traits/Resources/Updatable.php +++ b/src/Traits/Resources/Updatable.php @@ -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()); } } diff --git a/tests/Fixtures/Customers/update.json b/tests/Fixtures/Customers/update.json new file mode 100644 index 0000000..9a6952e --- /dev/null +++ b/tests/Fixtures/Customers/update.json @@ -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" +} diff --git a/tests/Unit/CustomerTest.php b/tests/Unit/CustomerTest.php index 871e2c2..d9f667b 100644 --- a/tests/Unit/CustomerTest.php +++ b/tests/Unit/CustomerTest.php @@ -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'); +});