<?php

	require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');

	/**
	 * Meta Tabelle
	 * Gedacht um zukünftig erweiterte Daten zu Bestellungen, Produkten etc. zu speichern
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_META." (
		id mediumint(9) NOT NULL AUTO_INCREMENT,
		meta_table varchar(255) NOT NULL,
		target_id int(9) NOT NULL,		
		meta_key varchar(255) NOT NULL,		
		meta_value longtext NOT NULL,
		PRIMARY KEY  (id),
		KEY target_id (target_id),
		KEY meta_key (meta_key),		
		KEY meta_table (meta_table)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";	
	
	dbDelta($sql);
	
	/**
	 * Produkttabelle
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_PRODUCTS." (
	  	id mediumint(9) NOT NULL AUTO_INCREMENT,		
	  	cdate datetime NOT NULL,		  
	  	partikel INT(11) NOT NULL,
	  	name VARCHAR(255) NOT NULL,	
	  	detailname VARCHAR(500) NOT NULL,
	  	anr VARCHAR(100) NOT NULL,
	  	typ VARCHAR(100) NOT NULL, 
	  	preis DOUBLE(10,2) NOT NULL,
		mwst_key VARCHAR(1) NOT NULL,
	  	beschreibung longtext NOT NULL,
	  	pgruppe INT(11) NOT NULL,
	  	ptemplate_file VARCHAR(255) NOT NULL,
	  	deleted INT(1) NOT NULL,
	  	lang_parent INT(11) NOT NULL,
	  	lang_locale VARCHAR(11) NOT NULL,
	  	rabatt VARCHAR(255) NOT NULL,	  	
	  	posturl VARCHAR(500) NOT NULL,
	  	posturl_verkauf INT(1) NOT NULL,
	  	posturl_bezahlung INT(1) NOT NULL,
	  	produktvars VARCHAR(255) NOT NULL,
		allowedpayments VARCHAR(255) NOT NULL,
		allowedshipping VARCHAR(255) NOT NULL,
		euleistungsortregel INT(1) NOT NULL,
		basket_multiple INT(1) NOT NULL, 
	  	PRIMARY KEY  (id),
	  	KEY lang_parent (lang_parent)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
	
	dbDelta($sql);
 	
	/**
	 * Kundentabelle
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_KU." (
   		id int(25) NOT NULL AUTO_INCREMENT,
   		knr VARCHAR(255) NOT NULL,
   		paypal_payer_id VARCHAR(255) NOT NULL,
   		title VARCHAR(50) NOT NULL,
   		name VARCHAR(255) NOT NULL,	   			
   		vname VARCHAR(255) NOT NULL,
   		email VARCHAR(255) NOT NULL,
   		firma VARCHAR(255) NOT NULL,   		
   		fax VARCHAR(255) NOT NULL,
   		strasse VARCHAR(255) NOT NULL,
   		plz VARCHAR(255) NOT NULL,
   		ort VARCHAR(255) NOT NULL,
   		land VARCHAR(100) NOT NULL, 
   		tel VARCHAR(100) NOT NULL,
   		geb DATE NOT NULL,
   		ustidnr VARCHAR(100) NOT NULL,   		
   		custom TEXT NOT NULL,
   		PRIMARY KEY  (id),
   		KEY land (land)
   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   	
   	dbDelta($sql);
	
	/**
	 * Tabelle für die Bestellungen
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_ORDER." (
   		id int(25) NOT NULL AUTO_INCREMENT,
   		onr VARCHAR(255) NOT NULL, 
   		cdate datetime NOT NULL,
   		k_id mediumint(9) NOT NULL, 
   		ip VARCHAR(15) NOT NULL,
   		useragent VARCHAR(255) NOT NULL,
   		comment TEXT NOT NULL,
   		price_gesamt DOUBLE(10,4) NOT NULL,
		price_gesamt_netto DOUBLE(10,4) NOT NULL,
		price_gesamt_brutto DOUBLE(10,4) NOT NULL,
   		price_shipping DOUBLE(10,4) NOT NULL,
		price_shipping_netto DOUBLE(10,4) NOT NULL,
		price_shipping_brutto DOUBLE(10,4) NOT NULL,			
   		price_payment DOUBLE(10,4) NOT NULL,
		price_payment_netto DOUBLE(10,4) NOT NULL,
		price_payment_brutto DOUBLE(10,4) NOT NULL,
   		price_rabatt DOUBLE(10,4) NOT NULL,
   		price_gs DOUBLE(10,4) NOT NULL,
   		mwst_payment DOUBLE(10,4) NOT NULL,
   		mwst_shipping DOUBLE(10,4) NOT NULL,
   		type_shipping VARCHAR(255) NOT NULL,
   		type_payment VARCHAR(255) NOT NULL,
   		shipping_title VARCHAR(255) NOT NULL,
   		shipping_vname VARCHAR(255) NOT NULL,
		shipping_name VARCHAR(255) NOT NULL,				
	  	shipping_strasse VARCHAR(255) NOT NULL,
	  	shipping_hausnr VARCHAR(255) NOT NULL,
	  	shipping_plz VARCHAR(255) NOT NULL,
	  	shipping_ort VARCHAR(255) NOT NULL,
	  	shipping_land VARCHAR(100) NOT NULL,
	  	shipping_firma VARCHAR(255) NOT NULL,
	  	bname VARCHAR(500) NOT NULL,
		bblz VARCHAR(500) NOT NULL,
		binhaber VARCHAR(500) NOT NULL,
		payed_date datetime NOT NULL,
		bnr VARCHAR(500) NOT NULL,
		bvars TEXT NOT NULL,
		pvars text NOT NULL,
   		status int(5) NOT NULL,
   		transaction varchar(255) NOT NULL,   			
   		dp_cron_planed datetime NOT NULL,
   		dp_cron_done datetime NOT NULL,
   		gs_id INT(11) NOT NULL,
   		kleinunternehmer INT(1) NOT NULL,
   		custom_data TEXT NOT NULL,
   		admincomment TEXT NOT NULL,
   		language VARCHAR(10) NOT NULL,
   		PRIMARY KEY  (id),
   		KEY k_id (k_id),
   		KEY gs_id (gs_id),
   		KEY shipping_land (shipping_land)
   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
	
	dbDelta($sql);
	
	/**
	 * Tabelle für die bestellten Produkte
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_ORDERPRODUCT." (
   		id mediumint(9) NOT NULL AUTO_INCREMENT,
   		o_id int(25) NOT NULL,
   		p_id int(25) NOT NULL,
		productkey varchar(255) NOT NULL,
		product_index int(11) NOT NULL,
   		menge mediumint(9) NOT NULL,
   		price DOUBLE(10,4) NOT NULL, 
		price_netto DOUBLE(10,4) NOT NULL,
		price_brutto DOUBLE(10,4) NOT NULL,
		mwst_key varchar(1) NOT NULL,   		
   		mwst_value double(4,2) NOT NULL,
   		mod_downloadprodukt_counter int(11) NOT NULL,
   		mod_vp_varkey VARCHAR( 255 ) NOT NULL, 
		allowedpayments VARCHAR( 255 ) NOT NULL, 
		allowedshipping VARCHAR( 255 ) NOT NULL,
		PRIMARY KEY  (id),
   		KEY o_id (o_id),
   		KEY p_id (p_id)
   	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
   	
   	dbDelta($sql);
	
	/**
	 * Tabelle für die Versandzonen
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_VZ." (
		id mediumint(9) NOT NULL AUTO_INCREMENT,
		name VARCHAR(255) NOT NULL,
		innereu int(1) NOT NULL,
		param TEXT NOT NULL,
		PRIMARY KEY  (id)		
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
	
	dbDelta($sql);
	
	/**
	 * Tabelle für die Länder
	 */	
	$sql = "CREATE TABLE ".WPSG_TBL_LAND." (
		id mediumint(9) NOT NULL AUTO_INCREMENT,
		name VARCHAR(255) NOT NULL,
		kuerzel VARCHAR(10) NOT NULL,
		vz mediumint(9) NOT NULL,
		mwst mediumint(9) NOT NULL,
		mwst_a DOUBLE(10,4) NULL,
		mwst_b DOUBLE(10,4) NULL,
		mwst_c DOUBLE(10,4) NULL,
		mwst_d DOUBLE(10,4) NULL,
		PRIMARY KEY  (id),
		KEY vz (vz),
		KEY mwst (mwst)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
	
	dbDelta($sql);
	
	/**
	 * Tabelle für das Bestellprotokoll
	 */
	$sql = "CREATE TABLE ".WPSG_TBL_OL." (
		id mediumint(9) NOT NULL AUTO_INCREMENT,
		o_id mediumint(9) NOT NULL,
		cdate datetime NOT NULL,
		title VARCHAR(500) NOT NULL,
		mailtext TEXT,
		PRIMARY KEY  (id),
		KEY o_id (o_id)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
	
	dbDelta($sql);

	include WPSG_PATH.'/lib/update.php';
	
?>