Location: PHPKode > projects > PhpRechnung > phpRechnung/offer/email_pdf.php
<?php
/*
	email_pdf.php

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

	phpRechnung benutzt die FPDF Bibliothek um PDF Dateien zu generieren.
	Copyright (C) Olivier PLATHEY, http://fpdf.org/ License: Freeware.

	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');

if ($PHPSendMail == "1" )
{
	require_once("../include/mail.inc.php");
}

CheckUser();
CheckSession();

if(isset($_REQUEST['myID']))
{
	$myID = $_REQUEST['myID'];
}
if(isset($_REQUEST['offerID']))
{
	$offerID = $_REQUEST['offerID'];
}
if(isset($_REQUEST['page']))
{
	$page = $_REQUEST['page'];
}
if(isset($_REQUEST['infoID']))
{
	$infoID = $_REQUEST['infoID'];
}
if(isset($_REQUEST['EmailTo']))
{
	$EmailTo = $_REQUEST['EmailTo'];
}
if(isset($_REQUEST['Type']))
{
	$Type = $_REQUEST['Type'];
}
if(isset($_REQUEST['OfferID1']))
{
	$OfferID1 = $_REQUEST['OfferID1'];
}
if(isset($_REQUEST['CustomerID1']))
{
	$CustomerID1 = $_REQUEST['CustomerID1'];
}
if(isset($_REQUEST['DateFrom1']))
{
	$DateFrom1 = $_REQUEST['DateFrom1'];
}
if(isset($_REQUEST['DateTill1']))
{
	$DateTill1 = $_REQUEST['DateTill1'];
}
if(isset($_REQUEST['Total1']))
{
	$Total1 = $_REQUEST['Total1'];
}
if(isset($_REQUEST['Customer1']))
{
	$Customer1 = $_REQUEST['Customer1'];
}
if(isset($_REQUEST['Order']))
{
	$Order = $_REQUEST['Order'];
}
if(isset($_REQUEST['Sort']))
{
	$Sort = $_REQUEST['Sort'];
}
if(isset($_REQUEST['Canceled']))
{
	$Canceled = $_REQUEST['Canceled'];
}

$Searchstring = "OfferID1=$OfferID1&CustomerID1=$CustomerID1&DateFrom1=$DateFrom1&DateTill1=$DateTill1&Total1=$Total1&Customer1=$Customer1";

// Database connection
//
DBConnect();

// Get Offer Information
//
$query = $db->GetAll("SELECT A.PREFIX, A.TITLE, A.FIRSTNAME, A.LASTNAME, A.ADDRESS, A.COMPANY, A.POSTALCODE, A.PRINT_NAME,
	A.CITY, A.COUNTRY, A.SALUTATION, A.METHODOFPAY, A.MYID, DATE_FORMAT(O.OFFER_DATE,'%d.%m.%Y') AS OFFER_DATE, O.INVOICEID, O.OFFERID, O.TOTAL_AMOUNT, O.MYID,
	O.CREATEDBY, O.STATUS, O.MESSAGE_DESC, O.METHOD_OF_PAY, DATE_FORMAT(O.METHOD_OF_PAY_DATE,'%d.%m.%Y') AS PAY_DATE, O.TAX1_TOTAL, O.TAX2_TOTAL, O.TAX3_TOTAL, O.TAX4_TOTAL, O.TAX1_DESC, O.TAX2_DESC, O.TAX3_DESC, O.TAX4_DESC, O.SUBTOTAL1, O.SUBTOTAL2, O.SUBTOTAL3, O.SUBTOTAL4
	FROM {$TBLName}addressbook AS A, {$TBLName}offer AS O WHERE A.MYID=O.MYID AND O.OFFERID=$offerID");

// If an error has occurred, display the error message
//
if (!$query)
	print($db->ErrorMsg());
else
	foreach($query as $f)
	{
		$Print_Company_Name = $f['PRINT_NAME'];
		$Date = $f['OFFER_DATE'];
		$ID = $f['OFFERID'];
		$CreatedBy = $f['CREATEDBY'];
		$status = $f['STATUS'];
		$MYID = $f['MYID'];
		$TITLE = $f['TITLE'];
		$PREFIX = $f['PREFIX'];
		$FIRSTNAME = $f['FIRSTNAME'];
		$LASTNAME = $f['LASTNAME'];
		$COMPANY = $f['COMPANY'];
		$ADDRESS = $f['ADDRESS'];
		$CITY = $f['CITY'];
		$POSTALCODE = $f['POSTALCODE'];
		$COUNTRY = $f['COUNTRY'];
		$SALUTATION = $f['SALUTATION'];
		$METHOD_OF_PAY = $f['METHOD_OF_PAY'];
		$METHOD_OF_PAY_DATE = $f['PAY_DATE'];
		$SUBTOTAL1 = $f['SUBTOTAL1'];
		$SUBTOTAL2 = $f['SUBTOTAL2'];
		$SUBTOTAL3 = $f['SUBTOTAL3'];
		$SUBTOTAL4 = $f['SUBTOTAL4'];
		$TAX1 = $f['TAX1_TOTAL'];
		$TAX2 = $f['TAX2_TOTAL'];
		$TAX3 = $f['TAX3_TOTAL'];
		$TAX4 = $f['TAX4_TOTAL'];
		$TAX1_DESC = $f['TAX1_DESC'];
		$TAX2_DESC = $f['TAX2_DESC'];
		$TAX3_DESC = $f['TAX3_DESC'];
		$TAX4_DESC = $f['TAX4_DESC'];
		$TOTAL = $f['TOTAL_AMOUNT'];
		$STATUS = $offer_status[$status];
		$MESSAGEID = $f['MESSAGE_DESC'];
	}

	$PrintD = Print_Date($Date);

	if($Type == 'Offer')
	{
		$FileName = "$a[offer_initials]-$PrintD-$ID";
		$Subject = "$a[offer] - $a[offer_number]: $FileName, $a[customer_no]: $MYID, $a[date_text]: $Date";
		$sendfile = "$PDFDirectory.$FileName";
	} else {
		if($STATUS != '3')
		{
			$query3 = "UPDATE {$TBLName}offer SET STATUS='2', MODIFIEDBY='$_SESSION[Username]' WHERE OFFERID=$offerID";
			if ($db->Execute($query3) === false)
			{
				die($db->ErrorMsg());
			}
		}
		$FileName = "$a[order_initials]-$PrintD-$ID";
		$Subject = "$a[order] - $a[order_number]: $FileName, $a[customer_no]: $MYID, $a[date_text]: $Date";
		$sendfile = "$PDFDirectory.$FileName";
	}

	$posquery = $db->GetAll("SELECT P.POSITIONID, P.POS_NAME, V.POSITIONID, V.POS_DESC,
	V.POS_QUANTITY, V.POS_PRICE, V.OFFERID, V.TAX, V.TAX_DIVIDE, V.TAX_MULTI, V.TAX_DESC FROM {$TBLName}article AS P, {$TBLName}offerpos AS V WHERE P.POSITIONID=V.POSITIONID AND V.OFFERID=$offerID");

	require_once('../include/pdf.inc.php');

	$PDF = @fopen($sendfile,"rb") or die("unable to open file ($sendfile)");
	$PDF_Size = filesize($sendfile);
	$PDF_File = fread($PDF,$PDF_Size);
	$PDF_Email = chunk_split(base64_encode($PDF_File));
	fclose($PDF);
	unlink($sendfile);
	$boundary = "=-".md5(rand())."";

	if ($PHPSendMail == "1" )
	{
		$header["From"] = "$CompanyName <$CompanyEmail>";
		$header["To"] = "$EmailTo";
		$header["Subject"] = "$Subject";
		$header["Organization"] = "$CompanyName";
		$header["MIME-Version"]	= "1.0";
		$header["Content-Type"]	= "multipart/mixed; boundary=\"$boundary\"";
		$header["Content-Transfer-Encoding"] = "7bit";
		$header["X-Mailer"] = "$a[programname] - PHP $phpversion";
	} else {
		$header = "From: {$CompanyName} <{$CompanyEmail}>\n";
		$header .= "Organization: {$CompanyName}\n";
		$header	.= "MIME-Version: 1.0\n"
		."Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
		$header	.= "Content-Transfer-Encoding: 7bit\n";
		$header	.="X-Mailer: {$a['programname']} - PHP ".PHP_VERSION."\n";
	}

	$body = "--$boundary\n"
	."Content-Type: text/plain; charset=$_SESSION[Charset]\n"
	."Content-Disposition: inline\n"
	."Content-Transfer-Encoding: 7bit\n"
	."\n"
	."$SALUTATION $PREFIX $FIRSTNAME $LASTNAME,\n"
	."\n"
	."$PDFAttachmentText\n"
	."\n"
	."--$boundary\n"
	."Content-Type: application/pdf; name=\"$FileName.pdf\"\n"
	."Content-Description: \"$Subject\"\n"
	."Content-Transfer-Encoding: base64\n"
	."Content-Disposition: attachment\n"
	."\n"
	."$PDF_Email\n"
	."\n"
	."--$boundary--\n";

	if(!empty($EmailTo))
	{
		if($PHPSendMail == "1")
		{
			$mail_object->send($EmailTo, $header, $body);
			$_SESSION['emailID'] = '1';
		} else {
			// If you have safe_mode = On, please delete ,"-f$CompanyEmail"
			// because the fifth parameter is not allowed in safe_mode
			//
			mail("$EmailTo",$Subject,$body,$header,"-f$CompanyEmail");
			$_SESSION['emailID'] = '1';
		}

		// Database connection
		//
		DBConnect();

		$query2 = "INSERT INTO {$TBLName}syslog (SYSLOGID, CREATED, DESCRIPTION, CREATEDBY, USERGROUP1, USERGROUP2)";
		$query2 .= "VALUES(NULL, '$CurrentDateTime', '$Subject was send by user $_SESSION[Username] (uid=$_SESSION[UserID]) from $IPAddress to E-Mail: $EmailTo', 'admin', '1', '2')";
		if ($db->Execute($query2) === false)
		{
			die($db->ErrorMsg());
		}
	}
	else
	{
		$_SESSION['emailID'] = '2';
	}

	if($infoID == '9')
	{
		Header("Location: $web/offer/searchlist.php?myID=$myID&offerID=$offerID&page=$page&$Searchstring&Order=$Order&Sort=$Sort&Canceled=$Canceled&$sessname=$sessid#$offerID");
	} else {
		Header("Location: $web/offer/list.php?myID=$myID&offerID=$offerID&page=$page&Order=$Order&Sort=$Sort&Canceled=$Canceled&$sessname=$sessid#$offerID");
	}
?>
Return current item: PhpRechnung