<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../database.php';

class venipak_update extends database
{
    public function updateTable($data)
    {
        foreach($data AS $pickup)
        {
            if(!$this->fetchRow('tb_pickup_points', ['cl_courier_provider' => 'Venipak', 'cl_country' => $pickup['cl_country'], 'cl_postcode' => $pickup['cl_postcode'], 'cl_code' => $pickup['cl_code']]))
            {
                $this->insert('tb_pickup_points', $pickup);
                $fp = fopen('venipak_update_text.txt', 'a');
                fwrite($fp, 'Inserted ' . $pickup['cl_name'] . '<br/>');  
                fclose($fp); 
            }
        }
    }
}


$url = 'https://go.venipak.lt/ws/get_pickup_points';
$data = json_decode(file_get_contents($url));

$insertArray = [];
$count = 0;
foreach($data AS $each)
{
    $insertResult[$count]['cl_name'] = $each->name;
        $insertResult[$count]['cl_address1'] = $each->name;
        $insertResult[$count]['cl_address2'] = $each->address;
        $insertResult[$count]['cl_city'] = $each->city;
        $insertResult[$count]['cl_postcode'] = $each->zip;
        $insertResult[$count]['cl_country'] = $each->country;;
        $insertResult[$count]['cl_courier_provider'] = 'Venipak';
         $insertResult[$count]['cl_code'] = $each->code;
        $count++;
        
        
}

$obj = new venipak_update(); 
$obj->updateTable($insertResult);