Create Sales Invoice: Status code 400

Hi,

I’m trying to create a new Sales Invoice through the old api with the following data

$invoice = [
  'IssueDate' => '2024-09-30',
  'Reference' => '<reference>',
  'Customer' => $customerId, // uuid
  'BillingAddress' => '<billing address>',
  'Lines' => [],
  'LineDescription' => true,
  'AmountsIncludeTax' => true,
  'CustomFields' => [],
];

foreach ($lines as $line) {
  $invoice['Lines'][] = [
    'Account' => self::SALES_INVOICES_LINE_ACCOUNT, // uuid
    'Description' => '<description>',
    'CustomFields' => [],
    'Qty' => self::SALES_INVOICE_QTY, // float
    'UnitPrice' => $price, // float
    'TaxCode' => self::TAX_CODE, // uuid
  ];
}

I POST /api/<id>/<uuid>.json the $invoice data but I keep getting a status code 400. I’ve search through this forum and google but so far no luck finding an answer to as why I keep getting 400 Bad Request.

Maybe you can get an idea from this sample code

<?php
function encodeBase64($data) {
    return base64_encode($data);
}

$url = 'https://{{subdomain}}.manager.io/api/Tm90aGluZw/ad12b60b-23bf-4421-94df-8be79cef533e.json';
$username = 'administrator';
$password = 'yourpassword';
$data = [
    "key" => "03efec58-6727-4cd0-a17e-85d053542c0e",
    "IssueDate" => "2024-10-03",
    "Reference" => "1",
    "Customer" => "fe2d3d05-ba77-411e-a486-e41b9ae3e565",
    "Description" => "Day 3",
    "Lines" => [
        [
            "Account" => "57d2142d-f9ff-41af-ab47-a93a1e045eb2",
            "CustomFields2" => new stdClass(),
            "SalesUnitPrice" => 80
        ]
    ],
    "CustomFields2" => new stdClass()
];

$options = [
    'http' => [
        'header'  => [
            "Content-type: application/json",
            "Authorization: Basic " . encodeBase64($username . ":" . $password)
        ],
        'method'  => 'POST',
        'content' => json_encode($data),
    ],
];

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

if ($result === FALSE) {
    die('Error occurred');
}

var_dump($result);
?>

or you can try using api2 for this purpose

1 Like

I appreciate the quick response, thank you!

After reviewing your example I noticed that you added "CustomFields2" => ... instead of "CustomFields" => .... I tried to remove the CustomFields entirely and now the invoice is created successfully.

Any idea why "CustomFields" => [] makes the request fail?

CustomFields and CustomFields2 is work well,

$data = [
    "IssueDate" => "2024-10-15",
    "Reference" => "2",
    "Customer" => "fe2d3d05-ba77-411e-a486-e41b9ae3e565",
    "Description" => "TEST CF IN SALES",
    "Lines" => [
        [
            "Item" => "3b36d138-4eae-4082-8c60-2b71810d370e",
            "CustomFields" => new stdClass(),
            "CustomFields2" => [
                "Strings" => [
                    "d02b03b8-3233-4fcd-b29c-d933e5ed1b52" => "CF 02"
                ]
            ],
            "Qty" => 1,
            "SalesUnitPrice" => 1000
        ],
        [
            "Item" => "3b36d138-4eae-4082-8c60-2b71810d370e",
            "CustomFields" => new stdClass(),
            "CustomFields2" => [
                "Strings" => [
                    "d02b03b8-3233-4fcd-b29c-d933e5ed1b52" => "CF 02"
                ]
            ],
            "Qty" => 2,
            "SalesUnitPrice" => 2000
        ]
    ],
    "HasLineNumber" => true,
    "SalesInvoiceFooters" => [],
    "CustomFields" => [
        "ab8d83a0-2f07-4c93-91f2-433619efb529" => "OLD-CF 01"
    ],
    "CustomFields2" => [
        "Strings" => [
            "2a31d119-8681-4eca-96de-f34d04094e08" => "CF 01"
        ]
    ]
];

2 Likes

I guess maybe it is because my empty CustomFields was an array which gets encoded as an array and not an object like stdClass?

I don’t know, I don’t understand how this works. I just asked the AI ​​copilot to generate the code, and it works.

I see, but it works. Thank you so much for helping me solving my issue :slight_smile: