<?php
/**********************************************************************
	Copyright (C) Evolve Techniologies
	Author : Danushka Widanage
	Email : dwidanage@gmail.com
	Company : Evolve Technologies(pvt)Ltd
***********************************************************************/
$page_security = 'SA_CUSTOMER_BALANCE';
$path_to_root="../..";

include($path_to_root . "/includes/session.inc");
add_access_extensions();

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");


$action = 'import';
if (isset($_GET['action'])) $action = $_GET['action'];
if (isset($_POST['action'])) $action = $_POST['action'];

page("Customer Opening Balances");

//------------------check the item status--------------
function check_customer($cust_code){
	echo $sql = "SELECT * FROM `".TB_PREF."debtors_master` WHERE debtor_ref = $cust_code";
}
				

if (isset($_POST['import'])) {
	if (isset($_FILES['imp']) && $_FILES['imp']['name'] != '') {
		$filename = $_FILES['imp']['tmp_name'];
		$sep = $_POST['sep'];
		$today = date('y-m-d');

		$fp = @fopen($filename, "r");
		if (!$fp)
			die("can not open file $filename");
			
		$lines = $i = $j = 0;
		while ($data = fgetcsv($fp, 8192, $sep)) {
			if ($lines++ == 0) continue;
			list($type, $cust_code, $credit_limit, $outstanding) = $data;
			$type = strtoupper($type);
			
			echo $cust_short_key = preg_replace('/[^a-zA-Z0-9-]/', '', $cust_code);	
			echo '<br>';
			
			
			if ($type == 'CUST_BAL') {
				begin_transaction();
				
				//------------------chechk the excisting customer-------------
				$result_customer = check_customer($cust_short_key);
				
				
				/*if(($result_serial_import == 0) && ($check_item == 1)){
					
				}else{
					if($result_serial_import == 1)
						display_error(_($item_code." item already in the system with serila no ".$serial_number));	
					if($check_item == 0)
						display_error(_($item_code." item Not in the system"));
					clear_import_items($trans_no);	
				}*/

				  $i++;
				commit_transaction();
			}
		}
		
	
		@fclose($fp);

		//if ($i+$j > 0) display_notification("$i Record posts created, $j Record posts updated.");

	} else display_error("No CSV file selected");
	
		
}

if ($action == 'import') echo 'Import';
else hyperlink_params($_SERVER['PHP_SELF'], _("Import"), "action=import", false);

echo "<br><br>";

if ($action == 'import') {
    start_form(true);

    start_table(TABLESTYLE2, "width=40%");


    table_section_title("Separator, Doc Type, Doc No and File");
    text_row("Field separator:", 'sep', ',', 2, 1);
?>
<tr>
<td>Export Type:</td>
<td><select  name='import_type' class='combo' title='' >
<option value='25'>GRN</option>
<option value='18'>Purchase Order</option>
<option value='20'>Direct Invoice</option>
</select>
</td>
</tr>
<?php
	
//	text_row("Trans No", 'trans_no', $_POST['trans_no'], 20, 20);
    label_row("CSV Import File:", "<input type='file' id='imp' name='imp'>");

    end_table(1);

    submit_center('import', "Import CSV File");

    end_form();
}
    end_page();
?>
