<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'import.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'import_ships.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'import_costs.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'invoices.php');
/**
 * imports class 
 *
 */
class imports extends databasetable
{
	/**
	 * initiates the imports class
	 *
	 */
	function imports(){
		$this->set_tablename(TABLE_IMPORTS);
		$this->set_index('imports_id');
		$this->related_tables = array(
			0=>array('table'=>TABLE_IMPORT_SHIPS,'key'=>$this->get_index(),'class'=>'import_ships' ),
			1=>array('table'=>TABLE_IMPORT_COSTS,'key'=>$this->get_index(),'class'=>'import_costs' )
		);
	}
	
	/**
	 * return a import object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_import($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;
			
		$ships = $this->get_ships($id);
		if(is_array($ships) and sizeOf($ships)>0)
			$values[0]['ships']=$ships;

		$invoices = $this->get_invoices($id);
		if(is_array($invoices) and sizeOf($invoices)>0)
			$values[0]['invoices']=$invoices;

		$import_instance = new import( (object) current($values) );
		return $import_instance;
	}

	function get_next_import_number(){
		$this_month = date('Y-m').'-01 00:00:00';
		$last_number_query = $this->query(sprintf( "SELECT import_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 = 'I'.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 = 'I'.date('y').'-'.date('m').'-0001';
		}
		return $number;
	}
	
	/**
	 * 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 get_costs($id){
		$costs = new import_costs;
		$array = array();
		$query = $this->query(sprintf("SELECT import_costs_id FROM %s WHERE imports_id='%s'",TABLE_IMPORT_COSTS,$id));
		if($query){
			foreach($query as $record){
				$cost = $costs->get_import_cost($record['import_costs_id']);
				$array[] = $cost->get_smarty_data_array();
			}
		}
		return $array;
	}

	function get_ships($id){
		$ships = new import_ships;
		$array = array();
		$query = $this->query(sprintf("SELECT import_ships_id FROM %s WHERE imports_id='%s'",TABLE_IMPORT_SHIPS,$id));
		if($query){
			foreach($query as $record){
				$ship = $ships->get_import_ship($record['import_ships_id']);
				$array[] = $ship->get_smarty_data_array();
			}
		}
		return $array;
	}

	function get_invoices($id){
		$invoices = new invoices;
		$array = array();
		$query = $this->query(sprintf("SELECT invoices_id FROM %s WHERE imports_id='%s'",TABLE_INVOICES,$id));
		if($query){
			foreach($query as $record){
				$invoice = $invoices->get_invoice($record['invoices_id']);
				$array[] = $invoice->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function get_saved_ship_id_array($id){
		$array = array();
		$query = $this->query(sprintf("SELECT import_ships_id FROM %s WHERE imports_id='%s'",TABLE_IMPORT_SHIPS,$id));
		if($query){
			foreach($query as $record){
				$array[] = $record['import_ships_id'];
			}
		}
		return $array;
	}
	
	function delete_costs($id,$costs_id){
		$query_raw = sprintf("DELETE FROM %s WHERE imports_id='%s' and import_costs_id='%s'",TABLE_IMPORT_COSTS,$id,$costs_id);
		$this->query($query_raw);
	}
	
	function update_status($id,$status_id){
		if($status_id == 1){
			$date_export = date('Y-m-d H:i:s');
		}else{
			$date_export = 0;
		}
		$this->query(sprintf("UPDATE %s SET status='%s',date_export='%s' WHERE %s='%s'",$this->get_tablename(),$status_id,$date_export,$this->get_index(),$id));
	}
	
	function check_update_status($id){
		$query = $this->query(sprintf("SELECT COUNT(import_ships_id) AS anz FROM %s WHERE status=0 AND imports_id='%s'",TABLE_IMPORT_SHIPS,$id));
		if($query){
			$record = current($query);
			if($record['anz'] == 0){
				return true;
			}
		}
		return false;		
	}
	
}
