asciisd / zoho

Zoho package for Laravel
36 stars 37 forks source link

Getting Error #34

Closed debindra closed 2 years ago

debindra commented 2 years ago

Hi there,

I tried to run below code I'm getting error $products = ZohoManager::useModule('Products')->getModuleInstance()->getRecords(['page' => $page, 'per_page' => 200])->getResponseJSON();

Error

message: "Undefined offset: 1", exception: "ErrorException",…} exception: "ErrorException" file: "/Applications/MAMP/htdocs/yamron/vendor/zohocrm/php-sdk-archive/src/crm/api/response/CommonAPIResponse.php" line: 105 message: "Undefined offset: 1"

I'm using php 7.3.11 but for other team mate, working this code very well.

If you need more, plz let me know.

aemaddin commented 2 years ago

That's mean $page variable is undefined, please check if $page has value or not.

debindra commented 2 years ago

Here is full function.


 public function inventoryList($sync = false, $page = 1)
    {

        if (InventItem::exists() && !$sync) {
            if (Request()->query('keyword')) {
                return InventItem::where('Product_Code', 'like', Request()->query('keyword'))->paginate(10);
            } else {
                return InventItem::paginate(10);
            }
        } else {

            set_time_limit(0);
            $products = ZohoManager::useModule('Products')->getModuleInstance()->getRecords(['page' => $page, 'per_page' => 200])->getResponseJSON();
            if (count($products['data']) > 0) {
                foreach ($products['data'] as $item) {
                    // if ($item['Product_Active'] == true) {
                    //     $statusPrdouct = 'Active';
                    // }
                    // if($item['Product_Active'] == false) {
                    //     $statusPrdouct = 'InActive';
                    // }
                    InventItem::updateOrCreate(['zoho_id' => $item['id']], [
                        // 'item_id' => $user['item_id'],
                        'zoho_id' => $item['id'],
                        'brand' => $item['Product_Brand'],
                        'Product_Code' => $item['Product_Code'],
                        'sku' => $item['Product_Code'],
                        'Product_Name' => $item['Product_Name'],
                        'status' => $item['Product_Active'],
                        //                    'quantity' => $item['Qty_in_Stock'],
                        'description' => $item['Description'] ?? '--',
                        'item_type' => 'inventory items',
                        'unit' => $item['Unit_Price'],
                        'Manufacturer' => $item['Manufacturer'] ?? '--',
                        'brand' => $item['Manufacturer'] ?? '--',
                        'purchase_type' => $item['Purchase_Type'] ?? '--',
                        'Product_Category' => $item['Product_Category'] ?? '--',
                        'Item_Serial_Number' => $item['Item_Serial_Number'] ?? '--',
                        'photographed' => $item['Photographed'],
                        'Cost' => $item['Cost'],
                        'currency_symbol' => $item['$currency_symbol'],
                        'minimum_sale_price' => $item['Minimum_Sale_Price'] ?? '--',
                        'type' => $item['Type'] ?? '--',
                        'weight' => $item['Weight'] ?? '--',
                        'clarity' => $item['Clarity'] ?? '--',
                        'shape' => $item['Shape'] ?? '--',
                        'color' => $item['Color'] ?? '--',
                        // 'selling_price' => $user['selling_price'],
                        'stock_locations' => $item['Current_Location'],
                        'item_pictures' => $item['Record_Image'] ?? '--',
                    ]);
                }

                if ($products['info']['more_records']) {
                    $page = $page + 1;
                    $this->inventoryList(1, $page);
                    $count = \DB::table('invent_items')->count();
                    return InventItem::paginate($count);
                } else {
                    return InventItem::paginate(200);
                }
            }
        }
    }
aemaddin commented 2 years ago

try this $page ?? 1 on page param to check if $page is empty return first page

ZohoManager::useModule('Products')
  ->getModuleInstance()
  ->getRecords(['page' => $page ?? 1, 'per_page' => 200])
  ->getResponseJSON();
debindra commented 2 years ago
Screen Shot 2022-01-03 at 19 41 48

@aemaddin

aemaddin commented 2 years ago

Aha, ok that's because you working on PHP 7.3, so you can change it to

ZohoManager::useModule('Products')
  ->getModuleInstance()
  ->getRecords(['page' => $page ? $page : 1, 'per_page' => 200])
  ->getResponseJSON();
debindra commented 2 years ago

@aemaddin, Now it's working as I upgraded to php version 7.4.21. For above your code, I put static value as 1 as but it did not work on 7.3.

aemaddin commented 2 years ago

maybe you can put all your code in "if" statement to make sure that $page variable has value then you do your code without need to check on ZohoManager on PHP 7.3.

if($page) {
    ZohoManager::useModule('Products')
      ->getModuleInstance()
      ->getRecords(['page' => $page, 'per_page' => 200])
      ->getResponseJSON();
}
debindra commented 2 years ago

Okay, will try.