<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Kvcodes CRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
/* Function : Leads Inquiry
*****************************************/
$page_security = 'CRM_LEADS';
$path_to_root = "../../..";
include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
add_access_extensions();
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/modules/KvcodesCRM/includes/crm.inc" );
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Leads Inquiry"), isset($_GET['lead_id']), false, "", $js);

simple_page_mode(true);
//------------------------------------------------------------------------------------------------
function edit_link($row) {
	global $page_nested, $path_to_root;

	$str = '';
	if ($page_nested)
		return '';

	return '<a href="'.$path_to_root.'/modules/KvcodesCRM/manage/leads.php?lead_id='.$row['lead_id'].'"><img src="'.$path_to_root.'/themes/default/images/edit.gif" style="vertical-align:middle;width:12px;height:12px;border:0;" title="Edit"></a>';
}

function delete_link($row)
{
	global $path_to_root;
	return button("Delete".$row['lead_id'], null, null, ICON_DELETE, '');

  	//return delete_button_cell(, _("Delete"));  
  	/*'<a href="/modules/KvcodesCRM/manage/leads.php?lead_id='.$row['lead_id'].'"><img src="'.$path_to_root.'/themes/default/images/edit.gif" style="vertical-align:middle;width:12px;height:12px;border:0;" title="Edit"></a>
  	<button type="submit" class="editbutton" name="Edit1" value="1" title="Edit"><img src="'.$path_to_root.'/themes/default/images/edit.gif" style="vertical-align:middle;width:12px;height:12px;border:0;">
</button>';*/ 
}

function show_converted($row)
{
	return $row['is_converted'] == 1;
}

//----------------------------------------------------------------------------------------------------
if ($Mode == 'Delete'){	
	$get_selected = GetRow('kv_crm_leads', array('lead_id' => $selected_id));
	//if($get_selected['is_converted'] == ){
		Delete('kv_crm_leads', array('lead_id' => $selected_id));
		Delete('kv_crm_tasks', array('type' => 1, 'type_id' => $selected_id));
		Delete('kv_crm_activities', array('type' => 1, 'type_id' => $selected_id));
		Delete('kv_crm_attachments', array('type' => 1, 'type_id' => $selected_id));
		display_notification(_('Selected Lead has been deleted'));		
		//meta_forward($_SERVER['PHP_SELF'], "Deleted=yes&empl_id=".$_POST['empl_id']);
	//} else
		//display_warning(_("Sorry, you can't delete this Loan, it has some repayments."));
	
	$Mode = 'RESET';
}

if ($Mode == 'RESET'){
	$selected_id = -1;
	$Ajax->activate('_page_body');
}

//------------------------------------------------------------------------------------------------

if (isset($_GET['lead_id'])){
	$_POST['lead_id'] = $_GET['lead_id'];
}

//------------------------------------------------------------------------------------------------

start_form();

if (!isset($_POST['lead_id']))
	$_POST['lead_id'] = 0; //get_global_customer();

start_table(TABLESTYLE_NOBORDER);
start_row();

if (!$page_nested){
	lead_list_cells(_("Select a Lead"). " :", 'lead_id', null,  _("Select Lead"), true, check_value('show_inactive'), false, array());
	//check_cells(_("Show Converted:"), 'show_inactive', null, true);
}
kv_common_type_cells(_('Lead Source') ,'lead_source_id',null, _("All Soruces"), true,1); 
kv_common_type_cells(_('Industry List') ,'industry_id',null, _("All Industries"), true,2); 
kv_common_type_cells(_('Lead Status'), 'lead_status_id', null,  _("Any Status"), true, 3);

kv_user_list_cells(_('Added By'),'added_by', null, _("All Users"), true);
kv_user_list_cells(_('Managed By'),'managed_by', null, _("All Users"), true);

submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
end_row();
end_table();

//------------------------------------------------------------------------------------------------
if (get_post('RefreshInquiry') || list_updated('lead_id') ||  list_updated('lead_source_id') || list_updated('industry_id') || list_updated('lead_status_id')){
	$Ajax->activate('_page_body');
}

//------------------------------------------------------------------------------------------------
$filters = array(); //array('`lead`.`is_converted`' => 0 );
if(get_post('lead_id')){
	$filters['`lead`.`lead_id`'] = get_post('lead_id');
}
/*if(get_post('show_inactive')){
	unset($filters['`lead`.`is_converted`']);
}*/
if(get_post('lead_source_id')){
	$filters['`lead`.`lead_source_id`'] 	= get_post('lead_source_id');
}
if(get_post('industry_id')){
	$filters['`lead`.`industry_id`'] 	= get_post('industry_id');
}
if(get_post('lead_status_id')){
	$filters['`lead`.`lead_status_id`'] 	= get_post('lead_status_id');
}
if(get_post('added_by')){
	$filters['`lead`.`added_by`'] 		= get_post('added_by');
}
if(get_post('managed_by')){
	$filters['`lead`.`managed_by`'] 		= get_post('managed_by');
}

$sql = GetDataJoin('kv_crm_leads AS lead', array( 
						0 => array('join' => 'INNER', 'table_name' => 'kv_crm_country AS country', 'conditions' => '`country`.`id` = `lead`.`country`'),
						1 => array('join' => 'LEFT', 'table_name' => 'kv_crm_picklist_values AS source', 'conditions' => '`lead`.`lead_source_id` = `source`.`id` AND `source`.`picklist_id` = 1'), 
						2 => array('join' => 'LEFT', 'table_name' => 'kv_crm_picklist_values AS industry', 'conditions' => '`lead`.`industry_id` = `industry`.`id` AND `source`.`picklist_id` = 2'), 
						3 => array('join' => 'LEFT', 'table_name' => 'kv_crm_picklist_values AS status', 'conditions' => '`lead`.`lead_status_id` = `status`.`id` AND `source`.`picklist_id` = 5'), 
					), 
					array('CONCAT(`lead`.`first_name`, " ", `lead`.`last_name`) AS name, `country`.`local_name`, `lead`.`work_phone`, `lead`.`mobile_phone`, `lead`.`email`, `source`. `description` AS SourceDescription, `industry`. `description` AS IndutryDesc, `status`.`description` AS StatusDesc, `lead`.`is_converted`, `lead`.`lead_id`'), $filters, null, false, false, true);

//------------------------------------------------------------------------------------------------
$cols = array(
	_("First Name") => array('name'=>'name', 'ord'=>''),	
	_("Country") => array('name'=>'country'), 
	_("Work Phone") => array('name'=>'work_phone'),
	_("Mobile Phone") => array('name'=>'mobile_phone'),
	_("E-mail") => array('name'=>'email'), 
	_("Lead Source") => array('name' => 'SourceDescription', 'ord'=>''), 
	_("Industry") => array('name'=>'IndutryDesc'),
	_("Status") => array('name'=>'StatusDesc'), 
		array('insert'=>true, 'fun'=>'edit_link'),
		array('insert'=>true, 'fun'=>'delete_link')
	);

$table =& new_db_pager('trans_tbl', $sql, $cols);
$table->set_marker('show_converted', _("Marked Leads are Converted to Customers."));

$table->width = "85%";

display_db_pager($table);

end_form();
end_page();