<?php

/*------------------------------------------------------------------------
# com_invoices - Invoice Manager for Joomla
# ------------------------------------------------------------------------
# author				Germinal Camps
# copyright 			Copyright (C) 2012 - 2016 JoomlaThat.com. All Rights Reserved.
# @license				http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: 			http://www.joomlathat.com
# Technical Support:	Forum - http://www.joomlathat.com/support
-------------------------------------------------------------------------*/

//no direct access
defined('_JEXEC') or die('Restricted access.');

function InvoicesBuildRoute( &$query ) {
	$segments									=	array();
	
	$database				=	JFactory::getDBO();
		if(!isset($query['view'])) $query['view'] = "";
		$view									=	strtolower( $query['view'] );
		//$segments[]								=	$task;

		switch ( $view ) {
			
			
			case 'invoice':
				if ( isset( $query['id'] ) && $query['id'] ) {
				
					$sql = "SELECT *
							FROM #__invoices_invoices
							WHERE id = ".$query["id"] ;
							
					$database->setQuery($sql);
					$result = $database->loadObject();
					
					$segments[]					=	$result->id . "-". JFilterOutput::stringURLSafe( $result->invoice_num );
					
					unset( $query['id'] );
					unset( $query['view'] );
				}
				break;
			
			default:
				break;
		}
		

	return $segments;
}

function InvoicesParseRoute( $segments ) {
	$vars										=	array();

	$database				=	JFactory::getDBO();
		
	$count										=	count( $segments );
	if ( $count > 0 ) {
		//$vars['view']							=	strtolower( $segments[0] );

		switch ( $count ) {
			
			case 0: // invoices
					
					$vars['view']				=	'invoices';
					
				
				break;
				
			case 1: // invoice
					$invoice						=	$segments[0] ;
					$invoice = explode("-", $invoice) ;
					$invoice = $invoice[0];
					
					$invoice = explode(":", $invoice) ;
					$invoice = $invoice[0];
					
					$vars['view']				=	'invoice';
					$vars['id']				=	$invoice;
					//print_r($vars);die;
				
				break;

			default:
				break;
		}
	}
	return $vars;
}


?>