Files
crm.e5.pl/modules/Accounts/copy_accounts.php
2024-04-27 09:23:34 +02:00

207 lines
7.1 KiB
PHP

<?php
$db=$GLOBALS['db'];
$not_in =
"UPPER(name) NOT LIKE 'MARCPOL%' AND
UPPER(name) NOT LIKE 'KAUFLAND%' AND
UPPER(name) NOT LIKE 'EURO%NET%' AND
UPPER(name) NOT LIKE 'CARREFOUR%' AND
UPPER(name) NOT LIKE 'MEDIA EXPERT%' AND
UPPER(name) NOT LIKE 'AMAZON%' AND
UPPER(name) NOT LIKE '%CZERWONA%' AND
UPPER(name) NOT LIKE 'AGD MARKET%' AND
UPPER(name) NOT LIKE 'DROGERIA NATURA%' AND
UPPER(name) NOT LIKE 'POLOMARKET%' AND
UPPER(name) NOT LIKE 'NORAUTO%'";
$acc = $db->query("SELECT * FROM accounts WHERE deleted='0' AND ".$not_in." order by date_entered");
$accounts = array();
while ($a = $db->fetchByAssoc($acc)) {
$docs = false;
$doc = $db->query("SELECT id FROM ecminvoiceouts where parent_id='".$a['id']."'");
if ($doc->num_rows>0) {
$docs = true;
//echo $a['name'].' FV<br>';
} else $no_inv = true;
$doc = $db->query("SELECT id FROM ecmpurchaseorders where parent_id='".$a['id']."'");
if ($doc->num_rows>0) {
$docs = true;
//echo $a['name'].' PO<br>';
}
$doc = $db->query("SELECT id FROM documents where parent_id='".$a['id']."'");
if ($doc->num_rows>0) {
$docs = true;
//echo $a['name'].' DMS<br>';
}
$doc = $db->query("SELECT id FROM ecmsales where parent_id='".$a['id']."'");
if ($doc->num_rows>0) {
$so = true;
//echo $a['name'].' ZS<br>';
}
$doc = $db->query("SELECT id FROM ecmstockdocouts where parent_id='".$a['id']."'");
if ($doc->num_rows>0) {
$docs = true;
//echo $a['name'].' WZ<br>';
} else $no_wz = true;
$doc = $db->query("SELECT id FROM ecmstockdocins where parent_id='".$a['id']."'");
if ($doc->num_rows>0) {
$docs = true;
//echo $a['name'].' PZ<br>';
}
if ($docs) {
continue;
}
if ($no_inv && $no_wz && $so) {
echo $a['name'].'<br>';
$accounts[] = $a['id'];
}
}
echo sizeof($accounts);
return;
foreach ($accounts as $id) {
// copyAccount($id);
/*
$res = $db->query("SELECT * FROM ecmaccountcategories_bean WHERE bean_id='$id'");
if ($res->num_rows > 0) {
$a = new Account();
$a->retrieve($id);
echo $a->name.'<br>';
unset($a);
}
*/
}
return;
function copyAccount($id) {
$db=$GLOBALS['db'];
$_GET['record'] = $id;
if($_GET['record']!=''){
// id nie jest puste? jedziemy i szukamy leadsa ...
$r=$db->query("select contact_id,id,date_modified,deleted from accounts_contacts where account_id='".$_GET['record']."' and deleted='0'");
if($r->num_rows>0){
while($row=$db->fetchByAssoc($r)){
$ct=$db->query("select * from contacts where id='".$row['contact_id']."' and deleted='0'");
while($row2=$db->fetchByAssoc($ct)){
$db->query("update accounts_contacts set deleted='1' where id='".$row2['id']."' and deleted='0'");
$mail=$db->query("select * from email_addr_bean_rel where bean_id='".$row2['id']."' and deleted='0'");
if($mail->num_rows>0){
while($mu=$db->fetchByAssoc($mail)){
$db->query("update email_addr_bean_rel set bean_module='Contacts' where id='".$mu['id']."' and deleted='0'");
}
}
}
}
}
$l=$db->query("select * from accounts where id='".$_GET['record']."' and deleted='0'");
$l2=$db->query("describe leads");
$pola2=array();
while($daj2=$db->fetchByAssoc($l2)){
$pola2[]=$daj2['Field'];
}
$pola=array();
$wartosci=array();
while($daj=$db->fetchByAssoc($l)){
foreach ($daj as $pole=>$wartosc){
if($pole=='account_type')$pole='lead_type';
if(in_array($pole,$pola2)==false)continue;
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=='shop_number')continue;
if($pole=='krs')continue;
if($pole=='regon')continue;
if($pole=='document_parent_iln' || $pole=='document_shipping_iln' || $pole=='document_bank_account' || $pole=='document_parent_code' || $pole=='document_ean')continue;
$pola[]=$pole;
$wartosci[]=$wartosc;
}
$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']."' and deleted='0'");
if($n1->num_rows>0){
while($nt=$db->fetchByAssoc($n1)){
$db->query("update notes set parent_type='Leads' where id='".$nt['id']."'");
}
}
//oferty
$n1=$db->query("select * from ecmquotes where parent_id='".$_GET['record']."' and deleted='0'");
if($n1->num_rows>0){
while($nt=$db->fetchByAssoc($n1)){
$db->query("update ecmquotes set parent_type='Leads' where id='".$nt['id']."'");
}
}
//calls
$n1=$db->query("select * from calls where parent_id='".$_GET['record']."' and deleted='0'");
if($n1->num_rows>0){
while($nt=$db->fetchByAssoc($n1)){
$db->query("update calls set parent_type='Leads' where id='".$nt['id']."'");
}
}
//tasks
$n1=$db->query("select * from tasks where parent_id='".$_GET['record']."' and deleted='0'");
if($n1->num_rows>0){
while($nt=$db->fetchByAssoc($n1)){
$db->query("update tasks set parent_type='Leads' where id='".$nt['id']."'");
}
}
//meetings
$n1=$db->query("select * from meetings where parent_id='".$_GET['record']."' and deleted='0'");
if($n1->num_rows>0){
while($nt=$db->fetchByAssoc($n1)){
$db->query("update meetings set parent_type='Leads' where id='".$nt['id']."'");
}
}
//kategorie kontaktów
$n1=$db->query("select * from ecmaccountcategories_bean where bean_id='".$_GET['record']."' and deleted='0'");
if($n1->num_rows>0){
while($nt=$db->fetchByAssoc($n1)){
$db->query("update ecmaccountcategories_bean set bean_name='Leads' where id='".$nt['id']."'");
}
}
// przenoszenie emaila
$mail=$db->query("select * from email_addr_bean_rel where bean_id='".$_GET['record']."' and deleted='0'");
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='Leads' where bean_id='".$_GET['record']."'");
}
}
// relacja kontrahent kontakt
$rr=$db->query("SELECT * FROM accounts_contacts where account_id='".$_GET['record']."' and deleted='0'");
if($rr->num_rows>0){
// o jest email!
while($re=$db->fetchByAssoc($rr)){
// podmianka relacji na kontrahenta
$db->query("INSERT INTO `leads_contactleads` VALUES('".implode("','",$re)."')");
$db->query("update accounts_contacts set deleted='1' where lead_id='".$_GET['record']."'");
}
}
// relacja adresy
$ad=$db->query("SELECT * FROM account_addresses where account_id='".$_GET['record']."' and deleted='0'");
if($ad->num_rows>0){
// o jest email!
while($re=$db->fetchByAssoc($ad)){
// podmianka relacji na kontrahenta
$db->query("INSERT INTO `lead_addresses` VALUES('".implode("','",$re)."')");
$db->query("update account_addresses set deleted='1' where account_id='".$_GET['record']."'");
}
}
}
}
?>