<?php

session_start();

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

include("config.inc.php");
include("Functions_Sales.php");
include("function.inc.php");
//include("Forms_DataGrid.php");
include("Forms_DataGrid_DeliveryStatus.php");
//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($HTTP_SESSION_VARS['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);
}

//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);
}

$query_checkStatus="select FI_DeliveryStatusFrom as statusfrom, FI_DeliveryStatusTo as statusto from ".$HTTP_SESSION_VARS['dbLabel'].".login 
				where user_account='".$HTTP_SESSION_VARS['useraccount']."'"; 
$res_checkstatus=mysql_query($query_checkStatus,$connection) or die(mysql_error());
$row_checkstatus = mysql_fetch_row($res_checkstatus);
$statusfrom = $row_checkstatus[0]; 
$statusto = $row_checkstatus[1];

$Parameters = array();
$Parameters['FI_SalesCheckSalesman'] = $FI_SalesCheckSalesman;
$Parameters['FI_DeliverySalesPrice'] = $FI_DeliverySalesPrice;
$Parameters['Login_NegativeQty'] = $Login_NegativeQty;
$Parameters['Login_ModifySellingPrice'] = $Login_ModifySellingPrice;
$Parameters['Login_ModifyDiscount'] = $Login_ModifyDiscount;
$Parameters['Login_MinAllowCostPer'] = $Login_MinAllowCostPer;
$Parameters['Login_MaxAllowCostPer'] = $Login_MaxAllowCostPer;
$Parameters['Module_Delivery'] = $Module_Delivery;
$Parameters['Module_NegativeQuantity'] = $Module_NegativeQuantity;
$Parameters['Module_Unit1Method'] = $Module_Unit1Method;
$Parameters['Module_Pack'] = $Module_Pack;
$Parameters['Module_DoubleUnit'] = $Module_DoubleUnit;
$Parameters['Module_Project'] = $Module_Project;
$Parameters['Module_CostCenter'] = $Module_CostCenter;
$Parameters['Module_ProjectDt'] = $Module_ProjectDt;
$Parameters['Module_CostCenterDt'] = $Module_CostCenterDt;
$Parameters['Module_RestrictProjCost'] = $Module_RestrictProjCost;
$Parameters['Module_RestrictCost'] = $Module_RestrictCost;
$Parameters['ShowCostCentLayout'] = $ShowCostCentLayout;
$Parameters['Module_SerialNo'] = $Module_SerialNo;
$Parameters['Module_ExpireDate'] = $Module_ExpireDate;
$Parameters['Module_DeliveryClosingQty'] = $Module_DeliveryClosingQty;
$Parameters['Module_DeliveryWithSorder'] = $Module_DeliveryWithSorder;
$Parameters['Sale_MaxExceedQty'] = $Sale_MaxExceedQty;
$Parameters['SalesTTC'] = $SalesTTC;
$Parameters['DecimalBase'] = $DecimalBase;
$Parameters['Setup_VatPercentage'] = $Setup_VatPercentage;
$Parameters['Module_AdditionalTaxes'] = $Module_AdditionalTaxes;
$Parameters['ExportTransportToJV'] = $ExportTransportToJV;
$Parameters['ExportBonusToJV'] = $ExportBonusToJV;
$Parameters['VatApplied'] = $VatApplied;
$Parameters['AllowModifyVAT'] = $AllowModifyVAT;

$Parameters['SetupDftWareHouse'] = $SetupDftWareHouse;
$Parameters['defaultwarehouse']  = $defaultwarehouse;

$Parameters['txtDate'] 		   = $txtDate;
$Parameters['txtClientCode']   = $txtClientCode;
$Parameters['txtClientDetail'] = $txtClientDetail;
$Parameters['txtCurrCode']     = $txtCurr;
$Parameters['Base1']    	   = $base1;
$Parameters['Base2']    	   = $base2;
$Parameters['useraccount']    	   =  $_SESSION['username'];


$Parameters['dbLabel']    	   =  $HTTP_SESSION_VARS['dbLabel'];
$Parameters['useraccount1']    	   =  $HTTP_SESSION_VARS['useraccount'];
$Parameters['Module_CostCenterParent']    	   =  $Module_CostCenterParent;
$Parameters['Module_ProjectParent']    	   =  $Module_ProjectParent;



$_SESSION['gdl_parameters']  = $Parameters;

$actionmode = $_POST['actionmode']; if($actionmode == '') $actionmode=$_GET['actionmode'];


