<?php

include './core.php';

if( !isset( $user ) || !$user->can_access( 'vendoren' ) ) access_error();

$lists = loadlists();

if(isset($_REQUEST['new']) || isset($_REQUEST['edit'])) {
	$herstellungsids = $MySQL->fetch_complete("SELECT her_herstellungsid, name_interface FROM server.herstellung ORDER BY `name_interface` ASC");
	$Smarty->assign('herstellungsids', $herstellungsids);
}

if( isset( $_REQUEST['new'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'list_id', 'herstellungsid', 'menge' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	    else {
	    	$res = $MySQL->query_first("SELECT COUNT(*) FROM server.sellbuy_list_herstellung WHERE sellbuy_lists_id=".$_REQUEST['list_id']." AND herstellung_her_herstellungsid=".$_REQUEST['herstellungsid']);
	    	if($res['COUNT(*)'] > 0) {
	    		$form_errors[] = form_error( 'duplicate_verkauf_herstellung' );
	    	}
	    }
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'list_id', 'herstellungsid', 'menge' );
			
			$page['content'] = $Smarty->fetch( 'vlists_new.tpl' );
			
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("INSERT INTO server.sellbuy_list_herstellung
	        		(sellbuy_lists_id, herstellung_her_herstellungsid, menge, lastchangedby)
	        	VALUES
	        		(".$_REQUEST['list_id'].",
	        		".$_REQUEST['herstellungsid'].",
	        		'".$_REQUEST['menge']."',
	        		".$user->ID.")");
	    }
	}
	
	$page['content'] = $Smarty->fetch( 'vlists_new.tpl' );
	
    $Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
	die();
}

if( isset( $_REQUEST['newlist'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'name', 'beschreibung' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'name', 'beschreibung' );
	    }
	    else
	    {
	        $MySQL->query("INSERT INTO server.sellbuy_lists
	        		(name, beschreibung, lastchangedby)
	        	VALUES
	        		('".$_REQUEST['name']."',
	        		'".$_REQUEST['beschreibung']."',
	        		".$user->ID.")");
	        
	        $lists = loadlists();
	    }
	} else {
		$page['content'] = $Smarty->fetch( 'vlists_newlist.tpl' );
		
	    $Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

if( isset( $_REQUEST['del'] ) )
{
    $MySQL->query( "DELETE FROM server.sellbuy_list_herstellung WHERE herstellung_her_herstellungsid = ".intval( $_REQUEST['del'] )." AND sellbuy_lists_id = ".intval( $_REQUEST['list'] ) );
}

if( isset( $_REQUEST['dellist'] ) )
{
    $MySQL->query( "DELETE FROM server.sellbuy_list_herstellung WHERE sellbuy_lists_id = ".intval( $_REQUEST['dellist'] ) );	
	$MySQL->query( "DELETE FROM server.sellbuy_lists WHERE id = ".intval( $_REQUEST['dellist'] ) );
	$lists = loadlists();
}

if( isset( $_REQUEST['edit'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'list_id', 'herstellungsid', 'menge' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'list_id', 'herstellungsid', 'menge' );
	    	
	    	$page['content'] = $Smarty->fetch( 'vlists_edit.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("UPDATE server.sellbuy_list_herstellung
	        	SET
	        		sellbuy_lists_id='".$_REQUEST['list_id']."',
	        		herstellung_her_herstellungsid='".$_REQUEST['herstellungsid']."',
	        		menge='".$_REQUEST['menge']."',
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 herstellung_her_herstellungsid = ".intval( $_REQUEST['edit'] )."
	        		 AND sellbuy_lists_id = ".intval( $_REQUEST['list'] ));
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( "SELECT * FROM server.sellbuy_list_herstellung WHERE herstellung_her_herstellungsid = ".intval( $_REQUEST['edit'] )." AND sellbuy_lists_id = ".intval( $_REQUEST['list'] ) );
		$formvalues['list'] = $_REQUEST['list'];
		$formvalues['edit'] = $_REQUEST['edit'];
		$formvalues['list_id'] = $formvalues['sellbuy_lists_id'];
		$formvalues['herstellungsid'] = $formvalues['herstellung_her_herstellungsid'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'vlists_edit.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

if( isset( $_REQUEST['editlist'] ) )
{
	if( isset( $_POST['send_form'] ) )
	{
	    $form_errors = array();
	
	    if( !check_fields( 'name', 'beschreibung' ) )
	        $form_errors[] = form_error( 'uncomplete' );
	
	    if( count( $form_errors ) > 0 )
	    {
	        $Smarty->assign( 'form_errors', $form_errors );
	        assign_formvalues( 'name', 'beschreibung', 'editlist' );
	    	
	    	$page['content'] = $Smarty->fetch( 'vlists_editlist.tpl' );
		
			$Smarty->assign( 'page', $page );
			$Smarty->display( 'main.tpl' );
			
			die();
	    }
	    else
	    {
	        $MySQL->query("UPDATE server.sellbuy_lists
	        	SET
	        		name='".$_REQUEST['name']."',
	        		beschreibung='".$_REQUEST['beschreibung']."',
	        		lastchangedby='".$user->ID."'
	        	WHERE 
	        		 id = ".intval( $_REQUEST['editlist'] ));
	        
	        $lists = loadlists();
	    }
	}
	else
	{
		$formvalues = $MySQL->query_first( "SELECT * FROM server.sellbuy_lists WHERE id = ".intval( $_REQUEST['editlist'] ) );
		$formvalues['editlist'] = $_REQUEST['editlist'];
		$Smarty->assign( 'formvalues', $formvalues );
		
		$page['content'] = $Smarty->fetch( 'vlists_editlist.tpl' );
	
		$Smarty->assign( 'page', $page );
		$Smarty->display( 'main.tpl' );
		
		die();
	}
}

$list_id = $lists[0][0];
if(isset($_REQUEST['list_id'])) {
	$list_id = intval($_REQUEST['list_id']);
	$formvalues = array('list_id' => $list_id);
	$Smarty->assign('formvalues', $formvalues);
}

if(!empty($list_id)) {
	$entries = $MySQL->fetch_complete( "
		SELECT 
			server.sellbuy_list_herstellung.*, 
			server.herstellung.name_interface AS name
		FROM server.sellbuy_list_herstellung
		JOIN server.herstellung ON(server.sellbuy_list_herstellung.herstellung_her_herstellungsid=server.herstellung.her_herstellungsid)
		WHERE sellbuy_lists_id=".$list_id."
		ORDER BY menge DESC" );
}

$Smarty->assign('entries', $entries);

$page['content'] = $Smarty->fetch( 'vlists.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

function loadlists() {
	global $MySQL, $Smarty;
	
	$lists = $MySQL->fetch_complete("SELECT id, name FROM server.sellbuy_lists ORDER BY `name` ASC");
	$Smarty->assign('lists', $lists);
	
	return $lists;
}

?>