<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Kvcodes CRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
*****************************************/

$page_security = 'CRM_SETTINGS';
$path_to_root="../../..";
include($path_to_root . "/includes/session.inc");
add_access_extensions();
include($path_to_root . "/gl/includes/gl_db.inc");
include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/modules/KvcodesCRM/includes/crm.inc" );
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/modules/KvcodesCRM/version.php" );
include_once($path_to_root . "/admin/db/company_db.inc");

$version_id = get_company_prefs('version_id');
global $version;
$js = '';
if($version_id['version_id'] == '2.4.1'){
	if ($SysPrefs->use_popup_windows) 
		$js .= get_js_open_window(900, 500);	

	if (user_use_date_picker()) 
		$js .= get_js_date_picker();
	
}else{
	if ($use_popup_windows)
		$js .= get_js_open_window(900, 500);
	if ($use_date_picker)
		$js .= get_js_date_picker();
}

page(_("CRM Settings"));

if(isset($_GET['Updated']) && $_GET['Updated'] == 'Yes'){
	display_notification("Settings Updated");
}

function can_process() {	

	/*if ($_POST['expd_percentage_amt'] == ""){
		display_error(_("You need to provide the maximum monthly pay limit percentage for employee loan."));
		set_focus('expd_percentage_amt');
		return false;
	} 
	if (!check_num('expd_percentage_amt'))	{
		display_error(_("Maximum EMI Limit should be a positive number"));
		set_focus('login_tout');
		$input_error = 1;
	}
	if (!check_num('ot_factor'))	{
		display_error(_("OT Multiplication Factor should be a positive number"));
		set_focus('ot_factor');
		$input_error = 1;
	}*/
	
	return true;	
}

if (isset($_POST['submit'])){
	kv_mail(['email' => $_POST['test_email'], 'name' => 'test user'], 'Testing Email', 'Testing Message', "txt", array(), null, false, false);
}
if (isset($_POST['addupdate'])&& can_process()) {	
	
	if(isset($_POST['pass']) && strlen($_POST['pass']) > 3)
		$_POST['smtp'] = array('user' => $_POST['user'], 'pass' => $_POST['pass'], 'type' => $_POST['type'], 'host' => $_POST['host'], 'port' => $_POST['port'], 'enable_SMTP_debugging' => check_value('enable_SMTP_debugging'), 'enable_SMTP' => check_value('enable_SMTP'));
	$options = array('home_country', 'master_role', 'customer_role', 'supplier_role', 'smtp', 'allow_customer_to_create_project', 'send_email_invoice', 'disable_all_email', 'send_email_payment', 'departments_list');
	begin_transaction();
	foreach ($_POST as $key => $value) {
		if(in_array($key, $options)){
			if(is_array($value))
				$value = base64_encode(serialize($value));
			Update('kv_crm_option', array('option_name' => $key) , array('option_value' => $value) );
		}
	}
	update_company_prefs(get_post( array('coy_name', 'email')));
	commit_transaction();	
	
	meta_forward($_SERVER['PHP_SELF'], "Updated=Yes");	
}

$all_settings =  GetAll('kv_crm_option');

foreach($all_settings as $settings){
	$data = @unserialize(base64_decode($settings['option_value']));
	if ($data !== false) {
	    $_POST[$settings['option_name']] = unserialize(base64_decode($settings['option_value']));
	} else {
	    $_POST[$settings['option_name']] = $settings['option_value']; 
	}	
}
if(isset($_POST['smtp']) && is_array($_POST['smtp'])){
	$_POST['user'] = $_POST['smtp']['user'];
	$_POST['port'] = $_POST['smtp']['port'];
	$_POST['host'] = $_POST['smtp']['host'];
	$_POST['type'] = $_POST['smtp']['type'];
	$_POST['enable_SMTP_debugging'] = isset($_POST['smtp']['enable_SMTP_debugging']) ? $_POST['smtp']['enable_SMTP_debugging'] : '' ;

	if(!isset($_POST['enable_SMTP']))
		$_POST['enable_SMTP'] = isset($_POST['smtp']['enable_SMTP']) ? $_POST['smtp']['enable_SMTP'] : '' ;
	//display_error($_POST['enable_SMTP'].json_encode($_POST['smtp']));
}
$_POST['email'] = get_company_pref('email');
$_POST['coy_name'] = get_company_pref('coy_name');
	start_form();
	div_start('smtp');
		start_outer_table(TABLESTYLE2); 

		if(list_updated('enable_SMTP')){
		$Ajax->activate('smtp');
	}

		table_section(1);
			table_section_title(_("General settings"));
			echo '<tr> <td> Ticket Departments from Roles : </td> <td>'.crm_security_roles_list('departments_list', null, false, false, false, true).'</td> </tr>';			
			security_roles_list_row(_("Master Access Role:"). "&nbsp;", 'master_role', false, false, false, check_value('show_inactive'));
			security_roles_list_row(_("Supplier Access Role:"). "&nbsp;", 'supplier_role', false, false, false, check_value('show_inactive'));
			security_roles_list_row(_("Customer Role:"). "&nbsp;", 'customer_role', false, false, false, check_value('show_inactive'));
			crm_country_list_row(_("Country:"), 'home_country', null);
			check_row(_("Allow Customers To Create New Project"), 'allow_customer_to_create_project');
			check_row(_("Send email when new Sales invoice generated"), 'send_email_invoice');
			check_row(_("Send email when new Sales Payment is recorded"), 'send_email_payment');
			check_row(_("Disable All emails moving from the system"), 'disable_all_email');
            label_row(_("Version"), '<a href="https://www.kvcodes.com/clients/products/client/item_details/crm-project-management-for-frontaccounting" target="_blank" > '.$version.'</a>');
            
			table_section(2);

			table_section_title(_("SMTP"));	
			start_row();
			crm_smtp_type_list_cells(_("Type"), 'type', null);
			end_row();
			check_row(_("Enable SMTP"), 'enable_SMTP', null, true);
			
if(check_value('enable_SMTP')){
			check_row(_("Enable SMTP Debugging mode"), 'enable_SMTP_debugging');
			text_row(_("Host name"), 'host', null, 50, 80);								
			text_row(_("Port No"), 'port', null, 5, 5);								
			text_row(_("Username/Email"), 'user', null, 50, 65);	

			password_row(_("Password"), 'pass', null);								
			table_section_title("");
				text_row(_("From Email"), 'email', null, 50, 65);
				text_row(_("From Name"), 'coy_name', null, 50, 65);
			table_section_title(_("Test SMTP Connection"));
				text_row(_("Test Email"), 'test_email', null, 50, 65);
	
				submit_row('submit', _("Send Test Email"), true, '', 'default');	

			}
			
		end_outer_table(1);
		br();
		submit_center('addupdate', _("Submit"), true, '', 'default');
		div_end();
	end_form();  

end_page(); 

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

function crm_security_roles_list($name, $selected_id=null, $new_item=false, $submit_on_change=false,  $show_inactive = false, $multiple=false){

    $sql = "SELECT id, role, inactive FROM ".TB_PREF."security_roles";

    return combo_input($name, $selected_id, $sql, 'id', 'description',
        array(
            'spec_option'=>$new_item ? _("New role") : false,
            'spec_id' => '',
            'select_submit'=> $submit_on_change,
            'multi'=> $multiple,
            'show_inactive' => $show_inactive
        ) );
}

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


?>
<style>select { width: auto !important;} </style>