<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'export.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'export_targets.php');
/**
 * exports class 
 *
 */
class exports extends databasetable
{
	/**
	 * initiates the exports class
	 *
	 */
	function exports(){
		$this->set_tablename(TABLE_EXPORTS);
		$this->set_index('exports_id');
		
		$this->related_tables = array(
			0=>array('table'=>TABLE_EXPORT_COSTS,'key'=>$this->get_index(),'class'=>'export_costs' ),
			1=>array('table'=>TABLE_EXPORT_TARGETS,'key'=>$this->get_index(),'class'=>'export_targets' )
		);
		
	}
	
	/**
	 * return a export object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_export($id){
		$values = $this->get_row_by_index($id);
		
			
		$costs = $this->get_costs($id);
		if(is_array($costs) and sizeOf($costs)>0)
			$values[0]['costs']=$costs;
		
		$targets = $this->get_targets($id);
		if(is_array($targets) and sizeOf($targets)>0)
			$values[0]['targets']=$targets;
		
		$export_instance = new export( (object) current($values) );
		return $export_instance;
	}

	function get_next_export_number(){
		$this_month = date('Y-m').'-01 00:00:00';
		$last_number_query = $this->query(sprintf( "SELECT export_number FROM %s WHERE date_added>'%s' ORDER BY date_added DESC LIMIT 1",$this->get_tablename(),$this_month ));
		if($last_number_query){
			$last_number = current($last_number_query);
			$num = split('-', $last_number['import_number']);
			$month = $num[1];
			$year = substr($num[0],1);
			if($year != date('y') or $month != date('m')){
				$number = 'E'.date('y').'-'.date('m').'-0001';
			}else{
				$next_id = $num[2];
				$next_id++;
				if(strlen($next_id)<4){
					$missing = 4-strlen($next_id);
					for($i=0;$i<$missing;$i++){
						$next_id = '0'.$next_id;
					}
				}
				$number = $num[0].'-'.$num[1].'-'.$next_id;
			}
		}else{
			$number = 'E'.date('y').'-'.date('m').'-0001';
		}
		return $number;
	}


	function get_costs($id){
		$array = array();
		$query = $this->query(sprintf("SELECT * FROM %s WHERE exports_id='%s'",TABLE_EXPORT_COSTS,$id));
		if($query){
			foreach($query as $record){
				$record['edit_link'] = yes_draw_href_image('images/icons/icon_edit.gif',IMAGE_EDIT,xtc_href_link(FILENAME_EXPORTS_EDIT_COSTS,'id='.$record['export_costs_id']),'exports_edit_costs','' ); 
				$record['delete_link'] = yes_draw_href_image_confirm(TEXT_REMOVE_ENTRY,'images/icons/delete.gif',IMAGE_EDIT,xtc_href_link(FILENAME_EXPORTS,'id='.$id.'&action=overview&subaction=delete_costs&costs_id='.$record['export_costs_id']),'exports_edit_costs','' ); 
				$array[] = $record;
			}
		}
		return $array;
	}
	
	function get_targets($id){
		$array = array();
		$export_targets = new export_targets;
		$query = $this->query(sprintf("SELECT export_targets_id FROM %s WHERE exports_id='%s'",TABLE_EXPORT_TARGETS,$id));
		if($query){
			foreach($query as $record){
				$export_target = $export_targets->get_export_target($record['export_targets_id']);
				$array[] = $export_target->get_smarty_data_array(); 
			}
		}
		return $array;
	}
	
	
	function delete_costs($id,$costs_id){
		$query_raw = sprintf("DELETE FROM %s WHERE exports_id='%s' and export_costs_id='%s'",TABLE_EXPORT_COSTS,$id,$costs_id);
		$this->query($query_raw);
	}
	
	/**
	 * get addressdata from companies for a given id and returns update sql array for a fieldset
	 *
	 * @param string $companies_id
	 * @param string $class
	 * @return array
	 */
	function get_companydata_from_db($companies_id,$class){
		$sql_array = array();
		$query = $this->query(sprintf("SELECT * FROM %s WHERE companies_id='%s'",TABLE_COMPANIES,$companies_id));
		if($query){
			$records = current($query);
			$sql_array = array(
				$class.'_name'=>$records['name'],
				$class.'_street_address'=>$records['street_address'],
				$class.'_suburb'=>$records['suburb'],
				$class.'_postcode'=>$records['postcode'],
				$class.'_city'=>$records['city'],
				$class.'_countries_id'=>$records['countries_id'],
				$class.'_vat_id'=>$records['vat_id']
			);
		}
		return $sql_array;
	}

	function update_status($id,$status_id){
		$this->query(sprintf("UPDATE %s SET status='%s' WHERE %s='%s'",$this->get_tablename(),$status_id,$this->get_index(),$id));
	}

}
