<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Kvcodes CRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
/* Function : Opportunities Inquiry
*****************************************/
$page_security = 'CRM_OPPORTUNITIES';
$path_to_root = "../../..";
if(!isset($page_nested))
	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 = "Opportunities Inquiry"), isset($_GET['opportunity_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/opportunities.php?opportunity_id='.$row['opportunity_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['opportunity_id'], null, null, ICON_DELETE, '');

  	//return delete_button_cell(, _("Delete"));  
  	/*'<a href="/modules/KvcodesCRM/manage/leads.php?opportunity_id='.$row['opportunity_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>';*/ 
}


//----------------------------------------------------------------------------------------------------
if ($Mode == 'Delete'){	
	$get_selected = GetRow('kv_crm_opportunities', array('opportunity_id' => $selected_id));
	//if($get_selected['is_converted'] == ){
		Delete('kv_crm_opportunities', array('opportunity_id' => $selected_id));
		
		Delete('kv_crm_activities', array('type' => 2, 'type_id' => $selected_id));
		Delete('kv_crm_attachments', array('type' => 2, '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['opportunity_id'])){
	$_POST['opportunity_id'] = $_GET['opportunity_id'];
}

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

start_form();

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

start_table(TABLESTYLE_NOBORDER);
start_row();

if (!$page_nested){
	opportunity_list_cells(_("Select an Opportunity"). " :", 'opportunity_id', null,  _("Select Opportunity"), true, check_value('show_inactive'), false, array());
	//check_cells(_("Show Converted:"), 'show_inactive', null, true);
}
kv_common_type_cells(_('Opportunity Source') ,'lead_source',null, _("All Soruces"), true,1);
kv_common_status_cells(_('Opportunity Status'), 'status_id', null,  _("Any Status"), true, 5);

kv_user_list_cells(_('Assigned To'),'assign_to', null, _("All Users"), true);
//$_GET['type_id'] = $selected_id;
			//$_GET['type']= ST_CUSTOMER;  //Customer type

if(isset($_GET['type']) && $_GET['type'] == ST_CUSTOMER){
	$_POST['cust_id'] = $_GET['type_id'];
	hidden('cust_id', $_POST['cust_id']);
} else {
	customer_list_cells(_('Customers'),'cust_id', null, _("All Customers"), true);
} 

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

//------------------------------------------------------------------------------------------------
if (get_post('RefreshInquiry') || list_updated('opportunity_id') || list_updated('lead_source_id') || list_updated('status_id')){
	$Ajax->activate('Opportunity_details');
}

div_start('Opportunity_details');
//------------------------------------------------------------------------------------------------
$filters = array(); //array('`lead`.`is_converted`' => 0 );
if(get_post('opportunity_id')){
	$filters = array('`lead`.`opportunity_id`' => get_post('opportunity_id'));
}
/*if(get_post('show_inactive')){
	unset($filters['`lead`.`is_converted`']);
}*/
if(get_post('lead_source')){
	$filters = array('`lead`.`lead_source`' 	=> get_post('lead_source'));
}

if(get_post('status_id')){
	$filters = array('`lead`.`status_id`' 	=> get_post('status_id'));
}
if(get_post('assign_to')){
	$filters = array('`lead`.`assign_to`' 		=> get_post('assign_to'));
}
if(get_post('cust_id')){
	$filters = array('`lead`.`cust_id`' 		=> get_post('cust_id'));
}

$sql = GetDataJoin('kv_crm_opportunities AS lead', array( 
						0 => array('join' => 'INNER', 'table_name' => 'currencies AS curr', 'conditions' => '`curr`.`curr_abrev` = `lead`.`amount_curr_abrev`'),
						1 => array('join' => 'LEFT', 'table_name' => 'kv_crm_picklist_values AS source', 'conditions' => '`lead`.`lead_source` = `source`.`id`AND `source`.`picklist_id` = 1'), 
						2 => array('join' => 'LEFT', 'table_name' => 'users AS usr', 'conditions' => '`lead`.`assign_to` = `usr`.`id`'), 
						3 => array('join' => 'LEFT', 'table_name' => 'kv_crm_status AS status', 'conditions' => '`lead`.`status_id` = `status`.`status_id`'), 
						4 => array('join' => 'LEFT', 'table_name' => 'debtors_master AS debtor', 'conditions' => '`lead`.`cust_id` = `debtor`.`debtor_no`'), 
		), array('`lead`.`organization_name`, `lead`.`opportunity_name`, `curr`.`currency`, `lead`.`amount`,  `source`.`description` AS SourceDescription, `usr`.`real_name`, `debtor`.`debtor_ref`, `status`.`status` AS StatusDesc, `lead`.`opportunity_id`'), $filters, null, false, false, true);
//display_warning($sql);
//------------------------------------------------------------------------------------------------
$cols = array(
	_("Opportunity Name") => array('name'=>'opportunity_name', 'ord'=>''),	
	_("Organization Name") => array('name'=>'organization_name'), 
	_("Currency") => array('name'=>'currency'),
	_("Amount") => array('name'=>'amount'),
	_("Lead Source") => array('name' => 'SourceDescription', 'ord'=>''), 
	_("Assigned To") => array('name'=>'real_name'),
	_("Customer") => array('name' => 'debtor_ref'), 
	_("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);
div_end();
end_form();
end_page();