<?
function generateNumber($template_id,$date,$table,$date_field,$prefix,$show_number)
{
	global $timedate;
	$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmdocumenttemplates where id='".$template_id."'"));
	//print mysql_error();
	$per=$r['documents_per'];
	$format=$r['document_number_format_id'];
	$date=$timedate->to_display($date, $timedate->get_date_format(), "Y-m-d");
	$exp=explode("-",$date);
	if($per=="day")$daten=$date;
	if($per=="month")$daten=$exp[0]."-".$exp[1];
	if($per=="year")$daten=$exp[0];
	$z="select number from ".$table." where ".$date_field." like '".$daten."%' order by number desc limit 1";
	print $z;
	$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query($z));
	print mysql_error();
	$no=$r['number']+1;
	//$number=$_GET['prefix']."/".$no."/".$exp[1]."/".$exp[0];
	if($format[1]=="/")$sep="/";
	elseif($format[1]==".")$sep=".";
	elseif($format[1]=="-")$sep="-";
	else $sep="";

	if($per=="month")
	{
		$format=str_replace("D "," ",$format);
		$format=str_replace("D-","",$format);
		$format=str_replace("D/","",$format);
		$format=str_replace("D.","",$format);
	}
	if($per=="year")
	{
		$format=str_replace("D "," ",$format);
		$format=str_replace("D-","",$format);
		$format=str_replace("M-","",$format);
		$format=str_replace("D/","",$format);
		$format=str_replace("M/","",$format);
		$format=str_replace("D.","",$format);
		$format=str_replace("M.","",$format);
	}
	$format=str_replace("Y",$exp[0],$format);
	$format=str_replace("M",$exp[1],$format);
	$format=str_replace("D",$exp[2],$format);
	$format=str_replace("NR",$no,$format);

	$number=$prefix.$sep.$format;
	if($show_number)return $number;
	else return $no;
}
function addToBooking($pl,$stock_in_id)
{
	/*$quantity=$pl['quantity'];
	$z="select quantity,order_id,id from ecmstockbookings where product_id='".$pl['id']."' and ecmstockin_id='' order by date_entered asc";
	$w=$GLOBALS['db']->query($z);
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		if($quantity>0)
		{
			if($r['quantity']<=$quantity)
			{
				$qty=$r['quantity'];
				$all=1;
			}
			else
			{
				$qty=$quantity;
				$need=$r['quantity']-$quantity;
				$all=0;
			}
			$quantity-=$qty;
			$data[]=array("stock_in_id"=>$stock_in_id,"qty"=>$qty,"product_id"=>$pl['id']);
			if($all==0)$data[]=array("stock_in_id"=>"","qty"=>$need,"product_id"=>$pl['id']);
			if($all==1)
			{
				//print "jest";
				$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id from acl_roles where name like 'manager'"));
				addInfoToReminder($r['order_id'],"EcmSales","Products Available","There are available products in stock for Your sales order",$rr['id']);
			}
			addToDbBookedProducts($data,$r['order_id']);
			$GLOBALS['db']->query("delete from ecmstockbookings where id='".$r['id']."'");
		}
	}*/
}
function addInfoToReminder($parent_id,$parent_module,$name,$description,$role_id,$time="")
{
	/*if($time=="")$time=date("Y-m-d H:i:s",mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"))-7200);
	else
	{
		$texp=explode(" ",$time);
		$dtexp=explode("-",$texp[0]);
		$ttexp=explode(":",$texp[1]);
		$time=date("Y-m-d H:i:s",mktime($ttexp[0],$ttexp[1],$ttexp[2],$dtexp[1],$dtexp[2],$dtexp[0])-7200);
	}
	$z="insert into ecmreminders(id,name,date_entered,date_modified,modified_user_id,created_by,description,deleted,assigned_user_id,date_start,status,reminder_time,parent_id,parent_module,role_id) values('".create_guid()."','".$name."','".$time."','".$time."','".$_SESSION['authenticated_user_id']."','".$_SESSION['authenticated_user_id']."','".$desciption."','0','".$_SESSION['authenticated_user_id']."','".$time."','Planned','3600','".$parent_id."','".$parent_module."','".$role_id."')";
	$GLOBALS['db']->query($z);*/
}
function insertProductsToStock($pl,$return_id,$stock_id)
{
	$guid=create_guid();
	$z="insert into ecmstockins(id,assigned_user_id,modified_user_id,date_entered,date_modified,created_by,product_id,pz_id,stock_id,quantity_in,quantity_out,price_in,date_in) values('".$guid."','".$_SESSION['authenticated_user_id']."','".$_SESSION['authenticated_user_id']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','".$_SESSION['authenticated_user_id']."','".$pl['id']."','".$return_id."','".$stock_id."','".$pl['quantity']."','0','".$pl['price']."','".date("Y-m-d H:i:s")."')";
	$GLOBALS['db']->query($z);
	addRelationPurchaseOrderStockIn($return_id,$guid);
	//addToBooking($pl,$guid);
}
function getStatus($pl,$stock_id)
{
	$status="products in stock";
	for($i=0; $i<count($pl); $i++)
	{  
		$w=$GLOBALS['db']->query("select * from ecmstockins where product_id='".$pl[$i]['id']."' and stock_id='".$stock_id."'");
		if(mysql_num_rows($w)==0)$status="registered";
		else
		{
			while($r=$GLOBALS['db']->fetchByAssoc($w))
			{
				if($r['quantity_in']-$r['quantity_out']<$pl[$i]['quantity'])
				{
					$status="registered";
					break;
				}
			}
		}
		if($status=="registered")break;
	}
	return $status;
}
function bookProducts($pl,$stock_id,$order_id)
{
	/*$data="";
	for($i=0; $i<count($pl); $i++)
	{  
		$qty=$pl[$i]['quantity'];
		$w=$GLOBALS['db']->query("select * from ecmstockins where product_id='".$pl[$i]['id']."' and stock_id='".$stock_id."' and (quantity_in-quantity_out)>0 order by date_entered asc");
		while($r=$GLOBALS['db']->fetchByAssoc($w))
		{
			$quantity=$r['quantity_in']-$r['quantity_out'];
			$ww=$GLOBALS['db']->query("select * from ecmstockbookings where ecmstockin_id='".$r['id']."' and used='0'");
			$used_qty=0;
			while($rr=$GLOBALS['db']->fetchByAssoc($ww))$used_qty+=$rr['quantity'];
			if($quantity>=$qty+$used_qty)
			{
				$data[]=array(
					"stock_in_id"=>$r['id'],
					"qty"=>$qty,
					"product_id"=>$pl[$i]['id']
				);
				$qty=0;
				break;
			}
			elseif($quantity<$qty+$used_qty)
			{
				if($quantity-$used_qty>0)
				{
					$data[]=array(
						"stock_in_id"=>$r['id'],
						"qty"=>$quantity-$used_qty,
						"product_id"=>$pl[$i]['id']
					);
					$qty-=$quantity-$used_qty;
				}
			}
		}
		if($qty>0)$data[]=array("stock_in_id"=>"","qty"=>$qty,"product_id"=>$pl[$i]['id']);
		//print_r($data);
		addToDbBookedProducts($data,$order_id);
		$data=array();
	}*/
}
function deleteProductFromStockIn($stock_in_id,$product_id,$qty)
{
	$GLOBALS['db']->query("update ecmstockins set quantity_out=quantity_out+".$qty." where id='".$stock_in_id."' and product_id='".$product_id."'");
}
function addProductToStockOut($stock_in_id,$product_id,$quantity,$price,$date,$wz_id,$stock_id)
{
	$z="insert into 
	ecmstockouts(
	id,
	assigned_user_id,
	modified_user_id,
	date_entered,
	date_modified,
	created_by,
	product_id,
	quantity_out,
	price_out,
	date_out,
	wz_id,
	stock_id,
	ecmstockin_id,
	deleted,
	name
	) values(
	'".create_guid()."',
	'".$_SESSION['authenticated_user_id']."',
	'".$_SESSION['authenticated_user_id']."',
	'".date("Y-m-d H:i:s")."',
	'".date("Y-m-d H:i:s")."',
	'".$_SESSION['authenticated_user_id']."',
	'".$product_id."',
	'".$quantity."',
	'".$price."',
	'".$date."',
	'".$wz_id."',
	'".$stock_id."',
	'".$stock_in_id."',
	'0',
	'out')";
	//print $z;
	$GLOBALS['db']->query($z);
	//print mysql_error();
}
function addToDbBookedProducts($data,$order_id)
{
	/*$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select booking_time from ecmstocksettings"));
	$time=$r['booking_time'];
	foreach($data as $row)
	{
		$GLOBALS['db']->query("insert into ecmstockbookings(id,assigned_user_id,modified_user_id,date_entered,date_modified,created_by,product_id,order_id,ecmstockin_id,valid_date,quantity,used) values('".create_guid()."','".$_SESSION['authenticated_user_id']."','".$_SESSION['authenticated_user_id']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','".$_SESSION['authenticated_user_id']."','".$row['product_id']."','".$order_id."','".$row['stock_in_id']."','".date("Y-m-d H:i:s",time()+$time)."','".$row['qty']."','0')");
		$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select pz_id from ecmstockins where id='".$row['stock_in_id']."'"));
		$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select po_id from ecmpzdocuments where id='".$r['pz_id']."'"));
		$GLOBALS['db']->query("insert into 
		ecmsales_ecmstockins
		(id,date_modified,so_id,ecmstockin_id,deleted) 
		values
		('".create_guid()."','".date("Y-m-d H:i:s")."','".$order_id."','".$row['stock_in_id']."','0')");
		$GLOBALS['db']->query("insert into 
		ecmsales_ecmpurchaseorders
		(id,date_modified,so_id,po_id,deleted) 
		values
		('".create_guid()."','".date("Y-m-d H:i:s")."','".$order_id."','".$rr['po_id']."','0')");
		$GLOBALS['db']->query("insert into 
		ecmsales_ecmpzdocuments
		(id,date_modified,so_id,pz_id,deleted) 
		values
		('".create_guid()."','".date("Y-m-d H:i:s")."','".$order_id."','".$r['pz_id']."','0')");
	}*/
}
function addRelationPurchaseOrderStockIn($pz_id,$stock_in_id)
{
	$w=$GLOBALS['db']->query("select po_id from ecmpzdocuments where id='".$pz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecmpurchaseorders_ecmstockins(id,date_modified,po_id,ecmstockin_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$r['po_id']."','".$stock_in_id."','0')");
	}
}
function addRelationWzDocumentStockIn($so_id,$wz_id)
{
	$w=$GLOBALS['db']->query("select ecmstockin_id from ecmsales_ecmstockins where so_id='".$so_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecmwzdocuments_ecmstockins(id,date_modified,wz_id,ecmstockin_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$wz_id."','".$r['ecmstockin_id']."','0')");
	}
}
function addRelationWzDocumentPzDocument($so_id,$wz_id)
{
	$w=$GLOBALS['db']->query("select pz_id from ecmsales_ecmpzdocuments where so_id='".$so_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecmwzdocuments_ecmpzdocuments(id,date_modified,wz_id,pz_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$wz_id."','".$r['pz_id']."','0')");
	}
}
function addRelationWzDocumentPurchaseOrder($so_id,$wz_id)
{
	$w=$GLOBALS['db']->query("select po_id from ecmsales_ecmpurchaseorders where so_id='".$so_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecmwzdocuments_ecmpurchaseorders(id,date_modified,wz_id,po_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$wz_id."','".$r['po_id']."','0')");
	}
}
function addRelationInvoicePurchaseOrder($wz_id,$ecminvoiceout_id)
{
	$w=$GLOBALS['db']->query("select po_id from ecmwzdocuments_ecmpurchaseorders where wz_id='".$wz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecminvoiceouts_ecmpurchaseorders(id,date_modified,ecminvoiceout_id,po_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$ecminvoiceout_id."','".$r['po_id']."','0')");
	}
}
function addRelationInvoicePzDocument($wz_id,$ecminvoiceout_id)
{
	$w=$GLOBALS['db']->query("select pz_id from ecmwzdocuments_ecmpzdocuments where wz_id='".$wz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecminvoiceouts_ecmpzdocuments(id,date_modified,ecminvoiceout_id,pz_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$ecminvoiceout_id."','".$r['pz_id']."','0')");
	}
}
function addRelationInvoiceSalesOrder($wz_id,$ecminvoiceout_id)
{
	$w=$GLOBALS['db']->query("select so_id from ecmwzdocuments where id='".$wz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecminvoiceouts_ecmsales(id,date_modified,ecminvoiceout_id,so_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$ecminvoiceout_id."','".$r['so_id']."','0')");
	}
}
function addRelationDeliveryNoteSalesOrder($wz_id,$dn_id)
{
	$w=$GLOBALS['db']->query("select so_id from ecmwzdocuments where id='".$wz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecmdeliverynotes_ecmsales(id,date_modified,dn_id,so_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$dn_id."','".$r['so_id']."','0')");
	}
}
function addRelationInvoiceStock($wz_id,$ecminvoiceout_id)
{
	$w=$GLOBALS['db']->query("select ecmstockin_id from ecmwzdocuments_ecmstockins where wz_id='".$wz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		//print "ok<br>";
		$GLOBALS['db']->query("insert into ecminvoiceouts_ecmstockins(id,date_modified,ecminvoiceout_id,ecmstockin_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$ecminvoiceout_id."','".$r['ecmstockin_id']."','0')");
	}
	//print mysql_error();
}
function addRelationDeliveryNoteInvoice($wz_id,$dn_id)
{
	$w=$GLOBALS['db']->query("select so_id from ecmwzdocuments where wz_id='".$wz_id."'");
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ecminvoiceouts_ecmsales(id,date_modified,ecminvoiceout_id,so_id,deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$ecminvoiceout_id."','".$r['so_id']."','0')");
	}
}
function addRelation($r_field,$r_relation,$r_id,$r_value,$l_field,$l_relation,$l_value)
{
	$z="select ".$r_field." from ".$r_relation." where ".$r_id."='".$r_value."'";
	print $z."<br>";
	$w=$GLOBALS['db']->query($z);
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$GLOBALS['db']->query("insert into ".$l_relation."(id,date_modified,".$l_field.",".$r_field.",deleted) values('".create_guid()."','".date("Y-m-d H:i:s")."','".$l_value."','".$r[$r_field]."','0')");
	print mysql_error();
	}	
}
?>
