<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'invoice.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'invoice_entries.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'invoice_import_costs.php');
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'invoice_import_ship_costs.php');

/**
 * invoices class 
 *
 */
class invoices extends databasetable
{
	/**
	 * initiates the invoices class
	 *
	 */
	function invoices(){
		$this->set_tablename(TABLE_INVOICES);
		$this->set_index('invoices_id');
		$this->related_tables = array(
			0=>array('table'=>TABLE_INVOICE_ENTRIES,'key'=>$this->get_index(),'class'=>'invoice_entries' ),
			1=>array('table'=>TABLE_INVOICE_IMPORT_COSTS,'key'=>$this->get_index(),'class'=>'invoice_import_costs' ),
			2=>array('table'=>TABLE_INVOICE_IMPORT_SHIP_COSTS,'key'=>$this->get_index(),'class'=>'invoice_import_ship_costs' )
		);
	}
	
	/**
	 * return a invoice object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_invoice($id){
		$values = $this->get_row_by_index($id);
		$entries = $this->get_entries($id);
		if(is_array($entries) and sizeOf($entries)>0){
			$values[0]['entries']=$entries;
			$values[0]['entries_total'] = $this->get_entries_total($entries);
		}
			
		$import_costs = $this->get_import_costs($id);
		if(is_array($import_costs) and sizeOf($import_costs)>0){
			$values[0]['import_costs']=$import_costs;
			$values[0]['import_costs_total'] = $this->get_import_costs_total($import_costs);
		}
			
		$import_ship_costs = $this->get_import_ship_costs($id);
		if(is_array($import_ship_costs) and sizeOf($import_ship_costs)>0){
			$values[0]['import_ship_costs']=$import_ship_costs;
			$values[0]['import_ship_costs_total'] = $this->get_import_ship_costs_total($import_ship_costs);
		}

		$values[0]['ship_name'] = $this->get_import_ship_name($id);
			
		$invoice_instance = new invoice( (object) current($values) );
		return $invoice_instance;
	}

	function get_next_invoice_number($source='invoice'){
		$prefix = ($source == 'invoice') ? 'I' : 'E';
		$this_month = date('Y-m').'-01 00:00:00';
		$last_number_query = $this->query(sprintf( "SELECT invoice_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['invoice_number']);
			$month = $num[1];
			$year = substr($num[0],1);
			if($year != date('y') or $month != date('m')){
				$number = $prefix.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 = $prefix.date('y').'-'.date('m').'-0001';
		}
		return $number;
	}
	
	function get_entries($id){
		$entries = new invoice_entries;
		$array = array();
		$query = $this->query(sprintf("SELECT invoice_entries_id FROM %s WHERE invoices_id='%s'",TABLE_INVOICE_ENTRIES,$id));
		if($query){
			foreach($query as $record){
				$entry = $entries->get_invoice_entry($record['invoice_entries_id']);
				$array[] = $entry->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function delete_entries($id,$entries_id){
		$query_raw = sprintf("DELETE FROM %s WHERE invoices_id='%s' and invoice_entries_id='%s'",TABLE_INVOICE_ENTRIES,$id,$entries_id);
		$this->query($query_raw);
	}
	
	function get_entries_total($entries_array){
		$total = 0;
		foreach($entries_array as $k=>$entry){
			$total += $entry['price'];
		}
		return $total;
	}

	// ---- IMPORT KOSTEN
	function add_import_costs($id,$import_costs_id){
		if(!$this->check_import_costs_id_saved($id,$import_costs_id)){
			$ic_data = $this->get_import_costs_data($import_costs_id);
			$insert_sql_array = array(
				'invoices_id'=>$id,
				'import_costs_id'=>$import_costs_id,
				'costs_text'=>$ic_data['costs_text'],
				'costs'=>($ic_data['costs']-$ic_data['costs_expensed']),
				'date_added'=>'now()'
			);
			xtc_db_perform(TABLE_INVOICE_IMPORT_COSTS,$insert_sql_array);
			$this->query(sprintf("UPDATE %s SET costs_expensed='%s' WHERE import_costs_id='%s'",TABLE_IMPORT_COSTS,$ic_data['costs'],$import_costs_id));
			return $ic_data['costs_text'];
		}
	}

	function update_import_costs($id,$import_costs_id,$costs){
		$this->query(sprintf("UPDATE %s SET costs='%s' WHERE import_costs_id='%s' and invoices_id='%s'",TABLE_INVOICE_IMPORT_COSTS,$costs,$import_costs_id,$id));
		$import_costs_data = $this->get_import_costs_data($import_costs_id);
		$costs_expensed = $import_costs_data['costs_expensed'];
		if($costs_expensed == $costs){
			// tu nix, wert bleibt gleich
		}else{
			$new_costs = $costs_expensed - ($costs_expensed - $costs);
			$this->query(sprintf("UPDATE %s SET costs_expensed=%s WHERE import_costs_id='%s'",TABLE_IMPORT_COSTS,$new_costs,$import_costs_id));
		}
	}
	
	function delete_import_costs($id,$invoice_import_costs_id){
		$invoice_import_costs = new invoice_import_costs;
		$invoice_import_cost = $invoice_import_costs->get_invoice_import_cost($invoice_import_costs_id);

		$this->query(sprintf("DELETE FROM %s WHERE invoice_import_costs_id='%s' and invoices_id='%s'",TABLE_INVOICE_IMPORT_COSTS,$invoice_import_costs_id,$id));
		$this->query(sprintf("UPDATE %s SET costs_expensed=costs_expensed-%s WHERE import_costs_id='%s'",TABLE_IMPORT_COSTS,$invoice_import_cost->data->costs,$invoice_import_cost->data->import_costs_id));
	}
	
	function get_import_costs_data($import_costs_id){
		$ic_query = $this->query(sprintf("SELECT * FROM %s WHERE import_costs_id='%s'",TABLE_IMPORT_COSTS,$import_costs_id));
		if($ic_query){
			return current($ic_query);
		}
	}
	
	function check_import_costs_id_saved($id,$import_costs_id){
		$check_query = $this->query(sprintf("SELECT invoice_import_costs_id FROM %s WHERE import_costs_id='%s' and invoices_id='%s'",TABLE_INVOICE_IMPORT_COSTS,$import_costs_id,$id));
		if($check_query){
			return true;
		}else{
			return false;
		}
	}
	
	function get_import_costs($id){
		$costs = new invoice_import_costs;
		$array = array();
		$query = $this->query(sprintf("SELECT invoice_import_costs_id FROM %s WHERE invoices_id='%s'",TABLE_INVOICE_IMPORT_COSTS,$id));
		if($query){
			foreach($query as $record){
				$cost = $costs->get_invoice_import_cost($record['invoice_import_costs_id']);
				$array[] = $cost->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function get_import_costs_total($import_costs_array){
		$total = 0;
		foreach($import_costs_array as $k=>$import_cost){
			$total += $import_cost['costs'];
		}
		return $total;
	}

	// ----- IMPORT KOSTEN FUER SCHIFFE
	
	
	function add_import_ship_costs($id,$import_ship_costs_id){
		if(!$this->check_import_ship_costs_id_saved($id,$import_ship_costs_id)){
			$isc_data = $this->get_import_ship_costs_data($import_ship_costs_id);
			$insert_sql_array = array(
				'invoices_id'=>$id,
				'import_ship_costs_id'=>$import_ship_costs_id,
				'costs_text'=>$isc_data['costs_text'],
				'costs'=>($isc_data['costs']-$isc_data['costs_expensed']),
				'date_added'=>'now()'
			);
			xtc_db_perform(TABLE_INVOICE_IMPORT_SHIP_COSTS,$insert_sql_array);
			$this->query(sprintf("UPDATE %s SET costs_expensed='%s' WHERE import_ship_costs_id='%s'",TABLE_IMPORT_SHIP_COSTS,$isc_data['costs'],$import_ship_costs_id));
			return $isc_data['costs_text'];
		}
	}

	function update_import_ship_costs($id,$import_ship_costs_id,$costs){
		$this->query(sprintf("UPDATE %s SET costs='%s' WHERE import_ship_costs_id='%s' and invoices_id='%s'",TABLE_INVOICE_IMPORT_SHIP_COSTS,$costs,$import_ship_costs_id,$id));
		$import_ship_costs_data = $this->get_import_ship_costs_data($import_ship_costs_id);
		$costs_expensed = $import_ship_costs_data['costs_expensed'];
		if($costs_expensed == $costs){
			// tu nix, wert bleibt gleich
		}else{
			$new_costs = $costs_expensed - ($costs_expensed - $costs);
			$this->query(sprintf("UPDATE %s SET costs_expensed=%s WHERE import_ship_costs_id='%s'",TABLE_IMPORT_SHIP_COSTS,$new_costs,$import_ship_costs_id));
		}
	}
	
	function delete_import_ship_costs($id,$invoice_import_ship_costs_id){
		$invoice_import_ship_costs = new invoice_import_ship_costs;
		$invoice_import_ship_cost = $invoice_import_ship_costs->get_invoice_import_ship_cost($invoice_import_ship_costs_id);

		$this->query(sprintf("DELETE FROM %s WHERE invoice_import_ship_costs_id='%s' and invoices_id='%s'",TABLE_INVOICE_IMPORT_SHIP_COSTS,$invoice_import_ship_costs_id,$id));
		$this->query(sprintf("UPDATE %s SET costs_expensed=costs_expensed-%s WHERE import_ship_costs_id='%s'",TABLE_IMPORT_SHIP_COSTS,$invoice_import_ship_cost->data->costs,$invoice_import_ship_cost->data->import_ship_costs_id));
	}
	
	function get_import_ship_costs_data($import_ship_costs_id){
		$isc_query = $this->query(sprintf("SELECT * FROM %s WHERE import_ship_costs_id='%s'",TABLE_IMPORT_SHIP_COSTS,$import_ship_costs_id));
		if($isc_query){
			return current($isc_query);
		}
	}
	
	function check_import_ship_costs_id_saved($id,$import_ship_costs_id){
		$check_query = $this->query(sprintf("SELECT invoice_import_ship_costs_id FROM %s WHERE import_ship_costs_id='%s' and invoices_id='%s'",TABLE_INVOICE_IMPORT_SHIP_COSTS,$import_ship_costs_id,$id));
		if($check_query){
			return true;
		}else{
			return false;
		}
	}
	
	function get_import_ship_costs($id){
		$costs = new invoice_import_ship_costs;
		$array = array();
		$query = $this->query(sprintf("SELECT invoice_import_ship_costs_id FROM %s WHERE invoices_id='%s'",TABLE_INVOICE_IMPORT_SHIP_COSTS,$id));
		if($query){
			foreach($query as $record){
				$cost = $costs->get_invoice_import_ship_cost($record['invoice_import_ship_costs_id']);
				$array[] = $cost->get_smarty_data_array();
			}
		}
		return $array;
	}
	
	function get_import_ship_costs_total($import_ship_costs_array){
		$total = 0;
		foreach($import_ship_costs_array as $k=>$import_ship_cost){
			$total += $import_ship_cost['costs'];
		}
		return $total;
	}
	
	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),true);
		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_import_ship_name($id){
		$query = $this->query(sprintf("SELECT orderer_ships_id FROM %s inv LEFT JOIN %s ish USING(import_ships_id) WHERE inv.invoices_id='%s'",TABLE_INVOICES,TABLE_IMPORT_SHIPS,$id));
		if($query){
			$data = current($query);
			return yes_get_ships_name($data['orderer_ships_id']);
		}
	}
}
