<?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('locations_omniva.csv','r');


class PickupUpdate extends database
{
    public function getAllOmnivaPickup()
    {
        return $this->fetchAll('tb_pickup_points', ['cl_courier_provider' => 'OMNIVA']);
    }
    
    public function updateDatabase($deleteResults, $insertResults)
    {
        $count = 1;
        foreach($deleteResults AS $deleteResult)
        {
          
            $this->update('tb_pickup_points',['cl_deleted_at' => date('Y-m-d H:i:s')], ['cl_name' => $deleteResult]);
             $fp = fopen('omniva_pickup_update_text.txt', 'a');
                                                //opens file in append mode  
            fwrite($fp, 'Deleted ' . ($count) . '<br/>');  
            fclose($fp); 
            
            $count++;
        }
        
        $count = 1;
        foreach($insertResults AS $insertResult)
        {
            $this->insert('tb_pickup_points',$insertResult);
            
             $fp = fopen('omniva_pickup_update_text.txt', 'a');
            fwrite($fp, 'Inserted ' . ($count) . '<br/>');  
            fclose($fp); 
            
            $count++;
            
            $count++;
        }
        
    }
}

$multiarray = [];
$omnivaLocationsFullArray = [];
while ($csvAsArray = fgetcsv($csvfile)) {
   
   $multiarray[] = explode(';', $csvAsArray[0]);
   
}

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



$obj = new PickupUpdate();
$omnivaPickups = $obj->getAllOmnivaPickup();

$nameDatabaseArray = [];
foreach($omnivaPickups AS $each)
{
    $nameDatabaseArray[] = $each['cl_name'];
}

//print_r($nameArray);

//echo '<br/><br/>';
//print_r($nameDatabaseArray);


$deleteResult = array_diff($nameDatabaseArray,$nameArray);
$insertResultName = array_diff($nameArray, $nameDatabaseArray);
$insertResult = [];

//print_r($omnivaLocationsFullArray);
$count = 0;
foreach($omnivaLocationsFullArray AS $each)
{
    $name = str_replace('"', '', $each[1]);
    if(in_array($name, $insertResultName))
    {
       
        $postcode = str_replace('"', '', $each[0]);
        
        
        $country = str_replace('"', '', $each[3]);
        $a1name = str_replace('"', '', $each[4]);
        $a2name = str_replace('"', '', $each[5]);
        $a3name = str_replace('"', '', $each[6]);
        $a4name = str_replace('"', '', $each[7]);
        $a5name = str_replace('"', '', $each[8]);
        $a6name = str_replace('"', '', $each[9]);
        $a7name = str_replace('"', '', $each[10]);
        $a8name = str_replace('"', '', $each[11]);
        
        
        $insertResult[$count]['cl_name'] = $name;
        $insertResult[$count]['cl_address1'] = $a1name . ',' . $a2name . ',' . $a3name . ',' . $a4name;
        $insertResult[$count]['cl_address2'] = $a5name . ',' . $a6name . ',' . $a7name . ',' . $a48ame;
        $insertResult[$count]['cl_city'] = $a2name;
        $insertResult[$count]['cl_postcode'] = $postcode;
        $insertResult[$count]['cl_country'] = $country;
        $insertResult[$count]['cl_courier_provider'] = 'OMNIVA';
        
        $count++;
    }
}


$obj->updateDatabase($deleteResult, $insertResult);