<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
//$csvAsArray = array_map('str_getcsv', file('locations_omniva.csv'));


require_once '../database.php';
$count = 1;
        
$csvfile = fopen('terminals.csv','r');


class PickupUpdate extends database
{
    public function getAllOmnivaPickup()
    {
        return $this->fetchAll('tb_pickup_points', ['cl_courier_provider' => 'OMNIVA']);
    }
    
    public function updateDatabase($insertResults)
    {
        
        $count = 1;
        foreach($insertResults AS $insertResult)
        {
            $this->insert('tb_pickup_points',$insertResult);
            
             $fp = fopen('terminal_pickup_update_text.txt', 'a');
            fwrite($fp, 'Inserted ' . ($count) . '<br/>');  
            fclose($fp); 
            
            $count++;
            
            $count++;
        }
        
    }
}

$multiarray = [];
$terminalsLocationsFullArray = [];
$count = 0;
while ($csvAsArray = fgetcsv($csvfile)) {
   if($count >= 1)
   {
    $multiarray[] = $csvAsArray;
   }
   $count++;
   
}

$count = 0;
$nameArray = [];
foreach($multiarray AS $each)
{
    if($count >= 1)
    {
        $name = str_replace('"', '', $each[1]);
        $name = str_replace("'", '', $name);
        $nameArray[] = trim($name);
        
        $terminalsLocationsFullArray[] = $each;
    }
    $count++;
}


$obj = new PickupUpdate();

$insertResult = [];

//print_r($omnivaLocationsFullArray);
$count = 0;
foreach($terminalsLocationsFullArray AS $each)
{
    $name = str_replace('"', '', $each[2]);
    
       
        $postcode = str_replace('"', '', $each[5]);
        
        
        $country = str_replace('"', '', $each[1]);
        $city = str_replace('"', '', $each[3]);
        $a2name = str_replace('"', '', $each[4]);
        
        
        
        $insertResult[$count]['cl_name'] = $name;
        $insertResult[$count]['cl_address1'] = $city;
        $insertResult[$count]['cl_address2'] = $a2name;
        $insertResult[$count]['cl_city'] = $city;
        $insertResult[$count]['cl_postcode'] = $postcode;
        $insertResult[$count]['cl_country'] = $country;
        $insertResult[$count]['cl_courier_provider'] = 'TERMINAL';
        
        
        $count++;
    
}


$obj->updateDatabase($insertResult);