InvenTree icon indicating copy to clipboard operation
InvenTree copied to clipboard

How to obtain output id for `/api/build/$buildId/complete/` endpoint?

Open jack-fdrv opened this issue 1 year ago • 7 comments

Please verify that this bug has NOT been raised before.

  • [x] I checked and didn't find a similar issue

Describe the bug*

To use endpoint /api/build/$buildId/complete/ we need to provide output ids. But there is no way from docs to obtain output ids some how. It should be returned when we create it with endpoint /api/build/$buildId/create-output/

Steps to Reproduce

  1. Create build with POST to /api/build/
  2. Then create build output with POST to '/api/build/$buildId/create-output/'
  3. Then try to complete the build with POST to /api/build/$buildId/complete/

Expected behaviour

From step 2 we should obtain output id which later we can use in completing build request /api/build/$buildId/complete/ which required output id.

Deployment Method

  • [ ] Docker
  • [ ] Package
  • [ ] Bare metal
  • [ ] Other - added info in Steps to Reproduce

Version Information

Version Information:

InvenTree-Version: 0.16.3 Django Version: 4.2.15 Commit Hash: b0b05e4 Commit Date: 2024-09-21

Database: postgresql Debug-Mode: False Deployed using Docker: True Platform: Linux-6.8.0-1019-aws-x86_64-with Installer: DOC

Active plugins: [{'name': 'InvenTreeBarcode', 'slug': 'inventreebarcode', 'version': '2.1.0'}, {'name': 'InvenTreeCoreNotificationsPlugin', 'slug': 'inventreecorenotificationsplugin', 'version': '1.0.0'}, {'name': 'InvenTreeCurrencyExchange', 'slug': 'inventreecurrencyexchange', 'version': '1.0.0'}, {'name': 'InvenTreeLabel', 'slug': 'inventreelabel', 'version': '1.1.0'}, {'name': 'InvenTreeLabelMachine', 'slug': 'inventreelabelmachine', 'version': '1.0.0'}, {'name': 'InvenTreeLabelSheet', 'slug': 'inventreelabelsheet', 'version': '1.0.0'}, {'name': 'DigiKeyPlugin', 'slug': 'digikeyplugin', 'version': '1.0.0'}, {'name': 'LCSCPlugin', 'slug': 'lcscplugin', 'version': '1.0.0'}, {'name': 'MouserPlugin', 'slug': 'mouserplugin', 'version': '1.0.0'}, {'name': 'TMEPlugin', 'slug': 'tmeplugin', 'version': '1.0.0'}]

Please verify if you can reproduce this bug on the demo site.

  • [ ] I can reproduce this bug on the demo site.

Relevant log output

[23-Nov-2024 03:48:03 UTC] trying to create buildArray
(
    [reference] => BO-4850
    [part] => 62
    [quantity] => 1
    [sales_order] => 761
    [create_child_builds] => 1
    [take_from] => 1
    [destination] => 1
    [priority] => 0
    [target_date] => 2024-11-23
)

[23-Nov-2024 03:48:04 UTC] ##  create build response: 201 - stdClass Object
(
    [pk] => 134
    [url] => /build/134/
    [title] => 
    [barcode_hash] => 
    [batch] => 
    [creation_date] => 2024-11-23
    [completed] => 0
    [completion_date] => 
    [destination] => 1
    [parent] => 
    [part] => 62
    [part_name] => Some name
    [project_code] => 
    [project_code_detail] => 
    [reference] => BO-4850
    [sales_order] => 761
    [quantity] => 1
    [status] => 10
    [status_text] => Pending
    [target_date] => 2024-11-23
    [take_from] => 1
    [notes] => 
    [link] => 
    [issued_by] => 
    [issued_by_detail] => 
    [responsible] => 
    [responsible_detail] => 
    [priority] => 0
)

[23-Nov-2024 03:48:04 UTC] -- run api createBuildOutput
[23-Nov-2024 03:48:05 UTC] ##  create build output response: 201 - stdClass Object
(
    [quantity] => 1.00000
    [batch_code] => BO-4850
    [location] => 1
    [auto_allocate] => 1
)

[23-Nov-2024 03:48:05 UTC] Build output result: stdClass Object
(
    [quantity] => 1.00000
    [batch_code] => BO-4850
    [location] => 1
    [auto_allocate] => 1
)

[23-Nov-2024 03:48:05 UTC] -- run api completeBuild
[23-Nov-2024 03:48:05 UTC] ##  complete build response: 400 - stdClass Object
(
    [outputs] => Array
        (
            [0] => stdClass Object
                (
                    [output] => Array
                        (
                            [0] => Build output does not match the parent build
                        )

                )

        )

)

jack-fdrv avatar Nov 23 '24 03:11 jack-fdrv

Image

jack-fdrv avatar Nov 23 '24 06:11 jack-fdrv

I was able to find it, but guys... we need to include id in response.... Have an extra api call for each build is not good...

/**
     * Get stock item created by build output
     * @param int $buildId Build ID
     * @param string $batchCode Build batch code
     * @param int $partId Part ID
     * @return int|null Stock item ID (output ID) or null if not found
     */
    function getBuildStockItem($buildId, $batchCode, $partId) {
        $endpoint = $this->inventree_url . "/api/stock/?build=" . $buildId . "&part=" . $partId . "&batch=" . $batchCode;

        error_log('##  get stock item endpoint: '. $endpoint);
        
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $endpoint,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                "Authorization: Token $this->token",
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        error_log('##  get stock item response: '. $statusCode . ' - ' . print_r(json_decode($response), 1));
        
        $data = json_decode($response);
        
        if ($data && is_array($data) && count($data) > 0) {
            // Return the ID of the first matching stock item
            return $data[0]->pk;
        }
        
        return null;
    }

jack-fdrv avatar Nov 23 '24 06:11 jack-fdrv

@jack-fdrv glad you got it worked out. If you feel that the API could be improved, we happily accept PRs 👍

SchrodingersGat avatar Nov 23 '24 07:11 SchrodingersGat

This issue seems stale. Please react to show this is still important.

github-actions[bot] avatar Jan 22 '25 11:01 github-actions[bot]

@jack-fdrv are you willing to work on this?

SchrodingersGat avatar Jan 22 '25 11:01 SchrodingersGat

This issue seems stale. Please react to show this is still important.

github-actions[bot] avatar Mar 24 '25 11:03 github-actions[bot]

Not stale

SchrodingersGat avatar Mar 24 '25 11:03 SchrodingersGat