Example #1
0
/** Function to get a to find duplicates in a particular module*/
function getDuplicateQuery($module, $field_values, $ui_type_arr)
{
    global $current_user;
    $tbl_col_fld = explode(",", $field_values);
    $i = 0;
    foreach ($tbl_col_fld as $val) {
        list($tbl[$i], $cols[$i], $fields[$i]) = explode(".", $val);
        $tbl_cols[$i] = $tbl[$i] . "." . $cols[$i];
        $i++;
    }
    $table_cols = implode(",", $tbl_cols);
    $sec_parameter = getSecParameterforMerge($module);
    if ($module == 'Contacts') {
        $nquery = "SELECT vtiger_contactdetails.contactid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_contactdetails\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_contactdetails.contactid\n\t\t\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid\n\t\t\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactaddress.contactaddressid = vtiger_contactsubdetails.contactsubscriptionid\n\t\t\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_contactdetails.contactid\n\t\t\t\tLEFT JOIN vtiger_account ON vtiger_account.accountid=vtiger_contactdetails.accountid\n\t\t\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\tFROM vtiger_contactdetails\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_contactdetails.contactid\n\t\t\t\t\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid\n\t\t\t\t\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactaddress.contactaddressid = vtiger_contactsubdetails.contactsubscriptionid\n\t\t\t\t\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid\n\t\t\t\t\t\tLEFT JOIN vtiger_account ON vtiger_account.accountid=vtiger_contactdetails.accountid\n\t\t\t\t\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\n\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_contactdetails.contactid ASC";
    } else {
        if ($module == 'Accounts') {
            $nquery = "SELECT vtiger_account.accountid AS recordid,\n\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\tFROM vtiger_account\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_account.accountid\n\t\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\n\t\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\n\t\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid=vtiger_accountscf.accountid\n\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_account.accountid\n\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\tFROM vtiger_account\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_account.accountid\n\t\t\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\n\t\t\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\n\t\t\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid=vtiger_accountscf.accountid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_account.accountid ASC";
        } else {
            if ($module == 'Leads') {
                $nquery = "SELECT vtiger_leaddetails.leadid AS recordid, vtiger_users_last_import.deleted,{$table_cols}\n\t\t\t\tFROM vtiger_leaddetails\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_leaddetails.leadid\n\t\t\t\tINNER JOIN vtiger_leadsubdetails ON vtiger_leadsubdetails.leadsubscriptionid = vtiger_leaddetails.leadid\n\t\t\t\tINNER JOIN vtiger_leadaddress ON vtiger_leadaddress.leadaddressid = vtiger_leadsubdetails.leadsubscriptionid\n\t\t\t\tLEFT JOIN vtiger_leadscf ON vtiger_leadscf.leadid=vtiger_leaddetails.leadid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_leaddetails.leadid\n\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\tFROM vtiger_leaddetails\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_leaddetails.leadid\n\t\t\t\t\t\tINNER JOIN vtiger_leadsubdetails ON vtiger_leadsubdetails.leadsubscriptionid = vtiger_leaddetails.leadid\n\t\t\t\t\t\tINNER JOIN vtiger_leadaddress ON vtiger_leadaddress.leadaddressid = vtiger_leadsubdetails.leadsubscriptionid\n\t\t\t\t\t\tLEFT JOIN vtiger_leadscf ON vtiger_leadscf.leadid=vtiger_leaddetails.leadid\n\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 AND vtiger_leaddetails.converted = 0 {$sec_parameter}\n\t\t\t\t\t\tGROUP BY {$table_cols} HAVING COUNT(*)>1) as temp\n\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\tWHERE vtiger_crmentity.deleted=0  AND vtiger_leaddetails.converted = 0 {$sec_parameter} ORDER BY {$table_cols},vtiger_leaddetails.leadid ASC";
            } else {
                if ($module == 'Products') {
                    $nquery = "SELECT vtiger_products.productid AS recordid,\n\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\tFROM vtiger_products\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid\n\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_products.productid\n\t\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid\n\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\tFROM vtiger_products\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid\n\t\t\t\t\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid\n\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_products.productid ASC";
                } else {
                    if ($module == "HelpDesk") {
                        $nquery = "SELECT vtiger_troubletickets.ticketid AS recordid,\n\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\tFROM vtiger_troubletickets\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_troubletickets.ticketid\n\t\t\tLEFT JOIN vtiger_ticketcf ON vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid\n\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_troubletickets.ticketid\n\t\t\tLEFT JOIN vtiger_attachments ON vtiger_attachments.attachmentsid=vtiger_crmentity.crmid\n\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\tLEFT JOIN vtiger_contactdetails ON vtiger_contactdetails.contactid = vtiger_troubletickets.parent_id\n\t\t\tLEFT JOIN vtiger_ticketcomments ON vtiger_ticketcomments.ticketid = vtiger_crmentity.crmid\n\t\t\tINNER JOIN (SELECT {$table_cols} FROM vtiger_troubletickets\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_troubletickets.ticketid\n\t\t\t\t\t\tLEFT JOIN vtiger_ticketcf ON vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid\n\t\t\t\t\t\tLEFT JOIN vtiger_attachments ON vtiger_attachments.attachmentsid=vtiger_crmentity.crmid\n\t\t\t\t\t\tLEFT JOIN vtiger_contactdetails ON vtiger_contactdetails.contactid = vtiger_troubletickets.parent_id\n\t\t\t\t\t\tLEFT JOIN vtiger_ticketcomments ON vtiger_ticketcomments.ticketid = vtiger_crmentity.crmid\n\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\tLEFT JOIN vtiger_contactdetails contd ON contd.contactid = vtiger_troubletickets.parent_id\n\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_troubletickets.ticketid ASC";
                    } else {
                        if ($module == "Potentials") {
                            $nquery = "SELECT vtiger_potential.potentialid AS recordid,\n\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\tFROM vtiger_potential\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_potential.potentialid\n\t\t\tLEFT JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid\n\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_potential.potentialid\n\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\tFROM vtiger_potential\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_potential.potentialid\n\t\t\t\t\t\tLEFT JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid\n\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_potential.potentialid ASC";
                        } else {
                            if ($module == "Vendors") {
                                $nquery = "SELECT vtiger_vendor.vendorid AS recordid,\n\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\tFROM vtiger_vendor\n\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_vendor.vendorid\n\t\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid=vtiger_vendor.vendorid\n\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_vendor.vendorid\n\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\tFROM vtiger_vendor\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_vendor.vendorid\n\t\t\t\t\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid=vtiger_vendor.vendorid\n\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0\n\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0  ORDER BY {$table_cols},vtiger_vendor.vendorid ASC";
                            } else {
                                $modObj = CRMEntity::getInstance($module);
                                if ($modObj != null && method_exists($modObj, 'getDuplicatesQuery')) {
                                    $nquery = $modObj->getDuplicatesQuery($module, $table_cols, $field_values, $ui_type_arr);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $nquery;
}
Example #2
0
function overwrite_duplicate_records($module, $focus, $autoDupType, $mappedFields)
{
    global $adb;
    global $dup_ow_count;
    global $process_fields;
    //Fix for 6187 : overwriting records during duplicate merge to handle uitype 10
    //handle uitype 10
    foreach ($focus->importable_fields as $fieldname => $uitype) {
        $uitype = $focus->importable_fields[$fieldname];
        if ($uitype == 10) {
            //added to handle security permissions for related modules :: for e.g. Accounts/Contacts in Potentials
            if (method_exists($focus, "add_related_to")) {
                if (!$focus->add_related_to($module, $fieldname)) {
                    if (array_key_exists($fieldname, $focus->required_fields)) {
                        $do_save = 0;
                        $skip_required_count++;
                        continue 2;
                    }
                }
            }
        }
    }
    $where_clause = "";
    $where = get_where_clause($module, $focus->column_fields);
    $sec_parameter = getSecParameterforMerge($module);
    if ($module == "Leads") {
        $sel_qry = "select vtiger_leaddetails.leadid from vtiger_leaddetails\r\n\t\tinner join vtiger_crmentity  on vtiger_crmentity.crmid = vtiger_leaddetails.leadid\r\n\t\tinner join vtiger_leadsubdetails on vtiger_leaddetails.leadid = vtiger_leadsubdetails.leadsubscriptionid\r\n\t\tinner join vtiger_leadaddress on vtiger_leadaddress.leadaddressid = vtiger_leaddetails.leadid\r\n\t\tleft join vtiger_leadscf on vtiger_leadscf.leadid = vtiger_leaddetails.leadid\r\n\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\r\n\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\r\n\t\twhere vtiger_crmentity.deleted = 0 AND vtiger_leaddetails.converted = 0 {$where} {$sec_parameter} order by vtiger_leaddetails.leadid ASC";
    } else {
        if ($module == "Accounts") {
            $sel_qry = "SELECT vtiger_account.accountid FROM vtiger_account\r\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_account.accountid\r\n\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\r\n\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\r\n\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid = vtiger_accountscf.accountid\r\n\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\r\n\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\r\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} {$sec_parameter} order by vtiger_account.accountid ASC";
        } else {
            if ($module == "Contacts") {
                $sel_qry = "SELECT vtiger_contactdetails.contactid FROM vtiger_contactdetails\r\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_contactdetails.contactid\r\n\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactaddress.contactaddressid = vtiger_contactdetails.contactid\r\n\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactsubdetails.contactsubscriptionid = vtiger_contactdetails.contactid\r\n\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid\r\n\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\r\n\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\r\n\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\r\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} {$sec_parameter} order by vtiger_contactdetails.contactid ASC";
            } else {
                if ($module == "Products") {
                    $sel_qry = "SELECT vtiger_products.productid FROM vtiger_products\r\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid\t\t\r\n\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid\r\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} order by vtiger_products.productid ASC";
                } else {
                    if ($module == "Vendors") {
                        $sel_qry = "SELECT vtiger_vendor.vendorid FROM vtiger_vendor\r\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_vendor.vendorid\r\n\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid = vtiger_vendor.vendorid\r\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} order by vtiger_vendor.vendorid ASC";
                    } else {
                        $sel_qry = "SELECT {$focus->table_name}.{$focus->table_index} FROM {$focus->table_name}\r\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = {$focus->table_name}.{$focus->table_index}";
                        // Consider custom table join as well.
                        if (isset($focus->customFieldTable)) {
                            $sel_qry .= " INNER JOIN " . $focus->customFieldTable[0] . " ON " . $focus->customFieldTable[0] . '.' . $focus->customFieldTable[1] . " = {$focus->table_name}.{$focus->table_index}";
                        }
                        $sel_qry .= " LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\r\n\t\tLEFT JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id\r\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} {$sec_parameter} order by {$focus->table_name}.{$focus->table_index} ASC";
                    }
                }
            }
        }
    }
    $result = $adb->query($sel_qry);
    $no_rows = $adb->num_rows($result);
    // now do any special processing for ex., map account with contact and potential
    $focus->process_special_fields();
    $process_fields = 'true';
    $moduleObj = new $module();
    if ($no_rows > 0) {
        for ($i = 0; $i < $no_rows; $i++) {
            $id_field = $moduleObj->table_index;
            $id_value = $adb->query_result($result, $i, $id_field);
            if ($i == 0) {
                $moduleObj->mode = "edit";
                $moduleObj->id = $id_value;
                if ($autoDupType == "merge") {
                    $moduleObj->retrieve_entity_info($id_value, $module);
                    foreach ($mappedFields as $index => $fieldName) {
                        if (!empty($focus->column_fields[$fieldName])) {
                            $moduleObj->column_fields[$fieldName] = $focus->column_fields[$fieldName];
                        }
                    }
                } else {
                    $moduleObj->column_fields = $focus->column_fields;
                }
                $moduleObj->saveentity($module);
            } else {
                DeleteEntity($module, $module, $moduleObj, $id_value, "");
            }
        }
        $dup_ow_count = $dup_ow_count + $no_rows;
        return true;
    } else {
        return false;
    }
}
Example #3
0
/** Function to get a to find duplicates in a particular module*/
function getDuplicateQuery($module, $field_values, $ui_type_arr)
{
    global $current_user;
    $tbl_col_fld = explode(",", $field_values);
    $i = 0;
    foreach ($tbl_col_fld as $val) {
        list($tbl[$i], $cols[$i], $fields[$i]) = explode(".", $val);
        $tbl_cols[$i] = $tbl[$i] . "." . $cols[$i];
        $i++;
    }
    $table_cols = implode(",", $tbl_cols);
    $sec_parameter = getSecParameterforMerge($module);
    if (stristr($_REQUEST['action'], 'ImportStep') || $_REQUEST['action'] == $_REQUEST['module'] . 'Ajax' && $_REQUEST['current_action'] == 'ImportSteplast') {
        if ($module == 'Contacts') {
            $ret_arr = get_special_on_clause($table_cols);
            $select_clause = $ret_arr['sel_clause'];
            $on_clause = $ret_arr['on_clause'];
            $nquery = "select vtiger_contactdetails.contactid as recordid,vtiger_users_last_import.deleted,{$table_cols} \n\t\t\t\t\tFROM vtiger_contactdetails\n\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_contactdetails.contactid\n\t\t\t\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid\n\t\t\t\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactaddress.contactaddressid = vtiger_contactsubdetails.contactsubscriptionid\n\t\t\t\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid \n\t\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_contactdetails.contactid\n\t\t\t\t\tLEFT JOIN vtiger_account ON vtiger_account.accountid=vtiger_contactdetails.accountid\n\t\t\t\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\n\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_contactdetails t\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.contactid\n\t\t\t\t\t\t\tINNER JOIN vtiger_contactaddress addr ON t.contactid = addr.contactaddressid\n\t\t\t\t\t\t\tINNER JOIN vtiger_contactsubdetails subd ON addr.contactaddressid = subd.contactsubscriptionid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_contactscf tcf ON t.contactid = tcf.contactid \n    \t\t\t\t\t\tLEFT JOIN vtiger_account acc ON acc.accountid=t.accountid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_customerdetails custd ON custd.customerid=t.contactid\n\t\t\t\t\t\t\tWHERE crm.deleted=0 group by {$select_clause}  HAVING COUNT(*)>1) as temp\n\t\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_contactdetails.contactid ASC";
        } else {
            if ($module == 'Accounts') {
                $ret_arr = get_special_on_clause($field_values);
                $select_clause = $ret_arr['sel_clause'];
                $on_clause = $ret_arr['on_clause'];
                $nquery = "SELECT vtiger_account.accountid AS recordid,vtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_account\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_account.accountid\n\t\t\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\n\t\t\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\n\t\t\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid=vtiger_accountscf.accountid \n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_account.accountid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_account t\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.accountid\n\t\t\t\t\t\t\tINNER JOIN vtiger_accountbillads badd ON t.accountid = badd.accountaddressid\n\t\t\t\t\t\t\tINNER JOIN vtiger_accountshipads sadd ON t.accountid = sadd.accountaddressid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_accountscf tcf ON t.accountid = tcf.accountid\n\t\t\t\t\t\t\tWHERE crm.deleted=0 group by {$select_clause} HAVING COUNT(*)>1) as temp \n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_account.accountid ASC";
            } else {
                if ($module == 'Leads') {
                    $ret_arr = get_special_on_clause($field_values);
                    $select_clause = $ret_arr['sel_clause'];
                    $on_clause = $ret_arr['on_clause'];
                    $nquery = "select vtiger_leaddetails.leadid as recordid, vtiger_users_last_import.deleted,{$table_cols} \n\t\t\t\t\tFROM vtiger_leaddetails \n\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_leaddetails.leadid \n\t\t\t\t\tINNER JOIN vtiger_leadsubdetails ON vtiger_leadsubdetails.leadsubscriptionid = vtiger_leaddetails.leadid \n\t\t\t\t\tINNER JOIN vtiger_leadaddress ON vtiger_leadaddress.leadaddressid = vtiger_leadsubdetails.leadsubscriptionid\n\t\t\t\t\tLEFT JOIN vtiger_leadscf ON vtiger_leadscf.leadid=vtiger_leaddetails.leadid \n\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_leaddetails.leadid \n\t\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_leaddetails t \n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.leadid \n\t\t\t\t\t\t\tINNER JOIN vtiger_leadsubdetails subd ON subd.leadsubscriptionid = t.leadid \n\t\t\t\t\t\t\tINNER JOIN vtiger_leadaddress addr ON addr.leadaddressid = subd.leadsubscriptionid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_leadscf tcf ON tcf.leadid=t.leadid \n\t\t\t\t\t\t\tWHERE crm.deleted=0 and t.converted = 0 group by {$select_clause} HAVING COUNT(*)>1) as temp \n\t\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . " \n\t\t\t\tWHERE vtiger_crmentity.deleted=0 AND vtiger_leaddetails.converted = 0 {$sec_parameter} ORDER BY {$table_cols},vtiger_leaddetails.leadid ASC";
                } else {
                    if ($module == 'Products') {
                        $ret_arr = get_special_on_clause($field_values);
                        $select_clause = $ret_arr['sel_clause'];
                        $on_clause = $ret_arr['on_clause'];
                        $nquery = "SELECT vtiger_products.productid AS recordid,vtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_products\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_products.productid\n\t\t\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid\n\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_products t\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.productid\n\t\t\t\t\t\tLEFT JOIN vtiger_productcf tcf ON tcf.productid=t.productid\n\t\t\t\t\t\tWHERE crm.deleted=0 group by {$select_clause} HAVING COUNT(*)>1) as temp\n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\tWHERE vtiger_crmentity.deleted=0 ORDER BY {$table_cols},vtiger_products.productid ASC";
                    } else {
                        if ($module == 'HelpDesk') {
                            $ret_arr = get_special_on_clause($field_values);
                            $select_clause = $ret_arr['sel_clause'];
                            $on_clause = $ret_arr['on_clause'];
                            $nquery = "SELECT vtiger_troubletickets.ticketid AS recordid,vtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_troubletickets\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_troubletickets.ticketid\n\t\t\t\tLEFT JOIN vtiger_account ON vtiger_account.accountid = vtiger_troubletickets.parent_id \n\t\t\t\tLEFT JOIN vtiger_contactdetails ON vtiger_contactdetails.contactid = vtiger_troubletickets.parent_id\n\t\t\t\tLEFT JOIN vtiger_ticketcf ON vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_troubletickets.ticketid\n\t\t\t\tLEFT JOIN vtiger_attachments ON vtiger_attachments.attachmentsid=vtiger_crmentity.crmid\n\t\t\t\tLEFT JOIN vtiger_ticketcomments ON vtiger_ticketcomments.ticketid = vtiger_crmentity.crmid\t\t\t\t\n\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_troubletickets t\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.ticketid\n\t\t\t\t\t\tLEFT JOIN vtiger_account acc ON acc.accountid = t.parent_id \n\t\t\t\t\t\tLEFT JOIN vtiger_contactdetails contd ON contd.contactid = t.parent_id\n\t\t\t\t\t\tLEFT JOIN vtiger_ticketcf tcf ON tcf.ticketid = t.ticketid\n\t\t\t\t\t\tWHERE crm.deleted=0 group by {$select_clause} HAVING COUNT(*)>1) as temp\n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\tWHERE vtiger_crmentity.deleted=0" . $sec_parameter . " ORDER BY {$table_cols},vtiger_troubletickets.ticketid ASC";
                        } else {
                            if ($module == 'Potentials') {
                                $ret_arr = get_special_on_clause($field_values);
                                $select_clause = $ret_arr['sel_clause'];
                                $on_clause = $ret_arr['on_clause'];
                                $nquery = "SELECT vtiger_potential.potentialid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_potential \n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_potential.potentialid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_potential.potentialid\n\t\t\t\tLEFT JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid \n\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_potential t\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.potentialid\n\t\t\t\t\t\tLEFT JOIN vtiger_potentialscf tcf ON tcf.potentialid=t.potentialid\n\t\t\t\t\t\tWHERE crm.deleted=0 group by {$select_clause} HAVING COUNT(*)>1) as temp\n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_potential.potentialid ASC";
                            } else {
                                if ($module == 'Vendors') {
                                    $ret_arr = get_special_on_clause($field_values);
                                    $select_clause = $ret_arr['sel_clause'];
                                    $on_clause = $ret_arr['on_clause'];
                                    $nquery = "SELECT vtiger_vendor.vendorid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_vendor\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_vendor.vendorid\n\t\t\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid=vtiger_vendor.vendorid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_vendor.vendorid\t\t\t\t\n\t\t\t\tINNER JOIN (select {$select_clause} from vtiger_vendor t\n\t\t\t\t\t\tINNER JOIN vtiger_crmentity crm ON crm.crmid=t.vendorid\n\t\t\t\t\t\tLEFT JOIN vtiger_vendorcf tcf ON tcf.vendorid=t.vendorid\n\t\t\t\t\t\tWHERE crm.deleted=0 group by {$select_clause} HAVING COUNT(*)>1) as temp\n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\tWHERE vtiger_crmentity.deleted=0 ORDER BY {$table_cols},vtiger_vendor.vendorid ASC";
                                } else {
                                    $ret_arr = get_special_on_clause($field_values);
                                    $select_clause = $ret_arr['sel_clause'];
                                    $on_clause = $ret_arr['on_clause'];
                                    $modObj = CRMEntity::getInstance($module);
                                    if ($modObj != null && method_exists($modObj, 'getDuplicatesQuery')) {
                                        $nquery = $modObj->getDuplicatesQuery($module, $table_cols, $field_values, $ui_type_arr, $select_clause);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        if ($module == 'Contacts') {
            $nquery = "SELECT vtiger_contactdetails.contactid AS recordid,\n\t\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\t\tFROM vtiger_contactdetails\n\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_contactdetails.contactid\n\t\t\t\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid\n\t\t\t\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactaddress.contactaddressid = vtiger_contactsubdetails.contactsubscriptionid\n\t\t\t\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid\n\t\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_contactdetails.contactid\n\t\t\t\t\tLEFT JOIN vtiger_account ON vtiger_account.accountid=vtiger_contactdetails.accountid\n\t\t\t\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\n\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\t\tFROM vtiger_contactdetails\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_contactdetails.contactid\n\t\t\t\t\t\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid\n\t\t\t\t\t\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactaddress.contactaddressid = vtiger_contactsubdetails.contactsubscriptionid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_account ON vtiger_account.accountid=vtiger_contactdetails.accountid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t                                WHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_contactdetails.contactid ASC";
        } else {
            if ($module == 'Accounts') {
                $nquery = "SELECT vtiger_account.accountid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_account\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_account.accountid\n\t\t\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\n\t\t\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\n\t\t\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid=vtiger_accountscf.accountid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_account.accountid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\tFROM vtiger_account\n\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_account.accountid\n\t\t\t\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\n\t\t\t\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\n\t\t\t\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid=vtiger_accountscf.accountid \n\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n                                WHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_account.accountid ASC";
            } else {
                if ($module == 'Leads') {
                    $nquery = "SELECT vtiger_leaddetails.leadid AS recordid, vtiger_users_last_import.deleted,{$table_cols} \n\t\t\t\t\tFROM vtiger_leaddetails \n\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_leaddetails.leadid \n\t\t\t\t\tINNER JOIN vtiger_leadsubdetails ON vtiger_leadsubdetails.leadsubscriptionid = vtiger_leaddetails.leadid \n\t\t\t\t\tINNER JOIN vtiger_leadaddress ON vtiger_leadaddress.leadaddressid = vtiger_leadsubdetails.leadsubscriptionid \n\t\t\t\t\tLEFT JOIN vtiger_leadscf ON vtiger_leadscf.leadid=vtiger_leaddetails.leadid\n\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_leaddetails.leadid \n\t\t\t\t\tINNER JOIN (SELECT {$table_cols} \n\t\t\t\t\t\t\tFROM vtiger_leaddetails \n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_leaddetails.leadid \n\t\t\t\t\t\t\tINNER JOIN vtiger_leadsubdetails ON vtiger_leadsubdetails.leadsubscriptionid = vtiger_leaddetails.leadid \n\t\t\t\t\t\t\tINNER JOIN vtiger_leadaddress ON vtiger_leadaddress.leadaddressid = vtiger_leadsubdetails.leadsubscriptionid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_leadscf ON vtiger_leadscf.leadid=vtiger_leaddetails.leadid \n\t\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 AND vtiger_leaddetails.converted = 0 {$sec_parameter}\n\t\t\t\t\t\t\tGROUP BY {$table_cols} HAVING COUNT(*)>1) as temp \n\t\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n\t\t\t\t\tWHERE vtiger_crmentity.deleted=0  AND vtiger_leaddetails.converted = 0 {$sec_parameter} ORDER BY {$table_cols},vtiger_leaddetails.leadid ASC";
                } else {
                    if ($module == 'Products') {
                        $nquery = "SELECT vtiger_products.productid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_products\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_products.productid\n\t\t\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid\n\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\t\tFROM vtiger_products\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid \n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0\n\t\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n                                WHERE vtiger_crmentity.deleted=0  ORDER BY {$table_cols},vtiger_products.productid ASC";
                    } else {
                        if ($module == "HelpDesk") {
                            $nquery = "SELECT vtiger_troubletickets.ticketid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_troubletickets\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_troubletickets.ticketid\n\t\t\t\tLEFT JOIN vtiger_ticketcf ON vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_troubletickets.ticketid\n\t\t\t\tLEFT JOIN vtiger_attachments ON vtiger_attachments.attachmentsid=vtiger_crmentity.crmid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_contactdetails ON vtiger_contactdetails.contactid = vtiger_troubletickets.parent_id\n\t\t\t\tLEFT JOIN vtiger_ticketcomments ON vtiger_ticketcomments.ticketid = vtiger_crmentity.crmid\n\t\t\t\tINNER JOIN (SELECT {$table_cols} FROM vtiger_troubletickets\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_troubletickets.ticketid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_ticketcf ON vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid \n\t\t\t\t\t\t\tLEFT JOIN vtiger_attachments ON vtiger_attachments.attachmentsid=vtiger_crmentity.crmid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_contactdetails ON vtiger_contactdetails.contactid = vtiger_troubletickets.parent_id\n\t\t\t\t\t\t\tLEFT JOIN vtiger_ticketcomments ON vtiger_ticketcomments.ticketid = vtiger_crmentity.crmid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_contactdetails contd ON contd.contactid = vtiger_troubletickets.parent_id\n\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n                                WHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_troubletickets.ticketid ASC";
                        } else {
                            if ($module == "Potentials") {
                                $nquery = "SELECT vtiger_potential.potentialid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_potential\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_potential.potentialid\n\t\t\t\tLEFT JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_potential.potentialid\n\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\t\tFROM vtiger_potential\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_potential.potentialid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid \n\t\t\t\t\t\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\t\n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0 {$sec_parameter}\n\t\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n                                WHERE vtiger_crmentity.deleted=0 {$sec_parameter} ORDER BY {$table_cols},vtiger_potential.potentialid ASC";
                            } else {
                                if ($module == "Vendors") {
                                    $nquery = "SELECT vtiger_vendor.vendorid AS recordid,\n\t\t\t\tvtiger_users_last_import.deleted," . $table_cols . "\n\t\t\t\tFROM vtiger_vendor\n\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_vendor.vendorid\n\t\t\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid=vtiger_vendor.vendorid\n\t\t\t\tLEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_vendor.vendorid\n\t\t\t\tINNER JOIN (SELECT {$table_cols}\n\t\t\t\t\t\t\tFROM vtiger_vendor\n\t\t\t\t\t\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_vendor.vendorid\n\t\t\t\t\t\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid=vtiger_vendor.vendorid \n\t\t\t\t\t\t\tWHERE vtiger_crmentity.deleted=0\n\t\t\t\t\t\t\tGROUP BY " . $table_cols . " HAVING COUNT(*)>1) as temp\n\t\t\t\tON " . get_on_clause($field_values, $ui_type_arr, $module) . "\n                                WHERE vtiger_crmentity.deleted=0  ORDER BY {$table_cols},vtiger_vendor.vendorid ASC";
                                } else {
                                    $modObj = CRMEntity::getInstance($module);
                                    if ($modObj != null && method_exists($modObj, 'getDuplicatesQuery')) {
                                        $nquery = $modObj->getDuplicatesQuery($module, $table_cols, $field_values, $ui_type_arr);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $nquery;
}
function overwrite_duplicate_records($module, $focus)
{
    global $adb;
    global $dup_ow_count;
    global $process_fields;
    $where_clause = "";
    $where = get_where_clause($module, $focus->column_fields);
    $sec_parameter = getSecParameterforMerge($module);
    if ($module == "Leads") {
        $sel_qry = "select vtiger_leaddetails.leadid from vtiger_leaddetails\n\t\tinner join vtiger_crmentity  on vtiger_crmentity.crmid = vtiger_leaddetails.leadid\n\t\tinner join vtiger_leadsubdetails on vtiger_leaddetails.leadid = vtiger_leadsubdetails.leadsubscriptionid\n\t\tinner join vtiger_leadaddress on vtiger_leadaddress.leadaddressid = vtiger_leaddetails.leadid\n\t\tleft join vtiger_leadscf on vtiger_leadscf.leadid = vtiger_leaddetails.leadid\n\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\twhere vtiger_crmentity.deleted = 0 AND vtiger_leaddetails.converted = 0 {$where} {$sec_parameter} order by vtiger_leaddetails.leadid ASC";
    } else {
        if ($module == "Accounts") {
            $sel_qry = "SELECT vtiger_account.accountid FROM vtiger_account\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_account.accountid\n\t\tINNER JOIN vtiger_accountbillads ON vtiger_account.accountid = vtiger_accountbillads.accountaddressid\n\t\tINNER JOIN vtiger_accountshipads ON vtiger_account.accountid = vtiger_accountshipads.accountaddressid\n\t\tLEFT JOIN vtiger_accountscf ON vtiger_account.accountid = vtiger_accountscf.accountid\n\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} {$sec_parameter} order by vtiger_account.accountid ASC";
        } else {
            if ($module == "Contacts") {
                $sel_qry = "SELECT vtiger_contactdetails.contactid FROM vtiger_contactdetails\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_contactdetails.contactid\n\t\tINNER JOIN vtiger_contactaddress ON vtiger_contactaddress.contactaddressid = vtiger_contactdetails.contactid\n\t\tINNER JOIN vtiger_contactsubdetails ON vtiger_contactsubdetails.contactsubscriptionid = vtiger_contactdetails.contactid\n\t\tLEFT JOIN vtiger_contactscf ON vtiger_contactscf.contactid = vtiger_contactdetails.contactid\n\t\tLEFT JOIN vtiger_customerdetails ON vtiger_customerdetails.customerid=vtiger_contactdetails.contactid\n\t\tLEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\tLEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} {$sec_parameter} order by vtiger_contactdetails.contactid ASC";
            } else {
                if ($module == "Products") {
                    $sel_qry = "SELECT vtiger_products.productid FROM vtiger_products\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_products.productid\t\t\n\t\tLEFT JOIN vtiger_productcf ON vtiger_productcf.productid = vtiger_products.productid\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} order by vtiger_products.productid ASC";
                } else {
                    if ($module == "Vendors") {
                        $sel_qry = "SELECT vtiger_vendor.vendorid FROM vtiger_vendor\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_vendor.vendorid\n\t\tLEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid = vtiger_vendor.vendorid\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} order by vtiger_vendor.vendorid ASC";
                    } else {
                        $sel_qry = "SELECT {$focus->table_name}.{$focus->table_index} FROM {$focus->table_name}\n\t\tINNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = {$focus->table_name}.{$focus->table_index}";
                        // Consider custom table join as well.
                        if (isset($focus->customFieldTable)) {
                            $sel_qry .= " INNER JOIN " . $focus->customFieldTable[0] . " ON " . $focus->customFieldTable[0] . '.' . $focus->customFieldTable[1] . " = {$focus->table_name}.{$focus->table_index}";
                        }
                        $sel_qry .= " LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid\n\t\tLEFT JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id\n\t\tWHERE vtiger_crmentity.deleted = 0 {$where} {$sec_parameter} order by {$focus->table_name}.{$focus->table_index} ASC";
                    }
                }
            }
        }
    }
    $result = $adb->query($sel_qry);
    $no_rows = $adb->num_rows($result);
    // now do any special processing for ex., map account with contact and potential
    $focus->process_special_fields();
    $process_fields = 'true';
    $moduleObj = new $module();
    if ($no_rows > 0) {
        for ($i = 0; $i < $no_rows; $i++) {
            $id_field = $moduleObj->table_index;
            $id_value = $adb->query_result($result, $i, $id_field);
            if ($i == 0) {
                $moduleObj->mode = "edit";
                $moduleObj->id = $id_value;
                $moduleObj->column_fields = $focus->column_fields;
                $moduleObj->save($module);
            } else {
                DeleteEntity($module, $module, $moduleObj, $id_value, "");
            }
        }
        $dup_ow_count = $dup_ow_count + $no_rows;
        return true;
    } else {
        return false;
    }
}