update warehouse id for closed credit notes

update warehouse id for closed credit notes

the code run successfully but its not updating the warehouse,
i send payload of warehouse id and name ,then tried to clone exisitng with updated fields .. lost one week in this..
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Libraries\ZohoConnector;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;

class CreditNoteController extends Controller
{
    private $zoho_client;

    public function __construct()
    {
        // Initialize Zoho connector
        $this->zoho_client = new ZohoConnector('');
    }

    public function updateCreditNotesFromCSV()
    {
        // Refresh Zoho token
        $this->zoho_client->refreshToken();
        
        // Path to CSV containing credit note IDs
        $csvPath = base_path('storage/zoho_credits.csv');

        if (!file_exists($csvPath)) {
            return response()->json(["error" => "CSV file not found at: $csvPath"]);
        }

        $csvContents = file_get_contents($csvPath);
        $rows = explode("\n", $csvContents);

        foreach ($rows as $row) {
            $creditnote_id = trim($row);

            if (!empty($creditnote_id)) {
                try {
                    // Fetch the credit note details from Zoho
                    $response = $this->zoho_client->getZoho("creditnotes", $creditnote_id);
                    Log::info("GET Response for Credit Note ID $creditnote_id: ", (array)$response);

                    if (isset($response->creditnote)) {
                        $creditnote = clone $response->creditnote;  // Clone the credit note to avoid directly modifying original response

                        echo "Items count: " . count($creditnote->line_items) . PHP_EOL;

                        // Loop through each line item and update the warehouse details
                        for ($i = 0; $i < count($creditnote->line_items); $i++) {
                            echo env("ORDER_WAREHOUSE_ID") . PHP_EOL;

                            // Update warehouse_id and warehouse_name for each line item
                            $creditnote->line_items[$i]->warehouse_id = "111";
                            $creditnote->line_items[$i]->warehouse_name = "abc";

                            echo $creditnote->line_items[$i]->warehouse_id . PHP_EOL;
                        }

                        // Send the updated credit note to Zoho via PUT request
                        Log::info("Updating Credit Note with Payload: ", ['payload' => $creditnote]);
                        $updateResponse = $this->zoho_client->updateCreditNote(json_encode($creditnote), $creditnote_id);

                        Log::info("PUT Response for Credit Note ID $creditnote_id: ", (array)$updateResponse);

                        // Determine update status and display the result
                        $updateStatus = isset($updateResponse->code) && $updateResponse->code == 0 ? "success" : "failed";
                        echo "Credit Note ID $creditnote_id update $updateStatus.\n";

                    } else {
                        echo "Failed to fetch Credit Note ID $creditnote_id.\n";
                    }
                } catch (\Exception $ex) {
                    Log::error("Error processing Credit Note ID $creditnote_id: " . $ex->getMessage());
                    echo "Error updating Credit Note ID $creditnote_id: " . $ex->getMessage() . "\n";
                }
            }
        }
    }
}