if ($actionmode == "search"){ 
//Read From Module Table
$querymodule=" Select ifNull(Module_Delivery,0) As Module_Delivery, ifNull(Module_DirectStock,0) As Module_DirectStock, ".
		     " ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent, ".
             " ifNull(Module_PrintWaybill,0) As Module_PrintWaybill, ifNull(Module_PostToJournal,0) As Module_PostToJournal From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$Parameters['dbLabel']    	   =  $HTTP_SESSION_VARS['dbLabel'];
$Parameters['useraccount1']    	   =  $HTTP_SESSION_VARS['useraccount'];
$Parameters['Module_CostCenterParent']    	   =  $Module_CostCenterParent;
$Parameters['Module_ProjectParent']    	   =  $Module_ProjectParent;



$_SESSION['gdl_parameters']  = $Parameters;
if($statusfrom == ''  || $statusto == '') $deliveryid= '';
else $deliveryid = GetReference($keyword,$vers,$statusfrom, $statusto, $connection,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess']);	
if (!$deliveryid) {
	$deliveryid=''; //echo $deliveryid; exit;
	$message=1;
}

}

if ($actionmode == "showdeliverydtlot"){

Show_Delivery_DeliveryLot($deliverydt_id,$connection,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$_SESSION['username']);
exit;
}


if ($actionmode == "submitstatus"){

$deliverystatus_id = $_GET["value1"];
$deliverystatus_date = $_GET["value2"];
$status_id = $_GET["value3"];
$remark = $_GET["value4"];
$username = $_GET["value5"];
$delivery_id = $_GET["value6"];
 
 if($deliverystatus_id==''){
 
 $query = "insert into delivery_deliverystatus(deliverystatus_date, status_id, username, remark, delivery_id,deliverystatus_last)
			values('".insertMySQLDate($deliverystatus_date)."'
					,".GetSQLValueString($status_id,'double')."
					,".GetSQLValueString($username,'text')."				
					,".GetSQLValueString($remark,'text')."
					,".GetSQLValueString($delivery_id,'double')."
					,1
					)";
 
	$result=mysql_query($query,$connection)	or die( mysql_error());
 if($result){
		$deliverystatus_id = mysql_insert_id();
		$query = "update  delivery_deliverystatus set deliverystatus_last=0 where deliverystatus_id <> ".$deliverystatus_id." and delivery_id = ".$delivery_id;
		//echo $query;
		$result=mysql_query($query,$connection)	or die( mysql_error());
		
		}
 }
 
 
 
Show_Delivery_DeliveryStatus($delivery_id,$connection,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$_SESSION['username']);
}

if ($actionmode == "submitlot"){

$i=0;
$deliverydt_id = $_GET["value10"];
while($i<10){

$val1='value'.$i;
$val2='value'.$i.$i;
$val3='value'.$i.$i.$i;
//echo $$val1.":".$$val2.":".$$val3."<br>";

if($$val2!='' and $$val3!=''){
 if($$val1==''){
 
 $query = "insert into delivery_lotnumber(lotnumber_qty, lotnumber_number, deliverydt_id)
			values(".GetSQLValueString($$val2,'double')."
					,".GetSQLValueString($$val3,'text')."				
					,".GetSQLValueString($deliverydt_id,'double')."
					)";
// echo  $query;
	$result=mysql_query($query,$connection)	or die( mysql_error());
 
 } else {
 
 $query = "update delivery_lotnumber set lotnumber_qty = ".GetSQLValueString($$val2,'double')."
					, lotnumber_number = ".GetSQLValueString($$val3,'text')."
					where lotnumber_id = ".$$val1;
// echo  $query;
	$result=mysql_query($query,$connection)	or die( mysql_error());
 
 }
}

$i+=1;
}

 
 
 
Show_Delivery_DeliveryLot($deliverydt_id,$connection,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$_SESSION['username']);
}


else if($actionmode=='deletelot'){

	$vid = $_GET["vid"];
	$deliverydt_id = $_GET["value4"];

	$query="delete from delivery_lotnumber where lotnumber_id='".$vid."'";
	$result  = mysql_query($query, $connection);		
	
 

		
Show_Delivery_DeliveryLot($deliverydt_id,$connection,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$_SESSION['username']);
	
 
	

}

function explodeword($date)
{
if ($date!=''){
$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

return $trim;
}
else return null;
}
if($actionmode == 'setStatus'){
	$deliveryid = $_GET['deliveryid'];
	$statusid = $_GET['statusid'];
	$query_update = "update delivery_deliverystatus set deliverystatus_last=0 where delivery_id=$deliveryid and deliverystatus_last=1";
	$result_update = mysql_query($query_update, $connection);
	$query_update2 = "update deliveries set Delivery_ShippedVia = '$txtShippedVia' where delivery_id=$deliveryid";
	$result_update2 = mysql_query($query_update2, $connection);
	$query = "insert into delivery_deliverystatus (deliverystatus_date, status_id, username, remark, delivery_id, deliverystatus_last) values 
				(now(), $statusid, '$username', '$txtstatusRemark', $deliveryid, 1 )";
	$result = mysql_query($query, $connection);		
	

}
if($actionmode == 'setStatus') header("location:./Form_SetDeliveryStatus.php");
else header("location:./Form_SetDeliveryStatus.php?deliveryid=$deliveryid&msg=$message");
?>