softwerkab / fortnox-csharp-api-sdk

.NET SDK for Fortnox API.
MIT License
53 stars 65 forks source link

Casting issue in Invoice-entity #263

Closed oscarhallbergcab closed 1 year ago

oscarhallbergcab commented 2 years ago

When calling the CreateInvoice-method, the property 'ContributionPercent' might contain the string value 'API_NORIGHT'. When parsed into the entity FortNox.SDK.Entities.Invoice this causes an exception: _Could not convert string to decimal: API_NORIGHT. Path 'Invoice.ContributionPercent', line 1, position 394._

The reason is that the 'ContributionPercent'-property is a decimal, and not a string. How can we get around this??!

P.S. I found a fix to a very similar problem in this (closed) Pull Request https://github.com/FortnoxAB/csharp-api-sdk/pull/240

richardrandak commented 2 years ago

Can you explain to me what the API_NORIGHT actually means? I would like to avoid making the fields strings just because of such values.. maybe there could be some de-serialization rule to map it to some integer constant?

oscarhallbergcab commented 2 years ago

I'm not 100% sure, but Fortnox API sometimes returns "API_NORIGHT" for some properties, basically it means that our integration does not have permission to read the property. A workaround in our case is to ask the client for the required permissions, but since we don't really need the 'ContributionPercent' that feels wrong.

A constant i.e. _decimal API_NORIGHT = -1 should solve the issue in this specific case, however I guess there might be more properties than "ContributionPercent" that might have the same behaviour.

richardrandak commented 1 year ago

I could solve it by de-serializing it to default value, so acting like it is not sent from the server at all

oscarhallbergcab commented 1 year ago

Yes, that also sounds like a good solution I think!

CaspecoAholml commented 1 year ago

We have solved this issue. This is the answer in Swedish from support "Hej,

Problemet med att contributionpercent och contributionvalue är en sträng och har värdet “API_NORIGHT” är för att ni inte har behörigheter för se täckningsbidrag och täckningsgrad i Fortnox. Då retunrar vi inte värdet som en Float utan retunerar att ni inte har rättigheter att se värdet.

Be användaren som aktiverade kopplingen att uppdatera sina behörigheter i Fortnox till det ni önskar jobba med och sedan gör om kopplingen.

Mer information om Täckningsbidrag/Täckningsgrad går att läsa om här: https://support.fortnox.se/ordlista-fakturering/tackningsbidrag-tackningsgrad-tb-tg-ordlista?from_search=108332621 https://support.fortnox.se/behorigheter-i-fortnox-program/behorigheter-i-fortnox-fakturering?from_search=108332621"

JakobHaggCAB commented 1 year ago

We talked to Fortnox support and they insist this is the correct behavior (sending API_NORIGHT as string) so it's up to the client to handle this for all properties that can receive this value. I made a PR that should solve this issue for all entities. BUT this will make is harder for developers to identify when customers are missing access that they need, but imo this is better than forcing everyone to buy all licenses.

oscarhallbergcab commented 1 year ago

Fixed by @JakobHaggCAB. Closing the issue.