invoiceninja / sdk-php

PHP wrapper for Invoice Ninja's REST API
https://www.invoiceninja.com
83 stars 42 forks source link

Update of in_stock_quantity variable is not possible #101

Closed alandolt closed 1 year ago

alandolt commented 1 year ago

Hello First thanks a lot for your work on invoiceninja. Everything seems to work, however, when updating the in_stock_quantity variable of a product over the sdk, the variable won't get updated, no matter what I try. I can update any other variable using the PHP SDK, however not the in_stock_quantity . The code I am using is the following:

<?php
require __DIR__ . '/vendor/autoload.php';
use InvoiceNinja\Sdk\InvoiceNinja;

$ninja = new InvoiceNinja("MyAPIKey");
$ninja->setUrl("MyURL");
$product = $ninja->products->update("ProductIDIwanttoUpdate", ['in_stock_quantity' => 77]);

When printing the array of possible variables, the in_stock_quantity variable is present, so I think it should also be possible to update it over the SDK. .

(
    [id] => Opnel5aKBz
    [user_id] => VolejRejNm
    [assigned_user_id] =>
    [product_key] => My product
    [notes] => Notes
    [cost] => 0
    [price] => 50
    [quantity] => 1
    [tax_name1] =>
    [tax_rate1] => 0
    [tax_name2] =>
    [tax_rate2] => 0
    [tax_name3] =>
    [tax_rate3] => 0
    [created_at] => 1675106214
    [updated_at] => 1675114179
    [archived_at] => 0
    [custom_value1] =>
    [custom_value2] =>
    [custom_value3] =>
    [custom_value4] =>
    [is_deleted] =>
    [in_stock_quantity] => 10
    [stock_notification] => 1
    [stock_notification_threshold] => 0
    [documents] => Array
        (
        )

Is there anything wrong with my code or is there a bug? Thanks a lot for any answer.

turbo124 commented 1 year ago

@alandolt

Your code is fine, in_stock_quantity is a protected property that the system updates. We block updates on this property in case it is inadvertantly changed.

you can pass an override if you want to update this property, simply add

update_in_stock_quantity=true

ie

$product = $ninja->products->update("ProductIDIwanttoUpdate", ['in_stock_quantity' => 77, 'update_in_stock_quantity' => 'true']);

And the property will update.

alandolt commented 1 year ago

Thank you very much, the proposed snippet works without problem. Maybe a short reasoning why I want to update the in stock quantity of products. I want to be able to sell kits, consisting of multiple sub products (items). The invoice should then only consist of the kit, however, in my inventory I should have the individual items a kit consist of. My idea is now to automatically update the available number of kits based on the in stock quantity of the individual items a kit consist of by updating its number directly over the API.

turbo124 commented 1 year ago

@alandolt

That makes sense when using it in that context. :+1: