<?php

	/**
	 * Integration von Post Affiliate Pro in wpShopGermany
	 */
	class wpsg_mod_pap extends wpsg_mod_basic 
	{
		
		var $lizenz = 1;
		var $id = 200;
		
		var $hilfeURL = 'http://wpshopgermany.de/?p=1582';
		var $version = "3.0";		
		var $free = true;
				
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Post Affiliate Pro', 'wpsg');
			$this->group = __('Sonstiges', 'wpsg');
			$this->desc = __('Ermöglicht die integration von Post Affiliat Pro.', 'wpsg');
									
		} // public function __construct()
		
		public function install()
		{
			
			require_once(WPSG_PATH_WP.'/wp-admin/includes/upgrade.php');
			
			/**
			 * Tabelle für die Lieferanten
			 */
			$sql = "CREATE TABLE ".WPSG_TBL_PAP." ( 
				`id` int(11) NOT NULL AUTO_INCREMENT,
  				`uid` int(11) NOT NULL,
  				`pap` enum('0','1') NOT NULL,
  				`papusername` varchar(255) NOT NULL,
  				`pappwd` varchar(255) NOT NULL,
  				`affiliateID` varchar(255) NOT NULL,
				PRIMARY KEY  (id)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
			
		   	dbDelta($sql); 
		
		} // public function install()
		
		public function init() 
		{
		
		} // public function init()
		
		public function settings_edit()
		{
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_pap/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_paplogin_pap_url', $_REQUEST['wpsg_paplogin_pap_url']);
			$this->shop->update_option('wpsg_paplogin_admin', $_REQUEST['wpsg_paplogin_admin']);
			$this->shop->update_option('wpsg_paplogin_passwort', $_REQUEST['wpsg_paplogin_passwort']);
			$this->shop->update_option('wpsg_paplogin_register', $_REQUEST['wpsg_paplogin_register']);
			$this->shop->update_option('wpsg_pap_registrierung', $_REQUEST['wpsg_pap_registrierung']);
			$this->shop->update_option('wpsg_pap_include_profil', $_REQUEST['wpsg_pap_include_profil']);
			$this->shop->update_option('wpsg_pap_order_tracking', $_REQUEST['wpsg_pap_order_tracking']);
			
		} // public function settings_save()
		 
		public function customer_updated(&$customer_id) 
		{ 
			
		} // public function customer_updated(&$customer_id)
		
		public function customer_created(&$customer_id, &$pwd) 
		{ 

			if (isset($_REQUEST['wpsg_mod_kundenverwaltung_register']) && $this->shop->get_option('wpsg_pap_registrierung') == '1')
			{
				
				// Ein Kunde hat sich registriert und die Option für neu registrierungen ist aktiviert
				$this->mkPapAccount($customer_id, $pwd);
				
			}
			
			if (isset($_REQUEST['wpsg_checkout']) && $this->shop->get_option('wpsg_paplogin_register') == '1')
			{
				
				// Ein Kunde hat sich im Zuge einer Bestellung registriert
				$this->mkPapAccount($customer_id, $pwd); 
				
			}
			
		} // public function customer_created(&$customer_id)
		
		/** Modulfunktionen */
		
		/*
		 * Legte für einen bestehenden Kunden einen PAP Account an
		 */
		public function mkPapAccount($kunde_id, $pwd)
		{

			require_once dirname(__FILE__)."/mod_pap/PapApi.class.php";
			
			$ApiSession = new Gpf_Api_Session($this->shop->get_option('wpsg_paplogin_pap_url')."/scripts/server.php");
			
			if (!$ApiSession->login($this->shop->get_option('wpsg_paplogin_admin'), $this->shop->get_option('wpsg_paplogin_passwort')))
			{
				
				echo "PAP Error: ".$ApiSession->getMessage();
				return;
				
			}
			else 
			{
	
				// Kunde laden
				$kunde = $this->shop->cache->loadKunden($kunde_id);
				 				 
				$kunde['land'] = $this->db->fetchOne("SELECT `bezeichnung` FROM `".WPSG_TBL_LAND."` WHERE `id` = '".wpsg_q($kunde['land'])."'");
				
				$affiliate = new Pap_Api_Affiliate($ApiSession);
 
				$affiliate->setField('username', $kunde['email']);
				$affiliate->setField('rpassword', $pwd);
				$affiliate->setField('firstname', $kunde['vname']);
				$affiliate->setField('lastname', $kunde['name']);				
				//$affiliate->setField('refid', '1');
				
				$affiliate->setData('1', '-'); // WebURL
				$affiliate->setData('2', ((trim($kunde['firma']) != "")?$kunde['firma']:' '));
				$affiliate->setData('3', $kunde['strasse']);
				$affiliate->setData('4', $kunde['ort']);
				$affiliate->setData('5', '2'); // State
				$affiliate->setData('6', $kunde['land']);
				$affiliate->setData('7', $kunde['plz']);
				$affiliate->setData('8', $kunde['tel']);
				$affiliate->setData('9', $kunde['fax']);
				$affiliate->setData('10', $kunde['ustidnr']);
				  
				$affiliate->setStatus('A'); // allowed values: A – approved, P – pending, D - declined 
				
				if(!$affiliate->add())
				{
					
					echo "PAP Error: ".$affiliate->getMessage();
					return false;
					
				}
				else
				{
					
					$affiliateID = $affiliate->getUserid();
					
					if ($affiliateID != false)
					{
						
						$this->db->ImportQuery(WPSG_TBL_PAP, array(
							'uid' => wpsg_q($kunde['id']),
							'pap' => '1',
							'papusername' => wpsg_q($kunde['email']),
							'pappwd' => wpsg_q($pwd),
							'affiliateID' => wpsg_q($affiliateID)
						));
						
					}
					else
					{
						
						echo "PAP Error: ".$affiliate->getMessage();
						return false; 
						
					}
					 
					return true;
					
				}

			}
			
		} //public function mkPapAccount($email, $pwd)
		
		/**
		 * update PAP Account 
		 */
		public function updatePapAccount($login, $arData)
		{
			
			require_once dirname(__FILE__)."/mod_pap/PapApi.class.php";
			
			$ApiSession = new Gpf_Api_Session($this->shop->get_option('wpsg_paplogin_pap_url')."/scripts/server.php");
			
			if(!$ApiSession->login($this->shop->get_option('wpsg_paplogin_admin'), $this->shop->get_option('wpsg_paplogin_passwort')))
			{
				
				echo ("PAP Error: ".$ApiSession->getMessage());
				return false;
				
			}
			else 
			{
				
				$papid = $this->db->fetchOne("SELECT `affiliateID` FROM `".WPSG_TBL_PAP."` WHERE `uid` = ".wpsg_q($login));

				//PAP Account Daten
				$affiliate = new Pap_Api_Affiliate($ApiSession);
				$affiliate->setUserid($papid);
  				$papAccount = $affiliate->load();
  				
  				if ($papAccount == '1')
  				{
										
					$affiliate->setField('firstname', $arData['vname']);
					$affiliate->setField('lastname', $arData['name']);		 
					
					$affiliate->setData('1', '-'); // WebURL
					$affiliate->setData('2', ((trim($arData['firma']) != "")?$arData['firma']:' '));
					$affiliate->setData('3', $arData['strasse']);
					$affiliate->setData('4', $arData['ort']);
					$affiliate->setData('5', '2'); // State
					$affiliate->setData('6', $arData['land']);
					$affiliate->setData('7', $arData['plz']);
					$affiliate->setData('8', $arData['tel']);
					$affiliate->setData('9', $arData['fax']);
					$affiliate->setData('10', $arData['ustidnr']);
					
  				}

				if(!$affiliate->save())
				{
					
					echo("PAP Error: ".$affiliate->getMessage());
					return false;
					
				}
				else
				{		
								
					return true;
					
				}

			}
			
		} //public function updatePapAccount($login)
		
	} // class wpsg_mod_pap extends wpsg_mod_basic

?>