<?php

session_start();

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("Functions_Sales.php");
include("function.inc.php");

if (!ini_get('register_globals')) {
    $superglobals = array($_SERVER, $_ENV,
        $_FILES, $_COOKIE, $_POST, $_GET);
    if (isset($_SESSION)) {
        array_unshift($superglobals, $_SESSION);
    }
    foreach ($superglobals as $superglobal) {
        extract($superglobal, EXTR_SKIP);
    }
}
//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


$querycostcent=" select costcent_applyvat ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
 			   "";
			   
$costcentres = mysql_query($querycostcent,$connection) or die(mysql_error());
$rows=mysql_fetch_array($costcentres);
$costcent_applyvat = $rows[0];
if($costcent_applyvat=='')	$costcent_applyvat = 1;

//Read From Module Table
$querymodule=" Select Module_AdditionalTaxes, ifNull(Module_NegativeQuantity,0) As Module_NegativeQuantity, ifNull(Module_rawmaterial,0) As Module_RawMaterial, ".
			 " ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ifNull(module_paymentterms,0) As Module_PaymentTerms, ".
             " ifNull(module_creditlimit,0) As Module_CreditLimit, ifNull(Module_Delivery,0) As Module_Delivery, ".
			 " ifNull(module_DeliveryWithSorder,0) As Module_DeliveryWithSorder, ifNull(Module_InvoiceCheckLimit,0) As Module_InvoiceCheckLimit, ".
			 " ifNull(Module_PostToJournal,0) As Module_PostToJournal, ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter, ".
			 " ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject,ifNull(Module_CreditLimitByProj,0) As Module_CreditLimitByProj,  ".
			 " ifNull(Module_SerialNo,0) As Module_SerialNo, ifNull(Module_Unit1Method,0) As Module_Unit1Method, ifNull(Module_ChangeInvoiceItems,0) As Module_ChangeInvoiceItems, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt, ".
			 " ifNull(Module_ExpireDate,0) As Module_ExpireDate ".
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


//Read From SalesSetup Table
$querySalesSetup="Select SalesTTC, TipsAccount, CashCur From SalesSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}

//User Access
$querylog=" select ifNull(Fa_clientpaymentterms,0) As FA_ClientPaymentTerms, ifNull(Clientscreditlimit,0) As FA_ClientCreditLimit, ".
          " ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(FI_DeliverySalesPrice,0) As FI_DeliverySalesPrice, ifNull(ModifyVAT,0) As ModifyVAT, ".
		  " ifNull(FI_AllowEditIfDelivered,0) As FI_AllowEditIfDelivered, ifNull(FI_ChangeInvoiceItems,0) As FI_ChangeInvoiceItems, ".
		  " ifNull(FI_FrmRestrictWarehouse,0) As FI_FrmRestrictWarehouse, ifNull(Login_negativeQty,0) As Login_negativeQty ".
		  " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

/*
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<script type="text/javascript" language="javascript">


</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>
<form name='insertsales' method=''>
<input type='text' name='mode' value='$mode'>
<input type='text' name='mainmode' value='$mainmode'>
<input type='text' name='SSaId' value='$SSaId'>
<input type='text' name='SSaDtId' value='$SSaDtId'>
<input type='text' name='SOption' value='$SOption'>
<input type='text' name='SSaVatChkbx' value='$SSaVatChkbx'>
<input type='text' name='SSorRef' value='$SSorRef'>
HERE;
*/
$message='';

//$mode=$_GET['mode'];
//echo $mode;
//exit;
 
if ($mode == "getclientname"){
	
		$query = "SELECT Ledger_number,Ledger_name 
					FROM Ledger ";
					
		if($choice==0)
			$query = $query." WHERE Upper(Ledger_number) like Upper('".str_replace('\\','\\\\',$clientcode)."%') ";
		else
			$query = $query." WHERE Upper(Ledger_name) like Upper('%".str_replace('\\','\\\\',$clientcode)."%') ";
		$result=mysql_query($query,$connection) or die(mysql_error());			
		$row=mysql_fetch_array($result);
		$totalrows = mysql_num_rows($result);
		$ClientCode = $row[0];
		$ClientName = $row[1];
		
		
		if($totalrows>1){
		echo '';
		echo"[BRK]";
		echo '';
		} else {
		
		echo $ClientCode;
		echo"[BRK]";
		echo $ClientName;
		}
exit;
}
 else
if ($mode == "getprojectname"){
	
    $query = "SELECT project_code,project_name 
                FROM project ";
                
    if($choice==0)
        $query = $query." WHERE Upper(project_code) like Upper('".str_replace('\\','\\\\',$projectcode)."%') ";
    else
        $query = $query." WHERE Upper(project_name) like Upper('%".str_replace('\\','\\\\',$projectcode)."%') ";
    $result=mysql_query($query,$connection) or die(mysql_error());			
    $row=mysql_fetch_array($result);
    $totalrows = mysql_num_rows($result);
    $projectCode = $row[0];
    $projectName = $row[1];
    
    
    if($totalrows>1){
    echo '';
    echo"[BRK]";
    echo '';
    } else {
    
    echo $projectCode;
    echo"[BRK]";
    echo $projectName;
    }
exit;
}
else
if ($mode == "getsalesmanname"){
	
		$query = "SELECT Ledger_number,Ledger_name 
					FROM salesmen ";
					
		if($choice==0)
			$query = $query." WHERE Upper(Ledger_number) = Upper('".str_replace('\\','\\\\',$clientcode)."') ";
		else
			$query = $query." WHERE Upper(Ledger_name) = Upper('".str_replace('\\','\\\\',$clientcode)."') ";
					
		$result = selectData($connection, $query);
		$clientsalesman = $result[0];
		$clientsalesmanname = $result[1];
		echo $clientsalesman;
		echo"[BRK]";
		echo $clientsalesmanname;
 
exit;
}











?>