<?php 
ini_set('display_errors',1);
$db=$GLOBALS['db'];
function escape($value) {
    $return = '';
    for($i = 0; $i < strlen($value); ++$i) {
        $char = $value[$i];
        $ord = ord($char);
        if($char !== "'" && $char !== "\"" && $char !== '\\' && $ord >= 32 && $ord <= 126)
            $return .= $char;
        else
            $return .= '\\x' . dechex($ord);
    }
    return $return;
}
if($_GET['record']!=''){
    // id nie jest puste? jedziemy i szukamy accountsa ... 
    $r=$db->query("select contact_id,id,date_modified,deleted from accounts_contacts where account_id='".$_GET['record']."'");
    
    if($r->num_rows>0){
        // o jest kontakt!
        while($row=$db->fetchByAssoc($r)){
            // wybieranie ścierwa
            $ct=$db->query("select * from contacts where id='".$row['contact_id']."'");

            while($row2=$db->fetchByAssoc($ct)){
         
                // przenoszenie ścierwa ...
            	$db->query("delete from contactleads  where id='".$row2['id']."'");
                $db->query("INSERT INTO `contactleads` VALUES('".implode("','",array_map('escape', $row2))."')");
          
               // echo "INSERT INTO `contacts` VALUES('".implode("','",$row2)."')";
                // usuwanie starego ścierwa ...
                $db->query("delete from contacts where id='".$row2['id']."'");
                // szukanie emaili...
                $mail=$db->query("select * from email_addr_bean_rel where bean_id='".$row2['id']."'");
                if($mail->num_rows>0){
                    // o jest email!
                    while($mu=$db->fetchByAssoc($mail)){
                        // podmianka relacji na kontakt
                        $db->query("update email_addr_bean_rel set bean_module='contacts' where id='".$mu['id']."'");
                    }
                }
            }
        }
    }
   // die();
    // asasa$$$
    $l=$db->query("select * from accounts where id='".$_GET['record']."'");
    $pola=array();
    $wartosci=array();
    while($daj=$db->fetchByAssoc($l)){
        
    
       foreach ($daj as $pole=>$wartosc){
           if($pole=='account_type')$pole='lead_type';
           if($pole=='ecmprice_id')continue;
           if($pole=='website_update')continue;
           if($pole=='document_recipient_code')continue;
           if($pole=='document_parent_order_no')continue;
           if($pole=='document_delivery_address')continue;
           if($pole=='invoice_to_ownership')continue;
           if($pole=='document_parent_iln')continue;
           if($pole=='document_parent_nip')continue;
           if($pole=='document_shipping_iln')continue;
           if($pole=='document_shipping_nip')continue;
           if($pole=='document_bank_account')continue;
           if($pole=='document_parent_code')continue;
           if($pole=='document_ean')continue;
          $pola[]=$pole;
          $wartosci[]=$wartosc;
       }
        $r=$db->query("select * from leads where id='".$_GET['record']."'");
        if($r->num_rows==1)$db->query("delete from leads where id='".$_GET['record']."'");
     $db->query("INSERT INTO `leads` (".implode(",",$pola).") VALUES ('".implode("','",$wartosci)."')");

      $db->query("update accounts set deleted='1' where id='".$daj['id']."'");
      
    }
    // notatki
    $n1=$db->query("select * from notes where parent_id='".$_GET['record']."'");

    if($n1->num_rows>0){
  
        while($nt=$db->fetchByAssoc($n1)){
            $db->query("update notes set parent_type='Accounts' where id='".$nt['id']."'");
        }
    }
    // przenoszenie emaila
    $mail=$db->query("select * from email_addr_bean_rel where bean_id='".$_GET['record']."'");
    if($mail->num_rows>0){
        // o jest email!
        while($mu=$db->fetchByAssoc($mail)){
            // podmianka relacji na kontrahenta
           $db->query("update email_addr_bean_rel set bean_module='Accounts' where bean_id='".$_GET['record']."'");
        }
    }
    $mail=$db->query("select * from ecmaccountcategories_bean where bean_id='".$_GET['record']."'");
    if($mail->num_rows>0){
        // o jest email!
        while($mu=$db->fetchByAssoc($mail)){
            // podmianka relacji na kontrahenta
            $db->query("update ecmaccountcategories_bean set bean_name='Accounts' where bean_id='".$_GET['record']."'");
        }
    }
    // relacja kontrahent kontakt
    $rr=$db->query("SELECT * FROM accounts_contacts where account_id='".$_GET['record']."'");
    if($rr->num_rows>0){
        // o jest email!
        while($re=$db->fetchByAssoc($rr)){
        	$re['deleted']='0';
   
            $db->query("delete from leads_contactleads where lead_id='".$re['id']."'");
            $db->query("INSERT INTO `leads_contactleads` VALUES('".implode("','",$re)."')");
            $db->query("delete from accounts_contacts where account_id='".$_GET['record']."'");
        }
    }
    
    // relacja adresy
    $ad=$db->query("SELECT * FROM account_addresses where account_id='".$_GET['record']."'");
    if($ad->num_rows>0){
        // o jest email!
        while($re=$db->fetchByAssoc($ad)){
            // podmianka relacji na kontrahenta
        	$db->query("delete from lead_addresses where lead_id='".$re['id']."'");
            $db->query("INSERT INTO `lead_addresses` VALUES('".implode("','",$re)."')");
      
            $db->query("delete from account_addresses where account_id='".$_GET['record']."'");
        }
    }
   // szukanie kontaktów

  header("Location: index.php?module=Leads&action=DetailView&record=".$_GET['record']);
}
?>