#!/usr/bin/env php
<?php
//$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
//require("/Library/Server/Web/Data/Sites/SE-production-git/SE/se-lib/bootstrap.php");
require("config_inc.php");

date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions


$queries[] = "create temporary table BI_audit_KW_requested_person_temp select * from BI_audit_KW_requested_person_copy";
$queries[] = "truncate table `CRM__#REF_TABLE__24`";
$queries[] = "insert into `CRM__#REF_TABLE__24` (PRIMARY_KEY, REMOTE_PRIMARY_KEY) select Id_BI_audit_KW_requested, ID from BI_audit_KW_requested_person_temp";
$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person = trim(Seller_person)";
$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_pesel = null where Seller_person_pesel = 0";
$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_KRS = null where Seller_person_KRS = 0";
$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_NIP = null where Seller_person_NIP = 0";
$queries[] = "update BI_audit_KW_requested_person_temp set Seller_person_REGON = null where Seller_person_REGON = 0";

foreach ($queries as $query) DB::getPDO()->query($query);

$query = "select Seller_person, Seller_person_pesel, Seller_person_KRS, Seller_person_NIP, Seller_person_REGON, count(*) as c from BI_audit_KW_requested_person_temp group by Seller_person, Seller_person_pesel, Seller_person_KRS, Seller_person_NIP, Seller_person_REGON having c>1";
$duplicates = DB::getPDO()->fetchall($query);

$itemsCount = 0; $companiesCount = 0;
try{
	foreach ($duplicates as $duplicate) {
		$query = "select ID from BI_audit_KW_requested_person_temp where Seller_person='" . addslashes($duplicate['Seller_person']) . "'" .
			" and Seller_person_pesel" . (($duplicate['Seller_person_pesel'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_pesel']) . "'") .
			" and Seller_person_KRS" . (($duplicate['Seller_person_KRS'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_KRS']) . "'") .
			" and Seller_person_NIP" . (($duplicate['Seller_person_NIP'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_NIP']) . "'") .
			" and Seller_person_REGON" . (($duplicate['Seller_person_REGON'] === null) ? " is null" : "='" . addslashes($duplicate['Seller_person_REGON']) . "'") .
			" order by ID";
		$items = DB::getPDO()->fetchall($query);
		$id = $items[0]['ID'];
		for ($i = 1; $i < count($items); $i++) {
			DB::getPDO()->query("update `CRM__#REF_TABLE__24` set REMOTE_PRIMARY_KEY={$id} where REMOTE_PRIMARY_KEY={$items[$i]['ID']}");
			DB::getPDO()->query("delete from BI_audit_KW_requested_person_temp where ID={$items[$i]['ID']}");
			$itemsCount++;
		}
		if ($items) $companiesCount++;
	}
} catch (Exception $e) {
	echo "{$e->getMessage()}\n{$query}\n";
}
$i = 1;
$results = DB::getPDO()->fetchall("select ID from BI_audit_KW_requested_person_temp order by ID");
foreach ($results as $result) {
	if ($result["ID"] != $i) {
		DB::getPDO()->query("update BI_audit_KW_requested_person_temp set ID={$i} where ID={$result["ID"]}");
		DB::getPDO()->query("update `CRM__#REF_TABLE__24` set REMOTE_PRIMARY_KEY={$i} where REMOTE_PRIMARY_KEY={$result["ID"]}");
	}
	$i++;
}
DB::getPDO()->query("ALTER TABLE `BI_audit_KW_requested_person_temp` ORDER BY `ID`");
DB::getPDO()->query("ALTER TABLE `BI_audit_KW_requested_person_temp` auto_increment = {$i} ROW_FORMAT = DYNAMIC");
DB::getPDO()->query("ALTER TABLE `BI_audit_KW_requested_person_temp` DROP COLUMN Id_BI_audit_KW_requested");
DB::getPDO()->query("truncate table `BI_audit_KW_requested_person`");
DB::getPDO()->query("insert into `BI_audit_KW_requested_person` select * from `BI_audit_KW_requested_person_temp`");

echo "Reduced {$itemsCount} entries for {$companiesCount} of " . count($duplicates) . " entries.\n";
