Location: PHPKode > projects > PhpRechnung > phpRechnung/invoice/new.php
<?php
/*
	new.php

	phpInvoice - is easy-to-use Web-based multilingual accounting software.
	Copyright (C) 2001 - 2008 Edy Corak < phprechnung at ecorak dot net >

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

require_once("../include/phprechnung.inc.php");
require_once("../include/company_settings.inc.php");
require_once("../include/smarty.inc.php");
CheckUser();
CheckSession();

if(isset($_REQUEST['myID']))
{
	$myID = $_REQUEST['myID'];
	$smarty->assign("myID","$myID");
}
if(isset($_REQUEST['invoiceID']))
{
	$invoiceID = $_REQUEST['invoiceID'];
	$smarty->assign("invoiceID","$invoiceID");
}
if(isset($_REQUEST['page']))
{
	$page = $_REQUEST['page'];
	$smarty->assign("page","$page");
}
if(isset($_REQUEST['infoID']))
{
	$infoID = $_REQUEST['infoID'];
	$smarty->assign("infoID","$infoID");
}
if(!empty($_REQUEST['InvoiceDate']))
{
	$InvoiceDate = $_REQUEST['InvoiceDate'];
} else {
	$InvoiceDate = date('d.m.Y');
}
if(isset($_REQUEST['MethodOfPaymentDate']))
{
	$MethodOfPaymentDate = $_REQUEST['MethodOfPaymentDate'];
	$smarty->assign("MethodOfPaymentDate","$MethodOfPaymentDate");
}
if(isset($_REQUEST['Order']))
{
	$Order = $_REQUEST['Order'];
	$smarty->assign("Order","$Order");
}
if(isset($_REQUEST['Sort']))
{
	$Sort = $_REQUEST['Sort'];
	$smarty->assign("Sort","$Sort");
}
if(isset($_REQUEST['Mark']))
{
	$Mark = $_REQUEST['Mark'];
	$smarty->assign("Mark","$Mark");
}
if(isset($_REQUEST['Note']))
{
	$Note = $_REQUEST['Note'];
	$smarty->assign("Note","$Note");
}
if(isset($_REQUEST['newofferID']))
{
	$newofferID = $_REQUEST['newofferID'];
	$smarty->assign("newofferID","$newofferID");
}
if(isset($_REQUEST['customerposID']))
{
	$customerposID = $_REQUEST['customerposID'];
	$smarty->assign("customerposID","$customerposID");
}
if(!empty($_REQUEST['Pos_Order']))
{
	$Pos_Order = $_REQUEST['Pos_Order'];
	$smarty->assign("Pos_Order",$Pos_Order);
}
else
{
	$Pos_Order = "T.POS_GROUP ASC, T.TMP_INVOICEID ASC";
	$smarty->assign("Pos_Order",$Pos_Order);
}
if(isset($_REQUEST['Pos_Sort']))
{
	$Pos_Sort = $_REQUEST['Pos_Sort'];
	$smarty->assign("Pos_Sort",$Pos_Sort);
}

// Assign needed text from language file
//
$smarty->assign("Title","$a[invoice] - $a[new]");
$smarty->assign("Print","$a[print]");
$smarty->assign("InvoiceInitials","$a[invoice_initials]");
$smarty->assign("CustomerNoInitials","$a[customer_no_initials]");

$smarty->assign("First_Name","$a[firstname]");
$smarty->assign("Last_Name","$a[lastname]");
$smarty->assign("Company_Name","$a[company]");
$smarty->assign("Customer_No","$a[customer_no]");
$smarty->assign("Customer","$a[customer]");
$smarty->assign("Find_Customer","$a[find_customer]");
$smarty->assign("Choose_Customer","$a[choose_customer]");
$smarty->assign("CustMethodOfPayment","$a[cust_method_of_payment]");
$smarty->assign("Date_Till","$a[date_till]");
$smarty->assign("Reminder_Days","$a[reminder_days]");

$smarty->assign("Invoice_No","$a[invoice_number]");
$smarty->assign("Invoice_Amount","$a[invoice_amount]");
$smarty->assign("Open_Invoice","$a[open_invoice]");
$smarty->assign("Print_Invoice","$a[print_invoice]");
$smarty->assign("Invoice_Amount","$a[invoice_amount]");
$smarty->assign("Invoice_Tax1","$a[invoice_tax1]");
$smarty->assign("Invoice_Tax2","$a[invoice_tax2]");
$smarty->assign("Invoice_Subtotal","$a[invoice_subtotal]");
$smarty->assign("PositionNew","$a[pos_new]");
$smarty->assign("PositionName","$a[pos_name]");
$smarty->assign("PositionText","$a[pos_text]");
$smarty->assign("PositionQuantity","$a[pos_quantity]");
$smarty->assign("PositionPrice","$a[pos_price]");
$smarty->assign("PositionAmount","$a[pos_amount]");
$smarty->assign("Invoice_Note","$a[invoice] - $a[note]");
$smarty->assign("Save_Invoice","$a[save_invoice]");
$smarty->assign("Save","$a[save]");
$smarty->assign("Choose_Message","$a[choose_message]");
$smarty->assign("Choose","$a[choose]");
$smarty->assign("NewEntry","$a[new_entry]");

// Database connection
//
DBConnect();

if(!isset($_REQUEST['tmpID']))
{
	$db->Execute("DELETE FROM {$TBLName}tmp_invoice WHERE USERNAME='$_SESSION[Username]'");
}

// Get offer data
//
if(!empty($_REQUEST['offerID']))
{
	$db->Execute("DELETE FROM {$TBLName}tmp_invoice WHERE USERNAME='$_SESSION[Username]'");	
	$offerID = $_REQUEST['offerID'];
	$smarty->assign("offerID","$offerID");	

	$query = $db->GetAll("SELECT OFFERPOSID, MYID, OFFERID, POSITIONID, POS_DESC, POS_QUANTITY, POS_PRICE, POS_GROUP, TAX, TAX_DESC, TAX_MULTI, TAX_DIVIDE FROM {$TBLName}offerpos WHERE OFFERID=$offerID ORDER BY POS_GROUP ASC, OFFERPOSID ASC");

	if (!$query)
		print($db->ErrorMsg());
	else
		foreach($query as $f)
		{
			$PosID = $f['POSITIONID'];
			$Pos_Desc = $f['POS_DESC'];
			$Pos_Quantity = $f['POS_QUANTITY'];
			$Pos_Price = $f['POS_PRICE'];
			$Pos_Group = $f['POS_GROUP'];
			$Tax = $f['TAX'];
			$Tax_Multi = $f['TAX_MULTI'];
			$Tax_Divide = $f['TAX_DIVIDE'];
			$Tax_Desc = $f['TAX_DESC'];

			$query1 = "INSERT INTO {$TBLName}tmp_invoice (TMP_INVOICEID, MYID, INVOICEID, POSITIONID, USERNAME, POS_DESC, POS_QUANTITY, POS_PRICE, POS_GROUP, TAX, TAX_MULTI, TAX_DIVIDE, TAX_DESC)";
			$query1 .= "VALUES (NULL, '$myID', '$invoiceID', '$PosID', '$_SESSION[Username]', '$Pos_Desc', '$Pos_Quantity', '$Pos_Price', '$Pos_Group', '$Tax', '$Tax_Multi', '$Tax_Divide', '$Tax_Desc')";

			if ($db->Execute($query1) === false)
			{
				die($db->ErrorMsg());
			}
		}
}

// Get company data from company_settings.inc.php
//
$smarty->assign("Invoice_Currency",$CompanyCurrency);
$smarty->assign("Country",$CompanyCountry);
$smarty->assign("TaxFree",$TaxFree);
$smarty->assign("SALESPRICE",$sales_price[$SalesPrices]);

// Get Customer Information
//
if(!empty($myID))
{
	$query = $db->GetAll("SELECT PREFIX, TITLE, FIRSTNAME, LASTNAME, ADDRESS, COMPANY, POSTALCODE, PRINT_NAME,
		CITY, COUNTRY, METHODOFPAY, MESSAGE, MYID FROM {$TBLName}addressbook WHERE MYID=$myID");

	// If an error has occurred, display the error message
	//
	if (!$query)
		print($db->ErrorMsg());
	else
		foreach($query as $f)
		{
			$smarty->assign("MYID",$f['MYID']);
			$smarty->assign("TITLE",$f['TITLE']);
			$smarty->assign("PREFIX",$f['PREFIX']);
			$smarty->assign("FIRSTNAME",$f['FIRSTNAME']);
			$smarty->assign("LASTNAME",$f['LASTNAME']);
			$smarty->assign("COMPANY",$f['COMPANY']);
			$smarty->assign("ADDRESS",$f['ADDRESS']);
			$smarty->assign("CITY",$f['CITY']);
			$smarty->assign("POSTALCODE",$f['POSTALCODE']);
			$smarty->assign("COUNTRY",$f['COUNTRY']);
			$smarty->assign("PRINT_NAME",$f['PRINT_NAME']);
			if(empty($_REQUEST['MethodOfPayment']))
			{
				$smarty->assign("NR_METHOD_OF_PAYMENT",$f['METHODOFPAY']);
			} else {
				$smarty->assign("NR_METHOD_OF_PAYMENT",$_REQUEST['MethodOfPayment']);
			}
			if(empty($_REQUEST['messageID']))
			{
				$smarty->assign("NR_MESSAGE",$f['MESSAGE']);
			} else {
				$smarty->assign("NR_MESSAGE",$_REQUEST['messageID']);
			}
		}
}

// Get the last entry from table 'invoice'
//
$query2 = $db->GetRow("SELECT MAX(INVOICEID) AS MAX_INVOICEID FROM {$TBLName}invoice");
if (!$query2)
	print($db->ErrorMsg());
else
	$maxInvoiceID = $query2['MAX_INVOICEID'] + 1;

$PrintD = Print_Date($InvoiceDate);
$smarty->assign("PrintDate",$PrintD.'-'.$maxInvoiceID);
$smarty->assign("maxInvoiceID","$maxInvoiceID");
$smarty->assign("INVOICE_DATE",$InvoiceDate);

$posquery = $db->GetAll("SELECT P.POSITIONID, P.POS_NAME, T.USERNAME, T.POSITIONID, T.POS_DESC, T.POS_QUANTITY, T.POS_PRICE, T.TAX, T.TAX_DIVIDE, T.TAX_MULTI, T.TAX_DESC, T.POS_GROUP, T.TMP_INVOICEID FROM {$TBLName}article AS P, {$TBLName}tmp_invoice AS T WHERE P.POSITIONID=T.POSITIONID ORDER BY $Pos_Order $Pos_Sort");
$numrows = count($posquery);

// Calculate positions
//
require_once('../include/pos.inc.php');

// Get the method of payment from database
//
$query5 = $db->GetAll("SELECT METHODOFPAYID, DESCRIPTION FROM {$TBLName}methodofpay ORDER BY DESCRIPTION ASC");
$query6 = $db->GetAll("SELECT MESSAGEID, DESCRIPTION FROM {$TBLName}message ORDER BY DESCRIPTION ASC");

// If an error has occurred, display the error message
//
if (!$query5)
	print($db->ErrorMsg());
else
	foreach($query5 as $result5)
	{
		$PaymentData[] = $result5;
	}
	$smarty->assign("PaymentData",$PaymentData);

	foreach($query6 as $result6)
	{
		$MessageData[] = $result6;
	}
	$smarty->assign("MessageData",$MessageData);

$smarty->display('invoice/new.tpl');

unset($_SESSION['NewID']);
?>
Return current item: PhpRechnung