<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Kvcodes CRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
/* Function : Projects Inquiry
*****************************************/
$page_security = 'CRM_PROJECTS';
$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 = "Projects Inquiry"), isset($_GET['project_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/projects?project_id='.$row['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['id'], null, null, ICON_DELETE, '');

  	//return delete_button_cell(, _("Delete"));  
  	/*'<a href="/modules/KvcodesCRM/manage/projects.php?project_id='.$row['project_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['inactive'] == 1;
}

//----------------------------------------------------------------------------------------------------
if ($Mode == 'Delete'){	
	$get_selected = GetRow('kv_crm_projects', array('id' => $selected_id));
	//if($get_selected['is_converted'] == ){
		Delete('kv_crm_projects', array('id' => $selected_id));
		Delete('kv_crm_tasks', array('type' => 2, 'type_id' => $selected_id));
		Delete('kv_crm_activities', array('type' => 3, 'type_id' => $selected_id));
		Delete('kv_crm_attachments', array('type' => 3, 'type_id' => $selected_id));
		display_notification(_('Selected Project 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['project_id'])){
	$_POST['project_id'] = $_GET['project_id'];
}

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

start_form();

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

start_table(TABLESTYLE_NOBORDER);
start_row();

if (!$page_nested){
	//projects_list_cells(_("Select a Project"). " :", 'project_id', null,  _("Select Project"), true, check_value('show_inactive'), false, array());
	
	$default_type = false;
	if(isset($_SESSION['wa_current_user']->type) && $_SESSION['wa_current_user']->type == ST_CUSTOMER ) {
		$_POST['clientid'] = $current_user = $_SESSION['wa_current_user']->person;
		//$allow_customer_to_create_project = GetSingleValue('kv_crm_option', 'option_value', array('option_name' => 'allow_customer_to_create_project'));
		//if($allow_customer_to_create_project)
			$default_type =  _("Select Project");
	}else {		
		$master_role = GetSingleValue('kv_crm_option', 'option_value', array('option_name' => 'master_role'));
		if($master_role != $_SESSION['wa_current_user']->access){
			$session_user_id = GetSingleValue('users', 'id', array('id' => $_SESSION['wa_current_user']->user));
		} else {
			$default_type =  _("Select Project");
		}
		$current_user = false;
	}
    projects_list_cells(_("Select a Project"). " :", 'project_id', null,  $default_type, true, check_value('show_inactive'), false, false, $current_user);
	//check_cells(_("Show Converted:"), 'show_inactive', null, true);
}
if(isset($_SESSION['wa_current_user']->type) && $_SESSION['wa_current_user']->type == ST_CUSTOMER )
	hidden('clientid', $_SESSION['wa_current_user']->person);
else
	customer_list_cells(_("Customer"), 'clientid', null,_("Select A Customer"), true); 

kv_common_type_cells(_('Project Status'), 'status', null,  _("Any Status"), true, 5);

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

//------------------------------------------------------------------------------------------------
if (get_post('RefreshInquiry') || list_updated('project_id') ||  list_updated('industry_id') || list_updated('status')){
	$Ajax->activate('_page_body');
}

//------------------------------------------------------------------------------------------------
$filters = array(); //array('`project`.`is_converted`' => 0 );
if(get_post('project_id')){
	$filters['`project`.`id`'] = get_post('project_id');
}

if(get_post('status')){
	$filters['`project`.`status`'] 	= get_post('status');
}
if(get_post('clientid')){
	$filters['`project`.`clientid`'] 		= get_post('clientid');
}
if(get_post('managed_by')){
	$filters['`project`.`managed_by`'] 		= get_post('managed_by');
}

$sql = GetDataJoin('kv_crm_projects AS project', array( 
						0 => array('join' => 'LEFT', 'table_name' => 'debtors_master AS debtor', 'conditions' => '`debtor`.`debtor_no` = `project`.`clientid`'), 
						1 => array('join' => 'LEFT', 'table_name' => 'kv_crm_picklist_values AS status', 'conditions' => '`project`.`status` = `status`.`id2` AND `status`.`picklist_id` = 5'), 
					), 
					array('`project`.`name` AS Pname,  `debtor`.`name`, `project`.`project_cost`, `project`.`start_date`, `project`. `deadline`, `status`.`description`, `project`.`inactive`,  `project`.`id`'), $filters, null, false, false, true);

//------------------------------------------------------------------------------------------------
$cols = array(
	_("First Name") => array('name'=>'Pname', 'ord'=>''),	
	_("Customer") => array('name'=>'name'), 
	_("Cost") => array('name'=>'project_cost'),
	_("Start Date") => array('name'=>'start_date', 'type'=>'date'),
	_("End Date") => array('name'=>'deadline','type'=>'date'), 
	_("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 Projects are Converted to Customers."));

$table->width = "85%";

display_db_pager($table);

end_form();
end_page();