Location: PHPKode > projects > PhpRechnung > phpRechnung/payment/newf.php
<?php
/*	newf.php

	phpRechnung - 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/smarty.inc.php");
CheckUser();
CheckSession();

if(isset($_POST['myID'])) {
	$myID = $_POST['myID'];
}
if(isset($_POST['invoiceID'])) {
	$invoiceID = $_POST['invoiceID'];
}
if(isset($_POST['page'])) {
	$page = $_POST['page'];
}
if(isset($_POST['Total_Amount'])) {
	$Total_Amount = $_POST['Total_Amount'];
}
if(isset($_POST['Total_Sum_Paid'])) {
	$Total_Sum_Paid = $_POST['Total_Sum_Paid'];
}
if(isset($_POST['MethodOfPayment'])) {
	$MethodOfPayment = $_POST['MethodOfPayment'];
}
if(isset($_POST['Card_Number'])) {
	$Card_Number = $_POST['Card_Number'];
}
if(isset($_POST['Valid_Thru'])) {
	$Valid_Thru = $_POST['Valid_Thru'];
}
if(isset($_POST['PaymentDate'])) {
	$PaymentDate = $_POST['PaymentDate'];
}
if(isset($_POST['Note'])) {
	$Note = $_POST['Note'];
}
if(isset($_POST['Sum_Paid'])) {
	$Sum_Paid = ereg_replace(",", ".", $_POST['Sum_Paid']);
}
if(isset($_REQUEST['Canceled']))
{
	$Canceled = $_REQUEST['Canceled'];
	$smarty->assign("Canceled","$Canceled");
}

function UserInput($mark)
{
	global $smarty, $page, $myID, $invoiceID, $Total_Amount, $Total_Sum_Paid, $MethodOfPayment,
		$Card_Number, $Valid_Thru, $PaymentDate, $Sum_Paid, $Order, $Sort;
	$smarty->assign("page","$page");
	$smarty->assign("myID","$myID");
	$smarty->assign("invoiceID","$invoiceID");
	$smarty->assign("Total_Amount","$Total_Amount");
	$smarty->assign("Total_Sum_Paid","$Total_Sum_Paid");
	$smarty->assign("MethodOfPayment","$MethodOfPayment");
	$smarty->assign("Card_Number","$Card_Number");
	$smarty->assign("Valid_Thru","$Valid_Thru");
	$smarty->assign("PaymentDate","$PaymentDate");
	$smarty->assign("Sum_Paid","$Sum_Paid");
	$smarty->assign("Order","$Order");
	$smarty->assign("Sort","$Sort");
	$smarty->assign("Mark",$mark);
}

$Total_Sum_Paid = number_format($Total_Sum_Paid + $Sum_Paid, 2, '.', '');

list($day, $month, $year) = explode(".", $PaymentDate);

if (empty($myID))
{
	$smarty->assign("FieldError","$a[customer] - $a[field_error]");
	UserInput("CustomerForm.invoiceID");
	$smarty->display('payment/newf.tpl');
}
else if (empty($PaymentDate))
{
	$smarty->assign("FieldError","$a[date_text] - $a[field_error]");
	UserInput("PaymentDateForm.PaymentDate");
	$smarty->display('payment/newf.tpl');
}
else if (!checkdate($month, $day, $year))
{
	$smarty->assign("FieldError","$a[date_text] - $a[field_error]");
	UserInput("PaymentDateForm.PaymentDate");
	$smarty->display('payment/newf.tpl');
}
else if ($Sum_Paid <= 0)
{
	$smarty->assign("FieldError","$a[payment] - $a[field_error]");
	UserInput("SumPaidForm.Sum_Paid");
	$smarty->display('payment/newf.tpl');
}
else
{
	// Database connection
	//
	DBConnect();

	// Check if this invoice is fully paid
	//
	$query = $db->Execute("SELECT INVOICEID, PAID FROM {$TBLName}invoice WHERE PAID='1' AND INVOICEID=$invoiceID");
	$numrows = $query->RowCount();

	if ($numrows)
	{
		$smarty->assign("FieldError","$a[payment_error]");
		UserInput("");
		$smarty->display('payment/newf.tpl');
	}
	else
	{

		// Convert payment date to mysql ISO standard
		//
		$PaymentDate = German_Mysql_Date($PaymentDate);

		// Get the current method of payment description
		//
		$query1 = $db->GetAll("SELECT METHODOFPAYID, DESCRIPTION FROM {$TBLName}methodofpay WHERE METHODOFPAYID=$MethodOfPayment");

		if (!$query1)
			print($db->ErrorMsg());
		else
			foreach($query1 as $f1)
			{
				$MethodOfPayment_Desc = $f1['DESCRIPTION'];
			}

		// Insert new payment
		//
		$query2 = "INSERT INTO {$TBLName}payment (PAYMENTID, MYID, INVOICEID, PAYMENT_DATE, METHODOFPAYID, METHOD_OF_PAY, CARDNR, VALIDTHRU, SUM_PAID, NOTE, CANCELED, CREATEDBY, MODIFIEDBY, USERGROUP1, USERGROUP2, CREATED, MODIFIED)";
		$query2 .= "VALUES (NULL, '$myID', '$invoiceID', '$PaymentDate', '$MethodOfPayment', '$MethodOfPayment_Desc', '$Card_Number', '$Valid_Thru', '$Sum_Paid', '$Note', '2', '$_SESSION[Username]', '$_SESSION[Username]', '$_SESSION[Usergroup1]', '$_SESSION[Usergroup2]', '$CurrentDateTime', '$CurrentDateTime')";

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

		// Get the last entry from 'payment'
		//
		$query3 = $db->GetRow("SELECT MAX(PAYMENTID) AS MAX_PAYMENTID FROM {$TBLName}payment");
		if (!$query3)
			print($db->ErrorMsg());
		else
			$maxPaymentID = $query3['MAX_PAYMENTID'];

		// Update invoice payment status
		//
		if ($Total_Sum_Paid === $Total_Amount)
		{
			$query4 = $db->Execute("UPDATE {$TBLName}invoice SET PAID='1', SUM_PAID='$Total_Sum_Paid', MODIFIED='$CurrentDateTime' WHERE INVOICEID=$invoiceID");
		} else {
			$query4 = $db->Execute("UPDATE {$TBLName}invoice SET SUM_PAID='$Total_Sum_Paid', MODIFIED='$CurrentDateTime' WHERE INVOICEID=$invoiceID");
		}

		// Calculate cash in hand
		//
		$query5 = $db->GetAll("SELECT TAKINGS, EXPENDITURES, CASH_IN_HAND_STARTING_WITH FROM {$TBLName}cashbook WHERE CANCELED=2");

		// If an error has occurred, display the error message
		//
		if (!$query5)
			print($db->ErrorMsg());
		else
			foreach($query5 as $result5)
			{
				$TTotalTakings += $result5['TAKINGS'];
				$TTotalExpenditures += $result5['EXPENDITURES'];
				$TCash_In_Hand_Starting_With += $result5['CASH_IN_HAND_STARTING_WITH'];
			}
		$TCash_In_Hand = $TCash_In_Hand_Starting_With + ( $TTotalTakings - $TTotalExpenditures );

		$TCash_In_Hand_Day = $TCash_In_Hand + $Sum_Paid;
	        $TCash_In_Hand_Day = ereg_replace(",", ".", $TCash_In_Hand_Day);

		$query6 = "INSERT INTO {$TBLName}cashbook (CASHBOOKID, MYID, INVOICEID, PAYMENTID, DESCRIPTION, CASHBOOK_DATE, CASH_IN_HAND, TAKINGS, CANCELED, CREATEDBY, MODIFIEDBY, USERGROUP1, USERGROUP2, CREATED, MODIFIED)";
		$query6 .= "VALUES (NULL, '$myID', '$invoiceID', '$maxPaymentID', '$MethodOfPayment_Desc - $a[invoice_number] $invoiceID', '$PaymentDate', '$TCash_In_Hand_Day', '$Sum_Paid', '2', '$_SESSION[Username]', '$_SESSION[Username]', '1', '2', '$CurrentDateTime', '$CurrentDateTime')";

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

		if ($MethodOfPayment != 2)
		{
			// Calculate cash in hand
			//
			$query7 = $db->GetAll("SELECT TAKINGS, EXPENDITURES, CASH_IN_HAND_STARTING_WITH FROM {$TBLName}cashbook WHERE CANCELED=2");

			// If an error has occurred, display the error message
			//
			if (!$query7)
				print($db->ErrorMsg());
			else
				foreach($query7 as $result7)
				{
					$ETotalTakings += $result7['TAKINGS'];
					$ETotalExpenditures += $result7['EXPENDITURES'];
					$ECash_In_Hand_Starting_With += $result7['CASH_IN_HAND_STARTING_WITH'];
				}
			$ECash_In_Hand = $ECash_In_Hand_Starting_With + ( $ETotalTakings - $ETotalExpenditures );

			$ECash_In_Hand_Day = $ECash_In_Hand - $Sum_Paid;
			$ECash_In_Hand_Day = ereg_replace(",", ".", $ECash_In_Hand_Day);

			$query8 = "INSERT INTO {$TBLName}cashbook (CASHBOOKID, MYID, INVOICEID, PAYMENTID, DESCRIPTION, CASHBOOK_DATE, CASH_IN_HAND, EXPENDITURES, CANCELED, CREATEDBY, MODIFIEDBY, USERGROUP1, USERGROUP2, CREATED, MODIFIED)";
			$query8 .= "VALUES (NULL, '$myID', '$invoiceID', '$maxPaymentID', '$MethodOfPayment_Desc - $a[invoice_number] $invoiceID', '$PaymentDate', '$ECash_In_Hand_Day', '$Sum_Paid', '2', '$_SESSION[Username]', '$_SESSION[Username]', '1', '2', '$CurrentDateTime', '$CurrentDateTime')";
			if ($db->Execute($query8) === false)
			{
				die($db->ErrorMsg());
			}
		}

		$_SESSION['NewID'] = "1";

		Header("Location: $web/payment/new.php?page=$page&infoID=$infoID&Order=$Order&Sort=$Sort&$sessname=$sessid");
	}

}
?>
Return current item: PhpRechnung