picqer / moneybird-php-client

PHP Client for Moneybird V2
MIT License
82 stars 77 forks source link

Custom/details fields #3

Closed godforhire closed 8 years ago

godforhire commented 8 years ago

I ran into the problem that custom fields for contacts were not saved in Moneybird. The only way I could get it to work was to change 'custom_fields' to 'custom_fields_attributes' in Entities/Contact.php and then set them via $contact->custom_fields_attributes = array(0 => ['id' => CF_ID1, 'value' => 'Some value'], 1 => ['id' => CF_ID2, 'value' => 'Some other value']);

Similary, details were not added to invoices generated through the API until I changed 'details' to 'details_attributes' in Entities/SalesInvoice.php.

Lastly, to save the custom fields for contacts,I had to add JSON_FORCE_OBJECT to json_encode(...) in Model.php, public function jsonWithNamespace().

This may not be the best solution, but it works for me.

stephangroen commented 8 years ago

The custom_fields for contacts were not implemented yet in the client. They should work the same as details for a sales invoice. I'll start to work on it, should be ready soon.

You will then be able to do the following:

<?php

$contact = $moneybird->contact();

$contact->company_name = 'WhatEvery';

$customField = $moneybird->contactCustomField();
$customField->id = 142467688176813879;
$customField->value = 'Tomorrow';

$customFields = [];
$customFields[] = $customField;

$contact->custom_fields = $customFields;
$contact->save();
stephangroen commented 8 years ago

Fixed in release v0.1.1. Should work now as mentioned above.

stephangroen commented 8 years ago

@godforhire The client will automatically change details to details_attributes when you create or patch a sales invoice, and do the same for custom_fields for contacts.

They shouldn't both be encoded with JSON_FORCE_OBJECT, because the Moneybird API doesn't work that way. They need details_attributes as an array with objects, but custom_fields_attributes as an object with objects. Release v0.1.1 fixes all this.

godforhire commented 8 years ago

Thanks a lot, I'll update to the latest release today.