0
votes

I am adding a new line item to the Existing invoice using QuickBook ONline PHP SDK (Latest version) but, for this, I used a sample script from QUickBOOK documentation but this always returning a below error: Cannot use object of type QuickBooksOnline\API\Data\IPPLine as an array

Here is my code:- `

require_once("vendor/autoload.php");
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2LoginHelper;
use QuickBooksOnline\API\Facades\Invoice;
use QuickBooksOnline\API\Facades\Line;

// Here is my OAuth token code

$targetInvoiceArray = $dataService->Query("select * from Invoice where DocNumber='7001'");
if(!empty($targetInvoiceArray) && sizeof($targetInvoiceArray) == 1){
$theInvoice = current($targetInvoiceArray);
}

$LineObj = Line::create([
"Description" => "Discount for Alex",
"Amount" => -10.00,
"DetailType" => "SalesItemLineDetail",
"SalesItemLineDetail" => [
"ItemRef" => [
"value" => 21,
"name" => "Discount"
]
]
]);
//Remove last element of Line Item.
$lineArray = array_pop($theInvoice->Line);

$lineArray[] = $LineObj;
// $lineArray.push($LineObj);
$updatedInvoice = Invoice::update($theInvoice, [
"sparse" => true,
"Line" => $lineArray
]);
$updatedResult = $dataService->Update($updatedInvoice);

`

Script throwing an error at this line $lineArray[] = $LineObj;

1

1 Answers

0
votes

After Lot of debugging I found that there is a bug in QuickBooks PHP SDK sample code. I resolved the issue by changing a few lines of code:

Replace the below lines:

$lineArray[] = $LineObj;
$updatedInvoice = Invoice::update($theInvoice, [
  "sparse" => true,
  "Line" => $lineArray
]);

With this code

$theInvoice->Line[] = $LineObj;
$updatedInvoice = Invoice::update($theInvoice, [
   "sparse" => true
]);