<?php session_start();
			if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
			include ('../scripts/classes.php');if ($_SESSION['lang']=="EN"){$lang="EN"; include ('../lang/en.php');}elseif ($_SESSION['lang']=="GR"){$lang="GR"; include ('../lang/gr.php'); }else {$lang="EN"; include ('../lang/en.php');}
			$user = $_SESSION['user'];
            $user_branch = $_SESSION['branch'];
			$array = getGlobalSettings(); //Retrieve Global Settings	
			$page_title = $$lbl_page_title_transaction_log; //Assign Page Title
			$mode=$lbl_mode_search_transaction; //Assign Mode
?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="../img/favicon.png" />
<link href="../cms1.css" rel="stylesheet" type="text/css" /> 
</head>
<body>
<script>top.document.title=self.document.title;</script>
	<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
			<div class="align-left">
			<?php
    ################################################################################
    ## +---------------------------------------------------------------------------+
    ## | 1. Creating & Calling:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** define a relative (virtual) path to datagrid.class.php file
    ##  *** (relatively to the current file)
    ##  *** RELATIVE PATH ONLY ***     
      define ("DATAGRID_DIR", "../datagrid/");                     /* Ex.: "datagrid/" */ 
      define ("PEAR_DIR", "../datagrid/pear/");                    /* Ex.: "datagrid/pear/" */
    
      require_once(DATAGRID_DIR.'datagrid.class.php');
      require_once(PEAR_DIR.'PEAR.php');
      require_once(PEAR_DIR.'DB.php');
    
    ##
    ##  *** creating variables that we need for database connection 

      // includes database connection parameters
      include_once('../datagrid/config.inc.php');
    
      ob_start();
      $db_conn = DB::factory('mysql');  /* don't forget to change on appropriate db type */
      $result_conn = $db_conn->connect(DB::parseDSN('mysql://'.$DB_USER.':'.$DB_PASS.'@'.$DB_HOST.'/'.$DB_NAME));
      if(DB::isError($result_conn)){ die($result_conn->getDebugInfo()); } 
    ##  *** put a primary key on the first place 
       ##  *** put a primary key on the first place  
      $sql = "SELECT
			cls_transactions.id,
			cls_transactions.transaction_id,
			cls_transactions.customer_id,
			cls_transactions.barcode_id,
			cls_transactions.transaction_type,
			cls_transactions.category,
			cls_transactions.coupon_number,
			cls_transactions.coupon_discount,
			cls_transactions.additional_info,
			cls_transactions.amount,
			cls_transactions.credits,
			cls_transactions.multiplier,
			cls_transactions.created_on AS created_on,
			cls_transactions.created_by,
            BRA.branch_name AS branch,
			CONCAT(cls_customers.last_name,' ',cls_customers.first_name) AS client,
			IF(cls_transactions.branch_id = '$user_branch', IF(ISNULL(cls_transactions.is_reversed) AND ISNULL(cls_transactions.reversal), '<img src=\"../img/erase.gif\" alt=\"Reverse\" />', ''), '') AS reverse
            FROM cls_transactions
			LEFT OUTER JOIN cls_customers ON cls_transactions.customer_id= cls_customers.id
            LEFT OUTER JOIN cls_branches BRA ON cls_transactions.branch_id= BRA.id 
			";

  
    ##  *** set needed options and create a new class instance 
      $debug_mode = false;        /* display SQL statements while processing */    
      $messaging = true;          /* display system messages on a screen */ 
      $unique_prefix = "tra_";    /* prevent overlays - must be started with a letter */
      $dgrid= new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
    ##  *** set encoding and collation (default: utf8/utf8_unicode_ci)
      $dg_encoding = "utf8";
      $dg_collation = "utf8_unicode_ci";
      $dgrid->SetEncoding($dg_encoding, $dg_collation);
    ##  *** set data source with needed options
      $default_order_field = "id";
      $default_order_type = "DESC";
      $dgrid->DataSource($db_conn, $sql, $default_order_field, $default_order_type);
	  //webnail get current mode
	  $mode = isset($_GET[$unique_prefix.'mode']) ? $_GET[$unique_prefix.'mode'] : "";       
    
    ## +---------------------------------------------------------------------------+
    ## | 2. General Settings:                                                      | 
    ## +---------------------------------------------------------------------------+
	    ##  *** datagrid layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - customized
    ##  *** use "view"=>"0" and "edit"=>"0" only if you work on the same tables
    ##  *** filter layouts: "0" - tabular(horizontal) - default, "1" - columnar(vertical), "2" - tabular(inline)
    	$layouts = array("view"=>"0", "edit"=>"1", "details"=>"1", "filter"=>"1"); 
    	$dgrid->SetLayouts($layouts);
	
	##  *** allow multirow operations
        $multirow_option = false;
        $dgrid->AllowMultirowOperations($multirow_option);
        $multirow_operations = array(
            "edit"  => array("view"=>true),
            "delete"  => array("view"=>true),
            "details" => array("view"=>false), 
			"clone" => array("view"=>false),
        );
        $dgrid->SetMultirowOperations($multirow_operations);
 		//webnail language handling
		if ($_SESSION['lang']=="EN"){$dg_language = "en";}elseif ($_SESSION['lang']=="GR"){	$dg_language = "gk";}else{	$dg_language = "en";}
		$dgrid->SetInterfaceLang($dg_language);  
         ##  *** set modes for operations ("type" => "link|button|image")
    ##  *** "view" - view mode | "edit" - add/edit/details modes
    ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
    $modes = array(
         "add"	  =>array("view"=>false, "edit"=>false, "type"=>"button", "show_add_button"=>"outside"),
         "edit"	  =>array("view"=>false, "edit"=>true,  "type"=>"image" ),
         "cancel"  =>array("view"=>true, "edit"=>true,  "type"=>"button"),
         "details" =>array("view"=>true, "edit"=>false, "type"=>"image"),
         "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image"),
    );
    $dgrid->SetModes($modes);   
    ##  *** set CSS class for datagrid
    ##  *** "default|blue|gray|green|pink|empty|x-blue|x-gray|x-green" or your own css style
     $css_class = "default";
     $dgrid->SetCssClass($css_class);
    ##  *** set DataGrid caption
    $dg_caption = "";
      $dgrid->SetCaption($dg_caption);
    
    ## +---------------------------------------------------------------------------+
    ## | 3. Printing & Exporting Settings:                                         | 
    ## +---------------------------------------------------------------------------+
    ##  *** set printing option: true(default) or false 
      $printing_option = true;
      $dgrid->AllowPrinting($printing_option);
	  $exporting_option = true;
		$exporting_directory = "../exports/";               
		$export_all = false;
		$dgrid->AllowExporting($exporting_option, $exporting_directory, $export_all);
		$exporting_types = array("excel"=>"true", "pdf"=>"false", "xml"=>"false");
		$dgrid->AllowExportingTypes($exporting_types);
    
    ## +---------------------------------------------------------------------------+
    ## | 4. Sorting & Paging Settings:                                             | 
    ## +---------------------------------------------------------------------------+
    ##  *** set paging option: true(default) or false 
      $paging_option = true;
      $rows_numeration = false;
      $numeration_sign = "";       
      $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
    ##  *** set paging settings
      $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
      $top_paging = array();
      $pages_array = array("15"=>"15", "25"=>"25", "50"=>"50", "100"=>"100");
      $default_page_size = 15;
      $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
    
    ## +---------------------------------------------------------------------------+
    ## | 5. Filter Settings:                                                       | 
    ## +---------------------------------------------------------------------------+
    ##  *** set filtering option: true or false(default)
      $filtering_option = true;
      $show_search_type = true;
      $dgrid->AllowFiltering($filtering_option, $show_search_type);
    ##  *** set additional filtering settings
    ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
    
    //$fill_from_branches = array("0"=>"No", "1"=>"Yes");
	$sql_17 = 'SELECT id, branch_name FROM cls_branches ORDER BY id ASC';					
    $result_17 = mysqli_query($mysqli, $sql_17) or die("Query failed : " . mysqli_error($mysqli));	
    
    while( $row = mysqli_fetch_assoc($result_17)){
        $fill_from_branches[$row['id']] = $row['branch_name'];
    }
    //print_r ($fill_from_branches);
      $filtering_fields = array(
/*$lbl_grid_list_campaign=>array("type"=>"dropdownlist", "table"=>"sms_campaign_header", "field"=>"description", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	
$lbl_grid_filter_status=>array("type"=>"dropdownlist", "table"=>"sms_status", "field"=>$filter_status, "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),*/
	_BRANCH =>array("type"=>"dropdownlist", "table"=>"cls_transactions", "field"=>"branch_id", "filter_condition"=>"", "source"=>$fill_from_branches, "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
    $lbl_grid_transaction_id=>array("type"=>"textbox", "table"=>"cls_transactions", "field"=>"transaction_id", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
	$lbl_grid_barcode=>array("type"=>"textbox", "table"=>"cls_transactions", "field"=>"barcode_id", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
	$lbl_grid_customer=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"last_name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	
	$lbl_grid_transaction_type=>array("type"=>"dropdownlist", "table"=>"cls_transactions", "field"=>"transaction_type", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	
	$lbl_grid_category=>array("type"=>"dropdownlist", "table"=>"cls_transactions", "field"=>"category", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
/*$lbl_grid_list_contact=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"contact_id", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),  */  
    $lbl_grid_created_on_from=>array("type"=>"calendar", "table"=>"cls_transactions", "field"=>"created_on", "field_type"=>"from", "show_operator"=>"false", "default_operator"=>">=", "calendar_type"=>"floating","case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
    $lbl_grid_created_on_to=>array("type"=>"calendar", "table"=>"cls_transactions", "field"=>"created_on", "field_type"=>"to", "show_operator"=>"false", "default_operator"=>"<=", "calendar_type"=>"floating", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
/*$lbl_grid_message_id=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"message_id", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "default_operator"=>"=", "on_js_event"=>""),    	
$lbl_grid_batch_id=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"batch_id", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "default_operator"=>"=", "on_js_event"=>""),  */  	
	 );	 
      $dgrid->SetFieldsFiltering($filtering_fields);
    
    ## +---------------------------------------------------------------------------+
    ## | 6. View Mode Settings:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** set view mode table properties
      $vm_table_properties = array("width"=>"100%");
      $dgrid->SetViewModeTableProperties($vm_table_properties);  
    ##  *** set columns in view mode
    ##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
    ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
 		//webnail  	  
	  $vm_colimns = array(

		"id" => array("header"=>"Id", "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		"client" => array("header"=>$lbl_grid_customer, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"barcode_id" => array("header"=>$lbl_grid_barcode, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		//"transaction_id" => array("header"=>$lbl_grid_transaction_id, "type"=>"label", "align"=>"left", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"transaction_type" => array("header"=>$lbl_grid_transaction_type, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		"category" => array("header"=>$lbl_grid_cat_group, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),									
	//	"coupon_number" => array("header"=>$lbl_grid_coupon_number, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),			
//		"coupon_discount" => array("header"=>$lbl_grid_coupon_discount, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		"amount" => array("header"=>$lbl_grid_amount, "type"=>"label", "align"=>"right", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"true", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2"),
		"credits" => array("header"=>$lbl_grid_customer_credits, "type"=>"label", "align"=>"right", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"true", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),				
//		"priority_queue" => array("header"=>$lbl_grid_queue, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
/*		"delivery_delay" => array("header"=>$lbl_grid_delivery_delay, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	*/
//		"scheduled_time" => array("header"=>$lbl_grid_scheduled_time, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),			
//		"source_module" => array("header"=>$lbl_grid_source_module, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
//		"success_flag" => array("header"=>$lbl_grid_success_flag, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
//		"multiplier"=>array("header"=>"Name_O", "type"=>"color",     "align"=>"center", "width"=>"", "wrap"=>"", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "view_type"=>"image"),
		"created_on" => array("header"=>$lbl_grid_created_on, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),							
		"created_by" => array("header"=>$lbl_grid_created_by, "type"=>"label",  "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
        "branch" => array("header"=>_BRANCH, "type"=>"label",  "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),        
        "batch_id"  => array("header"=>$lbl_grid_batch_id, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"",  "rel"=>""),			
    	 'reverse'=>array("header"=>$lbl_grid_custom_reverse, "type"=>"link", "align"=>"center", "summarize"=>"false", "visible"=>"true", "on_js_event"=>"onClick=\"return confirm('"._ARE_YOU_SURE_YOU_WANT_TO_REVERSE."')\"", "field_key"=>"id", "field_key_1"=>"field_name_1", "field_data"=>"field_name_2", "rel"=>"", "target"=>"_self", "href"=>"transactions_all.php?tra_mode=reverse&tra_rid={0}", "sortable"=>"false"),		   				
      );
      $dgrid->SetColumnsInViewMode($vm_colimns);
    
    ## +---------------------------------------------------------------------------+
    ## | 7. Add/Edit/Details Mode Settings:                                        | 
    ## +---------------------------------------------------------------------------+
    ##  *** set add/edit mode table properties
      $em_table_properties = array("width"=>"100%");
      $dgrid->SetEditModeTableProperties($em_table_properties);
    ##  *** set details mode table properties
      $dm_table_properties = array("width"=>"100%");
      $dgrid->SetDetailsModeTableProperties($dm_table_properties);
    ##  ***  set settings for add/edit/details modes
      		//webnail language handling
	  
	  $table_name  = "cls_transactions";
      $primary_key = "id";
      $condition   = "";
      $dgrid->SetTableEdit($table_name, $primary_key, $condition);	     
	  $em_columns = array(
	  		"id"   =>array("header"=>$lbl_grid_code, "type"=>"label", "width"=>""),
			"created_on" =>array("header"=>$lbl_grid_created_on, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),	
            "created_by" =>array("header"=>$lbl_grid_created_by,  "type"=>"label"),	
			"transaction_id"   =>array("header"=>$lbl_grid_transaction_id, "type"=>"label", "width"=>""),
			"additional_info"   =>array("header"=>$lbl_grid_additional_info, "type"=>"label", "width"=>""),
      );	   	

      $dgrid->SetColumnsInEditMode($em_columns);
    ##  *** set auto-generated columns in edit mode
      $auto_column_in_edit_mode = false;
      $dgrid->SetAutoColumnsInEditMode($auto_column_in_edit_mode);
      $foreign_keys = array(	
       // "created_by"=>array("table"=>"users", "field_key"=>"id", "field_name"=>"username", "view_type"=>"dropdownlist", "order_by_field"=>"id", "order_type"=>"ASC", "on_js_event"=>""),		
		
      ); 
      $dgrid->SetForeignKeysEdit($foreign_keys);
     
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
     $dgrid->SetSummarizeNumberFormat("2", ",", ".");     
    $dgrid->initFilteringState = "opened";
    $dgrid->Bind();        
    ob_end_flush();
    
    ################################################################################  	  
  
?>
<script>
function confirm {
var agree=confirm("<?php echo $lbl_confirm_reverse_custom ?>");Submit()
    if (agree)
        return true ;
    else
        return false ;
    }
</script>
<?php if(isset($_GET[$unique_prefix.'mode']) && ($_GET[$unique_prefix.'mode'] == "reverse") )
{
  // PREPARE THE IDs selected FOR A QUERY
				   	$rid =$_GET['tra_rid'];
				   	//echo "$rid";
					//$sql = "DELETE from sms_campaign_details WHERE campaign_id ='$rid'";
					//mysql_query($sql);
	//select record values to create a reversed negative
					$copy_sql = "SELECT customer_id, barcode_id, transaction_id, branch_id, transaction_type, category, coupon_number, coupon_discount, amount, credits, multiplier, trans_total_amount, reversal, is_reversed, created_on FROM cls_transactions WHERE id='$rid'";
					$result_copy = mysqli_query($mysqli, $copy_sql) or die(mysqli_error($mysqli));
					$numrows_count = mysqli_num_rows($result_copy);
						if ($numrows_count > 0){//check if result exists
							$random_string = rand_string(20); 
							$row = mysqli_fetch_assoc($result_copy);
							$is_reversed=$row['is_reversed'];
							$reversal=$row['reversal'];
							$row_credits=$row['credits'];//Credits to be checked if negative balance is created
							$customer_id=$row['customer_id'];
                            $transaction_branch_id = $row['branch_id']; //added on 17/10/2014 by Kyriazis for 1.4.8 Branch support
                            $rec_date = $row['created_on']; //added on 17/10/2014 by Kyriazis for 1.4.8 
							//Get current Customers Credits
							$check_sql = "SELECT credits FROM cls_customers WHERE id='$customer_id'";						
							$result_check = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli));
							$numrows_count = mysqli_num_rows($result_check);							
							if ($numrows_count > 0){//check if result exists	
								$line = mysqli_fetch_assoc($result_check);
								$check_credits=$line['credits'];
								
								if (($check_credits - $row_credits)<0){//negative balance?
									?>
									<script>
									alert('<?php echo $lbl_transaction_will_create_negative; ?>')
									</script>
									<?php
									echo "<meta http-equiv=\"refresh\" content=\"0;URL=".$_SERVER['PHP_SELF']."\">";
									exit();											
								}elseif ($is_reversed=='1'){
									?>
									<script>
									alert('<?php echo $lbl_already_reversed ?>')
									</script>
									<?php
									echo "<meta http-equiv=\"refresh\" content=\"0;URL=".$_SERVER['PHP_SELF']."\">";
									exit();
								}elseif($reversal=='1'){
									?>
									<script>
									alert('<?php echo $lbl_transaction_is_reversal ?>')
									</script>
									<?php
									echo "<meta http-equiv=\"refresh\" content=\"0;URL=".$_SERVER['PHP_SELF']."\">";
									exit();
								}else{
							;
							$barcode_id = $row['barcode_id'];
							$transaction_type = $row['transaction_type'];
							$transaction_id = $row['transaction_id'];
							$category = $row['category'];
							$coupon_number = $row['coupon_number'];
							$coupon_discount = $row['coupon_discount'];
							$amount = $row['amount']*(-1);
							$credits = $row['credits']*(-1);
							$multiplier = $row['multiplier'];
							$trans_total_amount = $row['trans_total_amount']*(-1);
							$additional_info = "Αντιλογισμός ".$transaction_id."-".$rid; //"Antilogismos "
					//create transaction.
					mysqli_query($mysqli, "SET NAMES 'utf8'" );	
					mysqli_query ($mysqli, "INSERT INTO cls_transactions 
					(id, customer_id, barcode_id, transaction_type, category, amount, credits, created_on, created_by, transaction_id, trans_total_amount, multiplier, additional_info, reversal, branch_id) 
					VALUES (NULL , '$customer_id', '$barcode_id', '$transaction_type', 'Αντιλογισμός-$transaction_type', '$amount',  '$credits', '$rec_date', '$user', '$random_string', '$amount', '$multiplier', CONCAT(NOW(), ' ".$additional_info."'), '1', '$transaction_branch_id')") or die(mysqli_error($mysqli));			}
					//update mother record
					mysqli_query ($mysqli, "UPDATE cls_transactions SET is_reversed='1' WHERE id='$rid'");
					//find last transaction date
					$sql_find_last="SELECT IFNULL(MAX(created_on),0) AS last FROM cls_transactions a
									INNER JOIN cls_customers b ON a.customer_id = b.id  WHERE 
									a.customer_id='$customer_id' AND ISNULL(is_reversed) AND ISNULL(reversal) OR a.customer_id='$customer_id' AND is_reversed='1' AND reversal='1' AND b.score>0 ";
					$result_find_last = mysqli_query($mysqli, $sql_find_last) or die(mysqli_error($mysqli));
 					$det = mysqli_fetch_assoc($result_find_last);
					$last = $det['last'];				
					
					//update customer header
					if ($transaction_type=='Αγορές'){	
						mysqli_query ($mysqli, "UPDATE cls_customers SET credits = credits + ('$credits'), score= score + ('$credits'), turnover= turnover + ('$amount'), last_transaction_date='$last' WHERE id='$customer_id'") or die(mysqli_error($mysqli));
					}elseif ($transaction_type=='Εξαργύρωση'){	
						mysqli_query ($mysqli, "UPDATE cls_customers set credits = credits + ('$credits'), redeem_amount= redeem_amount + ('$amount'), last_transaction_date='$last' WHERE id='$customer_id'") or die(mysqli_error($mysqli));
					}elseif	($transaction_type=='Σύσταση'){
 				       mysqli_query ($mysqli, "UPDATE cls_customers SET credits = credits + ('$credits'), score= score + ('$credits') WHERE id='$customer_id'") or die(mysqli_error($mysqli));
                     }
                    } //reversed if clause
			}		   // refresh your page
					   //echo "<meta>"; 
					   echo "<meta http-equiv=\"refresh\" content=\"0;URL=".$_SERVER['PHP_SELF']."\">"; 
   					
} 
?>
			</div>
		</fieldset>
	</div>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>